Expression: IExpression;
The Expression property returns cycle calculation condition.
To set up condition calculation options, use the IMsCalculationChainIterator.Transform property.
To limit the number of cycle iterations, use the IMsCalculationChainIterator.MaxIterations property.
To enable one-time cycle execution without checking of condition, use the IMsCalculationChainIterator.PostCondition property.
Executing the example requires that the repository contains a modeling container with the MS identifier that contains a metamodel with the METAMODEL_ITERATOR identifier.
Add links to the Metabase and Ms system assemblies.
Sub UserProc;
Var
mb: IMetabase;
MsKey: Integer;
MetaModel: IMsMetaModel;
Param: IMsModelParam;
CalcChain: IMsCalculationChainEntries;
Iterator: IMsCalculationChainIterator;
Model: IMsModel;
Trans, IterTrans: IMsFormulaTransform;
Slice: IMsFormulaTransformSlice;
Selector: IMsFormulaTransformSelector;
Formula: IMsFormula;
TransformVar: IMsFormulaTransformVariable;
Determ: IMsDeterministicTransform;
Expr: IExpression;
Begin
mb := MetabaseClass.Active;
// Get modelling container key
MsKey := mb.GetObjectKeyById("MS");
// Get metamodel
MetaModel := mb.ItemByIdNamespace("METAMODEL_ITERATOR", MsKey).Edit As IMsMetaModel;
// Clear all metamodel parameters
MetaModel.Params.Clear;
// Add a new parameter
Param := MetaModel.Params.Add;
Param.DefaultValue := 0;
Param.Id := "THRESHOLD";
Param.Name := "THRESHOLD";
// Clear metamodel calculation chain
CalcChain := MetaModel.CalculationChain;
CalcChain.Clear;
// Create a cycle in metamodel calculation chain
Iterator := CalcChain.AddIterator("While/Repeat");
// Set cycle execution condition
Iterator.PostCondition := True;
Iterator.MaxIterations := 600;
Expr := Iterator.Expression;
Expr.AsString := "{THRESHOLD}<5";
If Expr.ErrorInfo <> Null Then
Debug.WriteLine(Expr.ErrorInfo.ErrorMessage);
End If;
// Set condition calculation mode
IterTrans := Iterator.Transform;
IterTrans.CalculationType := MsCalculationType.Pointwise;
IterTrans.CalculationDirection := MsCalculationDirection.Forward;
// Set internal model in metamodel calculation chain
Model := Iterator.Contents.AddExclusiveModel.EditModel;
Model.AutoName := True;
// Set model calculation parameters
Trans := Model.Transform;
Trans.CalculationType := MsCalculationType.Pointwise;
Trans.CalculationDirection := MsCalculationDirection.Forward;
// Specify the THRESHOLD parameter as a modeling variable
TransformVar := Trans.Outputs.AddParamVariable("THRESHOLD");
Slice := TransformVar.Slices.Add(Null);
Selector := Trans.CreateSelector;
Selector.Slice := Slice;
Formula := Trans.Transform(Selector);
Formula.Kind := MsFormulaKind.Deterministic;
Determ := Formula.Method As IMsDeterministicTransform;
// Set model calculation formula
Expr := Determ.Expression;
Expr.AsString := "{THRESHOLD}+RandBetween(-2,3)";
If Expr.ErrorInfo <> Null Then
Debug.WriteLine(Expr.ErrorInfo.ErrorMessage);
End If;
// Save metamodel
(MetaModel As IMetabaseObject).Save;
End Sub UserProc;
After executing the example a cycle is created in the metamodel that is executed until the THRESHOLD value is less than 5. If the condition is not executed during 600 iterations, the cycle is finished.
See also: