层次结构表示形式(表格)

层次结构是一种机制,它为最终用户提供了更丰富的浅化和深化体验。

层次结构表示形式

在表格对象模型中,层次结构是根据用户选定的值从一个属性导航到另一个属性的路径。

AMO 中的层次结构

在使用 AMO 管理表格模型表时,AMO 中的层次结构存在一对一的对象匹配;在 AMO 中,层次结构由 Hierarchy 对象表示。

下面的代码段演示如何向现有表格模型添加层次结构。 代码中假定您具有 AMO 数据库对象 newDatabase 和 AMO 多维数据集对象 modelCube。

        private void addHierarchy(
                            AMO.Database newDatabase
                         ,  AMO.Cube modelCube
                         ,  string tableName
                         ,  string hierarchyName
                         ,  string levelsText
                     )
        {
            //Validate input
            if (string.IsNullOrEmpty(hierarchyName) || string.IsNullOrEmpty(levelsText))
            {
                MessageBox.Show(String.Format("Hierarchy Name or Layout must be provided."), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!overwriteHierarchy.Checked && newDatabase.Dimensions[tableName].Hierarchies.Contains(hierarchyName))
            {
                MessageBox.Show(String.Format("Hierarchy already exists.\nGive a new name or enable overwriting"), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                if (newDatabase.Dimensions[tableName].Hierarchies.Contains(hierarchyName))
                {
                    //Hierarchy exists... deleting it to write it later
                    newDatabase.Dimensions[tableName].Hierarchies.Remove(hierarchyName, true);
                    newDatabase.Dimensions[tableName].Update(AMO.UpdateOptions.AlterDependents);
                }
                AMO.Hierarchy currentHierarchy = newDatabase.Dimensions[tableName].Hierarchies.Add(hierarchyName, hierarchyName);
                currentHierarchy.AllMemberName = string.Format("(All of {0})", hierarchyName);
                //Parse hierarchyLayout
                using (StringReader levels = new StringReader(levelsText))
                {
                    //Each line:
                    //  must come with: The columnId of the attribute in the dimension --> this represents the SourceAttributeID
                    //  optional: A display name for the Level (if this argument doesn't come the default is the SourceAttributeID)
                    string line;
                    while ((line = levels.ReadLine()) != null)
                    {
                        if (string.IsNullOrEmpty(line) || string.IsNullOrWhiteSpace(line)) continue;
                        line = line.Trim();
                        string[] hierarchyData = line.Split(',');
                        if (string.IsNullOrEmpty(hierarchyData[0])) continue; //first argument cannot be empty or blank, 
                                                                              //assume is a blank line and ignore it
                        string levelSourceAttributeID = hierarchyData[0].Trim();
                        string levelID = (hierarchyData.Length > 1 && !string.IsNullOrEmpty(hierarchyData[1])) ? hierarchyData[1].Trim() : levelSourceAttributeID;
                        currentHierarchy.Levels.Add(levelID).SourceAttributeID = levelSourceAttributeID;
                    }
                }
                newDatabase.Dimensions[tableName].Update(AMO.UpdateOptions.AlterDependents);

            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Error creating hierarchy [{0}].\nError message: {1}", newHierarchyName.Text, ex.Message), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (newDatabase.Dimensions[tableName].Hierarchies.Contains(hierarchyName))
                {
                    //Hierarchy was created but exception prevented complete hierarchy to be written... deleting incomplete hierarchy
                    newDatabase.Dimensions[tableName].Hierarchies.Remove(hierarchyName, true);
                    newDatabase.Dimensions[tableName].Update(AMO.UpdateOptions.AlterDependents);
                }

            }
            newDatabase.Dimensions[tableName].Process(AMO.ProcessType.ProcessFull);
            modelCube.MeasureGroups[tableName].Process(AMO.ProcessType.ProcessFull);
        }

AMO2Tabular 示例

为了理解如何使用 AMO 创建和操作层次结构表示形式,请参阅 AMO 到表格示例中的源代码;具体来讲,请查看以下源文件:AddHierarchies.cs。 该示例在 Codeplex 上提供。 有关该代码的重要说明:提供该代码只是为了支持本文介绍的逻辑概念,不应用于生产环境中;也不应用于除教学之外的其他用途。