Monitor Identifiers for Changes

By default, AIMMS asks you whether you want to save changes to data when you close your project. This behavior depends on which data categories and case type are currently active.

You can also use the data change monitor functions to check when there are changes in a given subset of identifiers in your model since the last time you checked, based on a data category and case type.

Functions of data change monitor

The data change monitor has the following intrinsic functions:

Creating data change monitor

To create a new data change monitor, first provide it an ID and a subset of the predefined set AllIdentifiers containing the identifiers you want to monitor for changes.

In the example code below, s_WatchedIdentifiers is a subset of AllIdentifiers and sp_DataChangeMonitorID is a string parameter.

1!The identifiers we are interested in checking for changes
2s_WatchedIdentifiers := data { 'p_myParameter1' , 'p_myParameter2' ,
3                        'p_myDefinedParameter' } ;
4
5!Create a name that will be the ID for this data change monitor
6sp_DataChangeMonitorID := "Monitor parameters" ;

Then, use the intrinsic function DataChangeMonitorCreate as shown below:

1!Create the actual Data Change Monitor with the ID denoted
2!by the string parameter DataChangeMonitorID and monitor the
3!changes of identifiers present in the set WatchedIdentifiers
4DataChangeMonitorCreate(
5    ID                   : sp_DataChangeMonitorID  ,
6    monitoredIdentifiers : s_WatchedIdentifiers ,
7    ExcludeNoneSaveables : 1) ;

The third argument of the function indicates whether you want to exclude identifiers with the NoSave property. Identifiers that have this property will not be stored in cases.

It’s a good practice to delete any pre-existing monitor with the same name before you create the monitor, to avoid errors. You can do this with the example code below:

 1!Make sure that we delete any data change monitor with this name
 2!if it already exists. The function DataChangeMonitorDelete will
 3!return 1 in case of success, 0 in case it did not find a monitor
 4!with the given ID and -1 for all other errors.
 5p_retVal := DataChangeMonitorDelete( sp_DataChangeMonitorID )  ;
 6
 7if p_retVal = 1 then
 8    raise warning "Deleted existing data change monitor with ID \""
 9                  + sp_DataChangeMonitorID  + "\"" ;
10endif ;
11
12if p_retVal = -1 then
13    raise error "Error while deleting data change monitor with ID \""
14                + sp_DataChangeMonitorID +"\"\n"
15                + "CurrentErrorMessage = " + CurrentErrorMessage ;
16endif ;

Checking the data for changes

After you create the data change monitor, you can query it with the intrinsic function DataChangeMonitorHasChanged to check if the data monitored by it has changed. See the example below:

1!Now modify the data
2p_myParameter2 := 3.14 ;
3
4!And check if the data is indeed changed. You should see this
5!DialogMessage appear
6if DataChangeMonitorHasChanged(sp_DataChangeMonitorID) then
7    DialogMessage("Data changed (2) - Should show dialog") ;
8endif ;

Note that the data change monitor functions also work to monitor defined identifiers for changes. However, AIMMS might not recalculate the definition of a parameter if you have not used the explicit update statement or have not accessed the data of the identifier yet.

Let’s take the two parameters below:

1Parameter p_myParameter2;
2Parameter p_myDefinedParameter {
3    Definition: 2*p_myParameter2;
4}

If you are monitoring p_myDefinedParameter for changes, the following code will not show a dialog message:

 1p_myParameter2 := 1998 ;
 2
 3!you might expect the monitor to indicate here that the data has changed.
 4!However, as explained above, the data change monitor does not evaluate
 5!definitions, so as long as the identifier myDefinedParameter has not been
 6!updated (either explicitly with update statement or implicitly by accessing
 7!its data), the datachange monitor will not indicate any changes
 8if DataChangeMonitorHasChanged(sp_DataChangeMonitorID) then
 9    DialogMessage("Data defined parameter changed - Should not show dialog!");
10endif ;

The monitor detects the changed data only after the data of parameter p_myDefinedParameter has been accessed (e.g. by showing it in a GUI or using it in an assignment statement) or you have explicitly instructed AIMMS to recalculate the definition with the update statement. See below:

1!Explicitly update the parameter, causing an evaluation of the definition
2update p_myDefinedParameter ;
3
4!Now the data of myDefinedParameter has changed (because of the update
5!statement and the DataChangeMonitor will indicate a change also. This
6!means that you should see the dialogmessage pop up
7if DataChangeMonitorHasChanged(sp_DataChangeMonitorID) then
8    DialogMessage("Data defined parameter changed - Should show dialog");
9endif ;

Download example

You can download code snippets used in this article from the link below: