Calculating a Modeling Problem

Executing the example requires that the repository contains a modeling container with the CONT_MODEL identifier. This container should include a problem with the PROBLEM_1 identifier.

Fore Example

Add links to the Metabase, Ms system assemblies.

Public Class MCallback: Object, IMsProblemCalculationCallback
    Public Calculation: IMsProblemCalculation;
    
    Public Sub OnError(Message: String);
    Begin
        Debug.WriteLine(Message);
    End Sub OnError;
    
    Public Sub OnFinish;
    Begin
        Debug.WriteLine(Problem calculation is completed);
        Calculation := Null;
    End Sub OnFinish;
    
    Public Sub OnMessage(Message: String);
    Begin
        Debug.Write(DateTime.Now);
        Debug.WriteLine(": " + Message);
    End Sub OnMessage;
    
    Public Sub OnModelCalculation(Model: IMsModel);
    Begin
        Debug.WriteLine("Model calculation: " + (Modelnbsp;Asnbsp;IMetabaseObject).Id);
    End Sub OnModelCalculation;
    
    Public Sub OnStep;
    Begin
        Debug.WriteLine(Steps executed:  + Calculation.PointPassed.ToString +
             from  + Calculation.PointCount.ToString);
    End Sub OnStep;
    
    Public Sub OnBreak(Breakpoint: IMsBreakpoint);
    Begin
        Debug.WriteLine(Breakpoint.Name);
    End Sub OnBreak;
    
    Public Sub OnWarning(Message: string);
    Begin
        Debug.WriteLine("Warning: "nbsp;+nbsp;Message);
    End Sub OnWarning;
End Class MCallback;

Sub UserProc;
Var
    MB: IMetabase;
    Ms: IMetabaseObjectDescriptor;
    Problem: IMsProblem;
    CallBack: MCallback;
    CalcSett: IMsProblemCalculationSettings;
    Scens: IMsProblemScenarios;
    Calculation: IMsProblemCalculation;
    i: Integer;
Begin
    MB := MetabaseClass.Active;
    Ms := MB.ItemById("CONT_MODEL");
    Problem := MB.ItemByIdNamespace("PROBLEM_1", Ms.Key).Bind As IMsProblem;
    CallBack := New MCallback.Create;
    CalcSett := Problem.CreateCalculationSettings;
    CalcSett.Callback := CallBack;
    Scens := Problem.Scenarios;
    For i := 0 To Scens.Count - 1 Do
        CalcSett.ScenarioIncluded(Scens.Item(i)) := True;
    End For;
    Calculation := Problem.Calculate(CalcSett);
    Calculation.SaveHistory := True;
    CallBack.Calculation := Calculation;
    Calculation.Run;
End Sub UserProc;

Fore.NET Example

Imports Prognoz.Platform.Interop.Ms;

Public Class MCallback: Object, IMsProblemCalculationCallback
    Public Calculation: IMsProblemCalculation;
    
    Public Sub OnError(Message: String);
    Begin
        System.Diagnostics.Debug.WriteLine(Message);
    End Sub OnError;
    
    Public Sub OnFinish();
    Begin
        System.Diagnostics.Debug.WriteLine(Problem calculation is completed);
        Calculation := Null;
    End Sub OnFinish;
    
    Public Sub OnMessage(Message: String);
    Begin
        System.Diagnostics.Debug.Write(DateTime.Now);
        System.Diagnostics.Debug.WriteLine(": " + Message);
    End Sub OnMessage;
    
    Public Sub OnModelCalculation(Model: IMsModel);
    Begin
        System.Diagnostics.Debug.WriteLine("Model calculation: " + (Modelnbsp;Asnbsp;IMetabaseObject).Id);
    End Sub OnModelCalculation;
    
    Public Sub OnStep();
    Begin
        System.Diagnostics.Debug.WriteLine("Steps executed: " + Calculation.PointPassed.ToString() +
             from  + Calculation.PointCount.ToString());
    End Sub OnStep;
    
    Public Sub OnBreak(Breakpoint: IMsBreakpoint);
    Begin
        System.Diagnostics.Debug.WriteLine(Breakpoint.Name);
    End Sub OnBreak;
    
    Public Sub OnWarning(Message: string);
    Begin
        System.Diagnostics.Debug.WriteLine("Warning: "nbsp;+nbsp;Message);
    End Sub OnWarning;
End Class MCallback;

Public Class Program
    [STAThread]
    Public Shared Sub Main(Params: StartParams);
    Var
        MB: IMetabase;
        Ms: IMetabaseObjectDescriptor;
        Problem: IMsProblem;
        CallBack: MCallback;
        CalcSett: IMsProblemCalculationSettings;
        Scens: IMsProblemScenarios;
        Calculation: IMsProblemCalculation;
        i: Integer;
    Begin
        MB := Params.Metabase;
        Ms := MB.ItemById["CONT_MODEL"];
        Problem := MB.ItemByIdNamespace["PROBLEM_1", Ms.Key].Bind() As IMsProblem;
        CallBack := New MCallback.Create();
        CalcSett := Problem.CreateCalculationSettings();
        CalcSett.Callback := CallBack;
        Scens := Problem.Scenarios;
        For i := 0 To Scens.Count - 1 Do
            CalcSett.ScenarioIncluded[Scens.Item[i]] := True;
        End For;
        Calculation := Problem.Calculate(CalcSett);
        Calculation.SaveHistory := True;
        CallBack.Calculation := Calculation;
        Calculation.Run();
    End Sub;
End Class;

Result of the Fore and Fore.NET Examples Execution

After executing the example the problem with the PROBLEM_1 identifier is calculated. The calculation is performed by all the scenarios contained in the problem. For processing the events occurring during the calculation the MCallback user class object is used. Calculation history is saved.

See also:

Examples