-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenEpBuildingBlock.m
151 lines (126 loc) · 5.37 KB
/
OpenEpBuildingBlock.m
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
classdef OpenEpBuildingBlock
%OPENEPBUILDINGBLOCK Summary of this class goes here
% An OPENEPBUILDINGBLOCK object is used to execute OpenEP function
% calls which are encapsulated (described) in OpenEP workflow .xml
% files.
%
% Example:
% workflowblock1 = OpenEpBuildingBlock('/Users/steven/Dropbox/Matlab_Code/workingdir/openep-workflows/def/loadOpenEpData.xml');
% workflowblock2 = OpenEpBuildingBlock('/Users/steven/Dropbox/Matlab_Code/workingdir/openep-workflows/def/getVoltageMaps.xml');
properties (Access = public)
name
author
date
license
description
xmlPath
% these properties allow a work flow block to be displayed with
% options
inputs
outputs
% these properties define the actual function call
functionCall
% this property defines the chain of events
hasPrevious
previousBuildingBlock
end
properties (Access = private)
openEpFunction
end
methods
function obj = OpenEpBuildingBlock(xmlFilePath)
%OPENEPBUILDINGBLOCK Construct an instance of this class
% The constructor reads the contents of the XML file and
% creates an OpenEpBuildingBlock object. The only properties
% that are assigned within the constructor are those which
% have been read from the XML file. This prevents us having
% to read the XML file later whilst not undertaking
% unnecessary processing at this stage which might never be
% needed if the building block is not used.
obj.xmlPath = xmlFilePath;
workflowblock = xml_read(obj.xmlPath);
% store the metadata
obj.name = workflowblock.metadata.name;
obj.author = workflowblock.metadata.author;
obj.date = workflowblock.metadata.date;
obj.license = workflowblock.metadata.license;
obj.description = workflowblock.metadata.description;
% store the OpenEP function
obj.openEpFunction = workflowblock.openepfunction.functionname;
% parse the inputs
numInputs = numel(workflowblock.inputs.input);
for i = 1:numInputs
obj.inputs{i}.name = workflowblock.inputs.input(i).CONTENT;
obj.inputs{i}.type = workflowblock.inputs.input(i).ATTRIBUTE.type;
switch obj.inputs{i}.type
case 'select'
try
obj.inputs{i}.options = strsplit(workflowblock.inputs.input(i).ATTRIBUTE.options);
end
case 'numeric'
try
obj.inputs{i}.range = workflowblock.inputs.input(i).ATTRIBUTE.range;
end
end
end
% parse the outputs
numOutputs = numel(workflowblock.outputs.output);
for i = 1:numOutputs
obj.outputs{i}.name = workflowblock.outputs.output(i).CONTENT;
obj.outputs{i}.type = workflowblock.outputs.output(i).ATTRIBUTE.type;
end
%obj.hasPrevious
end
function save(obj)
%OPENEPBUILDINGBLOCK/SAVE Writes XML files describing this
% object
% update the workflowblock with each property
% write the workflowblock to file
xml_write(obj.xmlPath, workflowblock)
end
function obj = parseFunctionCall(obj)
%OPENEPBUILDINGBLOCK/PARSEFUNCTIONCALL creates the required
% OpenEP function call from this object which
workflowblock = xml_read(obj.xmlPath);
% parse the arguments
numArgs = numel(workflowblock.openepfunction.essential.argument);
for i = 1:numArgs
order(i) = workflowblock.openepfunction.essential.argument(i).position;
end
if numel(unique(order)) ~= numel(order)
error('OPENEP/OPENEPBUILDINGBLOCK: Multiple arguments have been given the same position in the XML specification file');
end
essentialArguments = '';
for i = 1:numArgs
iArg = order(i);
nameArg = workflowblock.openepfunction.essential.argument(iArg).value;
if isempty(essentialArguments)
essentialArguments = nameArg;
else
essentialArguments = [essentialArguments ...
', ' ...
nameArg]; %#ok<*AGROW>
end
end
obj.functionCall = [ obj.openEpFunction ...
'(' ...
essentialArguments ...
')' ...
];
end
function execute(obj)
% create the function call
parseFunctionCall(obj);
% first execute any previous function calls
if obj.hasPrevious
output = obj.previousBuildingBlock.execute();
end
% convert into variables in the local space
for i = 1:numel(output)
eval(sprintf('%s = %g', output(1).name, output(1).value));
end
% execute the function call
eval(obj.functionCall);
end
end
end