ICalcAlgorithm.CalculateAsync

Syntax

CalculateAsync([Objs: ICalcObjectsList = Null]): IAlgorithmCalculationAsyncResult;

Parameters

Objs. List of calculation algorithm objects.

NOTE. The list of calculation algorithm objects should be copied. To copy objects, use the ICalcObjectsList.Clone method.

Description

The CalculateAsync method executes asynchronous algorithm calculation.

Comments

The calculation takes into account parameter values specified in the ICalcAlgorithm.ParamValues property. If the Objs parameter is not specified, all calculation algorithm objects are calculated.

The asynchronous algorithm calculation should use the standby mode to detect calculation errors and to check if calculation is complete.

The standby mode is implemented by the GetState function:

Function GetState(CalcResult: IAlgorithmCalculationAsyncResult): CalculateState;
Var
    State: CalculateState;
    i: Integer = 
1000;
Begin
    
// Check calculation state
    If CalcResult <> Null Then
        State := CalcResult.State;
        
While 
            (State <> CalculateState.PausedOnDebug) 
And
            (State <> CalculateState.PausedOnError) 
And 
            (State <> CalculateState.Aborted) 
And 
            (State <> CalculateState.Stoped) 
Do
            State := CalcResult.State;
        
End While;
    
End If;
    
While i > 0 Do
        i := i - 
1;
    
End While;
    
Return State;
End Function GetState;

Example

Executing the example requires that the repository contains a calculation algorithm with the ALGORITHM identifier that contains at least two calculation objects.

Add links to the Algo, Metabase system assemblies. Add links to the assemblies required for working with calculation algorithms.

Sub UserProc;
Var
    MB: IMetabase;
    MObj: IMetabaseObjectDescriptor;
    Algo: ICalcObject;
    ObjList, CloneList: ICalcObjectsList;
    CalcAlgo: ICalcAlgorithm;
    CalcResult: IAlgorithmCalculationAsyncResult;
    Result: IAlgorithmCalculationResult;
    Load: IAlgorithmTimeResults;
    TimeResult: IAlgorithmTimeResult;
    ErrEvent: IAlgorithmCalculateErrorEvent;
    State: CalculateState;
    i: Integer;
    Sec: Double;
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
    ObjList := CalcAlgo.Items;
    // Copy list of calculation algorithm objects
    CloneList := ObjList.Clone;
    // Delete excluded objects before calculation
    For i := CloneList.Count - 1 To 0 Step - 1 Do
        If Not CalcAlgo.Included(CloneList.Item(i)) Then
            CloneList.Remove(CloneList.Item(i));
        End If;
    End For;
    // Display the number of objects included in algorithm calculation in the console
    Debug.WriteLine("Number of objects: " + CloneList.Count.ToString);
    // Execute asynchronous algorithm calculation
    CalcResult := CalcAlgo.CalculateAsync(CloneList);
    State := GetState(CalcResult);
    // Check if error occurs on calculation
    If State = CalculateState.PausedOnError Then
        ErrEvent := CalcResult.ErrorEvent;
        If ErrEvent <> Null Then
            // If an error occurs, display information about the error and about the object, on which it occurred, in the console
            Debug.WriteLine(ErrEvent.Message);
            Debug.WriteLine("Object with error: " + CalcResult.CurrentObject.Name);
            // Skip occurred error and continue calculation
            ErrEvent.ErrorState := SkipErrorState.IgnoreAll;
        End If;
    Elseif State = CalculateState.Stoped Then // Check if algorithm calculation is finished
        // Display message about algorithm calculation completion in the console
        Debug.WriteLine("Algorithm calculation is finished:");
        // Data about calculation results
        Result := CalcResult.Result;
        Load := Result.Load;
        Debug.WriteLine("Result of loading data sources:");
        For i := 0 To Load.Count - 1 Do
            TimeResult := Load.Item(i);
            Debug.WriteLine(" Name: " + TimeResult.Name);
            Debug.WriteLine(" Identifier: " + TimeResult.Id);
            Sec := TimeResult.ExecuteMilisecods / 1000;
            Debug.WriteLine(" Loading time: " + Sec.ToString + " sec.");
            Debug.WriteLine("");
        End For;
    End If;
End Sub UserProc;

Function GetState(CalcResult: IAlgorithmCalculationAsyncResult): CalculateState;
Var
    State: CalculateState;
    i: Integer = 1000;
Begin
    // Check calculation state
    If CalcResult <> Null Then
        State := CalcResult.State;
        While 
            (State <> CalculateState.PausedOnDebug) And
            (State <> CalculateState.PausedOnError) And 
            (State <> CalculateState.Aborted) And 
            (State <> CalculateState.Stoped) Do
            State := CalcResult.State;
        End While;
    End If;
    While i > 0 Do
        i := i - 1;
    End While;
    Return State;
End Function GetState;

After executing the example, the algorithm is asynchronously calculated. The console displays the number of calculated objects and the message about calculation completion, for example:

Number of objects: 3

Algorithm calculation is completed

See also:

ICalcAlgorithm