IMsProblemCalculationCallback.OnBeforeSaveData

Syntax

OnBeforeSaveData(Stub: IVariableStub; Matrix: IMatrix; Var ValueFlag: Integer; Var Cancel: Boolean);

OnBeforeSaveData(Stub: Prognoz.Platform.Interop.Cubes.IVariableStub; Matrix: Prognoz.Platform.Interop.Matrix.IMatrix; Var ValueFlag: Integer; Var Cancel: Boolean);

Parameters

Stub. Data source, to which data is saved.

Matrix. Matrix with saved values.

ValueFlag. Flag that is assigned to changed data in the Matrix matrix.

Cancel. Indicates whether data saving is canceled.

Description

The OnBeforeSaveData method implements the event that occurs before saving data.

Comments

The Matrix variable will have source and calculated data. Source data will have the 0 flag of changed values. Calculated data will have the flag that is equal to value of the ValueFlag parameter. ValueFlag will correspond to the value specified in the IMsProblem.ValueFlag or IMsProblemCalculationSettings.ValueFlag property. If required, one can change matrix data and flag in the ValueFlag parameter. The values are saved that have the flag of changed values equal to value of the ValueFlag parameter.

After the data is saved, the OnSaveData event is generated.

If data saving must be canceled, set the Cancel parameter to True or the ValueFlag parameter to 0.

Example

Executing the example requires a time series database with the TSDB identifier. The modeling container that is a child object of the database has a modeling problem with the TSDB_PROBLEM identifier.

Add links to the Cubes, Matrix, Metabase, Ms, Ui system assemblies.

Sub UserProc;
Var
    Mb: IMetabase;
    Rub: IRubricator;
    Problem: IMsProblem;
    Calculation: IMsProblemCalculation;
    CalcSettings: IMsProblemCalculationSettings;
Begin
    Mb := MetabaseClass.Active;
    Rub := MB.ItemById(
"TSDB").Bind As IRubricator;
    
// Get problem in TSDB modeling container
    Problem := Mb.ItemByIdNamespace("TSDB_PROBLEM", Rub.ModelSpace.Key).Edit As IMsProblem;
    
// Flag that will be assigned to changed data
    Problem.ValueFlag := 2;
    
// Set problem calculation parameters
    CalcSettings := Problem.CreateCalculationSettings;
    CalcSettings.FactIncluded := 
True;
    CalcSettings.BreakOnError := 
True;
    Calculation := Problem.Calculate(CalcSettings);
    
// Set event handler
    Calculation.Callback := New MCallback.Create;
    
// Calculate
    Calculation.Run;
End Sub UserProc;

Public Class MCallback: ProblemCalculationCallback
    
Public Sub OnBeforeSaveData(Stub: IVariableStub; Matrix: IMatrix; Var ValueFlag: Integer; Var Cancel: Boolean);
    
Var
        Iter: IMatrixIterator;
    
Begin
        
If Not WinApplication.ConfirmationBox("Save data?"NullThen
            Cancel := 
True;
        
End If;
        Iter := Matrix.CreateIterator;
        Iter.Move(IteratorDirection.First);
        
While Iter.Valid Do
            If Iter.ValueFlag <> 0 Then
                
If Iter.Value < 100 Then
                    Iter.ValueFlag := ValueFlag + 
1;
                
End If;
            
End If;
            Iter.Move(IteratorDirection.Next);
        
End While;
        
// Increase value of flag of changed data that will be saved by 1
        ValueFlag := ValueFlag + 1;
    
End Sub OnBeforeSaveData;

    
Public Sub OnSaveData(Stub: IVariableStub; Matrix: IMatrix; ValueFlag: Integer; AuditRecordKey: Variant);
    
Var
        Iter: IMatrixIterator;
        RecordCount: Integer;
    
Begin
        Iter := Matrix.CreateIterator;
        Iter.Move(IteratorDirection.First);
        
While Iter.Valid Do
            If Iter.ValueFlag = ValueFlag Then
                RecordCount := RecordCount + 
1;
            
End If;
            Iter.Move(IteratorDirection.Next);
        
End While;
        Debug.WriteLine("Data is saved on ValueFlag: " + ValueFlag.ToString + #13 + #10 +
            
"Number of saved values: " + RecordCount.ToString + #13 + #10 +
            
"Key of record in access protocol: " + AuditRecordKey);
    
End Sub OnSaveData;
End Class MCallback;

Imports Prognoz.Platform.Interop.Cubes;
Imports Prognoz.Platform.Interop.ForeSystem;
Imports Prognoz.Platform.Interop.Matrix;
Imports Prognoz.Platform.Interop.Metabase;
Imports Prognoz.Platform.Interop.Ms;
Imports Prognoz.Platform.Interop.Ui;

Public Shared Sub Main(Params: StartParams);
Var
    Mb: IMetabase;
    Rub: IRubricator;
    Problem: IMsProblem;
    Calculation: IMsProblemCalculation;
    CalcSettings: IMsProblemCalculationSettings;
Begin
    Mb := Params.Metabase;
    Rub := MB.ItemById[
"TSDB"].Bind() As IRubricator;
    
// Get problem in TSDB modeling container
    Problem := Mb.ItemByIdNamespace["TSDB_PROBLEM", Rub.ModelSpace.Key].Edit() As IMsProblem;
    
// Flag that will be assigned to changed data
    Problem.ValueFlag := 2;
    
// Set problem calculation parameters
    CalcSettings := Problem.CreateCalculationSettings();
    CalcSettings.FactIncluded := 
True;
    CalcSettings.BreakOnError := 
True;
    Calculation := Problem.Calculate(CalcSettings);
    
// Set event handler
    Calculation.Callback := New MCallback();
    
// Calculate
    Calculation.Run();
End Sub;

Public Class MCallback: IMsProblemCalculationCallback
    
Public Sub OnBeforeSaveData(Stub: IVariableStub; Matrix: IMatrix; Var ValueFlag: Integer; Var Cancel: Boolean);
    
Var
        WinAppCls: WinApplicationClass = 
New WinApplicationClassClass();
        Iter: IMatrixIterator;
    
Begin
        
If Not WinAppCls.ConfirmationBox("Save data?"NullThen
            Cancel := 
True;
        
End If;
        Iter := Matrix.CreateIterator();
        Iter.Move(IteratorDirection.itdFirst);
        
While Iter.Valid Do
            
If Iter.ValueFlag <> 0 Then
                
If Double.Parse(Iter.Value.ToString()) < 100 Then
                    Iter.ValueFlag := 3;
                
End If;
            
End If;
            Iter.Move(IteratorDirection.itdNext);
        
End While;
        
// Increase value of flag of changed data that will be saved by 1
        ValueFlag := 3;
    
End Sub;

    
Public Sub OnSaveData(Stub: IVariableStub; Matrix: IMatrix; ValueFlag: Integer; AuditRecordKey: Object);
    
Var
        Iter: IMatrixIterator;
        RecordCount: Integer;
    
Begin
        Iter := Matrix.CreateIterator();
        Iter.Move(IteratorDirection.itdFirst);
        
While Iter.Valid Do
            
If Iter.ValueFlag = ValueFlag Then
                RecordCount := RecordCount + 
1;
            
End If;
            Iter.Move(IteratorDirection.itdNext);
        
End While;
        System.Diagnostics.Debug.WriteLine(
"Data is saved on ValueFlag: " + ValueFlag.ToString() + char.ConvertFromUtf32(13) + char.ConvertFromUtf32(10) +
            
"Number of saved values: " + RecordCount.ToString() + char.ConvertFromUtf32(13) + char.ConvertFromUtf32(10) +
            
"Key of record in access protocol: " + AuditRecordKey);
    
End Sub;
    
    
Public Sub OnBreak(Breakp: IMsBreakpoint);
    
Begin
    
End Sub;
    
    
Public Sub OnMessage(Message: String);
    
Begin
        System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + 
" > " + Message);
    
End Sub;
    
    
Public Sub OnError(Message: String);
    
Begin
    
End Sub;
    
    
Public Sub OnFinish();
    
Begin
    
End Sub;
    
    
Public Sub OnModelCalculation(Model: IMsModel);
    
Begin
    
End Sub;
    
    
Public Sub OnStep();
    
Begin
    
End Sub;
    
    
Public Sub OnWarning(Message: string);
    
Begin
    
End Sub;
    
    
Public Sub OnStageStart(Stage: MsProblemCalculationStage; Data: object);
    
Begin
    
End Sub;
    
    
Public Sub OnStageFinish(Stage: MsProblemCalculationStage; Duration: Integer; Data: object);
    
Begin
    
End Sub;
    
    
Public Function OnGetActiveEvents(): MsProblemCalculationCallbackEvents;
    
Begin
        
Return MsProblemCalculationCallbackEvents.mspceAll
    
End Function;
End Class;

After executing the example the modeling problem is calculated. The calculated values will have the 2 flag (ValueFlag = 2).

A save request is displayed in the event that traces data saving. If the answer is positive, the 3 flag is set for calculated values less than 100. After this all the values with the 3 flag will be saved into the time series database. After the data is saved, the development environment console displays information about the number of saved values and key of the record in access protocol that corresponds to data saving.

See also:

IMsProblemCalculationCallback