ContentsIndexHome
PreviousUpNext
C++ Procedure example

The Procedure with arguments example in C++.

 1: #include "aimms/Include.h"
 2: #include <assert.h>
 3: 
 4: // This example demonstrates the use of a procedure with arguments.
 5: // Procedure arguments can be supplied in two ways:
 6: // - Locals can be retrieved using getArgument. This yields an aimms::IData object
 7: //   which can be cast to the actual class and can be used to read/write data directly into 
 8: //   those locals.
 9: // - Global arguments can be used.
10: 
11: int main(int argc, const char* argv[]) 
12: {
13:     if (argc != 3) {
14:         std::cerr << "Invalid number of arguments. usage: <location of AIMMS>  <location of project>" << std::endl; 
15:         return 1;
16:     }  
17: 
18:     aimms::ISession* session = 0;
19: 
20:     try{
21:         session = aimms::openSession(argv[1],argv[2]);
22: 
23:         // Retrieve the procedure.
24:         aimms::IProcedure* maxCheck = session->openProcedure("MaxCheck");
25:         
26:         // MaxCheck verifies that all values of P1(argument0) are less than or equal to MaxValue(argument1).
27:         // If so, it returns 1, else it returns 0.
28: 
29:         // the formal signature of MaxCheck:
30:         // argument0: input;   double parameter over FilledSet
31:         // argument1: input;   double parameter Scalar 
32:  
33:         
34:         // Since we do not have a scalar to supply as second argument, we use the argument of the procedure directly:
35:         aimms::IData* arg = maxCheck->getArgument(1);
36:         assert(arg->getType() == aimms::tScalar);
37:         assert(arg->getValueType() == aimms::vtDouble);
38:         aimms::IScalarData* maxValue = dynamic_cast<aimms::IScalarData*>(arg);
39:         maxValue->setValue(50.0);
40: 
41:         // We also need to open the parameter to be checked.
42:         aimms::IMultiDimData* testParameter = session->openMultiDim("FilledParameter");
43:     
44:         // Determine whether filledparameter has no value over 50:
45:         int result = maxCheck->run(testParameter, maxValue);
46: 
47:         // Since the maximum  is 9 the result will be true:
48:         assert(result == 1);
49: 
50:         // Determine whether filledparameter does have a value over 5: 
51:         maxValue->setValue(5.0);   
52:         result = maxCheck->run(testParameter, maxValue);
53: 
54:         // Since the maximum  is 9 the result will be false:
55:         assert(result == 0); 
56: 
57:         testParameter->close();
58: 
59:         // A procedure will close its arguments, so MaxValue does not need to be closed, although it is allowed.
60:         maxValue->close();
61:         maxCheck->close();
62: 
63:     }  catch (std::exception& e){
64:         std::cerr << e.what();
65: 
66:         if (session) {
67:             session->clearBuffers(); // discards all buffered (and since there is an exception caught, maybe errorneous) modifications 
68:             session->close();
69:         }
70:         return 1;
71:     }
72: 
73:     try{
74:         session->close();
75:         return 0;
76:     } catch (std::exception& e){
77:         std::cerr << e.what();
78:         return 1;
79:     }
80: }