Mind the following features on creating a modeling variable and working with it:
The Modeling Variable object class - MetabaseObjectClass.KE_CLASS_MSVARIABLE.
The user must also determine a parent container because a modeling variable can be stored only within a container.
By default, each of the created variables contains annual frequency and the Fact scenario dictionary.
To work with the created variable in the Fore language, use the IMsVariable interface. Using properties and methods of this interface the user can load data to variable, add additional dimensions to it, change calendar frequency and set up other parameters.
NOTE. The examples given below require the objects listed in variable description section.
When the variable is used in calculations, it should be presented as an abstract data source or as a term.
To use a variable as an abstract data source, it should be cast to the IVariableStub interface:
Sub Main;
Var
VarStub: IVariableStub;
MsVar: IMsVariable;
Begin
VarStub := MsVar As IVariableStub;
End Sub Main;
After this the variable can be added to the collection of input or output variables of the model implemented by the IMsFormulaTransformVariables interface:
Sub Main;
Var
Model: IMsModel;
ModelTransform: IMsFormulaTransform;
OutputsVar: IMsFormulaTransformVariables;
VarStub: IVariableStub;
TransformVariable: IMsFormulaTransformVariable;
Begin
ModelTransform := Model.Transform;
OutputsVar := ModelTransform.Outputs;
TransformVariable := InputsVar.Add(VarStub);
End Sub Main;
If the variable is added to the collection of input or output variables, it can be represented as a term implemented by the IMsFormulaTermInfo interface:
Sub Main;
Var
Model: IMsModel;
ModelTransform: IMsFormulaTransform;
VarStub: IVariableStub;
TransformVariable: IMsFormulaTransformVariable;
Slice: IMsFormulaTransformSlice;
TermInfo: IMsFormulaTermInfo;
Begin
ModelTransform := Model.Transform;
TransformVariable := ModelTransform.Outputs.Add(VarStub);
Slice := TransformVariable.Slices.Add(Null);
TermInfo := ModelTransform.CreateTermInfo;
TermInfo.Slice := Slice;
End Sub Main;
Using properties and methods of the IMsFormulaTermInfo interface one can determine the type of term passing to calculation, set initial transformation for the term and set up other parameters.
To create a balance of trade model, the user needs:
Variables containing source data: "Total Export, billion USD" and "Total Import, billon USD".
Variable, to which output data will be loaded: Balance of Trade, billion USD".
To execute the unit and create the Total Export, billion USD variable, add links to the Ms and Metabase assemblies.
Sub Main;
Var
MB: IMetabase;
CrInfo: IMetabaseObjectCreateInfo;
MObj: IMetabaseObject;
Begin
MB := MetabaseClass.Active;
// Determine parameters of variable creation
CrInfo := MB.CreateCreateInfo;
CrInfo.ClassID := MetabaseObjectClass.KE_CLASS_MSVARIABLE;
// Determine variable identifier
CrInfo.Id := "EXPORT";
// Determine variable name
CrInfo.Name := "Total export, billion USD";
// Select modeling container for variable
CrInfo.Parent := MB.ItemById("MODEL_SPACE");
// Create and save variable
MObj := MB.CreateObject(CrInfo).Edit;
MObj.Save;
// Display information about variable in the console window
Debug.WriteLine(The MObj.Name variable + with the MObj.Id identifier + is created +);
End Sub Main;
After executing the unit the Total Export, billion USD variable with the EXPORT identifier is created, and this information is displayed in the console window.
Then, in a similar way, create the Total Import, billion USD variable with the IMPORT identifier and the Balance of Trade, billion USD variable with the BALANCE identifier.
The next step is filling the Total Export, billion USD and Total Import, billion USD variables with source data. By default all created variables have annual frequency. Data is entered especially for this frequency.
To execute the unit to fill the Total Export, billion USD variable with source data, add links to the Ms and Metabase, Cubes, Dimensions, Matrix, MathFin assemblies.
Sub Main;
Var
MB: IMetabase;
ModelSp: IMetabaseObjectDescriptor;
MObj: IMetabaseObject;
MsVar: IMsVariable;
Cube: IAutoCube;
CubeInst: ICubeInstance;
Des: ICubeInstanceDestination;
DimSS: IDimSelectionSet;
DimS: IDimSelection;
Elem: IDimElementArray;
Mat: IMatrix;
Coord: IMatrixCoord;
Sto: ICubeInstanceStorage;
i: Integer;
Begin
// Set modeling variable that must be populated with data
MB := MetabaseClass.Active;
ModelSp := MB.ItemById("MODEL_SPACE");
MObj := MB.ItemByIdNamespace("EXPORT", ModelSp.Key).Edit;
MsVar := MObj As IMsVariable;
// Set manual method of populating with data
MsVar.DataFillType := MsVariableDataFillType.Manual;
// Get variable view in express analysis
Cube := MsVar.Cube;
CubeInst := (Cube As IMetabaseObject).Open(Null) As ICubeInstance;
Des := CubeInst.Destinations.DefaultDestination;
DimSS := Des.CreateDimSelectionSet;
// Select data frequency: Annual
DimS := DimSS.Item(0);
Elem := DimS.Dimension.Levels.Item(0).Elements;
For Each i In Elem Do
DimS.SelectElement(i, False);
End For;
// Select the Fact scenario dictionary
DimSS.Item(1).SelectAll;
// Get empty matrix of variable values
Mat := Des.Execute(DimSS);
Mat.ValueFlag := Mat.ValueFlag + 1;
Coord := Mat.CreateCoord;
Coord.Item(1) := 0;
// Populate matrix with random values from the range [50; 100]
Debug.WriteLine(Data of variable + MObj.Name + );
For Each i In Elem Do
Coord.Item(0) := i;
Mat.Item(Coord) := Math.RandBetween(50, 100);
Debug.WriteLine(Mat.Item(Coord));
End For;
// Save variable data
Sto := Des.CreateStorage;
Sto.SaveMatrix(Mat, Mat.ValueFlag);
MObj.Save;
End Sub Main;
After executing the unit the Total Export, billion USD variable is filled with the following data: random values in the [50; 100] range. This data is displayed in the console window.
In the similar way, fill the Total Import, billion USD variable with data. The Balance of Trade, billion USD variable will contain calculation results that is why there is no need to load data to this variable.
The next step is model creation.
See also: