Defining fields and hierarchies in MetaData

All fields and hierarchies must be defined centrally in MetaData before creating or importing tables. The MetaData object is a sort of common data universe for a number of tables sharing subsets of fields defined in MetaData.

There are three types of fields in MetaData: Text attributes, numeric attributes and key figures. For more information about field types see related help page.

MetaData functions with short intellisense explanations in Visual StudioIn order to see meta-data functions just write “MetaData.” in the text file in Visual Studio (don’t forget the point at the end). You will see the list of all available functions. For each function you select you can also see a short explanation of the function as intellisense text. Most of the meta-data functions are quite straightforward to understand and use.

Adding fields to MetaData

Following example illustrates how fields are added to a MetaData object before tables are created. Note that numeric attribute has two sub-types: integer and date attributes.

// create empty MetaData
var md = MetaData.CreateEmptyMasterData();
// define text attributes
MetaData.AddNewField(md, "brand", FieldType.TextAttribute);
MetaData.AddNewField(md, "model", FieldType.TextAttribute);
MetaData.AddNewField(md, "color", FieldType.TextAttribute);
MetaData.AddNewField(md, "type", FieldType.TextAttribute);
// define numeric attributes
MetaData.AddNewField(md, "year", FieldType.IntegerAttribute);
MetaData.AddNewField(md, "date", FieldType.DateAttribute);
// define key figures
MetaData.AddNewField(md, "price", FieldType.KeyFigure);
MetaData.AddNewField(md, "weight", FieldType.KeyFigure);
// display meta data in immediate window
System.Diagnostics.Debug.WriteLine("Meta data md = \n" + md);

A valid field name …

  • is a single word without blanks and whitespaces
  • starts with a letter,
  • may contain numbers, underscores and hyphens.

Note that field names are always stored with lower case letters in MetaData.

Adding hierarchy tables to MetaData

Following example shows how hierarchy tables are added to MetaData. Hierarchical relations among fields can be defined for text and numeric attributes excluding key figures.

A base attribute (like model in the following example) which is found at the bottom of a hierarchy (i.e. right-most column of hierarchy matrix) can appear in multiple hierarchies. But a higher (non-base) attribute like brand can appear only in a single hierarchy.

