-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGModelMutator.m
More file actions
510 lines (461 loc) · 22.5 KB
/
GModelMutator.m
File metadata and controls
510 lines (461 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
classdef GModelMutator
properties
uuid
version = 0;
original_model_path
original_model_name
model_name
root_model_path
num_original_subsystems
num_switched_subsystems
built_correct = 0;
end
methods
function obj = GModelMutator(uuid, root_model_identity)
try
obj.uuid = uuid;
obj.original_model_name = root_model_identity.get_model_name();
obj.original_model_path = root_model_identity.(Helper.model_path);
obj = obj.copy_version(0);
Helper.with_preserved_cfg(@close_system, obj.root_model_path, 0)
catch ME
Helper.log('log_construct', string(jsonencode(obj)) + newline + ME.identifier + " " + ME.message + newline + string(ME.stack(1).file) + ", Line: " + ME.stack(1).line)
obj.version = -1;
end
end
function obj = save_version(obj)
obj.version = obj.version + 1;
new_suffix = "v" + string(obj.version);
%check whether the system with new system name is currently open
new_name = extractBefore(obj.root_model_path, strlength(obj.root_model_path)-3) + new_suffix + extractAfter(obj.root_model_path, strlength(obj.root_model_path)-4);
Helper.with_preserved_cfg(@save_system, obj.model_name, new_name)
Helper.with_preserved_cfg(@close_system, obj.model_name + new_suffix, 0)
end
function obj = copy_version(obj, close_first)
obj.model_name = "model" + string(obj.uuid);
obj.root_model_path = Helper.mutate_playground + filesep + obj.model_name + extractAfter(obj.original_model_path,strlength(obj.original_model_path)-4);
obj.root_model_path = char(obj.root_model_path.replace("\", "/"));
if close_first
Helper.with_preserved_cfg(@close_system, obj.root_model_path, 0)
end
delete(obj.root_model_path)
pause(0.05); %hacky to avoid: The requested operation cannot be performed on a file with a user-mapped section open.
copyfile(obj.original_model_path, obj.root_model_path);
Helper.with_preserved_cfg(@load_system, obj.root_model_path);
set_param(obj.model_name, 'Lock', 'off')
set_param(obj.model_name, "LockLinksToLibrary", "off")%sometimes causes errors
end
function obj = switch_subs_in_model(obj, name2subinfo)
Helper.with_preserved_cfg(@load_system, obj.root_model_path);
curr_depth = 0;
while 1
sub_at_depth_found = 0;
sub_names = Helper.find_subsystems(obj.model_name);
sub_names{end + 1} = char(obj.model_name);
for i = 1:length(sub_names)
try
original_name = get_param(sub_names{i}, 'Name');
catch ME
Helper.log('log_copy_to_missing', string(jsonencode(obj)) + newline + sub_names{i} + ME.identifier + " " + ME.message + newline + string(ME.stack(1).file) + ", Line: " + ME.stack(1).line);
continue
end
original_parent = Helper.change_root_parent(get_param(sub_names{i}, 'Parent'), obj.original_model_name);
if isempty(original_parent)
original_name = obj.original_model_name;
end
key = struct(Helper.sub_name, original_name, Helper.sub_parents, original_parent, Helper.model_path, obj.original_model_path);
if ~name2subinfo.isKey({key})
continue
end
curr_sub = Subsystem(name2subinfo{{key}});
try
if curr_sub.local_depth == curr_depth
sub_at_depth_found = 1;
obj = obj.switch_sub(curr_sub, name2subinfo);
end
catch ME
Helper.log('log_switch_up', string(jsonencode(obj)) + newline + jsonencode(key) + ME.identifier + " " + ME.message + newline + string(ME.stack(1).file) + ", Line: " + ME.stack(1).line);
continue
end
obj.copy_version(1);
end
if ~sub_at_depth_found && curr_depth
break;
end
curr_depth = curr_depth + 1;
end
Helper.with_preserved_cfg(@close_system, obj.root_model_path, 0)
delete(obj.root_model_path)
end
function obj = switch_sub(obj, old_sub, name2subinfo)
[new_subs, mappings] = GModelMutator.find_equivalent_subs_and_mappings(old_sub, name2subinfo);
%new_subs{2} = new_subs{1}
%mappings{2} = mappings{1}
if length(new_subs) > 1
next_sub_index = GModelMutator.choose_new_sub(old_sub, new_subs);
new_sub = Subsystem(new_subs{next_sub_index});
mapping = mappings{next_sub_index};
obj = obj.switch_sub_with_sub(old_sub, new_sub, mapping);
obj = obj.check_models_correctness();
if obj.built_correct
obj = obj.save_version();
end
end
end
function obj = switch_sub_with_sub(obj, old_sub, new_sub, mapping)
copy_from = new_sub.identity;
Helper.with_preserved_cfg(@load_system, copy_from.model_path);
copy_to = Identity(old_sub.identity.sub_name, Helper.change_root_parent(old_sub.identity.sub_parents, char(obj.model_name)), obj.root_model_path);
copied_element = Identity(['sub' int2str(Helper.found_alt(1))], copy_to.sub_parents, obj.root_model_path);
copy_to = GModelMutator.copy_SS(obj.model_name, obj.root_model_path, copy_from, copy_to, copied_element, mapping);
GModelMutator.annotate(copy_to.get_qualified_name(), "Copied system from: " + copy_from.get_qualified_name() + newline + " into: " + old_sub.identity.hash())
%BuilderModel.annotate(copy_to.model_name, "Copied system into: " + '<a href="matlab:open_system(''' + copy_to.ancestor_names + ''')">Click Here</a>')
GModelMutator.annotate(obj.model_name, "Copied " + copy_from.get_qualified_name() + " to: " + copy_to.get_qualified_name())
end
function obj = check_models_correctness(obj)
obj.built_correct = obj.loadable() && (~Helper.needs_to_be_compilable || obj.compilable());
end
function ld = loadable(obj)
try
Helper.with_preserved_cfg(@load_system, obj.root_model_path);
ld = 1;
catch
ld = 0;
end
end
function cp = compilable(obj)
Helper.create_garbage_dir()
try
eval([char(obj.model_name), '([],[],[],''compile'');']);
cp = 1;
try
while 1
eval([char(obj.model_name), '([],[],[],''term'');']);
end
catch
end
catch ME
if contains(pwd, "tmp_garbage")
cd("..")
end
Helper.log('log_compile', string(jsonencode(obj)) + newline + ME.identifier + " " + ME.message + newline + string(ME.stack(1).file) + ", Line: " + ME.stack(1).line)
cp = 0;
end
if contains(pwd, "tmp_garbage")
cd("..")
end
Helper.clear_garbage();
end
end
methods (Static)
function [new_subs, mappings] = find_equivalent_subs_and_mappings(old_sub, name2subinfo)
new_subs = {}; mappings = {};
keys = name2subinfo.keys();
for i=1:length(keys)
alt_sub = name2subinfo{keys(i)};
mapping = old_sub.interface.get_mapping(alt_sub.(Helper.interface));
if isstruct(mapping)
if ~all(1:length(mapping.outmapping) == mapping.outmapping)
disp(mapping)
end
new_subs{end + 1} = alt_sub;
mappings{end + 1} = mapping;
end
end
end
function index = choose_new_sub(old_sub, new_subs)
%remove current sub from alt_subs, as to get a real alternative sub
new_subs(GModelMutator.find_identical(old_sub, new_subs)) = [];
%sort alt_subs by depth
[max_index, min_index] = GModelMutator.get_extremes(new_subs, Helper.local_depth);
if Helper.wish_property == Helper.deep
index = max_index;
return
elseif Helper.wish_property == Helper.shallow
index = min_index;
return
end
%neither: choose a random sub
index = randi(length(new_subs));
end
function [min_index, max_index] = get_extremes(subs, keyword)
min_index = -1; max_index = -1;
minimum = 2^32; maximum = -2^31;
for i=1:length(subs)
curr_val = subs{i}.(keyword);
if curr_val < minimum
minimum = curr_val;
min_index = i;
end
if curr_val > maximum
maximum = curr_val;
max_index = i;
end
end
if min_index < 0 || max_index < 0
dips("")
end
end
function index = find_identical(sub, subs)
index = -1;
for i=1:length(subs)
if sub.is_identical(subs{i})
index = i;
return
end
end
end
function connections = get_wiring(subsystem)
connections = struct;
try
lines = get_param(subsystem, 'LineHandles');
catch ME
%keyboard
end
connections.in_source_ports = GModelMutator.get_wiring_of(lines.Inport, "SrcPortHandle", subsystem);
connections.out_destination_ports = GModelMutator.get_wiring_of(lines.Outport, "DstPortHandle", subsystem);
connections.Enable = GModelMutator.get_wiring_of(lines.Enable, "SrcPortHandle", subsystem);
connections.Trigger = GModelMutator.get_wiring_of(lines.Trigger, "SrcPortHandle", subsystem);
connections.LConn = {};
connections.RConn = {};
pc = get_param(subsystem, 'PortConnectivity');
for i=1:length(pc)
if startsWith(pc(i).Type, 'LConn')
connections.LConn{end + 1} = pc(i).DstPort;
elseif startsWith(pc(i).Type, 'RConn')
connections.LConn{end + 1} = pc(i).DstPort;
end
end
connections.Ifaction = get_param(lines.Ifaction, "SrcPortHandle");
connections.Reset = lines.Reset;
end
function out_connection = get_wiring_of(lines_in, param_string, subsystem)
out_connection = {};
for i=1:length(lines_in)
line = lines_in(i);
if line == -1
out_connection{end + 1} = 0;%0 means no connection
else
other_port = get_param(line, param_string);
op = [];
for o=1:length(other_port)
try
if other_port(o) < 0
continue
end
if strcmp(get_param(other_port(o), 'Parent'), subsystem) %check whether we are connected to ourselves, treat as special case. also only connect to it once
if strcmp(param_string, "SrcPortHandle")
op(end + 1) = -get_param(other_port(o), "PortNumber");%negative numbers are actual Port Numbers from self-connected block, not Port Handles
end
else
op(end + 1) = other_port(o);
end
catch ME
%keyboard
end
end
out_connection{end + 1} = op;
end
end
end
function remove_lines(subsystem)
try
line_handles = get_param(subsystem, "LineHandles");
catch ME
%keyboard
end
GModelMutator.remove_lines2(line_handles.Outport);
GModelMutator.remove_lines2(line_handles.Inport);
GModelMutator.remove_lines2(line_handles.Enable);
GModelMutator.remove_lines2(line_handles.Trigger);
GModelMutator.remove_lines2(line_handles.LConn);
GModelMutator.remove_lines2(line_handles.RConn);
GModelMutator.remove_lines2(line_handles.Ifaction);
GModelMutator.remove_lines2(line_handles.Reset);
GModelMutator.remove_lines2(line_handles.Event);
end
function make_subsystem_editable(subsystem)
while Helper.get_depth(subsystem) > 0 && get_param(subsystem, "LinkStatus") ~= "none"
try
set_param(subsystem, "LinkStatus", "none")
catch
end
subsystem = get_param(subsystem, "Parent");
end
end
function remove_lines2(lines)
for i = 1:length(lines)
if lines(i) ~= -1
try
delete_line(lines(i))
catch ME%we delete from outport first, if self-connections are present, then they will be gone for the inports, here
end
end
end
end
function copy_to = copy_SS(model_name, model_path, copy_from, copy_to, copied_element, mapping)
if copy_to.is_root()
copy_to = GModelMutator.copy_to_root(model_name, model_path, copy_from, copy_to);
else
copy_to = GModelMutator.copy_to_non_root(copy_to, copy_from, copied_element, mapping);
end
end
function [copy_to, additional_level] = copy_to_root(model_name, root_model_path, copy_from, copy_to)
additional_level = 0;
if copy_from.is_root()
%copy from root to root
root_model_path = root_model_path + copy_from.model_path(end-3:end);
copyfile(copy_from.model_path, root_model_path);
Helper.with_preserved_cfg(@load_system, root_model_path);
copy_to = Identity(char(model_name), '', root_model_path);
else
%copy from subsystem to root
delete(root_model_path);Helper.with_preserved_cfg(@close_system, model_name, 0);%in mutate.m, the file exists, here
new_system(model_name);
Helper.with_preserved_cfg(@save_system, model_name, root_model_path);
Helper.with_preserved_cfg(@load_system, copy_from.model_path);
copy_to.sub_name = model_name;
Simulink.BlockDiagram.deleteContents(copy_to.get_qualified_name())
additional_level = 1;
add_block(copy_from.get_qualified_name(), copy_to.get_qualified_name()+"/"+copy_from.sub_name,'CopyOption','nolink');
copy_to.sub_parents = copy_to.sub_name;
copy_to.sub_name = copy_from.sub_name;
end
%set_param(copy_to.get_qualified_name(),"Lock","off")
%GModelMutator.resolve_all_links(copy_to.get_qualified_name())
%we don't need to rewire the inputs/outputs after copying
end
function resolve_all_links(sys_name)
handle = get_param(sys_name,'handle');
subsystem_handles = Helper.find_subsystems(handle);
for i = 1:length(subsystem_handles)
try
set_param(subsystem_handles(i),'LinkStatus','none')
catch ME
%keyboard
end
end
end
function copy_to = copy_to_non_root(copy_to, copy_from, copied_element, mapping)
%get prior wiring
try
get_param(copy_to.get_qualified_name(),'handle');
catch
copy_to.sub_name = [copy_to.sub_name ' synthed'];
end
GModelMutator.make_subsystem_editable(copy_to.get_qualified_name())
connected_blocks = GModelMutator.get_wiring(copy_to.get_qualified_name());
if strcmp(strrep(copy_to.get_qualified_name,newline,''), 'model3/variant dependent/variant dependent Test Control_1/3-phase Programmable Source/VariationSubSystem/PID Controller/Output Delay')
%keyboard
end
GModelMutator.remove_lines(copy_to.get_qualified_name());
old_pos = get_param(copy_to.get_qualified_name(), "Position");
delete_block(copy_to.get_qualified_name())
if copy_from.is_root()
%copy from root to subsystem
add_block('built-in/Subsystem', copied_element.get_qualified_name())
Simulink.BlockDiagram.copyContentsToSubsystem(copy_from.get_qualified_name(), copied_element.get_qualified_name())
set_param(copied_element.get_qualified_name(), 'Name', copy_to.get_sub_name_for_diagram())
else
%copy from subsystem to subsystem
try
add_block(copy_from.get_qualified_name(), copy_to.get_qualified_name(),'CopyOption','nolink');
catch ME
GModelMutator.last_minute_cleanup_crew(copy_from)
add_block(copy_from.get_qualified_name(), copy_to.get_qualified_name(),'CopyOption','nolink');
end
end
set_param(copy_to.get_qualified_name(), "Position", old_pos)
%now, rewire
GModelMutator.add_lines(copy_to, connected_blocks, mapping)
%GModelMutator.resolve_all_links(copy_to.get_qualified_name())
end
function last_minute_cleanup_crew(copy_from)
disp("cleanup " + string(copy_from.get_qualified_name))
model = copy_from.get_model_name;
lock_links_masks = {'LinkStatus', 'none'; 'Lock', 'off'; 'LockLinksToLibrary', 'off'; 'Permissions', 'ReadWrite'; 'LinkStatus', 'none'; 'Lock', 'off'; 'LockLinksToLibrary', 'off'; 'Mask', 'off'};
block_functions = {'OpenFcn' 'LoadFcn' 'MoveFcn' 'NameChangeFcn' 'PreCopyFcn' 'CopyFcn' 'ClipboardFcn' 'PreDeleteFcn' 'DeleteFcn' 'DestroyFcn' 'UndoDeleteFcn' 'InitFcn' 'StartFcn' 'ContinueFcn' 'PauseFcn' 'StopFcn' 'PreSaveFcn' 'PostSaveFcn' 'CloseFcn' 'ModelCloseFcn', 'DeleteChildFcn', 'ErrorFcn', 'ParentCloseFcn'};
blocks = find_system(model, 'LookUnderMasks', 'on', 'FollowLinks','on', 'Variants','AllVariants', 'IncludeCommented', 'on');
for j = 1:numel(blocks)
for ll = 1:size(lock_links_masks, 1)
try
set_param(blocks{j}, lock_links_masks{ll, 1}, lock_links_masks{ll, 2});
catch
end
end
for bf = 1:length(block_functions)
try
set_param(blocks{j}, block_functions{bf}, '');
catch
end
end
end
save_system(model)
end
function add_lines(system, ports, mapping)
ph = get_param(system.get_qualified_name(), "PortHandles");
for i=1:length(ports.in_source_ports)
if ports.in_source_ports{i} ~= 0
try
if ports.in_source_ports{i} < 0%are we self-connected?
add_line(system.sub_parents, ph.Outport(-ports.in_source_ports{i}), ph.Inport(mapping.inmapping(i)), 'autorouting','on')
else
add_line(system.sub_parents, ports.in_source_ports{i}, ph.Inport(mapping.inmapping(i)), 'autorouting','on')
end
catch ME
%keyboard
end
end
end
for i=1:length(ports.out_destination_ports)
outports = ports.out_destination_ports{i};
if outports ~= 0
for j=1:length(outports)
try
add_line(system.sub_parents, ph.Outport(mapping.outmapping(i)), outports(j), 'autorouting','on')
catch ME
%keyboard
end
end
end
end
GModelMutator.add_special_lines(system, ports, ph)
end
function add_special_lines(system, ports, ph)
special_lines = {{ports.Enable,ph.Enable}, {ports.Trigger,ph.Trigger}, {[ports.LConn ports.RConn],[ph.LConn ph.RConn]},{ph.Ifaction,ports.Ifaction},{ports.Reset, ph.Reset}};
for i=1:length(special_lines)
srcdsts = special_lines{i};
dsts = srcdsts{1};
for d=1:length(dsts)
if isfloat(dsts)
dests = dsts;
else
dests = dsts{d};
if iscell(dsts{d})
dests = dsts{1};
end
end
src = srcdsts{2};
src = src(d);
for j=1:length(dests)
try
if strcmp(get_param(src, 'PortType'), 'outport')
add_line(system.sub_parents, src, dests(j), 'autorouting','on');
else
add_line(system.sub_parents, dests(j), src, 'autorouting','on');
end
catch
end
end
end
end
end
function annotate(system, text)
a = Simulink.Annotation(system, string(randi(1000)));
a.FontSize = 18;
a.BackgroundColor = 'lightBlue';
%a.Interpreter = 'rich';
a.Text = text;
end
end
end