Ms > Examples > Creating a Modeling Variable: Filling the Data using the Formula
Executing the example requires that the repository includes a modeling container with the KONT_MODEL identifier. The container includes a modeling variable with the VAR_1 identifier.
Add links to the Cubes, Dimensions, Matrix, Metabase, Ms, Transform system assemblies.
Sub UserProc;
Var
MB: IMetabase;
CrInfo: IMetabaseObjectCreateInfo;
MObj: IMetabaseObject;
MsVar: IMsVariable;
Sampling: IMsVariableSampling;
//Cube with modeling variable data
Cube: IAutoCube;
//To set up aggregation by calendar
AggrManager: MatrixAggregatorManager;
IAggrManager: IMatrixAggregatorManager;
AggrModel: IMatrixAggregatorModel;
BasicAggregator: IBasicMatrixAggregator;
LevelBasicAggregator: IBasicMatrixLevelAggregator;
Dim: IAutoCubeDimension;
DimModel: IDimensionModel;
DimLvl: IDimLevels;
//To create a formula
Trans: IMsFormulaTransform;
VarTrans: IMsFormulaTransformVariable;
InputVar: IVariableStub;
Tree: IMsFormulaTransformSlicesTree;
Slice: IMsFormulaTransformSlice;
Selector: IMsFormulaTransformSelector;
Formula: IMsFormula;
Determ: IMsDeterministicTransform;
Term: IMsFormulaTerm;
TermInfo: IMsFormulaTermInfo;
s: string;
//To load data
LoadSetting: IMsVariableLoadSettings;
DimFix: IMsProblemDimensionFix;
DimSS: IDimSelectionSet;
DimS: IDimSelection;
Period: IMsModelPeriod;
Begin
MB := MetabaseClass.Active;
CrInfo := MB.CreateCreateInfo;
CrInfo.ClassID := MetabaseObjectClass.KE_CLASS_MSVARIABLE;
CrInfo.Id := "VAR_2";
CrInfo.Name := "Variable No. 2";
CrInfo.Parent := MB.ItemById("Kont_Model");
MObj := MB.CreateObject(CrInfo).Edit;
MsVar := MObj As IMsVariable;
//Data population method
MsVar.DataFillType := MsVariableDataFillType.Formula;
Sampling := MsVar.Sampling;
//Calculation step for variable: Years and half-years
Sampling.Level(DimCalendarLevel.Year) := True;
Sampling.Level(DimCalendarLevel.HalfYear) := True;
Cube := MsVar.Cube;
//Parameters of aggregation by calendar levels
AggrManager := New MatrixAggregatorManager.Create;
IAggrManager := AggrManager As IMatrixAggregatorManager;
AggrModel := IAggrManager.CreateAggregator("BasicMatrixAggregator");
Dim := Cube.Dimensions.Calendar;
DimModel := Dim.Dimension;
DimLvl := DimModel.Levels;
//Aggregation by calendar dimension
AggrModel.Dimension := DimModel;
BasicAggregator := AggrModel As IBasicMatrixAggregator;
//Level, for which aggregation is set up - Years
LevelBasicAggregator := BasicAggregator.LevelAggregation(DimLvl.Item(0));
//Level, which values are used on aggregation - Half-years
LevelBasicAggregator.Include(DimLvl.Item(1)) := True;
//Aggregation method - Sum
LevelBasicAggregator.Operation := BasicAggregatorOperation.Sum;
Dim.Aggregator(Cube.Destinations.Item(0)) := AggrModel;
//Set up the equation, by which variable data will be calculated
Trans := MsVar.Transform;
InputVar := MB.ItemByIdNamespace("VAR_1", MB.ItemById("KONT_MODEL").Key).Bind As IMsVariableStub;
Trans.Inputs.Add(InputVar);
VarTrans := Trans.Outputs.Item(0);
Tree := VarTrans.SlicesTree(VarTrans);
Slice := Tree.CreateSlice(1);
Selector := Trans.CreateSelector;
Selector.Slice := Slice;
Formula := Trans.Transform(Selector);
Formula.Kind := MsFormulaKind.Deterministic;
//Calculation step
Formula.Level := DimCalendarLevel.HalfYear;
//Calculation formula
Determ := Formula.Method As IMsDeterministicTransform;
Slice := Trans.Inputs.Item(0).Slices.Add(Null);
Determ.Operands.Add(Slice);
Term := Determ.Operands.Item(0);
TermInfo := Term.TermInfo;
//Lag
TermInfo.Lag := "-1";
//Transformation
TermInfo.InversionInfo.Inversion := TsInversion.DLog;
Term.TermInfo := TermInfo;
s := Term.TermToInnerText + "*10";
Determ.Expression.AsString := s;
//Load data by set formula
LoadSetting := MsVar.CreateLoadSettings;
Period := LoadSetting.Period;
Period.IdentificationStartDate := DateTime.ComposeDay(2000, 1, 1);
Period.IdentificationEndDate := DateTime.ComposeDay(2010, 1, 1);
LoadSetting.ScenarioIncluded(-1) := True;
DimFix := LoadSetting.DimensionFix;
DimSS := DimFix.Selection;
For Each DimS In DimSS Do
DimS.SelectAll;
End For;
MsVar.Execute(LoadSetting);
MObj.Save;
End Sub UserProc;
After executing the example a modeling variable is created in the modeling container. The way of data filling is by formula. Two levels are presented in the variable frequency. They are Years and Half-years. For the Years level of calendar the aggregation is set: Half-year level data is summed. For formula composing one variable is added. The lag and transformation are set, after that the formula is composed. For specified period the data is loaded to the variable by the Fact dimension.
See also: