TestGAMSDatabase.cs
1using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using GAMS;
6 using NLog;
7 using NUnit.Framework;
8 
9 namespace GamsApiTests
10 {
12  {
13  static readonly String PROPERTIES_FILE = "test.properties";
14  static GAMSWorkspace globalWS;
15  static GAMSDatabase globalDB;
16  static GAMSWorkspace ws;
17  static GAMSDatabase db;
18  static Logger logger = LogManager.GetCurrentClassLogger();
19 
20  [OneTimeSetUp]
21  public static void OneTimeSetUp()
22  {
23  logger.Debug("entering TestGAMSDatabase | OneTimeSetUp");
24  HouseKeeper.initializeTestFrom(PROPERTIES_FILE, "TestGAMSDatabase");
25  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
26  wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
27  wsInfo.SystemDirectory = HouseKeeper.gamsSystemDir;
28  wsInfo.Debug = DebugLevel.Off;
29  globalWS = new GAMSWorkspace(wsInfo);
30  globalDB = globalWS.AddDatabase("globalDB");
31  initializeDatabase(globalDB);
32  logger.Debug("exiting TestGAMSDatabase | OneTimeSetUp");
33  }
34 
35  [OneTimeTearDown]
36  public static void OneTimeTearDown()
37  {
38  logger.Debug("entering TestGAMSDatabase | OneTimeTearDown");
39  if (globalDB != null)
40  globalDB.Dispose();
41  HouseKeeper.rcleanup(HouseKeeper.gamsWorkingDir);
42  logger.Debug("exiting TestGAMSDatabase | OneTimeTearDown");
43  }
44 
45  [SetUp]
46  public void SetUp()
47  {
48  ws = null;
49  db = null;
50  }
51 
52  [TearDown]
53  public void TearDown()
54  {
55  if (db != null)
56  db.Dispose();
57  }
58 
59  [Test]
60  public void testAddSet()
61  {
62  logger.Debug("entering TestGAMSDatabase | testAddSet");
63  ws = new GAMSWorkspace(HouseKeeper.gamsAbsoluteWorkingDir, null, DebugLevel.Off);
64  db = ws.AddDatabase("testAddSet");
65 
66  String relaxedDomainName = "*";
67 
68  // Set 1.1
69  String s11Name = "set11";
70  GAMSSet s11 = db.AddSet(s11Name, 1);
71 
72  Assert.AreEqual(db, s11.GAMSDatabase, "expect same db reference");
73  Assert.That(s11.Dim == 1, "expect s11 with one dimension");
74  Assert.NotNull(s11.Domains, "do not expect a NULL list object returned");
75  Assert.That(s11.Domains.Count > 0, "do not expect an empty list returned");
76  foreach (Object dom in s11.Domains)
77  {
78  Assert.That(dom is String, "expect String as relaxed domain");
79  Assert.That(relaxedDomainName.Equals(dom), "expect [" + relaxedDomainName +
80  "] string as a relaxed domain.");
81  }
82  Assert.That(s11Name.Equals(s11.Name), "expect ["+s11Name+"] as set name.");
83  Assert.That(s11.Text.Equals(""), "expect an expmty string as set explanatory text.");
84  testSymbolWithoutARecord(s11, s11Name);
85  Assert.AreEqual(1, db.NrSymbols, "expect one symbol in database [db]");
86 
87  // Set 1.2
88  String s12Name = "set12";
89  String s12Text = "This is explanatory text for set12";
90  GAMSSet s12 = db.AddSet(s12Name, 1, s12Text);
91 
92  Assert.AreEqual(db, s12.GAMSDatabase, "expect same db reference");
93  Assert.That(s12.Dim == 1, "expect s12 with one dimension");
94  Assert.NotNull(s12.Domains, "do not expect a NULL list object returned.");
95  Assert.That(s12.Domains.Count > 0, "do not expect an empty list returned");
96  foreach (Object dom in s12.Domains)
97  {
98  Assert.That(dom is String ,"expect string as relaxed domain.");
99  Assert.That(relaxedDomainName.Equals(dom), "expect ["+
100  relaxedDomainName+"] string as relaxed domain.");
101  }
102  Assert.That(s12Name.Equals(s12.Name), "expect [" + s12Name + "] as set name.");
103  Assert.That(s12.Text.Equals(s12Text), "expect [" + s12Text + "] as set explanatory text");
104  testSymbolWithoutARecord(s12, s12Name);
105  Assert.AreEqual(2, db.NrSymbols, "expect two symbols in db");
106 
107  List<String> plants = new List<String> { "Seattle", "San-Diego" };
108  foreach (String p in plants)
109  s12.AddRecord(p);
110 
111  GAMSSet set = db.GetSet(s12Name);
112 
113  GAMSSetRecord rec = set.FirstRecord();
114  Assert.AreEqual(1, rec.Keys.Count());
115  Assert.AreEqual("Seattle", rec.Keys[0]);
116 
117  rec = set.LastRecord();
118  Assert.AreEqual(1, rec.Keys.Count());
119  Assert.AreEqual("San-Diego", rec.Keys[0]);
120 
121  Assert.AreEqual(2, db.NrSymbols, "expect 2 symbols in database [db]");
122 
123  // Set 2
124  String s2Name = "set2";
125  String s2Text = "This is explanatory text for set2";
126 
127  GAMSSet s2 = db.AddSet(s2Name, 2, s2Text);
128 
129  Assert.AreEqual(db, s2.GAMSDatabase);
130  Assert.That(s2.Dim == 2);
131  Assert.NotNull(s2.Domains);
132  Assert.That(s2.Domains.Count > 0);
133  foreach (Object dom in s2.Domains){
134  Assert.That(s2Name.Equals(s2.Name));
135  Assert.That(relaxedDomainName.Equals( (String) dom ));
136  }
137  Assert.That(s2Name.Equals(s2.Name));
138  Assert.That(s2Text.Equals(s2.Text));
139  testSymbolWithoutARecord(s2, s2Name);
140  Assert.AreEqual(3, db.NrSymbols);
141 
142  // Set 3
143  String s3Name = "set3";
144  String s3Text = "This is explanatory text for set3";
145  Object[] domains1 = new Object[] { s11 };
146 
147  GAMSSet s3 = db.AddSet(s3Name, s3Text, domains1);
148  Assert.AreEqual(db, s3.GAMSDatabase);
149  Assert.That(s3.Dim == 1);
150  Assert.NotNull(s3.Domains);
151  Assert.That(s3.Domains.Count > 0);
152 
153  foreach (Object dom in s3.Domains)
154  {
155  // dom and GAMSSet refer the same instance
156  Assert.That(dom is GAMSSet);
157  set = (GAMSSet)dom;
158  // dom and s11 are not the same object instance
159  Assert.That(s11 != set);
160  // dom and s11 do refer to the strings of the same value "s11"
161  Assert.That(s11.Name.Equals(set.Name));
162  }
163  Assert.That(s3Name.Equals(s3.Name));
164  Assert.That(s3Text.Equals(s3.Text));
165  testSymbolWithoutARecord(s3, s3Name);
166  Assert.AreEqual(db.NrSymbols, 4);
167 
168  // Set 4
169  String s4Name = "set4";
170  String s4Text = "This is explanatory text for set4";
171  Object[] domains2 = new Object[] { s11, s12 };
172  GAMSSet s4 = db.AddSet(s4Name, s4Text, domains2);
173 
174  Assert.AreEqual(db, s4.GAMSDatabase);
175  Assert.That(s4.Dim == 2);
176  Assert.NotNull(s4.Domains);
177  Assert.That(s4.Domains.Count > 0);
178 
179  Assert.That(s4.Domains.ElementAt(0) is GAMSSet);
180  GAMSSet domSet1 = (GAMSSet)s4.Domains.ElementAt(0);
181  Assert.That(s11 != domSet1);
182  Assert.That(s11.Name.Equals(domSet1.Name));
183 
184  Assert.That(s4.Domains.ElementAt(1) is GAMSSet);
185  GAMSSet domSet2 = (GAMSSet) s4.Domains.ElementAt(0);
186  Assert.That(s12 != domSet2);
187  Assert.That(s4.Text.Equals(s4Text));
188 
189  Assert.That(s4Name.Equals(s4.Name));
190  Assert.That(s4Text.Equals(s4.Text));
191 
192  testSymbolWithoutARecord(s4, s4Name);
193 
194  Assert.AreEqual(db.NrSymbols, 5);
195 
196  // File 1
197  FileInfo dbFile = new FileInfo(HouseKeeper.gamsAbsoluteWorkingDir +
198  Path.DirectorySeparatorChar + db.Name +
199  HouseKeeper.GDX_FILE_EXTENSION);
200  Assert.False(dbFile.Exists);
201  Assert.AreEqual(db.NrSymbols, 5);
202 
203  db.Export();
204  dbFile.Refresh();
205 
206  Assert.That(dbFile.Exists);
207  Assert.That(dbFile.Length > 0);
208  Assert.AreEqual(db.NrSymbols, 5);
209 
210  // File 2
211  String gdxName = "testAddSetABC";
212  dbFile = new FileInfo(HouseKeeper.gamsAbsoluteWorkingDir +
213  Path.DirectorySeparatorChar + gdxName +
214  HouseKeeper.GDX_FILE_EXTENSION);
215  Assert.False(dbFile.Exists);
216 
217  db.Export(gdxName);
218  dbFile.Refresh();
219 
220  Assert.That(dbFile.Exists);
221  Assert.That(dbFile.Length > 0);
222  Assert.AreEqual(db.NrSymbols, 5);
223  logger.Debug("exiting TestGAMSDatabase | testAddSet");
224  }
225 
226  [Test]
227  public void testAddSetZeroDimension()
228  {
229  logger.Debug("test exception set withzero dimension");
230  Assert.Throws<GAMSException>(() => globalDB.AddSet("set0", 0));
231  }
232 
233  [Test]
234  public void testAddSetWithDuplicateIdentifier()
235  {
236  logger.Debug("test exception set with duplicate identifier");
237  GAMSSet g = globalDB.GetSet("plants");
238  Assert.Throws<GAMSException>(() => globalDB.AddSet(g.Name, g.Dim, g.Text));
239  }
240 
241  [Test]
242  public void testAddSetWithNullIdentifier()
243  {
244  logger.Debug("test exception set with null identifier");
245  Assert.Throws<GAMSException>(() => globalDB.AddSet(null , 1, "text"));
246  }
247 
248  [Test]
249  public void testAddParameterWithNullIdentifier()
250  {
251  logger.Debug("test exception parameter with null identifier");
252  Assert.Throws<GAMSException>(() => globalDB.AddParameter(null, 1, "text"));
253  }
254 
255  [Test]
256  public void testAddVariableWithNullIdentifier()
257  {
258  logger.Debug("test exception variable with null identifier");
259  Assert.Throws<GAMSException>(() => globalDB.AddVariable(null, 1, VarType.Binary, "text"));
260  }
261 
262  [Test]
263  public void testAddEquationWithNullIdentifier()
264  {
265  logger.Debug("test exception equation with null identifier");
266  Assert.Throws<GAMSException>(() => globalDB.AddEquation(null, 1, EquType.E, "text"));
267  }
268 
269  [Test]
270  public void testAddSetWithEmptyIdentifier()
271  {
272  logger.Debug("test exception set with empty identifier");
273  Assert.Throws<GAMSException>(() => globalDB.AddSet("", 1, "text"));
274  }
275 
276  [Test]
277  public void testAddParamterWithEmptyIdentifier()
278  {
279  logger.Debug("test exception paramter with empty identifier");
280  Assert.Throws<GAMSException>(() => globalDB.AddParameter("", 1, "text"));
281  }
282 
283  [Test]
284  public void testAddVariableWithEmptyIdentifier()
285  {
286  logger.Debug("test exception variable with empty identifier");
287  Assert.Throws<GAMSException>(() => globalDB.AddVariable("", 1, VarType.Binary, "text"));
288  }
289 
290  [Test]
291  public void testAddEquationWithEmptyIdentifier()
292  {
293  logger.Debug("test exception equation with empty identifier");
294  Assert.Throws<GAMSException>(() => globalDB.AddEquation("", 1, EquType.E, "text"));
295  }
296 
297  [Test]
298  public void testAddParameter()
299  {
300  logger.Debug("entering TestGAMSDatabase | testAddParamter");
301  ws = new GAMSWorkspace();
302  db = ws.AddDatabase();
303 
304  String pName = "parameter0";
305  GAMSParameter p0 = db.AddParameter(pName, 0);
306  Assert.NotNull(p0);
307  Assert.That(p0.Dim == 0);
308  Assert.That(pName.Equals(p0.Name));
309  Assert.AreEqual(db.NrSymbols, 1);
310  testSymbolWithoutARecord(p0, pName);
311  logger.Debug("extiting TestGAMSDatabase | testAddParamter");
312  }
313 
314  [Test]
315  public void testAddParamterWithDuplicateName()
316  {
317  logger.Debug("test exception parameter with duplicate name");
318  GAMSParameter demand = globalDB.GetParameter("demand");
319  Assert.Throws<GAMSException>(() => globalDB.AddParameter(demand.Name, demand.Dim, demand.Text));
320  }
321 
322  [Test]
323  public void testAddEquation()
324  {
325  logger.Debug("TestGAMSDatabase | testAddEquation");
326  ws = new GAMSWorkspace();
327  db = ws.AddDatabase();
328 
329 
330  String eqName = "z";
331  String eqText = "Equality";
332  GAMSEquation eq1 = db.AddEquation(eqName, 1, EquType.E, "Equality");
333  logger.Info("equation z added");
334  Assert.AreEqual(eq1.GAMSDatabase, db);
335  Assert.NotNull(eq1.Domains);
336  Assert.That(eq1.Domains.Count > 0);
337  Assert.That(eqText.Equals(eq1.Text));
338  Assert.That(eqName.Equals(eq1.Name));
339  Assert.AreEqual(EquType.E, eq1.EquType);
340  testSymbolWithoutARecord(eq1, eqName);
341  Assert.AreEqual(db.NrSymbols, 1);
342 
343  GAMSEquation z = db.GetEquation("z");
344  logger.Info("equation z retrieved");
345  Assert.AreEqual(eq1.Dim, 1);
346  Assert.AreEqual(z.Dim, 1);
347  Assert.AreEqual(EquType.E, z.EquType);
348  Assert.AreEqual(eq1.EquType, z.EquType);
349  Assert.AreEqual(eqText, z.Text);
350  Assert.AreEqual(db.NrSymbols, 1);
351  logger.Debug("TestGAMSDatabase | testAddEquation");
352  }
353 
354  [Test]
355  public void testAddVariable()
356  {
357  logger.Debug("TestGAMSDatabase | testAddVariable");
358 
359  ws = new GAMSWorkspace();
360  db = ws.AddDatabase("xyz");
361 
362  db.AddVariable("x", 1, VarType.Binary, "binary variable x");
363  db.AddVariable("y", 1, VarType.Integer, "integer variable y");
364 
365  Assert.AreEqual(db.NrSymbols, 2);
366 
367  GAMSVariable x = db.GetVariable("x");
368  Assert.AreEqual(1, x.Dim);
369  Assert.AreEqual(VarType.Binary, x.VarType);
370  Assert.AreEqual("binary variable x", x.Text);
371  Assert.AreEqual(db.NrSymbols, 2);
372 
373  GAMSVariable y = db.GetVariable("y");
374  Assert.AreEqual(1, y.Dim);
375  Assert.AreEqual(VarType.Integer, y.VarType);
376  Assert.AreEqual("integer variable y", y.Text);
377  Assert.AreEqual(db.NrSymbols, 2);
378 
379  Assert.Throws<GAMSException>(() => db.GetVariable("z"));
380  Assert.AreEqual(db.NrSymbols, 2);
381  logger.Debug("TestGAMSDatabase | testAddVariable");
382  }
383 
384  [Test]
385  public void testGetNonExistingSet1()
386  {
387  logger.Info("test exception non exisiting Set1");
388  Assert.Throws<GAMSException>(() => globalDB.GetSet("demand"));
389  }
390 
391  [Test]
392  public void testGetNonExistingSet2()
393  {
394  logger.Info("test exception non exisiting Set2");
395  Assert.Throws<GAMSException>(() => globalDB.GetSet("ThisSetDoesNotExist"));
396  }
397 
398  [Test]
399  public void testGetNonExistingParameter()
400  {
401  logger.Info("test exception non exisiting parameter");
402  Assert.Throws<GAMSException>(() => globalDB.GetParameter("ThisParameterDoesNotExist"));
403  }
404 
405  [Test]
406  public void testGetNonExistingVariable()
407  {
408  logger.Info("test exception non exisiting variable");
409  Assert.Throws<GAMSException>(() => globalDB.GetVariable("ThisVariableDoesNotExist"));
410  }
411 
412  [Test]
413  public void testGetNonEistingEquation()
414  {
415  logger.Info("test exception non exisiting equation");
416  Assert.Throws<GAMSException>(() => globalDB.GetEquation("ThisEquationDoesNotExist"));
417  }
418 
419  [Test]
420  public void testGetSymbolNullIdentifier()
421  {
422  logger.Info("test exception symbol null identifier");
423  Assert.Throws<GAMSException>(() => globalDB.GetSymbol(null));
424  }
425 
426  [Test]
427  public void testGetSetNullIdentifier()
428  {
429  logger.Info("test exception set null identifier");
430  Assert.Throws<GAMSException>(() => globalDB.GetSet(null));
431  }
432 
433  [Test]
434  public void testGetParameterNullIdentifier()
435  {
436  logger.Info("test exception parameter null identifier");
437  Assert.Throws<GAMSException>(() => globalDB.GetParameter(null));
438  }
439 
440  [Test]
441  public void testGetVariableNullIdentifier()
442  {
443  logger.Info("test exception variable null identifier");
444  Assert.Throws<GAMSException>(() => globalDB.GetVariable(null));
445  }
446 
447  [Test]
448  public void testGetEquationNullIdentifier()
449  {
450  logger.Info("test exception equation null identifier");
451  Assert.Throws<GAMSException>(() => globalDB.GetEquation(null));
452  }
453 
454  [Test]
455  public void testGetSymbolEmptyIdentifier()
456  {
457  logger.Info("test exception symbol empty identifier");
458  Assert.Throws<GAMSException>(() => globalDB.GetSymbol(""));
459  }
460 
461  [Test]
462  public void testGetSetEmptyIdentifier()
463  {
464  logger.Info("test exception set empty identifier");
465  Assert.Throws<GAMSException>(() => globalDB.GetSet(""));
466  }
467 
468  [Test]
469  public void testGetParamterEmptyIdentifier()
470  {
471  logger.Info("test exception paramter empty identifier");
472  Assert.Throws<GAMSException>(() => globalDB.GetParameter(""));
473  }
474 
475  [Test]
476  public void testGetEquationEmptyidentifier()
477  {
478  logger.Info("test exception equation empty identifier");
479  Assert.Throws<GAMSException>(() => globalDB.GetEquation(""));
480  }
481 
482  [Test]
483  public void testGetVariableEmptyIdentifier()
484  {
485  logger.Info("test exception variable empty identifier");
486  Assert.Throws<GAMSException>(() => globalDB.GetVariable(""));
487  }
488 
489  [Test]
490  public void testAccessToClearedDatabase()
491  {
492  logger.Debug("entering TestGAMSDatabase | testAccessToClearedDatabse");
493 
494  ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
495  GAMSJob job = TestGAMSWorkspace.createAndRunJobFromGamsLib(ws, "trnsport");
496  db = job.OutDB;
497  int numberOfSymbol = db.NrSymbols;
498  db.Clear();
499  logger.Info("database db cleared");
500  Assert.AreEqual(numberOfSymbol, db.NrSymbols);
501 
502  foreach(GAMSSymbol symbol in db){
503  logger.Info("symbol [" + symbol.ToString() + "] retrieved from database db");
504  Assert.NotNull(symbol);
505  Assert.AreEqual(0, symbol.NumberRecords);
506  }
507  logger.Debug("exiting TestGAMSDatabase | testAccessToClearedDatabase");
508  }
509 
510  [Test]
511  public void testAccessToDisposedDatabase1()
512  {
513  logger.Info("test exception access to disposed database 1");
514  ws = new GAMSWorkspace();
515  db = ws.AddDatabase(globalDB);
516 
517  Assert.NotNull(db.GetSet("plants"));
518  foreach (GAMSSymbol symbol in db)
519  Assert.NotNull(symbol);
520 
521  db.Dispose();
522  logger.Info("database db disposed");
523 
524  // do nothing, must not be able to iterate through database.
525  Assert.Throws<GAMSException>(() => db.GetSymbol("x"));
526 
527 
528 
529  }
530 
531  [Test]
532  public void testAccessToDisposedDatabase2()
533  {
534  logger.Debug("test exception access to disposed databse 2");
535  ws = new GAMSWorkspace();
536  db = ws.AddDatabase(globalDB);
537  Assert.NotNull(db.GetSet("plants"));
538  db.Dispose();
539  logger.Debug("database db disposed");
540  Assert.Throws<GAMSException>(() => db.GetSet("plants"));
541  }
542 
543  [Test]
544  public void testExport1()
545  {
546  logger.Debug("entering TestGAMSDatabase | testExport1");
547  String gdxFileName = globalDB.Name;
548  FileInfo dbFile = new FileInfo(HouseKeeper.gamsAbsoluteWorkingDir +
549  Path.DirectorySeparatorChar +
550  gdxFileName +
551  HouseKeeper.GDX_FILE_EXTENSION);
552  Assert.False(dbFile.Exists, "do not expect an existence of [" + dbFile.FullName + "].");
553  globalDB.Export();
554  logger.Info("database globalDB exported");
555  dbFile.Refresh();
556 
557  Assert.That(dbFile.Exists, "expect an existence of [" + dbFile.Name + "]");
558  Assert.That(dbFile.Length > 0, "expect non-empty [" + dbFile.FullName + "].");
559  logger.Debug("exiting testGAMSDAtabase | testExport1");
560  }
561 
562  [Test]
563  public void testExport2()
564  {
565  logger.Debug("entering TestGAMSDatabase | testExport2");
566  ws = new GAMSWorkspace();
567  db = ws.AddDatabase(globalDB, "testExport2_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
568  String gdxFileName = db.Name;
569  FileInfo dbFile = new FileInfo(ws.WorkingDirectory + Path.DirectorySeparatorChar +
570  gdxFileName + HouseKeeper.GDX_FILE_EXTENSION);
571  Assert.False(dbFile.Exists, "do not expect an existence of ["+dbFile.FullName+"].");
572 
573  db.Export();
574  dbFile.Refresh();
575  logger.Info("database db exported");
576 
577  Assert.That(dbFile.Exists, "expect an existence of ["+dbFile.FullName+"].");
578  Assert.That(dbFile.Length > 0, "expect non-empty ["+dbFile.FullName+"].");
579  logger.Debug("exiting TestGAMSDatabase | testExport2");
580  }
581 
582  [Test]
583  public void testExportDomainInfo()
584  {
585  logger.Debug("entering TestGAMSDatabase | testExportDomainInfo");
586 
587  ws = new GAMSWorkspace();
588  String dbName = "testExportDomainInfo_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
589  db = ws.AddDatabase(globalDB, dbName);
590 
591  Object[] domains = new Object[] { db.GetSet("plants"), "*" };
592  db.AddSet("testDomain1", "this is a test parameter", domains);
593 
594  domains = new Object[] { db.GetSet("plants"), "markets" };
595  db.AddParameter("testDomain2", "this is a test parameter", domains);
596 
597  db.Export();
598  logger.Info("database db exported");
599 
600  String gdxFileName = db.Name;
601  FileInfo dbFile = new FileInfo(ws.WorkingDirectory + Path.DirectorySeparatorChar +
602  gdxFileName + HouseKeeper.GDX_FILE_EXTENSION);
603  GAMSDatabase gdxDb = ws.AddDatabaseFromGDX(dbFile.FullName);
604  logger.Debug("database from DGX file [" + dbFile.FullName + "] added to gdxDb");
605 
606  // test domain 1
607  GAMSSet t1 = gdxDb.GetSet("testDomain1");
608  logger.Info("set [testDomain1] retrieved from databse gdxDb");
609 
610  Assert.NotNull(t1, "expect parameter [testDomain1] from gdxDB");
611  Assert.AreEqual(2, t1.Domains.Count, "expect parameter [testDomain1] with 2 domains");
612  Assert.That(t1.Domains[0] is GAMSSet, "expect parameter [testDomain1] with regular domain");
613  Assert.AreEqual("plants", ((GAMSSet)t1.Domains[0]).Name, "expect parameter [testDomain1] with regular domain [plants]");
614  Assert.That(t1.Domains[1] is String, "expect parameter [testDomain1] with relax domain");
615  Assert.AreEqual("*", (String)t1.Domains[1], "expect parameter [testDomain1] with relaxed domain (*)");
616 
617  // test domain 2
618  GAMSParameter t2 = gdxDb.GetParameter("testDomain2");
619  logger.Info("parameter [testDomain2] retrieved from database gdxDB");
620 
621  Assert.NotNull(t2, "expect parameter [testDomain1] from gdxDB");
622  Assert.AreEqual(2, t2.Domains.Count, "expect parameter [testDomain1] with 2 domains");
623  Assert.That(t2.Domains[0] is GAMSSet, "expect parameter [testDomain1] with regular domain");
624  Assert.AreEqual("plants", ((GAMSSet)t2.Domains[0]).Name, "expect parameter [testDomain1] with regular domain (plants)");
625  Assert.That(t2.Domains[1] is GAMSSet, "expect parameter [testDomain1] with regular domain");
626  Assert.AreEqual("markets", ((GAMSSet)t2.Domains[1]).Name, "expect parameter [testDomain1] with regular domain (markets)");
627 
628  // GAMSSet plants
629  GAMSSet i = gdxDb.GetSet("plants");
630  logger.Info("set [plants] retrieved from database gdxDb");
631 
632  Assert.NotNull(i, "expect parameter [plants] from gdxDB");
633  Assert.AreEqual(1, i.Domains.Count, "expect parameter [plants] with 1 domain");
634  Assert.That(i.Domains[0] is String, "expect parameter [plants] with relax domain");
635  Assert.AreEqual("*", (String)i.Domains[0], "expect parameter [plants] with relax domain (*)");
636 
637  // parameter capacity
638  GAMSParameter c = gdxDb.GetParameter("capacity");
639  logger.Info("parameter [capacity] retrieved from database gdxDB");
640 
641  Assert.NotNull(c, "expect parameter [capacity] from gdxDB");
642  Assert.AreEqual(1, c.Domains.Count, "expect parameter [capacity] with 1 domain");
643  Assert.That(c.Domains[0] is GAMSSet);
644  Assert.AreEqual("plants", ((GAMSSet)c.Domains[0]).Name, "expect parameter [capacity] with regular domain (plants)");
645 
646  // parameter demand
647  GAMSParameter d = gdxDb.GetParameter("demand");
648  logger.Info("parameter [demand] retrieved from database gdxDB");
649 
650  Assert.NotNull(d, "expect parameter [demand] from gdxDB");
651  Assert.AreEqual(1, d.Domains.Count, "expect parameter [demand] with one domain");
652  Assert.That(d.Domains[0] is GAMSSet, "expect parameter [demand] with regular domain");
653  Assert.AreEqual("markets", ((GAMSSet)d.Domains[0]).Name, "expect parameter [demand] with regular domain (markets)");
654 
655  // parameter distance
656  GAMSParameter distance = gdxDb.GetParameter("distance");
657  logger.Info("parameter [distance] retrieved from database gdxDB");
658 
659  Assert.NotNull(distance, "expect parameter [distance] from gdxDB");
660  Assert.AreEqual(2, distance.Domains.Count, "expect parameter [distance] with 2 domains");
661  foreach (Object dom in d.Domains)
662  Assert.That(dom is GAMSSet, "expect parameter [distance] with true domain");
663  Assert.AreEqual("plants", ((GAMSSet) distance.Domains[0]).Name, "expect parameter [distance] with regular domain (plants)");
664  Assert.AreEqual("markets", ((GAMSSet)distance.Domains[1]).Name, "expect parameter [distance] with regular domain (markets)");
665  logger.Debug("exiting TestGAMSDatabase | testExportDomainInfo");
666  }
667 
668  [Test]
669  public void testExportWithGDXFileName()
670  {
671  logger.Debug("entering TestGAMSDatabase | testExportWithGDXFileName");
672  String gdxFileName = "testExportWithGDXFileName_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
673  FileInfo dbFile = new FileInfo(HouseKeeper.gamsAbsoluteWorkingDir + Path.DirectorySeparatorChar +
674  gdxFileName + HouseKeeper.GDX_FILE_EXTENSION);
675  Assert.False(dbFile.Exists, "do not expect an existence of [" + dbFile.FullName + "].");
676 
677  globalDB.Export(gdxFileName);
678  dbFile.Refresh();
679  logger.Info("database globalDB exported to file " + gdxFileName);
680 
681  Assert.That(dbFile.Exists, "expect an existence of [" + dbFile.FullName + "].");
682  Assert.That(dbFile.Length > 0, "expect non-empty ["+dbFile.FullName+"].");
683  logger.Debug("exiting TestGAMSDatabase | testExportWithGDXFileName");
684  }
685 
686  [Test]
687  public void testExportDisposedDatabase()
688  {
689  logger.Debug("test exception export disposed database");
690  ws = new GAMSWorkspace();
691  db = ws.AddDatabase(globalDB);
692  db.Dispose();
693  logger.Info("database db disposed");
694 
695  Assert.Throws<GAMSException> (() => db.Export());
696  }
697 
698  [Test]
699  public void testDisposeExportedDatabase()
700  {
701  logger.Debug("TestGAMSDatabase | testDisposeExportedDatabase");
702 
703  ws = new GAMSWorkspace();
704  db = ws.AddDatabase(globalDB, "testDisposeExportedDatabase_"+DateTime.Now.ToString("yyyyMMdd_HHmmss"));
705 
706  String gdxFileName = db.Name;
707  FileInfo dbFile = new FileInfo(ws.WorkingDirectory + Path.DirectorySeparatorChar +
708  gdxFileName + HouseKeeper.GDX_FILE_EXTENSION);
709  Assert.False(dbFile.Exists, "do not expect an existence of [" + dbFile.FullName + "].");
710  db.Export();
711  logger.Info("db exported");
712  dbFile.Refresh();
713 
714  db.Dispose();
715  logger.Info("db disposed");
716 
717  // Java tests are checking for db.isDisposed here
718  Assert.That(dbFile.Exists, "expect an existence of [" + dbFile.FullName + "].");
719  Assert.That(dbFile.Length > 0, "expect non-empty [" + dbFile.FullName + "].");
720  logger.Debug("exiting TestGAMSDatabase | testDisposeExportedDatabase");
721  }
722 
723  [Test]
724  public void testExportDomainViolatedDatabase()
725  {
726  logger.Debug("test exception export domain violated database");
727  ws = new GAMSWorkspace();
728  db = ws.AddDatabase(globalDB);
729  GAMSParameter c = db.GetParameter("capacity");
730  c.AddRecord("Albuquerque").Value = 1;
731 
732  Assert.Throws<GAMSException>( () => db.Export());
733  }
734 
735  [Test]
736  public void testExportSuppressedDomainCheckingDatabase()
737  {
738  logger.Debug("entering TestGAMSDatabase | testExportSuppressedDomainCheckingDatabase");
739 
740  ws = new GAMSWorkspace();
741  db = ws.AddDatabase(globalDB, "testExportSuppressedDomainCheckingDatabase_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
742  GAMSParameter c = db.GetParameter("capacity");
743  c.AddRecord("Albuquerque").Value = 1;
744 
745  String gdxFileName = db.Name;
746  FileInfo dbFile = new FileInfo(ws.WorkingDirectory + Path.DirectorySeparatorChar +
747  gdxFileName + HouseKeeper.GDX_FILE_EXTENSION);
748  Assert.False(dbFile.Exists, "do not expect an existence of [" + dbFile.FullName + "].");
749 
750  db.SuppressAutoDomainChecking = true;
751  Assert.That(db.SuppressAutoDomainChecking, "expect auto domain checking suppressed of database [db] to be [true].");
752 
753  db.Export();
754  logger.Debug("db disposed");
755  dbFile.Refresh();
756 
757  Assert.That(db.SuppressAutoDomainChecking, "expect auto domain checking suppressed of database [db] to be [true].");
758  Assert.That(dbFile.Exists, "expect an existence of [" + dbFile.FullName + "].");
759  Assert.That(dbFile.Length > 0, "expect non-empty [" + dbFile.FullName + "].");
760  logger.Debug("exiting TestGAMSDatabase | testExportSuppressedDomainCheckingDatabase");
761  }
762 
763  [Test]
764  public void testExportNonSuppressedDomainCheckingDatabaseWithDomainViolation()
765  {
766  logger.Debug("test exception export non suppressed domain checking db with domain violation");
767  ws = new GAMSWorkspace();
768  db = ws.AddDatabase(globalDB);
769  GAMSParameter c = db.GetParameter("capacity");
770  c.AddRecord("Alburquerque").Value = 1;
771 
772  db.SuppressAutoDomainChecking = false;
773  Assert.False(db.SuppressAutoDomainChecking, "expect auto domain checking suppressed of database [db] to be [false].");
774 
775  Assert.Throws<GAMSException>(() => db.Export());
776  }
777 
778  [Test]
779  public void testCheckDomain1()
780  {
781  logger.Debug("entering TestGAMSDatabase | testCheckDomain1");
782  Assert.That(globalDB.GetParameter("capacity").CheckDomains(), "expect Parameter [capacity] without domain violation");
783  Assert.That(globalDB.GetParameter("demand").CheckDomains(), "expect Parameter [demand] without domain violation");
784  Assert.That(globalDB.GetParameter("distance").CheckDomains(), "expect Parameter [distance] without domain violation");
785  Assert.That(globalDB.CheckDomains(), "do not expect Database [globalDB] with domain violation");
786  logger.Debug("exiting TestGAMSDatabase | testCheckDomain1");
787  }
788 
789  [Test]
790  public void testCheckDomain2()
791  {
792  logger.Debug("entering TestGAMSDatabase | testCheckDomain2");
793  ws = new GAMSWorkspace();
794  db = ws.AddDatabase(globalDB);
795  GAMSSet i = db.GetSet("plants");
796  i.AddRecord("Alburquerque");
797  GAMSParameter c = db.GetParameter("capacity");
798  c.AddRecord("Alburquerque").Value = 1;
799 
800  Assert.That(db.CheckDomains, "do not expect Database [db] with domain violation");
801  Assert.That(i.CheckDomains(), "do not expect Set [plants] with domain violation");
802  Assert.That(c.CheckDomains(), "do not expect Parameter [capacity] with domain violation");
803  logger.Debug("exiting TestGAMSDatabase | testCheckDomain2");
804  }
805 
806  [Test]
807  public void testCheckDomain3()
808  {
809  logger.Debug("entering TestGAMSDatabase | testCheckDomain3");
810 
811  ws = new GAMSWorkspace();
812  db = ws.AddDatabase(globalDB);
813 
814  GAMSParameter c = db.GetParameter("capacity");
815  String cityName = "Alburqueque";
816  c.AddRecord(cityName).Value = 1;
817  Assert.False(db.CheckDomains(), "do not expect Database [db] with domain violation");
818  Assert.False(c.CheckDomains(), "expect Parameter [capacity] with domain violation");
819 
820  int dvCnt = 0;
821  foreach (GAMSDatabaseDomainViolation item in db.GetDatabaseDVs(5, 5))
822  {
823  foreach (GAMSSymbolDomainViolation info in item.ViolRecs)
824  {
825  dvCnt++;
826  if (dvCnt == 1)
827  {
828  Assert.AreEqual(info.ViolRec.Keys[0], cityName, "expect 1 domain violation record of parameter [capacity] with key [" + cityName + "].");
829  Assert.True(info.ViolInd[0], "expect 1 domain violation record of parameter [capaity] with violation array [true].");
830  }
831  else
832  {
833  Assert.AreEqual(1, dvCnt, "do not expect more than 1 domain violation record of parameter [capacity].");
834  }
835  }
836  }
837  Assert.AreEqual(1, dvCnt, "expect 1 domain violation record of parameter [capacity].");
838  logger.Debug("exiting TestGAMSDatabase | testCheckDomain3");
839  }
840 
841  [Test]
842  public void testCheckDomain4()
843  {
844  logger.Debug("entering TestGAMSDatabase | testCheckDomain4");
845  ws = new GAMSWorkspace();
846  db = ws.AddDatabase(globalDB);
847 
848  GAMSParameter c = db.GetParameter("capacity");
849  c.AddRecord("Alburquerque").Value = 1;
850 
851  GAMSParameter d = db.GetParameter("distance");
852  d.AddRecord(new String[] { "Seattle", "aa" }).Value = 1;
853  d.AddRecord(new String[] { "bb", "Seattle" }).Value = 1;
854  d.AddRecord(new String[] { "x", "y" }).Value = 1;
855 
856  Assert.False(d.CheckDomains(), "expect Parameter [distance] with domain violation");
857  int dvCnt = 0;
858  foreach (GAMSSymbolDomainViolation item in d.GetSymbolDVs(0))
859  {
860  dvCnt++;
861  foreach (String item2 in item.ViolRec.Keys)
862  {
863  Assert.NotNull(item2, "do not expect a NULL key from domain violation record");
864  }
865  }
866  Assert.AreEqual(3, dvCnt, "expect 3 domain violation records of parameter [distance].");
867 
868  dvCnt = 0;
869  foreach (GAMSDatabaseDomainViolation item in db.GetDatabaseDVs(0, 0))
870  {
871  foreach (GAMSSymbolDomainViolation info in item.ViolRecs)
872  {
873  dvCnt++;
874  }
875  }
876  Assert.AreEqual(4, dvCnt, "expect 4 domain violation records of database [db].");
877  logger.Debug("exiting TestGAMSDatabase | testCheckDomain4");
878  }
879 
880  [Test]
881  public void testCheckDomainWithMaxViolation()
882  {
883  logger.Debug(" TestGAMSDatabase | testCheckDomainWithMaxViolation");
884 
885  ws = new GAMSWorkspace();
886  db = ws.AddDatabase(globalDB);
887 
888  //create some domain violation entries
889  GAMSParameter c = db.GetParameter("capacity");
890  c.AddRecord("Alburquerque").Value = 1;
891 
892  GAMSParameter d = db.GetParameter("distance");
893  d.AddRecord(new String[] { "Seattle", "aa" }).Value = 1;
894  d.AddRecord(new String[] { "bb", "Seattle" }).Value = 1;
895  d.AddRecord(new String[] { "x", "y" }).Value = 1;
896 
897  int dvCnt = 0;
898  foreach (GAMSDatabaseDomainViolation item in db.GetDatabaseDVs(3, 3))
899  {
900  foreach (GAMSSymbolDomainViolation info in item.ViolRecs)
901  {
902  dvCnt++;
903  }
904  }
905  Assert.AreEqual(3, dvCnt, "expect 3 domain violation records of database [db].");
906  logger.Debug("exiting TestGAMSDatabase | testCheckDomainWithMaxViolation");
907  }
908 
909  [Test]
910  public void testCheckDomainWithMaxViolationPerSymbol()
911  {
912  logger.Debug("entering TestGAMSDatabase | testCheckDomainWithMaxViolationPerSymbol");
913 
914  ws = new GAMSWorkspace();
915  db = ws.AddDatabase(globalDB);
916 
917  // create some domain violation entries
918  GAMSParameter c = db.GetParameter("capacity");
919  c.AddRecord("Alburquerque").Value = 1;
920 
921  GAMSParameter d = db.GetParameter("distance");
922  d.AddRecord(new String[] { "Seattle", "aa" }).Value = 1;
923  d.AddRecord(new String[] { "bb", "Seattle" }).Value = 1;
924  d.AddRecord(new String[] { "x", "y" }).Value = 1;
925 
926  int dvCnt = 0;
927  foreach (GAMSSymbolDomainViolation item in d.GetSymbolDVs(0))
928  dvCnt++;
929  Assert.AreEqual(3, dvCnt, "expect 3 domain violation records of symbol [distance].");
930 
931  dvCnt = 0;
932  foreach (GAMSSymbolDomainViolation item in d.GetSymbolDVs(2))
933  dvCnt++;
934  Assert.AreEqual(2, dvCnt, "expect 2 domain violation records of symbol [distance].");
935 
936  dvCnt = 0;
937  foreach (GAMSDatabaseDomainViolation item in db.GetDatabaseDVs(0, 2))
938  {
939  foreach (GAMSSymbolDomainViolation info in item.ViolRecs)
940  {
941  dvCnt++;
942  }
943  }
944  Assert.AreEqual(3, dvCnt, "expect 3 domain violation records of database [db].");
945  logger.Debug("exiting TestGAMSDatabase | testCheckDomainWithMaxViolationPerSymbol");
946  }
947 
948  [Test, Timeout(20000)]
949  [Ignore("heavy load")]
950  public void testAddOneMillionSets()
951  {
952  logger.Debug("entering TestGAMSDatabase | testAddOneMillionSets");
953 
954  ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, null, DebugLevel.Off);
955  db = ws.AddDatabase("testAddOneMillionSets");
956 
957  Assert.NotNull(db, "expect a successful creation of GAMSDatabase.");
958 
959  for (int i = 0; i < 1000000; i++)
960  db.AddSet("set" + i, 1);
961  Assert.AreEqual(1000000, db.NrSymbols, "expect 1,000,000 symbols in database.");
962  FileInfo dbFile = new FileInfo(HouseKeeper.gamsAbsoluteWorkingDir + Path.DirectorySeparatorChar +
963  db.Name + HouseKeeper.GDX_FILE_EXTENSION);
964  Assert.False(dbFile.Exists, "do not expect an existence of [" + dbFile.FullName + "].");
965 
966  db.Export();
967  dbFile.Refresh();
968 
969  Assert.That(dbFile.Exists, "expect an existence of [" + db.Name + dbFile.FullName + "].");
970  Assert.That(dbFile.Length > 0, "expect non-empty [" + dbFile.FullName + "].");
971  Assert.AreEqual(1000000, db.NrSymbols, "expect 1,000,000 symbols in database.");
972  logger.Debug("exiting TestGAMSDatabase | testAddOneMillionSets");
973  }
974 
975  [Test, Timeout(20000)]
976  [Ignore("heavy load")]
977  public void testAddOneMillionParameters()
978  {
979  logger.Debug("entering TestGAMSDatabase | testAddOneMillionParamters");
980 
981  ws = new GAMSWorkspace();
982  db = ws.AddDatabase("testAddOneMillionParameters");
983  Assert.NotNull(db, "expect a successful creation of GAMSDatabase.");
984 
985  for (int i = 0; i < 1000000; i++)
986  db.AddParameter("parameter" + i, 1);
987 
988  Assert.AreEqual(1000000, db.NrSymbols, "expect 1,000,000 symbols in database.");
989  int count = 0;
990  foreach(GAMSSymbol symbol in db){
991  Assert.NotNull(symbol, "do not expect a NULL symbol instance.");
992  Assert.True(symbol is GAMSParameter, "expect a GAMSParameter instance");
993  Assert.AreEqual("parameter" + count, symbol.Name, "expect a parameter with name [parameter" + count + "].");
994  Assert.AreEqual(1, symbol.Dim, "expect a parameter with 1 dimension.");
995  Assert.AreEqual(0, symbol.NumberRecords, "expect a parameter without a record.");
996  count++;
997  }
998  Assert.AreEqual(1000000, count, "expect 1,000,000 symbols in database.");
999  logger.Debug("exiting TestGAMSDatabase | testAddOneMillionParameters");
1000  }
1001 
1002 
1003  [Test]
1004  public void testAddDatabaseFromNonExistGDXFile()
1005  {
1006  logger.Debug("test exception add db from non existing gdx file");
1007 
1008  ws = new GAMSWorkspace(HouseKeeper.gamsAbsoluteWorkingDir, null, DebugLevel.Off);
1009  Assert.Throws<GAMSException>(() => ws.AddDatabaseFromGDX(null));
1010  }
1011 
1012  [Test]
1013  public void testAddDatabaseFromNullGDXFile()
1014  {
1015  logger.Debug("test exception add db from null gdx file");
1016 
1017  ws = new GAMSWorkspace(HouseKeeper.gamsAbsoluteWorkingDir, null, DebugLevel.Off);
1018  Assert.Throws<GAMSException>(() => ws.AddDatabaseFromGDX("thisIsProbablyANonExistingGDXFileName"));
1019  }
1020 
1021  [Test]
1022  public void testAddDatabaseFromGDX()
1023  {
1024  logger.Debug("entering TestGAMSDatabase | testAddDatabaseFromGDX");
1025 
1026  String gdxFileName = "testAddDatabaseFromGDX";
1027  FileInfo dbFile = new FileInfo(HouseKeeper.gamsAbsoluteWorkingDir +
1028  Path.DirectorySeparatorChar + gdxFileName +
1029  HouseKeeper.GDX_FILE_EXTENSION);
1030  Assert.False(dbFile.Exists, "do not expect existence of [" + dbFile.FullName + "].");
1031  globalDB.Export(gdxFileName);
1032  dbFile.Refresh();
1033 
1034  Assert.That(dbFile.Exists, "expect an existence of [" + dbFile.FullName + "].");
1035  Assert.That(dbFile.Length > 0, "expect non-empty [" + dbFile.FullName + "].");
1036 
1037  ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, null, DebugLevel.Off);
1038  db = ws.AddDatabaseFromGDX(gdxFileName + HouseKeeper.GDX_FILE_EXTENSION);
1039  logger.Debug("database db added from gdx file [" + gdxFileName + "]");
1040 
1041  Assert.NotNull(db, "do not expect a NULL GAMSDatabase.");
1042  Assert.NotNull(db.Name, "do not expect a NULL database name.");
1043  Assert.AreEqual(db.NrSymbols, globalDB.NrSymbols, "expect same set of symbols in [" + db.Name + "] as in [" + globalDB.Name + "].");
1044 
1045  foreach (GAMSSymbol symb in db)
1046  Assert.NotNull(globalDB.GetSymbol(symb.Name), "expect to find every symbol in [" + db.Name + "] from [" + globalDB.Name + "]");
1047  foreach (GAMSSymbol symb in globalDB)
1048  Assert.NotNull(db.GetSymbol(symb.Name), "expect to find every symbol in [" + globalDB.Name + "] from [" + db.Name + "]");
1049  logger.Debug("exiting TestGAMSDatabase | testAddDatabaseFromGDX");
1050  }
1051 
1052  [Test]
1053  public void testGAMSWorkspaceAddDatabseWithSourceDB1()
1054  {
1055  logger.Debug("entering TestGAMSDatabase | testGAMSWorkspaceAddDatabseWithSourceDB1");
1056 
1057  db = globalWS.AddDatabase(globalDB);
1058  Assert.NotNull(db, "do not expect a NULL GAMSDatabase.");
1059  Assert.NotNull(db.Name, "do not expect a NULL database name.");
1060  Assert.AreEqual(db.NrSymbols, globalDB.NrSymbols, "expect same set of symbols in [" + db.Name + "] as in [" + globalDB.Name + "].");
1061 
1062  foreach (GAMSSymbol symb in db)
1063  Assert.NotNull(globalDB.GetSymbol(symb.Name), "expect to find every symbol in [" + db.Name + "] from [" + globalDB.Name + "]");
1064  foreach (GAMSSymbol symb in globalDB)
1065  Assert.NotNull(db.GetSymbol(symb.Name), "expect to find every symbol in [" + globalDB.Name + "] from [" + db.Name + "]");
1066  logger.Debug("exiting TestGAMSDatabase | testAddDatabaseFromGDX");
1067  }
1068 
1069  [Test]
1070  public void testGAMSWorkspaceAddDatabseWithSourceDB2()
1071  {
1072  logger.Debug("testGAMSDatabase | testGAMSWorkspaceAddDatabseWithSourceDB2");
1073 
1074  String newDBName = "aNewDatabaseName";
1075  db = globalWS.AddDatabase(globalDB, newDBName);
1076 
1077  Assert.NotNull(db, "do not expect a NULL GAMSDatabase.");
1078  Assert.NotNull(db.Name, "do not expect a NULL database name.");
1079  Assert.AreEqual(db.NrSymbols, globalDB.NrSymbols, "expect same set of symbols in [" + db.Name + "] as in [" + globalDB.Name + "].");
1080  logger.Debug("exiting TestGAMSDatabase | testGAMSWorkspaceAddDatabseWithSourceDB2");
1081  }
1082 
1083  public static void initializeDatabase(GAMSDatabase db)
1084  {
1085  logger.Debug("entering TestGAMSDatabase | initializeDatabase");
1086  List<String> plants = new List<String> { "Seattle", "San-Francisco", "San-Diego" };
1087  List<String> markets = new List<String> { "New-York", "Chicago", "Topeka" };
1088 
1089  Dictionary<String, Double> capacity = new Dictionary<string, double>();
1090  capacity.Add("Seattle", 350.0);
1091  capacity.Add("San-Diego", 600.0);
1092 
1093  Dictionary<String, Double> demand = new Dictionary<string, double>();
1094  demand.Add("New-York", 325.0);
1095  demand.Add("Chicago", 300.0);
1096  demand.Add("Topeka", 275.0);
1097 
1098  Dictionary<String[], Double> distance = new Dictionary<String[], Double>();
1099  distance.Add(new String[] { "Seattle", "New-York" }, 2.5);
1100  distance.Add(new String[] { "Seattle", "Chicago" }, 1.7);
1101  distance.Add(new String[] { "Seattle", "Topeka" }, 1.8);
1102  distance.Add(new String[] { "San-Diego", "New-York"}, 2.5);
1103  distance.Add(new String[] { "San-Diego", "Chicago" }, 1.8);
1104  distance.Add(new String[] { "San-Diego", "Topeka" }, 1.4);
1105 
1106 
1107  // plants
1108  String iName = "plants";
1109  String iText = "canning plants";
1110  GAMSSet i = db.AddSet(iName, 1, iText);
1111 
1112  Assert.AreEqual(db, i.GAMSDatabase, "expect same database reference");
1113  Assert.That(i.Dim == 1, "expect plants with one dimension");
1114  Assert.NotNull(i.Domains, "do not expect a NULL list object returned");
1115  Assert.That(i.Domains.Count > 0, "do not expect an empty list returned.");
1116  Assert.That(iName.Equals(i.Name), "except [" + iName + "] as set name.");
1117  Assert.That(i.Text.Equals(iText), "expect [" + iText + "] as set explanatory text.");
1118  testSymbolWithoutARecord(i, iName);
1119  Assert.AreEqual(1, db.NrSymbols, "expect one symbol in db");
1120 
1121  foreach (String pl in plants)
1122  i.AddRecord(pl);
1123 
1124  GAMSSet i1 = db.GetSet(iName);
1125  Assert.AreEqual(plants.Count, i1.NumberRecords, "expect [" + plants.Count + "] as a number of plant records.");
1126 
1127  int n = 0;
1128  foreach (GAMSSetRecord record in i)
1129  {
1130  Assert.NotNull(record, "do not expect a NULL set record.");
1131  switch (n)
1132  {
1133  case 0:
1134  Assert.AreEqual(1, record.Keys.Count());
1135  Assert.AreEqual("Seattle", record.Keys[0]);
1136  break;
1137  case 1:
1138  Assert.AreEqual(1, record.Keys.Count());
1139  Assert.AreEqual("San-Francisco", record.Keys[0]);
1140  break;
1141  case 2:
1142  Assert.AreEqual(1, record.Keys.Count());
1143  Assert.AreEqual("San-Diego", record.Keys[0]);
1144  break;
1145  }
1146  n++;
1147  }
1148  Assert.AreEqual(3, n, "expect set [" + i.Name + "] with 3 iteration records.");
1149 
1150  // markets
1151  String jName = "markets";
1152  String jText = "markets";
1153  GAMSSet j = db.AddSet(jName, 1, jText);
1154 
1155  Assert.AreEqual(db, j.GAMSDatabase, "expect same db reference");
1156  Assert.That(j.Dim == 1, "expect plants with one dimension");
1157  Assert.NotNull(j.Domains, "do not expect a NULL lust object returned.");
1158  Assert.That(j.Domains.Count > 0, "do not expect empty list returned");
1159  Assert.That(jName.Equals(j.Name));
1160  Assert.That(jText.Equals(j.Text));
1161  testSymbolWithoutARecord(j, jName);
1162  Assert.AreEqual(2, db.NrSymbols, "expect two symbols in db");
1163 
1164  int count = 0;
1165  foreach (GAMSSymbol sym in db)
1166  {
1167  Assert.NotNull(sym);
1168  count++;
1169  }
1170  Assert.AreEqual(2, db.NrSymbols);
1171  Assert.AreEqual(db.NrSymbols, count);
1172 
1173  foreach (String market in markets)
1174  j.AddRecord(market);
1175 
1176  GAMSSet j1 = db.GetSet(jName);
1177  Assert.AreEqual(markets.Count, j1.NumberRecords);
1178 
1179  // capacity
1180  String cName = "capacity";
1181  String cText = "capacity of plant p in cases";
1182  Object[] cdomain = new Object[] { i };
1183 
1184  GAMSParameter c = db.AddParameter(cName, cText, cdomain);
1185 
1186  Assert.AreEqual(db, c.GAMSDatabase);
1187  Assert.That(c.Dim == 1, "expect c with one dimension");
1188  Assert.NotNull(c.Domains);
1189  Assert.That(c.Domains.Count > 0);
1190  Assert.That(cName.Equals(c.Name));
1191  Assert.That(cText.Equals(c.Text));
1192  testSymbolWithoutARecord(c, cName);
1193  Assert.AreEqual(3, db.NrSymbols);
1194 
1195  foreach (KeyValuePair<String, Double> entry in capacity)
1196  c.AddRecord(entry.Key).Value = entry.Value;
1197 
1198  GAMSParameter c1 = db.GetParameter(cName);
1199  Assert.AreEqual(c1.NumberRecords, capacity.Count);
1200  Assert.AreEqual(3, db.NrSymbols);
1201 
1202  // demand
1203  String dName = "demand";
1204  String dText = "demand at market m in cases";
1205  Object[] ddomain = new Object[] { j };
1206 
1207  GAMSParameter d = db.AddParameter(dName, dText, ddomain);
1208 
1209  Assert.AreEqual(db, d.GAMSDatabase);
1210  Assert.That(d.Dim == 1);
1211  Assert.NotNull(d.Domains);
1212  Assert.That(d.Domains.Count > 0);
1213  Assert.That(dName.Equals(d.Name));
1214  Assert.That(dText.Equals(d.Text));
1215  testSymbolWithoutARecord(d, dName);
1216  Assert.AreEqual(4, db.NrSymbols);
1217 
1218  foreach (KeyValuePair<String,Double> entry in demand)
1219  d.AddRecord(entry.Key).Value = entry.Value;
1220 
1221  GAMSParameter d1 = db.GetParameter(dName);
1222  Assert.AreEqual(demand.Count, d1.NumberRecords);
1223 
1224  GAMSParameterRecord rec = d1.FirstRecord();
1225  Assert.AreEqual(1, rec.Keys.Count());
1226  Assert.AreEqual("New-York", rec.Keys[0]);
1227 
1228  rec = d1.LastRecord();
1229  Assert.AreEqual(1, rec.Keys.Count());
1230  Assert.AreEqual("Topeka", rec.Keys[0]);
1231 
1232  rec = d1.FirstRecord("Chicago");
1233  Assert.AreEqual(1, rec.Keys.Count());
1234  Assert.AreEqual("Chicago", rec.Keys[0]);
1235 
1236  Assert.AreEqual(4, db.NrSymbols);
1237 
1238  int noSymbols = 0;
1239  foreach (GAMSSymbol symbol in db)
1240  {
1241  Assert.NotNull(symbol);
1242  noSymbols++;
1243  foreach (GAMSSymbolRecord r in symbol)
1244  {
1245  Assert.NotNull(r);
1246  }
1247  }
1248  Assert.AreEqual(4, db.NrSymbols);
1249 
1250  // add a 2 dimensional parameter with domain information
1251  Object[] domains = new Object[] { i, j };
1252  GAMSParameter dist = db.AddParameter("distance", "distance in thousands of miles", domains);
1253  foreach (String[] t in distance.Keys)
1254  {
1255  String[] keys = new String[t.Length];
1256  keys = t.ToArray<String>();
1257  Double val;
1258  distance.TryGetValue(t, out val);
1259  dist.AddRecord(keys).Value = val;
1260  }
1261  Assert.AreEqual(6, dist.NumberRecords);
1262  Assert.AreEqual(5, db.NrSymbols);
1263  logger.Debug("exiting TestGAMSDatabase | initializeDatabase");
1264  }
1265 
1266  private static void testSymbolWithoutARecord(GAMSSymbol symbol, string symbolName)
1267  {
1268  Assert.That(symbol.NumberRecords == 0);
1269 
1270  Assert.Throws<GAMSException>(() => symbol.FirstRecord());
1271  Assert.Throws<GAMSException>(() => symbol.LastRecord());
1272  Assert.Throws<GAMSException>(() => symbol.LastRecord("x"));
1273  Assert.Throws<GAMSException>(() => symbol.LastRecord("x", "y"));
1274  Assert.Throws<GAMSException>(() => symbol.FindRecord("x"));
1275  }
1276  }
1277 }
GAMSSymbolRecord FindRecord(params string[] keys)
GAMSDatabase GAMSDatabase
new GAMSSetRecord AddRecord(params string[] keys)
GAMSSymbol GetSymbol(string symbolIdentifier)
bool SuppressAutoDomainChecking
new GAMSParameterRecord AddRecord(params string[] keys)
GAMSDatabase OutDB
static void initializeTestFrom(String filename, String subdir)
initialize class properties from fileName and prepare directory subdir
Definition: HouseKeeper.cs:114
List< object > Domains
GAMSParameter GetParameter(string parameterIdentifier)
GAMSSymbolRecord FirstRecord()
GAMSSymbolRecord LastRecord()
static bool rcleanup(String dir)
(recursively) delete all contents under dir, and delete dir.
Definition: HouseKeeper.cs:77
GAMSDatabase AddDatabaseFromGDX(string gdxFileName, string databaseName=null, string inModelName=null)
new GAMSParameterRecord FirstRecord()
GAMSSet GetSet(string setIdentifier)
GAMSVariable GetVariable(string variableIdentifier)
GAMSEquation AddEquation(string identifier, int dimension, EquType equType, string explanatoryText="")
new GAMSParameterRecord LastRecord()
List< GAMSSymbolDomainViolation > ViolRecs
void Export(string filePath=null)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
List< GAMSDatabaseDomainViolation > GetDatabaseDVs(int MaxViol=0, int MaxViolPerSym=0)
GAMSVariable AddVariable(string identifier, int dimension, VarType varType, string explanatoryText="")
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSEquation GetEquation(string equationIdentifier)