// hierarchy table 1
TableFields Hierarchy1Fields = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(Hierarchy1Fields, "brand");
TableFields.AddNewField(Hierarchy1Fields, "model");
TableFields.AddNewField(Hierarchy1Fields, "year");
MatrixTable Hierarchy1Tbl = MatrixTable.CreateTableWithElements_A(Hierarchy1Fields,
	"BMW", "BMW BX", 2008,
	"BMW", "BMW B9", 2008,
	"Audi", "Audi A1", 2008,
	"Audi", "Audi A5", 2008
// ordered hierarchy fields from left (highest) to right (lowest)
// NOTE: "year" is the highest-level attribute, "model" is the lowest
TextVector OrderedH1Fields = TextVector.CreateVectorWithElements("year", "brand", "model");
// add hierarchy table to MetaData
MetaData.AddNewHierarchy(md, OrderedH1Fields, Hierarchy1Tbl);
// hierarchy table 2
TableFields Hierarchy2Fields = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(Hierarchy2Fields, "type");
TableFields.AddNewField(Hierarchy2Fields, "model");
MatrixTable Hierarchy2Tbl = MatrixTable.CreateTableWithElements_A(Hierarchy2Fields,
	"Sports", "BMW BX",
	"Sports", "Audi A5",
	"Economy", "Audi A1",
	"Luxury", "BMW B9"
// ordered hierarchy fields from left (highest) to right (lowest)
TextVector OrderedH2Fields = TextVector.CreateVectorWithElements("type", "model");
// add hierarchy table to MetaData
MetaData.AddNewHierarchy(md, OrderedH2Fields, Hierarchy2Tbl);
// display meta data in immediate window
System.Diagnostics.Debug.WriteLine("Meta data md = \n" + md);

MetaData after adding two hierarchy tablesDefinition: Hierarchy matrix is a hierarchy table with ordered fields from left to right. The leftmost field is the highest field, the rightmost field is the lowest field in the hierarchy.

Remember that in a finaquant table of the MatrixTable, text attributes always come before numeric attributes from left to right. But in a hierarchy matrix (actually an ordered view of hierarchy table) a numeric attribute like year may come before a text attribute like brand. Following code shows how you can see a hierarchy matrix with ordered fields (md is the MetaData object):

// view hierarchy tables with ordered fields: Left-most field is the highest one in hierarchy
MatrixTable.View_MatrixTable(md.GetHierarchyTable(0), "Hierarchy table 1", md.HierarchyFields[0]);

First hierarchy table in MetaData with ordered fields

A hierarchy matrix must exhibit 1-to-n relationship between higher and lower attributes. That is, a higher attribute can have multiple lower (child) attributes, but a lower attribute can only have a single higher (parent) attribute. For example, a brand can have multiple models, but a model can only belong to a single brand. Otherwise, the matrix won’t represent a real hierarchy. You will get a run-time error if you try to add a non-hierarchical matrix to meta data.

Adding a hierarchically related attribute to a table

Adding a hierarchically related attribute to tableFollowing example shows how a hierarchically related higher attribute (type) can be added to a table using the hierarchy information in MetaData. Because the lower attribute model is hierarchically related with type, model values like “BMW B9″ can be translated to type values.

Note that the model value “BMW15″ can’t be translated into a type value, because it is not defined in the corresponding hierarchy matrix (H2).

// define table fields
var tfields1 = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(tfields1, "model");
TableFields.AddNewField(tfields1, "color");
TableFields.AddNewField(tfields1, "year");
TableFields.AddNewField(tfields1, "price");
// Create table with values for each field (A)
var table1 = MatrixTable.CreateTableWithElements_A(tfields1,
	"BMW BX",   "red",      2010,   25000.0,
	"Audi A1",  "blue",     2008,   32000.0,
	"BMW B9",   "black",    2010,   28000.0,
	"BMW B15",  "black",    2009,   44000.0
// display table in immediate window
System.Diagnostics.Debug.WriteLine("table1 = \n" + table1);
// view table
MatrixTable.View_MatrixTable(table1, "Table: price(model, color, year)");

Table: price per model, color and year

// add related attribute "type" to table1
// --> translate "model" to "type" using hierarchy in MetaData
var table2 = MatrixTable.InsertHierarchicallyRelatedAttribute(table1, "type", ValueNotFoundStr: "NOTFOUND");
// display table in immediate window if related attribute is added successfully to table
System.Diagnostics.Debug.WriteLine("table2 = \n" + table2);
// view table
MatrixTable.View_MatrixTable(table2, "Table with related text attribute type");

Table with related attribute type

Exporting/importing MetaData objects to/from XML files

A MetaData object can be exported to or imported from as an XML file. If desired, a timestamp indicating the file version can automatically be added to the end of file name as suffix.

// store MetaData as XML file; no timestamp in file name
MetaData.ExportToXMLfile(md, Filename: "MyMetaData", 
	FileDir: @"C:\Windows\Temp\", IfAddTimeStamp: false);
// read MetaData from XML file
MetaData md2 = MetaData.ImportFromXMLfile(Filename: "MyMetaData", 
	FileDir: @"C:\Windows\Temp\");
// display meta data in immediate window
System.Diagnostics.Debug.WriteLine("Meta data md2 = \n" + md2);

Properties of a MetaData object

List of properties of a MetaData object instanceFollowing code illustrates how the properties of a MetaData object can be accessed. Write “md.” (where md is the object variable) to see all available properties in Visual Studio.

Properties are accessible internal parameters (stored data or fields) of an object in C# jargon.

// Some examples for MetaData properties
System.Diagnostics.Debug.WriteLine("md.FieldCount = " + md.FieldCount);
System.Diagnostics.Debug.WriteLine("md.HierarchyCount = " + md.HierarchyCount);
System.Diagnostics.Debug.WriteLine("md.HierarchyFields[0] = " + md.HierarchyFields[0]);
System.Diagnostics.Debug.WriteLine("md.HierarchyMatrices[0] = \n" + md.HierarchyMatrices[0]);
System.Diagnostics.Debug.WriteLine("md.HierarchyFields[1] = " + md.HierarchyFields[1]);
System.Diagnostics.Debug.WriteLine("md.HierarchyMatrices[1] = \n" + md.HierarchyMatrices[1]);

MetaData properties

Copyright secured by Digiprove © 2012 Tunc Ali Kütükcüoglu

Leave a Reply