Loading...
Searching...
No Matches
Alias.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Text.RegularExpressions;
6using System.Diagnostics;
7using System.IO;
8using GAMS;
9
10namespace Alias
11{
24 class Alias
25 {
26 static int Main(string[] args)
27 {
28 try
29 {
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__ || __APPLE__
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 = @"
134set i / i1*i3 /
135 j / j1*j3 /
136 ij / #i:#j /
137alias (i,ii), (j,jj), (ij,iijj);
138parameter
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
149Set i(*) /
150'i1',
151'i2',
152'i3' /;
153
154Set j(*) /
155'j1',
156'j2',
157'j3' /;
158
159Set ij(*,*) /
160'i1'.'j1',
161'i2'.'j2',
162'i3'.'j3' /;
163
164Alias (ii, i);
165
166Alias (jj, j);
167
168Alias (iijj, ij);
169
170Parameter a(i) /
171'i1' 1,
172'i2' 1,
173'i3' 1 /;
174
175Parameter 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
190Set i(*) /
191'i1',
192'i2',
193'i3' /;
194
195Parameter 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}
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter GetParameter(string parameterIdentifier)
GAMSSet GetSet(string setIdentifier)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Export(string filePath=null)
GAMSDatabase OutDB
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
void CopySymbol(GAMSSymbol target)
List< object > Domains
List< string > DomainsAsStrings
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
Definition: Alias.cs:11