001: // -------------------------------------------------------------- -*- C++ -*-
002: // File: customdatasource.cpp
003: // --------------------------------------------------------------------------
004: // Licensed Materials - Property of IBM
005: // 
006: // 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 
007: // Copyright IBM Corporation 1998, 2013. All Rights Reserved.
008: //
009: // Note to U.S. Government Users Restricted Rights:
010: // Use, duplication or disclosure restricted by GSA ADP Schedule
011: // Contract with IBM Corp.
012: /////////////////////////////////////////////////////////////////////////////// 
013: 
014: 
015: #include <ilopl/iloopl.h>
016: 
017: #ifndef DATADIR
018: #ifdef ILO_WINDOWS
019: #define DIRSEP "\\"
020: #else
021: #define DIRSEP "/"
022: #endif
023: #define DATADIR ".." DIRSEP ".."  DIRSEP ".." DIRSEP ".." DIRSEP "opl" DIRSEP
024: #endif
025: 
026: ILOSTLBEGIN
027: 
028: 
029: class MyCustomDataSource : public IloOplDataSourceBaseI {
030: public:
031:   MyCustomDataSource(IloEnv& env) : IloOplDataSourceBaseI(env) {};
032:   void read() const;
033: };
034: 
035: 
036: int main(int argc,char* argv[]) {
037:   int status = 127;
038:   IloEnv env;
039: 
040:   try {
041:     IloOplErrorHandler handler(env,cout);
042:     IloOplModelSource modelSource(env, DATADIR "customDataSource" DIRSEP "customDataSource.mod");
043:     IloOplSettings settings(env,handler);
044:     IloOplModelDefinition def(modelSource,settings);
045:     IloCplex cplex(env);
046:     IloOplModel opl(def,cplex);
047:     MyCustomDataSource ds(env);
048:     IloOplDataSource dataSource(&ds);
049:     opl.addDataSource(dataSource);
050:     opl.generate();
051:     status = 0;
052: 
053:   } catch (IloOplException & e) {
054:     cout << "### OPL exception: " << e.getMessage() << endl;
055:   } catch( IloException & e ) {
056:     cout << "### CONCERT exception: ";
057:     e.print(cout);
058:     status = 2;
059:   } catch (...) {
060:     cout << "### UNEXPECTED ERROR ..." << endl;
061:     status = 3;
062:   }
063:   env.end();
064: 
065:   cout << endl << "--Press <Enter> to exit--" << endl;
066:   getchar();
067:   
068:   return status;
069: }
070: 
071: void MyCustomDataSource::read() const {
072:   IloOplDataHandler handler = getDataHandler();
073: 
074:   // initialize the int 'simpleInt'
075:   handler.startElement("anInt");
076:   handler.addIntItem(3);
077:   handler.endElement();
078: 
079:   // initialize the int array 'simpleIntArray'
080:   handler.startElement("anIntArray");
081:   handler.startArray();
082:   handler.addIntItem(1);
083:   handler.addIntItem(2);
084:   handler.addIntItem(3);
085:   handler.endArray();
086:   handler.endElement();
087: 
088:   // initialize int array indexed by float 'anArrayIndexedByFloat'
089:   handler.startElement("anArrayIndexedByFloat");
090:   handler.startIndexedArray();
091:   handler.setItemNumIndex(2.0);
092:   handler.addIntItem(1);
093:   handler.setItemNumIndex(2.5);
094:   handler.addIntItem(2);
095:   handler.setItemNumIndex(1.0);
096:   handler.addIntItem(3);
097:   handler.setItemNumIndex(1.5);
098:   handler.addIntItem(4);
099:   handler.endIndexedArray();
100:   handler.endElement();
101:   
102:   // initialize int array indexed by string 'anArrayIndexedByString'
103:   handler.startElement("anArrayIndexedByString");
104:   handler.startIndexedArray();
105:   handler.setItemStringIndex("idx1");
106:   handler.addIntItem(1);
107:   handler.setItemStringIndex("idx2");
108:   handler.addIntItem(2);
109:   handler.endIndexedArray();
110:   handler.endElement();
111:   
112:   // initialize a tuple in the order the components are declared
113:   handler.startElement("aTuple");
114:   handler.startTuple();
115:   handler.addIntItem(1);
116:   handler.addNumItem(2.3);
117:   handler.addStringItem("not named tuple");
118:   handler.endTuple();
119:   handler.endElement();
120:   
121:   // initialize a tuple using tuple component names.
122:   handler.startElement("aNamedTuple");
123:   handler.startNamedTuple();
124:   handler.setItemName("f");
125:   handler.addNumItem(3.45);
126:   handler.setItemName("s");
127:   handler.addStringItem("named tuple");
128:   handler.setItemName("i");
129:   handler.addIntItem(99);
130:   handler.endNamedTuple();
131:   handler.endElement();
132: 
133:   // initialize the tuple set 'simpleTupleSet'
134:   handler.startElement("aTupleSet");
135:   handler.startSet();
136:   // first tuple
137:   handler.startTuple();
138:   handler.addIntItem(1);
139:   handler.addNumItem(2.5);
140:   handler.addStringItem("a");
141:   handler.endTuple();
142:   // second element
143:   handler.startTuple();
144:   handler.addIntItem(3);
145:   handler.addNumItem(4.1);
146:   handler.addStringItem("b");
147:   handler.endTuple();
148:   handler.endSet();
149:   handler.endElement();
150: 
151:   // initialize element 3 and 2 of the tuple array 'simpleTupleArray' in that particular order
152:   handler.startElement("aTupleArray");
153:   handler.startIndexedArray();
154:   // initialize 3rd cell
155:   handler.setItemIntIndex(3);
156:   handler.startTuple();
157:   handler.addIntItem(1);
158:   handler.addNumItem(2.5);
159:   handler.addStringItem("a");
160:   handler.endTuple();
161:   // initialize second cell
162:   handler.setItemIntIndex(2);
163:   handler.startTuple();
164:   handler.addIntItem(3);
165:   handler.addNumItem(4.1);
166:   handler.addStringItem("b");
167:   handler.endTuple();
168:   handler.endIndexedArray();
169:   handler.endElement();
170: 
171:   // initialize int array indexed by tuple set 'anArrayIndexedByTuple'
172:   handler.startElement("anArrayIndexedByTuple");
173:   handler.startIndexedArray();
174:   handler.startItemTupleIndex();
175:   handler.addIntItem(3);
176:   handler.addNumItem(4.1);
177:   handler.addStringItem("b");
178:   handler.endItemTupleIndex();
179:   handler.addIntItem(1);
180:   handler.endIndexedArray();
181:   handler.endElement();
182: 
183:   //initialize a 2-dimension int array 'a2DIntArray'
184:   handler.startElement("a2DIntArray");
185:   handler.startArray();
186:   for (int i=1;i<=2;i++) {
187:     handler.startArray();
188:     for (int j=1;j<=3;j++)
189:       handler.addIntItem(i*10+j);
190:     handler.endArray();
191:   }  
192:   handler.endArray();
193:   handler.endElement();
194: }
195: 
196: