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