Compartir a través de


Representación de jerarquía (tabular)

Las jerarquías son un mecanismo para proporcionar una experiencia de resumen y exploración en profundidad más completa al usuario final.

Representación de jerarquía

En los modelos de objetos tabulares, una jerarquía es una ruta de navegación entre un atributo a otro en función de los valores seleccionados por el usuario.

Jerarquía en AMO

Cuando se usa AMO para administrar una tabla modelo tabular, hay una correspondencia uno a uno entre los objetos de una jerarquía de AMO. En AMO, una jerarquía se representa mediante el objeto Hierarchy.

En el fragmento de código siguiente se muestra cómo se agrega una jerarquía en un modelo tabular existente. En el código se presupone que tiene un objeto de base de datos de AMO, newDatabase, y un objeto de cubo de 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);
        }

Ejemplo AMO2Tabular

Para consultar una descripción acerca de cómo se usa AMO para crear y manipular representaciones de jerarquías, vea el código fuente del ejemplo AMO a tabular. Revise específicamente el siguiente archivo de código fuente: AddHierarchies.cs. El ejemplo está disponible en Codeplex. Nota importante sobre el código: el código se proporciona solo como apoyo de los conceptos lógicos explicados aquí y no debe usarse en un entorno de producción; no debe usarse para otros fines excepto el pedagógico.