Alias.cs
1using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Text.RegularExpressions;
6 using System.Diagnostics;
7 using System.IO;
8 using GAMS;
9 
10 namespace Alias
11 {
24  class Alias
25  {
26  static int Main(string[] args)
27  {
28  try
29  {
30  GAMSWorkspace ws;
31  if (Environment.GetCommandLineArgs().Length > 1)
32  ws = new GAMSWorkspace(systemDirectory: Environment.GetCommandLineArgs()[1]);
33  else
34  ws = new GAMSWorkspace();
35 
36  // Create initial data containing a GAMS Alias
37  // The OO API does not know about Aliases and will retrieve it as a set
38  GAMSJob j1 = ws.AddJobFromString(GetDataText());
39  j1.Run();
40  CheckAliasLogic("j1.OutDB ", j1.OutDB);
41  j1.OutDB.Export("outdb.gdx");
42  MyAssert(SameGdxDump(ws, "outdb.gdx", gdxdump1()), "Unexpected result of gdxdump outdb.gdx");
43 
44  // Copy constructor should preserve aliases and other
45  GAMSDatabase db = ws.AddDatabase(j1.OutDB);
46  CheckAliasLogic("db ", db);
47  db.Export("db.gdx");
48  MyAssert(SameGdxDump(ws, "db.gdx", gdxdump1()), "Unexpected result of gdxdump db.gdx");
49 
50  GAMSDatabase db2 = ws.AddDatabase();
51  GAMSSet ii = db2.AddSet(db.GetSet("ii").Name, db.GetSet("ii").Text, "*");
52  db.GetSet("ii").CopySymbol(ii);
53 
54  GAMSParameter aaOrig = db.GetParameter("aa");
55  GAMSParameter aa = db2.AddParameter(db.GetParameter("aa").Name, db.GetParameter("aa").Text, ii);
56  aaOrig.CopySymbol(aa);
57  db2.Export("db2.gdx");
58  MyAssert(SameGdxDump(ws, "db2.gdx", gdxdump2()), "Unexpected result of gdxdump db2.gdx");
59 
60  // If the domain is an Alias, Domains should return the aliased Set,
61  // but DomainsAsStrings should return the name of the Alias
62  MyAssert(((GAMSSet)aaOrig.Domains[0]).Name == "i", "The domain set should be the original set");
63  MyAssert(aaOrig.DomainsAsStrings[0] == "ii", "The domain as string should be the alias name");
64  }
65  catch (Exception e)
66  {
67  Console.WriteLine("###");
68  Console.WriteLine("### Exception caught:" + e.Message);
69  Console.WriteLine("###");
70  return 1;
71  }
72  return 0;
73  }
74 
75  static void MyAssert(bool test, string msg)
76  {
77  if (!test)
78  throw new System.Exception(msg);
79  }
80 
81  static void CheckAliasLogic(string prefix, GAMSDatabase aliasDB)
82  {
83  // Check number of symbols
84  MyAssert(aliasDB.NrSymbols == 5, prefix + "aliasDB should have NrSymbols=5: i,j,ij,a,aa.");
85 
86  int cntSymbols = 0;
87  foreach (GAMSSymbol sym in aliasDB) cntSymbols++;
88  MyAssert(cntSymbols == 5, prefix + "foreach (GAMSSymbol sym in aliasDB) should result in 5 symbols: i,j,ij,a,aa.");
89 
90  // See if we can retrieve alias sets
91  MyAssert(aliasDB.GetSet("ii").Name == "i", prefix + "We should get set i when asking for alias ii.");
92  MyAssert(aliasDB.GetSet("jj").Name == "j", prefix + "We should get set j when asking for alias jj.");
93  MyAssert(aliasDB.GetSet("iijj").Name == "ij", prefix + "We should get set ij when asking for alias iijj.");
94 
95  // Check domain logic
96  MyAssert(aliasDB.CheckDomains() == true, prefix + "Check domains should be true");
97  MyAssert(aliasDB.GetParameter("aa").Domains[0] is GAMSSet, prefix + "domain[0] of aa should be set");
98  MyAssert(((GAMSSet)aliasDB.GetParameter("aa").Domains[0]).Name == "i", prefix + "domain[0] of aa should point to i");
99 
100  aliasDB.GetSet("ii").DeleteRecord("i1");
101  MyAssert(aliasDB.CheckDomains() == false, prefix + "Check domains should be false after removal of i1");
102  aliasDB.GetSet("ii").AddRecord("i1");
103  MyAssert(aliasDB.CheckDomains() == true, prefix + "Check domains should be true after adding i1 again");
104  }
105 
106  static bool SameGdxDump(GAMSWorkspace ws, string gdxfile, string expectedResult)
107  {
108  string result = string.Empty;
109  ProcessStartInfo start = new ProcessStartInfo();
110 #if __MonoCS__
111  start.FileName = ws.SystemDirectory + Path.DirectorySeparatorChar + "gdxdump";
112 #else
113  start.FileName = ws.SystemDirectory + Path.DirectorySeparatorChar + "gdxdump.exe";
114 #endif
115  start.Arguments = ws.WorkingDirectory + Path.DirectorySeparatorChar + gdxfile;
116  start.UseShellExecute = false;
117  start.RedirectStandardOutput = true;
118  using (Process process = Process.Start(start))
119  {
120  //
121  // Read in all the text from the process with the StreamReader.
122  //
123  using (StreamReader reader = process.StandardOutput)
124  {
125  result = Regex.Replace(reader.ReadToEnd(), @"\s", ""); // remove white space
126  }
127  }
128  return String.Equals(result,expectedResult,StringComparison.OrdinalIgnoreCase);
129  }
130 
131  static String GetDataText()
132  {
133  String data = @"
134 set i / i1*i3 /
135  j / j1*j3 /
136  ij / #i:#j /
137 alias (i,ii), (j,jj), (ij,iijj);
138 parameter
139  a(i) / #i 1 /, aa(ii) / #ii 2 /;
140 ";
141  return data;
142  }
143 
144  static String gdxdump1()
145  {
146  String data = @"
147 $onempty
148 
149 Set i(*) /
150 'i1',
151 'i2',
152 'i3' /;
153 
154 Set j(*) /
155 'j1',
156 'j2',
157 'j3' /;
158 
159 Set ij(*,*) /
160 'i1'.'j1',
161 'i2'.'j2',
162 'i3'.'j3' /;
163 
164 Alias (ii, i);
165 
166 Alias (jj, j);
167 
168 Alias (iijj, ij);
169 
170 Parameter a(i) /
171 'i1' 1,
172 'i2' 1,
173 'i3' 1 /;
174 
175 Parameter aa(ii) /
176 'i1' 2,
177 'i2' 2,
178 'i3' 2 /;
179 
180 $offempty
181 ";
182  return Regex.Replace(data, @"\s", ""); // remove white space
183  }
184 
185  static String gdxdump2()
186  {
187  String data = @"
188 $onempty
189 
190 Set i(*) /
191 'i1',
192 'i2',
193 'i3' /;
194 
195 Parameter aa(i) /
196 'i1' 2,
197 'i2' 2,
198 'i3' 2 /;
199 
200 $offempty
201 ";
202  return Regex.Replace(data, @"\s", ""); // remove white space
203  }
204 
205  }
206 }
GAMSDatabase OutDB
List< object > Domains
GAMSParameter GetParameter(string parameterIdentifier)
List< string > DomainsAsStrings
Definition: Alias.cs:11
GAMSSet GetSet(string setIdentifier)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="")
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
void Export(string filePath=null)
void CopySymbol(GAMSSymbol target)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)