Mind the following features on creating a modeling variable and working with it:
The class of the Modeling Variable object - 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 created variable contains annual frequency and the Fact scenario dictionary.
Use the IMsVariable interface to work with the created variable using the Fore language. 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 UserProc;
Var
VarStub: IVariableStub;
MsVar: IMsVariable;
Begin
VarStub := MsVar As IVariableStub;
End Sub UserProc;
After this the variable can be added to the collection of input or output variables of the model implemented by the IMsFormulaTransformVariables interface:
Sub UserProc;
Var
Model: IMsModel;
ModelTransform: IMsFormulaTransform;
OutputsVar: IMsFormulaTransformVariables;
VarStub: IVariableStub;
TransformVariable: IMsFormulaTransformVariable;
Begin
ModelTransform := Model.Transform;
OutputsVar := ModelTransform.Outputs;
TransformVariable := InputsVar.Add(VarStub);
End Sub UserProc;
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 UserProc;
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 UserProc;
Using properties and methods of the IMsFormulaTermInfo interface the user 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 UserProc;
Var
MB: IMetabase;
CrInfo: IMetabaseObjectCreateInfo;
MObj: IMetabaseObject;
Begin
MB := MetabaseClass.Active;
// Set variable creation parameters
CrInfo := MB.CreateCreateInfo;
CrInfo.ClassID := MetabaseObjectClass.KE_CLASS_MSVARIABLE;
// Set variable identifier
CrInfo.Id := "EXPORT";
// Set variable name
CrInfo.Name := "Total - export, billion USD";
// Set variable modeling container
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("Variable is created '" + MObj.Name + "' with identifier '" + MObj.Id + "'");
End Sub UserProc;
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 for populating the Total Export, billion USD variable with initial data, add links to the Cubes, Dimensions, MathFin, Matrix, Metabase, Ms system assemblies.
Sub UserProc;
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 should be populated with data
MB := MetabaseClass.Active;
ModelSp := MB.ItemById("MODEL_SPACE");
MObj := MB.ItemByIdNamespace("EXPORT", ModelSp.Key).Edit;
MsVar := MObj As IMsVariable;
// Set data population method - manually
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 the frequency, for which data will be specified: Years
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 variable data matrix
Mat := Des.Execute(DimSS);
Mat.ValueFlag := Mat.ValueFlag + 1;
Coord := Mat.CreateCoord;
Coord.Item(1) := 0;
// Populate matrix with random numbers from the range [50; 100]
Debug.WriteLine("Variable data '" + 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 UserProc;
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: