Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d12b274

Browse files
committedOct 11, 2022
Merge branch 'remove_ref_sets' into 'master'
remove/modify symbols if referenced domain set is removed Closes #19 See merge request devel/gams-transfer-matlab!35
2 parents 7fce0e3 + 91a02d2 commit d12b274

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed
 

‎CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ GAMS Transfer Matlab v0.3.0
4646
defined by source order (e.g. symbol order in GDX file) rather than user
4747
supplied order. To establish a custom order after the read, use
4848
`reorderSymbols`.
49+
- Aliases are now removed if the aliased set is removed.
50+
- Domains are now set to `*` (universe) if the domain set is removed.
4951
- Deprecated `Symbol.getUELLabels`. Use `Symbol.getUELs` instead.
5052
- Deprecated `Symbol.initUELs`. Use `Symbol.setUELs` instead.
5153
- Deprecated `Container.getUniverseSet`. Use `Container.getUELs` instead.

‎src/Container.m

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -892,25 +892,46 @@ function removeSymbols(obj, names)
892892
names = {names};
893893
end
894894

895+
removed_symbols = cell(1, numel(names));
896+
j = 0;
897+
% remove symbols from data
895898
for i = 1:numel(names)
896899
if ~obj.hasSymbols(names{i})
897900
continue;
898901
end
899-
symbol = obj.getSymbols(names{i});
902+
j = j + 1;
903+
removed_symbols{j} = obj.getSymbols(names{i});
900904

901905
% remove symbol
902-
obj.data = rmfield(obj.data, symbol.name);
906+
obj.data = rmfield(obj.data, removed_symbols{j}.name);
903907

904908
% force recheck of deleted symbol (it may still live within an
905909
% alias, domain or in the user's program)
906-
symbol.isValid(false, true);
910+
removed_symbols{j}.isValid(false, true);
907911

908912
% unlink container
909-
symbol.unsetContainer();
913+
removed_symbols{j}.unsetContainer();
910914
end
915+
removed_symbols = removed_symbols(1:j);
911916

912-
% force recheck of all remaining symbols in container
913-
obj.isValid(false, true);
917+
% remove symbols from domain references
918+
symbols = fieldnames(obj.data);
919+
remove_aliases = {};
920+
for i = 1:numel(symbols)
921+
symbol = obj.data.(symbols{i});
922+
if isa(symbol, 'GAMSTransfer.Alias')
923+
for j = 1:numel(removed_symbols)
924+
if symbol.alias_with.name == removed_symbols{j}.name
925+
remove_aliases{end+1} = symbol.name;
926+
end
927+
end
928+
else
929+
symbol.unsetDomain(removed_symbols);
930+
end
931+
end
932+
if numel(remove_aliases) > 0
933+
obj.removeSymbols(remove_aliases);
934+
end
914935
end
915936

916937
%> Reestablishes a valid GDX symbol order

‎src/Symbol.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,19 @@ function unsetContainer(obj)
22142214
obj.modified = true;
22152215
end
22162216

2217+
function unsetDomain(obj, unset_domains)
2218+
for i = 1:obj.dimension_
2219+
if ~isa(obj.domain{i}, 'GAMSTransfer.Set') && ~isa(obj.domain{i}, 'GAMSTransfer.Alias')
2220+
continue
2221+
end
2222+
for j = 1:numel(unset_domains)
2223+
if obj.domain{i}.name == unset_domains{j}.name
2224+
obj.domain{i} = '*';
2225+
end
2226+
end
2227+
end
2228+
end
2229+
22172230
end
22182231

22192232
methods (Hidden, Access = protected)

‎test/test_container.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,16 +1988,16 @@ function test_remove(t, cfg)
19881988
t.assert(~a1.modified);
19891989
t.assert(~x1.modified);
19901990
gdx.removeSymbols('i1');
1991-
t.assert(numel(fieldnames(gdx.data)) == 2);
1992-
t.assert(isfield(gdx.data, 'a1'));
1991+
t.assert(numel(fieldnames(gdx.data)) == 1);
19931992
t.assert(isfield(gdx.data, 'x1'));
19941993
t.assert(~i1.isValid());
19951994
t.assert(~a1.isValid());
1996-
t.assert(~x1.isValid());
1995+
t.assert(x1.isValid());
1996+
t.assertEquals(x1.domain{1}, '*');
19971997
t.assert(gdx.modified);
19981998
t.assert(i1.modified);
1999-
t.assert(~a1.modified);
2000-
t.assert(~x1.modified);
1999+
t.assert(a1.modified);
2000+
t.assert(x1.modified);
20012001

20022002
t.add('remove_2');
20032003
gdx.removeSymbols({'i1', 'a1', 'x1'});
@@ -2030,16 +2030,16 @@ function test_remove(t, cfg)
20302030
t.assert(~a1.modified);
20312031
t.assert(~x1.modified);
20322032
gdx.removeSymbols('I1');
2033-
t.assert(numel(fieldnames(gdx.data)) == 2);
2034-
t.assert(isfield(gdx.data, 'a1'));
2033+
t.assert(numel(fieldnames(gdx.data)) == 1);
20352034
t.assert(isfield(gdx.data, 'x1'));
20362035
t.assert(~i1.isValid());
20372036
t.assert(~a1.isValid());
2038-
t.assert(~x1.isValid());
2037+
t.assert(x1.isValid());
2038+
t.assertEquals(x1.domain{1}, '*');
20392039
t.assert(gdx.modified);
20402040
t.assert(i1.modified);
2041-
t.assert(~a1.modified);
2042-
t.assert(~x1.modified);
2041+
t.assert(a1.modified);
2042+
t.assert(x1.modified);
20432043

20442044
t.add('remove_diffcase_2');
20452045
gdx.removeSymbols({'I1', 'A1', 'X1'});

0 commit comments

Comments
 (0)
Please sign in to comment.