IMsCalculationChainEntries.AddTargetConstraint

Syntax

AddTargetConstraint(TargetEntry: IMsCalculationChainTarget;
                    TargetConstraint: IMsTargetConstraint): IMsCalculationChainTargetConstraint;

Parameters

TargetEntry. Criterion function, to which constraint is added.

TargetConstraint. Added constraint.

Description

The AddTargetConstraint method adds to calculation chain a constraint to the specified criterion function.

Comments

It is necessary to add a constraint to the calculation chain if modeling problem being set is used to work in the web application.

If TargetConstraint is set to Null, the constraint will be created automatically and will be added to the criterion function collection of constraints. To set parameters of the created constraint, use the IMsCalculationChainTargetConstraint.Constraint property.

To add a criterion function to the calculation chain, use the IMsCalculationChainEntries.AddTargetProblem method.

Example

Executing the example requires that the repository contains a modeling container with the MS identifier containing a modeling problem with the CHAIN_TARGET_CONSTRANT_D identifier. The problem must be used for web application and contain a criterion function.

Add links to the Metabase, Ms system assemblies.

Sub UserProc;
Var
    mb: IMetabase;
    MsObj: IMetabaseObjectDescriptor;
    Ms: IMsModelSpace;
    Problem: IMsProblem;
    CalcChain: IMsCalculationChainEntries;
    I, j: Integer;
    MetaTarget: IMsCalculationChainTarget;
    TargetProblem: IMsTargetProblem;
    Constraints: IMsTargetConstraints;
    Constraint: IMsTargetConstraint;
    TargetModel: IMsModel;
    TargetTrans: IMsFormulaTransform;
    TargetTransformVar: IMsFormulaTransformVariable;
    ConstrSlice: IMsFormulaTransformSlice;
    Tree: IMsFormulaTransformSlicesTree;
    ConstrTermX1: IMsFormulaTerm;
    TargetConstrArray: IMsTargetConstraintInfoArray;
    TargetConstrInfo: IMsTargetConstraintInfo;
    MetaConstraint: IMsCalculationChainTargetConstraint;
Begin
    // Get current repository
    mb := MetabaseClass.Active;
    // Get modeling container
    MsObj := mb.ItemById("MS");
    Ms := MsObj.Bind As IMsModelSpace;
    // Get modeling problem
    Problem := mb.ItemByIdNamespace("CHAIN_TARGET_CONSTRANT_D", MsObj.Key).Edit As IMsProblem;
    // Get calculation chain
    CalcChain := Problem.MetaModel.CalculationChain;
    // Search criterion function
    j := CalcChain.Count - 1;
    For i := 0 To j Do
        If CalcChain.Item(i).Type = MsCalculationChainEntryType.Target Then
            MetaTarget := CalcChain.Item(i) As IMsCalculationChainTarget;
            // Set custom criterion function name
            MetaTarget.UseCustomName := True;
            MetaTarget.Name := "Criterion function with constraints";
        End If;
        // Remove existing constraints from calculation chain
        If CalcChain.Item(i).Type = MsCalculationChainEntryType.TargetConstraint Then
            CalcChain.Remove(i);
            j := j - 1;
        End If;
    End For;
    // Get criterion problem
    TargetProblem := MetaTarget.TargetProblem;
    // Remove existing constraints from criterion problem
    Constraints := TargetProblem.Constraints;
    Constraints.Clear;
    // Add a new constraint
    Constraint := Constraints.Add;
    TargetModel := TargetProblem.Model;
    TargetTrans := TargetModel.Transform;
    TargetTransformVar := TargetTrans.Inputs.Add(TargetTrans.Outputs.Item(0).VariableStub);
    Tree := TargetTransformVar.SlicesTree(TargetTransformVar);
    ConstrSlice := Tree.CreateSlice(1);
    ConstrTermX1 := Constraint.Operands.Add(ConstrSlice);
    // Set constraint expression
    Constraint.Expression.AsString := ConstrTermX1.TermToInnerText;
    // Set fixation mode for constraint limits
    Constraint.LowerBoundFixed := MsBoundFixType.NotFixed;
    Constraint.UpperBoundFixed := MsBoundFixType.NotFixed;
    // Set values of criterion function constraint
    TargetConstrArray := Constraint.ConstraintInfoArray;
    For i := 0 To 4 Do
        TargetConstrInfo := TargetConstrArray.Add;
        TargetConstrInfo.TimeMoment := i;
        TargetConstrInfo.LowerBoundFixed := False;
        TargetConstrInfo.LowerBound := 0.74 + i * 0.1;  
        TargetConstrInfo.UpperBoundFixed := False;
        TargetConstrInfo.UpperBound := 1.74 + i * 0.1;  
    End For;
    // Add criterion function constraint to calculation chain
    MetaConstraint := CalcChain.AddTargetConstraint(MetaTarget, Constraint);
    MetaConstraint.Name := "Criterion function constraint '" + MetaTarget.Name + "' ";
    // Save changes
    (Problem As IMetabaseObject).Save;
End Sub UserProc;

After executing the example the criterion function is renamed, a constraint is also created for it.

See also:

IMsCalculationChainEntries