-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfind_node.m
More file actions
executable file
·190 lines (169 loc) · 6.36 KB
/
Copy pathfind_node.m
File metadata and controls
executable file
·190 lines (169 loc) · 6.36 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
function [nodes, path] = find_node(root, varargin)
% FIND_NODE Search the comparison tree for nodes.
% Note1: LeftRootFile and RightRootFile must be loaded/opened.
% Note2: Does not return the xmlcomp.Edits objects itself.
%
% Inputs:
% root xmlcomp.Edits object.
% varargin Search constraint as a 'Name', 'Value' pair. See below.
%
% Outputs:
% nodes Node array of xmlcomp.Node objects.
% path Cell array of node paths in the comparison tree (not
% model paths).
%
% Usage:
% nodes = FIND_NODE(ROOT, 'CONSTRAINT1', 'VALUE1', ...)
% constrains the search of FIND_NODES to the specified constraint/value
% pairs (not case-sensitive, except for NodeName). The following describes
% the available constraint/value pairs:
%
% NodeType ['block' | 'line' | 'port' | 'annotation' | 'mask' | 'block_diagram' | 'stateflow' | ...]
% ChangeType ['added' | 'deleted' | 'modified' | 'renamed' | 'none']
% BlockType ['SubSystem' | 'Inport' | 'Outport' | ...]
% StateflowType ['Stateflow.Annotation' | 'Sateflow.Transition' | 'Sateflow.State' | ...]
% NodeName <Node.Name>
% FirstOnly [('off'), 'on'] For changes with 2 nodes (e.g., none, modified),
% returns the first 'before' node. This is
% useful for counting nodes.
%
% Multiple values for a single constraint can be provided via a cell array.
%
% Example:
% >> allNodes = find_node(Edits)
%
% allNodes =
%
% 27x1 Node array with properties:
%
% Children
% Edited
% Name
% Parameters
% Parent
% Partner
% Validate inputs
try
assert(isa(root, 'xmlcomp.Edits'))
catch
message = 'Node argument must be an xmlcomp.Edits object.';
error(message)
end
% Parse varargin
changeType = lower(getInput('ChangeType', varargin));
firstOnly = lower(getInput('FirstOnly', varargin, 'off'));
% Find the nodes
% Don't have to check both branches, depending on the ChangeType:
% 1) Check RIGHT branch for Added, Modified, Renamed
% 2) Check LEFT branch for Deleted, Modified, Renamed
% 3) Check BOTH if no ChangeTypes or None
nodesFoundLeft = [];
nodesFoundRight = [];
if isempty(changeType) || any(ismember(changeType, {'none', 'added', 'modified', 'renamed'}))
nodesFoundRight = findNode(root.RightRoot, root, root.RightFileName, varargin);
end
if strcmp(firstOnly, 'off')
if isempty(changeType) || any(ismember(changeType, {'none', 'deleted', 'modified', 'renamed'}))
nodesFoundLeft = findNode(root.LeftRoot, root, root.LeftFileName, varargin);
end
else
if isempty(changeType) || any(ismember(changeType, {'none', 'deleted'}))
varargin{2} = {'added', 'deleted'};
nodesFoundLeft = findNode(root.LeftRoot, root, root.LeftFileName, varargin);
end
end
nodes = [nodesFoundLeft; nodesFoundRight]; % Combine node lists in case both sides were checked
% Get paths of the found nodes
if nargout > 1
path = cell(length(nodes),1);
for i = 1:length(nodes)
try
path{i} = getPathTree(nodes(i));
catch
path{i} = '';
end
end
end
end
function out = findNode(node, root, file, varargin)
% FINDNODE Recurse through the tree to find the node.
%
% Inputs:
% node xmlcomp.Node object at which to start the search.
% root xmlcomp.Edits object.
% file RightFileName or LeftFileName to search in.
% varargin Search constraints.
%
% Outputs:
% out xmlcomp.Node objects that are found to satisfy the constraints.
% Parse varargin
varargin = varargin{:}; % Remove nesting cells from passing varargin in
nodeType = lower(getInput('NodeType', varargin));
changeType = lower(getInput('ChangeType', varargin));
blockType = lower(getInput('BlockType', varargin));
stateflowType = lower(getInput('StateflowType', varargin));
nameValue = getInput('NodeName', varargin);
% Check against varargin constraints
meetsConstraints = true;
if ~isempty(nodeType)
if iscell(nodeType)
isNodeType = ismember(nodeType, lower(getNodeType(node, file)));
else
isNodeType = strcmpi(nodeType, getNodeType(node, file));
end
if ~any(isNodeType)
meetsConstraints = false;
end
end
if ~isempty(changeType) && meetsConstraints
if iscell(changeType)
isChangeType = ismember(changeType, lower(getNodeChangeType(node, root)));
else
isChangeType = strcmpi(changeType, getNodeChangeType(node, root));
end
if ~any(isChangeType)
meetsConstraints = false;
end
end
if ~isempty(blockType) && meetsConstraints
if iscell(blockType)
isBlockType = ismember(blockType, lower(getNodeBlockType(node, file)));
else
isBlockType = strcmpi(blockType, getNodeBlockType(node, file));
end
if ~any(isBlockType)
meetsConstraints = false;
end
end
if ~isempty(stateflowType) && meetsConstraints
if iscell(stateflowType)
isStateflowType = ismember(stateflowType, lower(getStateflowType(node)));
else
isStateflowType = strcmpi(stateflowType, getStateflowType(node));
end
if ~any(isStateflowType)
meetsConstraints = false;
end
end
if ~isempty(nameValue) && meetsConstraints
if iscell(nameValue)
isMatchedName = ismember(nameValue, node.Name);
else
isMatchedName = strcmp(nameValue, node.Name);
end
if ~any(isMatchedName)
meetsConstraints = false;
end
end
if meetsConstraints
out = node;
else
out = {};
end
if ~hasChildren(node)
return;
end
for i = 1:length(node.Children)
out = [out; findNode(node.Children(i), root, file, varargin)];
end
end