1package com.gams.examples.alias;
3import java.io.BufferedReader;
5import java.io.IOException;
6import java.io.InputStreamReader;
7import java.util.ArrayList;
27 public static void main(String[] args)
throws Exception {
33 File workingDirectory =
new File(System.getProperty(
"user.dir"),
"alias");
34 workingDirectory.mkdir();
44 checkAliasLogic(
"j1.OutDB", j1.
OutDB());
47 MyAssert(SameGdxDump(ws,
"outdb.gdx", gdxdump1),
" Unexpected result of gdxdump outdb.gdx");
51 checkAliasLogic(
"db ", db);
53 MyAssert(SameGdxDump(ws,
"db.gdx", gdxdump1),
"Unexpected result of gdxdump db.gdx");
64 MyAssert(SameGdxDump(ws,
"db2.gdx", gdxdump2),
" Unexpected result of gdxdump db2.gdx");
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");
73 static void MyAssert(
boolean test, String msg)
throws Exception {
75 throw new Exception(msg);
78 static void checkAliasLogic(String prefix,
GAMSDatabase aliasDB)
throws Exception {
80 MyAssert(aliasDB.
getNumberOfSymbols() == 5, prefix +
" aliasDB should have NrSymbols=5: i,j,ij,a,aa.");
85 MyAssert(cntSymbols == 5, prefix +
" there sould be 5 GAMSSymbols in aliasDB: i,j,ij,a,aa.");
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.");
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");
98 MyAssert(aliasDB.
checkDomains() ==
false, prefix +
" Check domains should be false after removal of i1");
100 MyAssert(aliasDB.
checkDomains() ==
true, prefix +
" Check domains should be true after adding i1 again");
104 static boolean SameGdxDump(
GAMSWorkspace ws, String gdxfile, String expectedResult) {
106 List<String> arguments =
new ArrayList<String>();
110 ProcessBuilder pb =
new ProcessBuilder(arguments);
114 Process p = pb.start();
116 BufferedReader stdOutput =
new BufferedReader(
new InputStreamReader(p.getInputStream()));
117 StringBuilder sb =
new StringBuilder();
119 while ((s = stdOutput.readLine()) !=
null) {
125 if (p.waitFor() != 0)
128 return (expectedResult.replaceAll(
"\\s+",
"").equalsIgnoreCase( sb.toString().replaceAll(
"\\s+",
"")) );
130 }
catch (InterruptedException e) {
132 }
catch (IOException e) {
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 +
146 static String gdxdump1 =
147 "$onempty"+ GAMSGlobals.LINE_SEPARATOR +
148 GAMSGlobals.LINE_SEPARATOR +
149 "Set i(*) /"+ GAMSGlobals.LINE_SEPARATOR +
150 "'i1'," + GAMSGlobals.LINE_SEPARATOR +
151 "'i2'," + GAMSGlobals.LINE_SEPARATOR +
152 "'i3' /;" + GAMSGlobals.LINE_SEPARATOR +
153 GAMSGlobals.LINE_SEPARATOR +
154 "Set j(*) /"+ GAMSGlobals.LINE_SEPARATOR +
155 "'j1'," + GAMSGlobals.LINE_SEPARATOR +
156 "'j2'," + GAMSGlobals.LINE_SEPARATOR +
157 "'j3' /;" + GAMSGlobals.LINE_SEPARATOR +
158 GAMSGlobals.LINE_SEPARATOR +
159 "Set ij(*,*) /" + GAMSGlobals.LINE_SEPARATOR +
160 GAMSGlobals.LINE_SEPARATOR +
161 "'i1'.'j1'," + GAMSGlobals.LINE_SEPARATOR +
162 "'i2'.'j2'," + GAMSGlobals.LINE_SEPARATOR +
163 "'i3'.'j3' /;" + GAMSGlobals.LINE_SEPARATOR +
164 GAMSGlobals.LINE_SEPARATOR +
165 "Alias (ii, i);" + GAMSGlobals.LINE_SEPARATOR +
166 GAMSGlobals.LINE_SEPARATOR +
167 "Alias (jj, j);" + GAMSGlobals.LINE_SEPARATOR +
168 GAMSGlobals.LINE_SEPARATOR +
169 "Alias (iijj, ij);" + GAMSGlobals.LINE_SEPARATOR +
170 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 +
175 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 +
180 GAMSGlobals.LINE_SEPARATOR +
183 static String gdxdump2 =
184 "$onempty" + GAMSGlobals.LINE_SEPARATOR +
185 GAMSGlobals.LINE_SEPARATOR +
186 "Set i(*) /" + GAMSGlobals.LINE_SEPARATOR +
187 "'i1'," + GAMSGlobals.LINE_SEPARATOR +
188 "'i2'," + GAMSGlobals.LINE_SEPARATOR +
189 "'i3' /;" + GAMSGlobals.LINE_SEPARATOR +
190 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 +
195 GAMSGlobals.LINE_SEPARATOR +
GAMSParameter getParameter(String identifier)
GAMSSet addSet(String identifier, int dimension)
GAMSParameter addParameter(String identifier, int dimension)
GAMSSet getSet(String identifier)
static final String LINE_SEPARATOR
static final String FILE_SEPARATOR
boolean deleteRecord(String ... keys)
List< String > getDomainsAsStrings()
void copySymbol(GAMSSymbol<?> target)
T addRecord(Vector< String > keys)
List< Object > getDomains()
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
String workingDirectory()
GAMSDatabase addDatabase()
The Object-oriented API does not have the concept of a GAMS alias.
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).