TargetTerm: IMsFormulaTerm;
The TargetTerm property returns the parameters of expression (term) elements used in criterion function.
Executing the example requires that the repository contains a calculation algorithm with the ALGORITHM identifier. A linear optimization block is created and set up in the calculation algorithm.
Add links to the Algo, Cubes, Dimensions, Metabase, Ms system assemblies. Add links to the assemblies required for working with calculation algorithms.
Sub UserProc;
Var
MB: IMetabase;
MObj: IMetabaseObjectDescriptor;
Algo, CalcBlock: ICalcObject;
List: ICalcObjectsList;
CalcAlgo: ICalcAlgorithm;
LinearBlock: ICalcLinearOptimizationBlock;
TargetTerm: IMsFormulaTerm;
TargetTermInfo: IMsFormulaTermInfo;
StubSel: IStubSelection;
Begin
MB := MetabaseClass.Active;
// Get calculation algorithm
MObj := MB.ItemById("ALGORITHM");
Algo := CalcObjectFactory.CreateCalcObject(MObj, True);
CalcAlgo := Algo As ICalcAlgorithm;
// Get list of calculation algorithm objects
List := CalcAlgo.Items;
// Get the first linear optimization block
CalcBlock := List.Item(0);
LinearBlock := CalcBlock As ICalcLinearOptimizationBlock;
// Check if data source corresponds with criterion function
If LinearBlock.IsValidStub(LinearBlock.StubOut) Then
Debug.WriteLine("Data source corresponds with criterion function.");
Else
Debug.WriteLine("Data source does not correspond with criterion function.");
End If;
// Check parameters of terms used in criterion function
Debug.WriteLine("Parameters of terms used in criterion function.");
TargetTerm := LinearBlock.TargetTerm;
// Check settings of term parameters
Debug.WriteLine("Settings of term parameters:");
Debug.WriteLine("Term key: " + targetTerm.Key.ToString);
Debug.WriteLine("Slice name: " + TargetTerm.Slice.Name);
StubSel := LinearBlock.StubResult;
TargetTermInfo := LinearBlock.TargetTermInfo;
Debug.WriteLine("Data, on which data is taken from the specified variable slice when this term is used: " + TargetTermInfo.Date.ToString);
Debug.WriteLine("Expression: '" + StubSel.Term.TermToText + "'");
ShowSelectionSet("Data source selection: ", StubSel.SelectionSet);
ShowSelectionSet("Data source selection (taking into account parameters): ", StubSel.GetRealSelectionSet);
ShowSelectionSet("Expression slice selection: ", StubSel.Term.Slice.Selection);
Debug.WriteLine("Change criterion function");
FillTargetFunction(LinearBlock);
StubSel.RefreshSelection;
ShowSelectionSet("Expression slice selection: ", StubSel.Term.Slice.Selection);
End Sub UserProc;
Sub ShowSelectionSet(begMsg: String; SelectionSet: IDimSelectionSet);
Var
s: String;
i: Integer;
Begin
s := "";
For i := 0 To SelectionSet.Count - 1 Do
s := s + "[" + SelectionSet.Item(i).ToString + "]" + (i = SelectionSet.Count - 1 ? "" : ",");
End For;
Debug.WriteLine(begMsg + s);
End Sub ShowSelectionSet;
Sub FillTargetFunction(LinearBlock: ICalcLinearOptimizationBlock);
Var
oneDimension: IDimensionModel;
oneDimensionInstance: IDimInstance;
sel: IDimSelection;
iteratorDim: IMsDimIteratorDimension;
i, cnt: Integer;
Begin
cnt := LinearBlock.StubOut.DimensionCount;
For i := 1 To cnt - 1 Do
oneDimension := LinearBlock.StubOut.Dimension(i);
If Not oneDimension.IsCalendar Then
oneDimensionInstance := ((oneDimension As IMetabaseObjectDescriptor).Open(Null) As IDimInstance);
// Cannot add facts to criterion function
If oneDimensionInstance.Ident <> "FACTS" Then
sel := LinearBlock.TargetFunction.FindByKey(oneDimensionInstance.Key);
If sel = Null Then
LinearBlock.TargetFunction.Add(oneDimensionInstance);
// These dimensions should be removed from iterator
iteratorDim := linearBlock.Iterator.Dimensions.FindByKey(oneDimensionInstance.Key);
CopyFromIterToTerm(iteratorDim, linearBlock);
LinearBlock.Iterator.Dimensions.RemoveByKey(iteratorDim.Dimension.Key);
End If;
End If;
End If;
End For;
End Sub FillTargetFunction;
Sub CopyFromIterToTerm(dimIter: IMsDimIteratorDimension; linearBlock: ICalcLinearOptimizationBlock);
Var
dimParam: IMsParametrizedDimension;
sel: IDimSelection;
Begin
sel := linearBlock.TargetFunction.FindByKey(dimIter.Dimension.Key);
If sel <> Null Then
sel.DeselectAll;
End If;
dimParam := linearBlock.TargetTerm.Slice.ParametrizedDimensions.FindByKey(dimIter.Dimension.Key);
dimParam.Aggregation.AggregateAll := False;
dimParam.ParamAttributes.Parameter := Null;
If dimIter.Filter.ParameterAsSelection <> Null Then
dimParam.ParamAttributes.Parameter := linearBlock.Params.FindById(dimIter.Filter.ParameterAsSelection.Id);
Else
If dimIter.Filter.Selection = Null Then
dimParam.Aggregation.AggregateAll := True;
Else
sel := linearBlock.TargetFunction.FindByKey(dimIter.Dimension.Key);
dimIter.Filter.Selection.CopyTo(sel, True);
End If;
End If;
CopyAdvFilter(dimIter.Filter.AdvancedFilter, dimParam.Aggregation.AdvancedFilter);
End Sub CopyFromIterToTerm;
Sub CopyAdvFilter(advFilterSource, advFilterTarget: IMsDimIteratorAdvancedFilter);
Var
sourceItem, targetItem: IMsDimIteratorAdvancedFilterCondition;
i, cnt: Integer;
Begin
advFilterTarget.Clear;
cnt := advFilterSource.Count;
For i := 0 To cnt - 1 Do
sourceItem := advFilterSource.Item(i);
targetItem := advFilterTarget.Add;
targetItem.Attribute := sourceItem.Attribute;
targetItem.Inversed := sourceItem.Inversed;
targetItem.Operator_ := sourceItem.Operator_;
targetItem.ParamAttributes.Parameter := sourceItem.ParamAttributes.Parameter;
targetItem.Value := sourceItem.Value;
End For;
End Sub CopyAdvFilter;
After executing the example the development environment console displays information about terms used in the criterion function. After this the criterion function will be changed: if any data consumer dimensions were not included in the criterion function, they will be added to it. Filtering settings and advanced dimension filter will be saved.
See also: