haorenhui
2023-10-30 6d6cc10d9e8e242661da7fd655dec155a09d676c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Quintiq file version 2.0
#parent: #root
Method OnDatasetConstructed
{
  Description:
  [*
    Run this when the dataset with a `LibOpt_Optimization` inside is constructed.
    
    We advise to use the following snippet in the OnConstructed of the dataset:
    
        if( isnull( this.Optimization() ) )
        {
            this.Optimization( relnew );
        }
        this.Optimization().OnDatasetConstructed();
  *]
  TextBody:
  [*
    // Migration: Update existing datasets where `LibOpt_Optimization` does not have a `LibOpt_Beacon`.
    LibOpt_Beacon::Update( this );
    this.Update();
    
    traverse( this, Optimizer.Run, run )
    {
      run.OnDatasetConstructed();
    }
    
    // Reset the `LibOpt_Optimization.NextRunNr`
    // When a run is deserialized it may get a `RunNr` that is higher or equal to the `NextRunNr`.
    // While the dataset is running, the correct `NextRunNr` can be obtained from the `AlgorithmStore`, where it's backed up.
    // After a server restart this value is no longer in the `AlgorithmStore`, hence we need to calculate it again.
    this.NextRunNr( maxvalue( this.NextRunNr(),
                              max( this, Optimizer.Run, run, true, run.RunNr() ) + 1 ) );
    
    // If the current dataset is a copy of some other dataset, then we set the MDSKeyParentDataset attribute.
    // This allows us to return to the parent dataset by using the 'Reload' button in the 'Replannable snapshots' form. 
    if( this.MDSID() <> this.MDSKeyCurrentDataset() )
    {
      this.MDSKeyParentDataset( this.MDSKeyCurrentDataset() );  
    }
    this.MDSKeyCurrentDataset( this.MDSID() );
    
    // Get the name of the current dataset.
    // This value is used to set `LibOpt_ControllerRun.DatasetName`, which is an attribute used for visualization purposes.
    // the guard below is here in order to prevent errors when the dataset is in temporary storage. For these cases
    // the dataset name is not important.
    this.DatasetName( guard( MDSEditor::Editor().ObjectInfo( this.MDSID() ).Name(), 'TEMPORARY' ) ); 
    
    // Delete all LibOpt_DatasetCopyConditional that were flagged for deletion before this dataset was loaded or copied
    LibOpt_DatasetCopyConditional::DeleteDatasetCopyConditionalWhenFlagged( this, true );
    
    // Create a standalone storage StoredAlgorithmManager (if it isn't already created)
    if( LibOpt_ConfigurationSettings::GetSetting_CreateGlobalAlgorithmStoreDataset() )
    {
      LibOpt_StoredAlgorithmManager::LoadCreateStoredAlgorithmManager();
      // Check for each LibOpt_StoredAlgorithm object if it is still linked to an existing algorithm in the AlgorithmStore. If not, delete the object.
      LibOpt_StoredAlgorithm::Refresh( this );
    }
    
    traverse( this, Optimizer.Run, run, 
              run.StartedOn() <> DateTime::MinDateTime() and run.StartedOnPrecision() = 0 )
    {
      freq := ( OS::PrecisionCounter() / OS::PrecisionCounterFrequency() );
      diff := DateTime::Now() - run.StartedOn();
      run.StartedOnPrecision( freq - diff.TotalInSeconds() );
    }
    
    traverse( this, Optimizer.Run, run, 
              run.FinishedOn() <> DateTime::MinDateTime() and run.FinishedOnPrecision() = 0  )
    {
      freq := ( OS::PrecisionCounter() / OS::PrecisionCounterFrequency() );
      diff := DateTime::Now() - run.FinishedOn();
      max_snapshot := max( run, Snapshot, snap, true, guard( snap.astype( LibOpt_SnapshotComponent ).PrecisionTimeStampDone(), snap.PrecisionTimeStamp() ) );
      run.FinishedOnPrecision( maxvalue( max_snapshot, freq - diff.TotalInSeconds() ) );
    }
  *]
}