alias.py
Go to the documentation of this file.
14 
15 from gams import *
16 import sys
17 import os
18 import subprocess
19 import re
20 
21 
22 def gdxdump2():
23  data = '''
24 $onempty
25 
26 Set i(*) /
27 'i1',
28 'i2',
29 'i3' /;
30 
31 Parameter aa(i) /
32 'i1' 2,
33 'i2' 2,
34 'i3' 2 /;
35 
36 $offempty
37 '''
38  return re.sub( '\s', '', data)
39 
40 
41 def gdxdump1():
42  data = '''
43 $onempty
44 
45 Set i(*) /
46 'i1',
47 'i2',
48 'i3' /;
49 
50 Set j(*) /
51 'j1',
52 'j2',
53 'j3' /;
54 
55 Set ij(*,*) /
56 'i1'.'j1',
57 'i2'.'j2',
58 'i3'.'j3' /;
59 
60 Alias (ii, i);
61 
62 Alias (jj, j);
63 
64 Alias (iijj, ij);
65 
66 Parameter a(i) /
67 'i1' 1,
68 'i2' 1,
69 'i3' 1 /;
70 
71 Parameter aa(ii) /
72 'i1' 2,
73 'i2' 2,
74 'i3' 2 /;
75 
76 $offempty
77 '''
78  return re.sub( '\s', '', data)
79 
80 
82  return '''
83 set i / i1*i3 /
84  j / j1*j3 /
85  ij / #i:#j /
86 alias (i,ii), (j,jj), (ij,iijj);
87 parameter
88  a(i) / #i 1 /, aa(ii) / #ii 2 /;
89 '''
90 
91 
92 def same_gdx_dump(ws, gdxfile, expected_result):
93  result = ""
94 
95  cmd = [os.path.join(ws.system_directory, "gdxdump"), os.path.join(ws.working_directory, gdxfile)]
96  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
97  result, err = p.communicate()
98  result = re.sub( '\s', '', result.decode())
99  return result.lower() == expected_result.lower()
100 
101 
102 def my_assert(test, msg):
103  if not test:
104  raise Exception(msg)
105 
106 
107 def check_alias_logic(prefix, alias_db):
108  # Check number of symbols
109  my_assert(len(alias_db) == 5, prefix + "alias_db should have NrSymbols=5: i,j,ij,a,aa.")
110 
111  cnt_symbols = 0
112  for sym in alias_db:
113  cnt_symbols += 1
114  my_assert(cnt_symbols == 5, prefix + "'for sym in alias_db' should result in 5 symbols: i,j,ij,a,aa.")
115 
116  # See if we can retrieve alias sets
117  my_assert(alias_db["ii"].name == "i", prefix + "We should get set i when asking for alias ii.")
118  my_assert(alias_db["jj"].name == "j", prefix + "We should get set j when asking for alias jj.")
119  my_assert(alias_db["iijj"].name == "ij", prefix + "We should get set ij when asking for alias iijj.")
120 
121  # Check domain logic
122  my_assert(alias_db.check_domains() == True, prefix + "Check domains should be true")
123  my_assert(type(alias_db["aa"].domains[0]) is GamsSet, prefix + "domain[0] of aa should be set")
124  my_assert((alias_db["aa"].domains[0]).name == "i", prefix + "domain[0] of aa should point to i")
125 
126  alias_db["ii"].delete_record("i1")
127  my_assert(alias_db.check_domains() == False, prefix + "Check domains should be false after removal of i1")
128  alias_db["ii"].add_record("i1")
129  my_assert(alias_db.check_domains() == True, prefix + "Check domains should be true after adding i1 again")
130 
131 
132 if __name__ == "__main__":
133  try:
134  if len(sys.argv) > 1:
135  ws = GamsWorkspace(system_directory = sys.argv[1])
136  else:
137  ws = GamsWorkspace()
138 
139  # Create initial data containing a GAMS Alias
140  # The OO API does not know about Aliases and will retrieve it as a set
141  j1 = ws.add_job_from_string(get_data_text())
142  j1.run()
143  check_alias_logic("j1.out_db ", j1.out_db)
144  j1.out_db.export("outdb.gdx")
145  my_assert(same_gdx_dump(ws, "outdb.gdx", gdxdump1()), "Unexpected result of gdxdump outdb.gdx")
146 
147  # Copy constructor should preserve aliases and other
148  db = ws.add_database(source_database= j1.out_db)
149  check_alias_logic("db ", db)
150  db.export("db.gdx")
151  my_assert(same_gdx_dump(ws, "db.gdx", gdxdump1()), "Unexpected result of gdxdump db.gdx")
152 
153  db2 = ws.add_database()
154  ii = db2.add_set_dc(db["ii"].name, ["*"], db["ii"].text)
155  db["ii"].copy_symbol(ii)
156 
157  aa_orig = db["aa"]
158 
159  aa = db2.add_parameter_dc(db["aa"].name, [ii], db["aa"].text, )
160  aa_orig.copy_symbol(aa)
161  db2.export("db2.gdx")
162  my_assert(same_gdx_dump(ws, "db2.gdx", gdxdump2()), "Unexpected result of gdxdump db2.gdx")
163 
164  # If the domain is an Alias, Domains should return the aliased Set,
165  # but DomainsAsStrings should return the name of the Alias
166  my_assert(aa_orig.domains[0].name == "i", "The domain set should be the original set")
167  my_assert(aa_orig.domains_as_strings[0] == "ii", "The domain as string should be the alias name")
168 
169  except Exception as e:
170  raise Exception("\n###\n### Exception caught:" + str(e) + "\n###")
171 
def get_data_text()
Definition: alias.py:81
def check_alias_logic(prefix, alias_db)
Definition: alias.py:107
def gdxdump2()
Definition: alias.py:22
def my_assert(test, msg)
Definition: alias.py:102
def gdxdump1()
Definition: alias.py:41
def same_gdx_dump(ws, gdxfile, expected_result)
Definition: alias.py:92