Alias.java
1package com.gams.examples.alias;
2 
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.util.ArrayList;
8 import java.util.List;
9 
10 import com.gams.api.GAMSDatabase;
11 import com.gams.api.GAMSGlobals;
12 import com.gams.api.GAMSJob;
13 import com.gams.api.GAMSParameter;
14 import com.gams.api.GAMSSet;
15 import com.gams.api.GAMSSymbol;
16 import com.gams.api.GAMSWorkspace;
18 
25 public class Alias {
26 
27  public static void main(String[] args) throws Exception {
28  // check workspace info from command line arguments
29  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
30  if (args.length > 0)
31  wsInfo.setSystemDirectory( args[0] );
32  // create a directory
33  File workingDirectory = new File(System.getProperty("user.dir"), "alias");
34  workingDirectory.mkdir();
35  wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
36  // create a workspace
37  GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
38 
39  // Create initial data containing a GAMS Alias
40  // The OO API does not know about Aliases and will retrieve it as a set
41  GAMSJob j1 = ws.addJobFromString(data);
42  j1.run();
43 
44  checkAliasLogic("j1.OutDB", j1.OutDB());
45 
46  j1.OutDB().export("outdb.gdx");
47  MyAssert(SameGdxDump(ws, "outdb.gdx", gdxdump1), " Unexpected result of gdxdump outdb.gdx");
48 
49  // Copy constructor should preserve aliases and other
50  GAMSDatabase db = ws.addDatabase(j1.OutDB());
51  checkAliasLogic("db ", db);
52  db.export("db.gdx");
53  MyAssert(SameGdxDump(ws, "db.gdx", gdxdump1), "Unexpected result of gdxdump db.gdx");
54 
55  GAMSDatabase db2 = ws.addDatabase();
56  GAMSSet ii = db2.addSet(db.getSet("ii").getName(), db.getSet("ii").getText(), "*");
57  db.getSet("ii").copySymbol(ii);
58 
59  GAMSParameter aaOriginal = db.getParameter("aa");
60  GAMSParameter aa = db2.addParameter(db.getParameter("aa").getName(), db.getParameter("aa").getText(), ii);
61  aaOriginal.copySymbol(aa);
62  db2.export("db2.gdx");
63 
64  MyAssert(SameGdxDump(ws, "db2.gdx", gdxdump2), " Unexpected result of gdxdump db2.gdx");
65 
66  // If the domain is an alias, domains should return the aliased set,
67  // but getDomainsAsStrings should return the name of the alias
68  MyAssert(aaOriginal.getDomains().get(0) instanceof GAMSSet, "The domain set should be a GAMSSet");
69  MyAssert(((GAMSSet)aaOriginal.getDomains().get(0)).getName().equals("i"), "The domain set should be the original set");
70  MyAssert(aaOriginal.getDomainsAsStrings().get(0).equals("ii"), "The domain as string should be the alias name");
71  }
72 
73  static void MyAssert(boolean test, String msg) throws Exception {
74  if (!test)
75  throw new Exception(msg);
76  }
77 
78  static void checkAliasLogic(String prefix, GAMSDatabase aliasDB) throws Exception {
79  // Check number of symbols
80  MyAssert(aliasDB.getNumberOfSymbols() == 5, prefix + " aliasDB should have NrSymbols=5: i,j,ij,a,aa.");
81 
82  int cntSymbols = 0;
83  for (@SuppressWarnings("unused") GAMSSymbol<?> sym : aliasDB)
84  cntSymbols++;
85  MyAssert(cntSymbols == 5, prefix + " there sould be 5 GAMSSymbols in aliasDB: i,j,ij,a,aa.");
86 
87  // See if we can retrieve alias sets
88  MyAssert(aliasDB.getSet("ii").getName().equals("i"), prefix + " We should get set i when asking for alias ii.");
89  MyAssert(aliasDB.getSet("jj").getName().equals("j"), prefix + " We should get set j when asking for alias jj.");
90  MyAssert(aliasDB.getSet("iijj").getName().equals("ij"), prefix + " We should get set ij when asking for alias iijj.");
91 
92  // Check domain logic
93  MyAssert(aliasDB.checkDomains() == true, prefix + " Check domains should be true");
94  MyAssert(aliasDB.getParameter("aa").getDomains().get(0) instanceof GAMSSet, prefix + " domain[0] of aa should be set");
95  MyAssert(((GAMSSet)aliasDB.getParameter("aa").getDomains().get(0)).getName().equals("i"), prefix + " domain[0] of aa should point to i");
96 
97  aliasDB.getSet("ii").deleteRecord("i1");
98  MyAssert(aliasDB.checkDomains() == false, prefix + " Check domains should be false after removal of i1");
99  aliasDB.getSet("ii").addRecord("i1");
100  MyAssert(aliasDB.checkDomains() == true, prefix + " Check domains should be true after adding i1 again");
101 
102  }
103 
104  static boolean SameGdxDump(GAMSWorkspace ws, String gdxfile, String expectedResult) {
105 
106  List<String> arguments = new ArrayList<String>();
107  arguments.add( ws.systemDirectory() + GAMSGlobals.FILE_SEPARATOR + "gdxdump" );
108  arguments.add( new File(ws.workingDirectory(),gdxfile).getAbsolutePath() );
109 
110  ProcessBuilder pb = new ProcessBuilder(arguments);
111  pb.directory( new File(ws.systemDirectory()) );
112 
113  try {
114  Process p = pb.start();
115 
116  BufferedReader stdOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
117  StringBuilder sb = new StringBuilder();
118  String s = null;
119  while ((s = stdOutput.readLine()) != null) {
120  sb.append(s);
121  sb.append(GAMSGlobals.LINE_SEPARATOR);
122  }
123  stdOutput.close();
124 
125  if (p.waitFor() != 0)
126  return false;
127 
128  return (expectedResult.replaceAll("\\s+","").equalsIgnoreCase( sb.toString().replaceAll("\\s+","")) );
129 
130  } catch (InterruptedException e) {
131  return false;
132  } catch (IOException e) {
133  return false;
134  }
135  }
136 
137  static String data =
138  "set i / i1*i3 /" + GAMSGlobals.LINE_SEPARATOR +
139  " j / j1*j3 /" + GAMSGlobals.LINE_SEPARATOR +
140  " ij / #i:#j /" + GAMSGlobals.LINE_SEPARATOR +
141  "alias (i,ii), (j,jj), (ij,iijj);"+ GAMSGlobals.LINE_SEPARATOR +
142  "parameter" + GAMSGlobals.LINE_SEPARATOR +
143  " a(i) / #i 1 /, aa(ii) / #ii 2 /;"+ GAMSGlobals.LINE_SEPARATOR +
145 
146  static String gdxdump1 =
147  "$onempty"+ GAMSGlobals.LINE_SEPARATOR +
149  "Set i(*) /"+ GAMSGlobals.LINE_SEPARATOR +
150  "'i1'," + GAMSGlobals.LINE_SEPARATOR +
151  "'i2'," + GAMSGlobals.LINE_SEPARATOR +
152  "'i3' /;" + GAMSGlobals.LINE_SEPARATOR +
154  "Set j(*) /"+ GAMSGlobals.LINE_SEPARATOR +
155  "'j1'," + GAMSGlobals.LINE_SEPARATOR +
156  "'j2'," + GAMSGlobals.LINE_SEPARATOR +
157  "'j3' /;" + GAMSGlobals.LINE_SEPARATOR +
159  "Set ij(*,*) /" + GAMSGlobals.LINE_SEPARATOR +
161  "'i1'.'j1'," + GAMSGlobals.LINE_SEPARATOR +
162  "'i2'.'j2'," + GAMSGlobals.LINE_SEPARATOR +
163  "'i3'.'j3' /;" + GAMSGlobals.LINE_SEPARATOR +
165  "Alias (ii, i);" + GAMSGlobals.LINE_SEPARATOR +
167  "Alias (jj, j);" + GAMSGlobals.LINE_SEPARATOR +
169  "Alias (iijj, ij);" + GAMSGlobals.LINE_SEPARATOR +
171  "Parameter a(i) /" + GAMSGlobals.LINE_SEPARATOR +
172  "'i1' 1," + GAMSGlobals.LINE_SEPARATOR +
173  "'i2' 1," + GAMSGlobals.LINE_SEPARATOR +
174  "'i3' 1 /;" + GAMSGlobals.LINE_SEPARATOR +
176  "Parameter aa(ii) /" + GAMSGlobals.LINE_SEPARATOR +
177  "'i1' 2," + GAMSGlobals.LINE_SEPARATOR +
178  "'i2' 2," + GAMSGlobals.LINE_SEPARATOR +
179  "'i3' 2 /;" + GAMSGlobals.LINE_SEPARATOR +
181  "$offempty"+ GAMSGlobals.LINE_SEPARATOR;
182 
183  static String gdxdump2 =
184  "$onempty" + GAMSGlobals.LINE_SEPARATOR +
186  "Set i(*) /" + GAMSGlobals.LINE_SEPARATOR +
187  "'i1'," + GAMSGlobals.LINE_SEPARATOR +
188  "'i2'," + GAMSGlobals.LINE_SEPARATOR +
189  "'i3' /;" + GAMSGlobals.LINE_SEPARATOR +
191  "Parameter aa(i) / \n" +
192  "'i1' 2," + GAMSGlobals.LINE_SEPARATOR +
193  "'i2' 2," + GAMSGlobals.LINE_SEPARATOR +
194  "'i3' 2 /;" + GAMSGlobals.LINE_SEPARATOR +
196  "$offempty"+ GAMSGlobals.LINE_SEPARATOR;
197 
198 }
199 
static final String LINE_SEPARATOR
GAMSParameter getParameter(String identifier)
void setSystemDirectory(String directory)
The Object-oriented API does not have the concept of a GAMS alias.
Definition: Alias.java:25
GAMSParameter addParameter(String identifier, int dimension)
GAMSSet addSet(String identifier, int dimension)
GAMSSet getSet(String identifier)
GAMSJob addJobFromString(String source)
void setWorkingDirectory(String directory)
static final String FILE_SEPARATOR
List< String > getDomainsAsStrings()
GAMSDatabase OutDB()
T addRecord(Vector< String > keys)
void copySymbol(GAMSSymbol<?> target)
boolean deleteRecord(String ... keys)