Copying Expression for Metamodel Calculation Condition

Description

Consider copying an expression for calculation condition from one metamodel to another one.

Requirements

Executing the example requires that the repository contains a modeling container with the MS identifier, which contains metamodels with the METAMODEL, METAMODEL_DEST identifiers and models with the MODEL_DEFAULT and MODEL identifiers. The models are based on the data from the time series database containing mandatory attribute being a link to the MDM dictionary with the DICT_COUNTRY identifier. This dictionary is stored in the MDM repository with the MDM_REPO identifier. The METAMODEL_DEST metamodel must contain calculation condition.

Add links to the Dal, Metabase, Ms system assemblies.

Example

Sub UserProc;
Var
    mb: IMetabase;
    MsKey: Integer;
    pMetaModelSrc, pMetaModelDest: IMsMetaModel;
    param: IMsModelParam;
    params: IMsModelParams;
    branchScr, branchDest: IMsCalculationChainBranch;
    cas, casDest: IMsBranchCase;
    pCondExpr, pCondExprDest: IMsBranchConditionExpression;
    expr, exprDest: IExpression;
    CalcChain: IMsCalculationChainEntries;
    CalcEntry: IMsCalculationChainEntry;
    i, k, n: Integer;
    CondDest: IMsBranchCondition;
    gen: IMsTransformStringGenerator;
    s: String;
Begin
    mb := MetabaseClass.Active;
    // Get modeling container
    MsKey := mb.GetObjectKeyById("MS");
    // Get metamodel
    pMetaModelSrc := mb.ItemByIdNamespace("METAMODEL", MsKey).Edit As IMsMetaModel;
    // Create a parameter in the metamodel    
    params := pMetaModelSrc.Params;
    params.Clear;
    param := params.Add;
    param.Hidden := True;
    param.LinkedObject := mb.ItemByIdNamespace("DIC_COUNTRY", mb.GetObjectKeyById("MDM_REPO")).Bind;
    param.DataType := DbDataType.String;
    // Clear metamodel calculation chain
    pMetaModelSrc.CalculationChain.Clear;
    // Add condition to the metamodel calculation chain
    branchScr := pMetaModelSrc.CalculationChain.AddBranch("Condition");
    // Specify condition parameter
    branchScr.Parameter := param;
    // Create condition branch
    cas := branchScr.CaseList.Add;
    // Specify a model which will be calculated if no condition branch is executed
    branchScr.DefaultContents.AddModel(mb.ItemByIdNamespace("MODEL_DEFAULT", MsKey).Bind As IMsModel);
    // Specify a model which is calculated on executing a condition
    cas.Contents.AddModel(mb.ItemByIdNamespace("MODEL", MsKey).Bind As IMsModel);
    // Specify branch calculation condition
    pCondExpr := cas.Conditions.Add(MsBranchConditionType.Expression) As IMsBranchConditionExpression;
    expr := pCondExpr.Expression;
    expr.AsString := "{" + param.Id + "}=" + """" + "World" + """";
    If expr.ErrorInfo <> Null
        Then
            // If the expression contains error, display it in the console window
            Debug.WriteLine(expr.ErrorInfo.ErrorMessage);
        Else
            // Save changes in the metamodel
            (pMetaModelSrc As IMetabaseObject).Save;
            // Get the metamodel to which the expression for calculating the condition will be copied
            pMetaModelDest := mb.ItemByIdNamespace("METAMODEL_DEST", MsKey).Edit As IMsMetaModel;
            // Create a parameter in the metamodel    
            params := pMetaModelSrc.Params;
            params.Clear;
            param := params.Add;
            param.Hidden := True;
            param.LinkedObject := mb.ItemByIdNamespace("DIC_COUNTRY", mb.GetObjectKeyById("MDM_REPO")).Bind;
            param.DataType := DbDataType.String;
            // Get metamodel calculation chain
            CalcChain := pMetaModelDest.CalculationChain;
            For i := 0 To CalcChain.Count - 1 Do
                CalcEntry := CalcChain.Item(i);
                // Get conditions from the calculation chain
                If CalcEntry.Type = MsCalculationChainEntryType.Branch Then
                    branchDest := CalcEntry As IMsCalculationChainBranch;
                    // Search in condition branches
                    For k := 0 To branchDest.CaseList.Count - 1 do
                        casDest := branchDest.CaseList.Item(k);
                        // Pick conditional branches
                        For n := 0 To casDest.Conditions.Count - 1 Do
                            CondDest := casDest.Conditions.Item(n);
                            // Copy condition expression from one branch to another
                            If CondDest.Type = MsBranchConditionType.Expression Then
                                pCondExprDest := CondDest As IMsBranchConditionExpression;
                                gen := pCondExpr.Transform.CreateStringGenerator;
                                s := gen.Execute;
                                s := String.Remove(s, 03);
                                exprDest := pCondExprDest.Expression;
                                exprDest.AsString := s;
                                If exprDest.ErrorInfo <> Null Then
                                    // If the branch contains error, display it in the console window
                                    Debug.WriteLine("Condition expression contains error: " + exprDest.ErrorInfo.ErrorMessage);
                                    Else
                                        // Save changes in the metamodel
                                        (pMetaModelDest As IMetabaseObject).Save;
                                End If;
                            End If;
                        End For;
                    End For;
                End If;
            End For;
    End If;
End Sub UserProc;

See also:

Examples | IMsBranchCondition | IMsMetaModel