Loading...
Searching...
No Matches
alias.m
1function alias(varargin)
2
3 % check workspace info from arguments
4 if nargin > 0
5 wsInfo = gams.control.WorkspaceInfo();
6 wsInfo.systemDirectory = varargin{1};
7 ws = gams.control.Workspace(wsInfo);
8 else
9 ws = gams.control.Workspace();
10 end
11
12 data = {
13 'set i / i1*i3 /'
14 ' j / j1*j3 /'
15 ' ij / #i:#j /'
16 'alias (i,ii), (j,jj), (ij,iijj);'
17 'parameter'
18 ' a(i) / #i 1 /, aa(ii) / #ii 2 /;'};
19 data = sprintf('%s\n', data{:});
20
21 gdxdump1 = {
22 '$onempty'
23 ''
24 'Set i(*) /'
25 '''i1'', '
26 '''i2'', '
27 '''i3'' /;'
28 ''
29 'Set j(*) /'
30 '''j1'', '
31 '''j2'', '
32 '''j3'' /;'
33 ''
34 'Set ij(*,*) /'
35 '''i1''.''j1'', '
36 '''i2''.''j2'', '
37 '''i3''.''j3'' /;'
38 ''
39 'Alias (ii, i);'
40 ''
41 'Alias (jj, j);'
42 ''
43 'Alias (iijj, ij);'
44 ''
45 'Parameter a(i) /'
46 '''i1'' 1, '
47 '''i2'' 1, '
48 '''i3'' 1 /;'
49 ''
50 'Parameter aa(ii) /'
51 '''i1'' 2, '
52 '''i2'' 2, '
53 '''i3'' 2 /;'
54 ''
55 '$offempty'
56 ''};
57 gdxdump1 = sprintf('%s\n', gdxdump1{:});
58
59 gdxdump2 = {
60 '$onempty'
61 ''
62 'Set i(*) /'
63 '''i1'', '
64 '''i2'', '
65 '''i3'' /;'
66 ''
67 'Parameter aa(i) /'
68 '''i1'' 2, '
69 '''i2'' 2, '
70 '''i3'' 2 /;'
71 ''
72 '$offempty'
73 ''};
74 gdxdump2 = sprintf('%s\n', gdxdump2{:});
75
76 % Create initial data containing a Alias
77 % The OO API does not know about Aliases and will retrieve it as a set
78 j1 = ws.addJobFromString(data);
79 j1.run();
80
81 checkAliasLogic('j1.outDB', j1.outDB);
82
83 j1.outDB.export('outDB.gdx');
84 assert(SameGdxDump(ws, 'outDB.gdx', gdxdump1), 'Unexpected result of gdxdump outDB.gdx');
85
86 % Copy constructor should preserve aliases and other
87 db = ws.addDatabase(j1.outDB);
88 checkAliasLogic('db', db);
89 db.export('db.gdx');
90 assert(SameGdxDump(ws, 'db.gdx', gdxdump1), 'Unexpected result of gdxdump db.gdx');
91
92 db2 = ws.addDatabase();
93 ii = db2.addSet(db.getSet('ii').name, db.getSet('ii').text, '*');
94 db.getSet('ii').copySymbol(ii);
95
96 aaOriginal = db.getParameter('aa');
97 aa = db2.addParameter(db.getParameter('aa').name, db.getParameter('aa').text, ii);
98 aaOriginal.copySymbol(aa);
99 db2.export('db2.gdx');
100
101 assert(SameGdxDump(ws, 'db2.gdx', gdxdump2), 'Unexpected result of gdxdump db2.gdx');
102
103 % If the domain is an alias, domains should return the aliased set,
104 % but getDomainsAsStrings should return the name of the alias
105 assert(isa(aaOriginal.domains{1}, 'Set'), 'The domain set should be a Set');
106 assert(strcmp(aaOriginal.domains{1}.name, 'i'), 'The domain set should be the original set');
107 assert(strcmp(aaOriginal.domainsAsStrings{1}, 'ii'), 'The domain as string should be the alias name');
108end
109
110function checkAliasLogic(prefix, aliasDB)
111 % Check number of symbols
112 assert(aliasDB.numberOfSymbols == 5, '%s aliasDB should have NrSymbols=5: i,j,ij,a,aa.', prefix);
113 assert(numel(aliasDB.symbols) == 5, '%s there sould be 5 Symbols in aliasDB: i,j,ij,a,aa.', prefix);
114
115 % See if we can retrieve alias sets
116 assert(strcmp(aliasDB.getSet('ii').name, 'i'), '%s We should get set i when asking for alias ii.', prefix);
117 assert(strcmp(aliasDB.getSet('jj').name, 'j'), '%s We should get set j when asking for alias jj.', prefix);
118 assert(strcmp(aliasDB.getSet('iijj').name, 'ij'), '%s We should get set ij when asking for alias iijj.', prefix);
119
120 % Check domain logic
121 assert(aliasDB.checkDomains(), '%s Check domains should be true', prefix);
122 domains = aliasDB.getParameter('aa').domains;
123 assert(isa(domains{1}, 'gams.control.Set'), '%s domain[0] of aa should be set', prefix);
124 assert(strcmp(domains{1}.name, 'i'), '%s domain[0] of aa should point to i', prefix);
125
126 aliasDB.getSet('ii').deleteRecord('i1');
127 assert(~aliasDB.checkDomains(), '%s Check domains should be false after removal of i1', prefix);
128 aliasDB.getSet('ii').addRecord('i1');
129 assert(aliasDB.checkDomains(), '%s Check domains should be true after adding i1 again', prefix);
130end
131
132function is_equal = SameGdxDump(ws, gdxfile, expectedResult)
133 cmd = fullfile(ws.systemDirectory(), 'gdxdump');
134 cmd_arg = fullfile(ws.workingDirectory(), gdxfile);
135
136 [status, cmdout] = system([cmd, ' ', cmd_arg]);
137 cmdout = strsplit(cmdout, char(10));
138 found_start = false;
139 for i = numel(cmdout):-1:1
140 if found_start
141 cmdout(i) = [];
142 end
143 if strcmp(cmdout{i}, '$onempty')
144 found_start = true;
145 end
146 end
147 expectedResult = strsplit(expectedResult, char(10));
148
149 is_equal = true;
150 if numel(expectedResult) ~= numel(cmdout)
151 is_equal = false;
152 return
153 end
154 for i = 1:numel(cmdout)
155 if ~strcmp(cmdout{i}, expectedResult{i})
156 is_equal = false;
157 return
158 end
159 end
160end