LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
parseXML.m
1function myLN = parseXML(filename, verbose)
2% MYLN = PARSEXML(FILENAME, VERBOSE)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7
8import javax.xml.parsers.DocumentBuilderFactory;
9import javax.xml.parsers.DocumentBuilder;
10import org.w3c.dom.Document;
11import org.w3c.dom.NodeList;
12import org.w3c.dom.Node;
13import org.w3c.dom.Element;
14import java.io.File;
15
16import LayeredNetwork.*;
17
18% LQN
19myLN = LayeredNetwork(strrep(filename,'_','\_'));
20
21if nargin<2%~exist('verbose','var')
22 verbose = 0;
23end
24
25% init Java XML parser and load file
26dbFactory = DocumentBuilderFactory.newInstance();
27dBuilder = dbFactory.newDocumentBuilder();
28
29fid=fopen(filename,'r');
30if fid==-1
31 line_error(mfilename,'File cannot be found. Verify the current directory and the specified filename.');
32else
33 fclose(fid);
34end
35
36if isempty(fileparts(filename))
37 doc = dBuilder.parse(which(filename));
38else
39 doc = dBuilder.parse(filename);
40end
41doc.getDocumentElement().normalize();
42if verbose > 0
43 line_printf(['Parsing LQN file: ',filename]);
44 line_printf(['Root element :',char(doc.getDocumentElement().getNodeName())]);
45end
46
47hosts = cell(0); %list of hosts - Proc
48tasks = cell(0); %list of tasks - Task, ProcID
49entries = cell(0); %list of entries - Entry, TaskID, ProcID
50activities = cell(0); %list of activities - Act, TaskID, ProcID
51procID = 1;
52taskID = 1;
53entryID = 1;
54actID = 1;
55procObj = cell(0);
56taskObj = cell(0);
57entryObj = cell(0);
58actObj = cell(0);
59
60procList = doc.getElementsByTagName('processor');
61for i = 0:procList.getLength()-1
62 %Element - Host
63 procElement = procList.item(i);
64 name = char(procElement.getAttribute('name'));
65 scheduling = char(procElement.getAttribute('scheduling'));
66 multiplicity = str2double(char(procElement.getAttribute('multiplicity')));
67 replication = str2double(char(procElement.getAttribute('replication')));
68
69 if isnan(replication)
70 replication=1;
71 end
72 if strcmp(scheduling, 'inf')
73 if isfinite(multiplicity)
74 line_warning(mfilename,'A finite multiplicity is specified for a host processor with INF scheduling. Remove it or set it to "inf".\n');
75 end
76 multiplicity = Inf;
77 elseif isnan(multiplicity)
78 multiplicity = 1;
79 end
80 quantum = str2double(char(procElement.getAttribute('quantum')));
81 if isnan(quantum)
82 quantum = 0.001;
83 end
84 speedFactor = str2double(char(procElement.getAttribute('speed-factor')));
85 if isnan(speedFactor)
86 speedFactor = 1.0;
87 end
88 newProc = Processor(myLN, name, multiplicity, SchedStrategy.fromText(scheduling), quantum, speedFactor);
89 newProc.setReplication(replication);
90 procObj{end+1,1} = newProc;
91
92 taskList = procElement.getElementsByTagName('task');
93 for j = 0:taskList.getLength()-1
94 %Element - Task
95 taskElement = taskList.item(j);
96 name = char(taskElement.getAttribute('name'));
97 scheduling = char(taskElement.getAttribute('scheduling'));
98 replication = str2double(char(taskElement.getAttribute('replication')));
99 if isnan(replication)
100 replication=1;
101 end
102
103 multiplicity = str2double(char(taskElement.getAttribute('multiplicity')));
104 if strcmp(scheduling, 'inf')
105 if isfinite(multiplicity)
106 line_warning(mfilename,'A finite multiplicity is specified for a task with inf scheduling. Remove it or set it to inf.\n');
107 end
108 multiplicity = Inf;
109 elseif isnan(multiplicity)
110 multiplicity = 1;
111 end
112 thinkTimeMean = str2double(char(taskElement.getAttribute('think-time')));
113 if isnan(thinkTimeMean)
114 thinkTimeMean = 0.0;
115 end
116 if thinkTimeMean <= 0.0
117 thinkTime = Immediate.getInstance();
118 else
119 thinkTime = Exp.fitMean(thinkTimeMean);
120 end
121 newTask = Task(myLN, name, multiplicity, SchedStrategy.fromText(scheduling), thinkTime);
122 newTask.setReplication(replication);
123 taskObj{end+1,1} = newTask;
124
125 entryList = taskElement.getElementsByTagName('entry');
126 for k = 0:entryList.getLength()-1
127 %Element - Entry
128 entryElement = entryList.item(k);
129 name = char(entryElement.getAttribute('name'));
130 newEntry = Entry(myLN, name);
131 openArrivalRate = str2double(char(entryElement.getAttribute('open-arrival-rate')));
132 if ~isnan(openArrivalRate)
133 newEntry.openArrivalRate = openArrivalRate;
134 end
135 entryObj{end+1,1} = newEntry;
136
137 % Parse forwarding calls
138 forwardingList = entryElement.getElementsByTagName('forwarding');
139 for fw = 0:forwardingList.getLength()-1
140 fwdElement = forwardingList.item(fw);
141 destName = char(fwdElement.getAttribute('dest'));
142 probStr = char(fwdElement.getAttribute('prob'));
143 if isempty(probStr)
144 prob = 1.0;
145 else
146 prob = str2double(probStr);
147 end
148 newEntry.forward(destName, prob);
149 end
150
151 %entry-phase-activities
152 entryPhaseActsList = entryElement.getElementsByTagName('entry-phase-activities');
153 if entryPhaseActsList.getLength > 0
154 entryPhaseActsElement = entryPhaseActsList.item(0);
155 actList = entryPhaseActsElement.getElementsByTagName('activity');
156 name = cell(actList.getLength(),1);
157 for l = 0:actList.getLength()-1
158 %Element - Activity
159 actElement = actList.item(l);
160 phase = str2double(char(actElement.getAttribute('phase')));
161 name{phase} = char(actElement.getAttribute('name'));
162 hostDemandMean = str2double(char(actElement.getAttribute('host-demand-mean')));
163 hostDemandSCV = str2double(char(actElement.getAttribute('host-demand-cvsq')));
164 if isnan(hostDemandSCV)
165 hostDemandSCV = 1.0;
166 end
167 if hostDemandMean <= 0.0
168 hostDemand = Immediate.getInstance();
169 else
170 if hostDemandSCV <= 0.0
171 hostDemand = Det(hostDemandMean);
172 elseif hostDemandSCV == 1.0
173 hostDemand = Exp.fitMean(hostDemandMean);
174 else
175 hostDemand = APH.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
176 end
177 end
178 if phase == 1
179 boundToEntry = newEntry.name;
180 else
181 boundToEntry = '';
182 end
183 callOrder = char(actElement.getAttribute('call-order'));
184 newAct = Activity(myLN, name{phase}, hostDemand, boundToEntry, callOrder);
185
186 % Parse activity think-time
187 actThinkTimeMean = str2double(char(actElement.getAttribute('think-time')));
188 if ~isnan(actThinkTimeMean) && actThinkTimeMean > 0.0
189 newAct.setThinkTime(actThinkTimeMean);
190 end
191
192 actObj{end+1,1} = newAct;
193
194 %synch-call
195 synchCalls = actElement.getElementsByTagName('synch-call');
196 for m = 0:synchCalls.getLength()-1
197 callElement = synchCalls.item(m);
198 dest = char(callElement.getAttribute('dest'));
199 mean = str2double(char(callElement.getAttribute('calls-mean')));
200 newAct = newAct.synchCall(dest,mean);
201 end
202
203 %asynch-call
204 asynchCalls = actElement.getElementsByTagName('asynch-call');
205 for m = 0:asynchCalls.getLength()-1
206 callElement = asynchCalls.item(m);
207 dest = char(callElement.getAttribute('dest'));
208 mean = str2double(char(callElement.getAttribute('calls-mean')));
209 newAct = newAct.asynchCall(dest,mean);
210 end
211
212 activities{end+1,1} = newAct.name;
213 activities{end,2} = taskID;
214 activities{end,3} = procID;
215 newTask = newTask.addActivity(newAct);
216 newAct.parent = newTask;
217 actID = actID+1;
218 end
219
220 %precedence
221 for l = 1:length(name)-1
222 newPrec = ActivityPrecedence(name(l), name(l+1));
223 newTask = newTask.addPrecedence(newPrec);
224 end
225
226 %reply-entry
227 if ~isempty(name)
228 %newEntry.replyActivity{1} = name{1};
229 end
230 end
231
232 entries{end+1,1} = newEntry.name;
233 entries{end,2} = taskID;
234 entries{end,3} = procID;
235 newTask = newTask.addEntry(newEntry);
236 newEntry.parent = newTask;
237 entryID = entryID+1;
238 end
239
240 %task-activities
241 taskActsList = taskElement.getElementsByTagName('task-activities');
242 if taskActsList.getLength > 0
243 taskActsElement = taskActsList.item(0);
244 actList = taskActsElement.getElementsByTagName('activity');
245 for l = 0:actList.getLength()-1
246 %Element - Activity
247 actElement = actList.item(l);
248 if strcmp(char(actElement.getParentNode().getNodeName()),'task-activities')
249 name = char(actElement.getAttribute('name'));
250 hostDemandMean = str2double(char(actElement.getAttribute('host-demand-mean')));
251 hostDemandSCV = str2double(char(actElement.getAttribute('host-demand-cvsq')));
252 if isnan(hostDemandSCV)
253 hostDemandSCV = 1.0;
254 end
255 if hostDemandMean <= 0.0
256 hostDemand = Immediate.getInstance();
257 else
258 if hostDemandSCV <= 0.0
259 hostDemand = Det(hostDemandMean);
260 elseif hostDemandSCV < 1.0
261 hostDemand = APH.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
262 elseif hostDemandSCV == 1.0
263 hostDemand = Exp.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
264 else
265 hostDemand = HyperExp.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
266 end
267 end
268 boundToEntry = char(actElement.getAttribute('bound-to-entry'));
269 callOrder = char(actElement.getAttribute('call-order'));
270 newAct = Activity(myLN, name, hostDemand, boundToEntry, callOrder);
271
272 % Parse activity think-time
273 actThinkTimeMean = str2double(char(actElement.getAttribute('think-time')));
274 if ~isnan(actThinkTimeMean) && actThinkTimeMean > 0.0
275 newAct.setThinkTime(actThinkTimeMean);
276 end
277
278 actObj{end+1,1} = newAct;
279
280 %synch-call
281 synchCalls = actElement.getElementsByTagName('synch-call');
282 for m = 0:synchCalls.getLength()-1
283 callElement = synchCalls.item(m);
284 dest = char(callElement.getAttribute('dest'));
285 mean = str2double(char(callElement.getAttribute('calls-mean')));
286 newAct = newAct.synchCall(dest,mean);
287 end
288
289 %asynch-call
290 asynchCalls = actElement.getElementsByTagName('asynch-call');
291 for m = 0:asynchCalls.getLength()-1
292 callElement = asynchCalls.item(m);
293 dest = char(callElement.getAttribute('dest'));
294 mean = str2double(char(callElement.getAttribute('calls-mean')));
295 newAct = newAct.asynchCall(dest,mean);
296 end
297
298 activities{end+1,1} = newAct.name;
299 activities{end,2} = taskID;
300 activities{end,3} = procID;
301 newTask = newTask.addActivity(newAct);
302 newAct.parent = newTask;
303 actID = actID+1;
304 end
305 end
306
307 %precedence
308 precList = taskActsElement.getElementsByTagName('precedence');
309 for l = 0:precList.getLength()-1
310 precElement = precList.item(l);
311
312 %pre
313 preTypes = {ActivityPrecedenceType.PRE_SEQ,ActivityPrecedenceType.PRE_AND,ActivityPrecedenceType.PRE_OR};
314 for m = 1:length(preTypes)
315 preType = preTypes{m};
316 preList = precElement.getElementsByTagName(ActivityPrecedenceType.toText(preType));
317 if preList.getLength() > 0
318 break
319 end
320 end
321 preElement = preList.item(0);
322 preParams = [];
323 preActList = preElement.getElementsByTagName('activity');
324
325 % assumes that preType is a numeric precedence ID
326 % if strcmp(preType,ActivityPrecedenceType.toText(ActivityPrecedenceType.PRE_OR))
327 if preType == ActivityPrecedenceType.PRE_OR
328 preActs = cell(preActList.getLength(),1);
329 preParams = zeros(postActList.getLength(),1);
330 for m = 0:preActList.getLength()-1
331 preActElement = preActList.item(m);
332 preActs{m+1} = char(preActElement.getAttribute('name'));
333 preParams(m+1) = str2double(char(preActElement.getAttribute('prob')));
334 end
335 % elseif strcmp(preType,ActivityPrecedenceType.toText(ActivityPrecedenceType.PRE_AND))
336 elseif preType == ActivityPrecedenceType.PRE_AND
337 preActs = cell(preActList.getLength(),1);
338 for m = 0:preActList.getLength()-1
339 preActElement = preActList.item(m);
340 preActs{m+1} = char(preActElement.getAttribute('name'));
341 end
342 preParams = str2double(char(preElement.getAttribute('quorum')));
343 else % simple PRE
344 preActs = cell(1,1);
345 preActElement = preActList.item(0);
346 preActs{1} = char(preActElement.getAttribute('name'));
347 end
348 if isnan(preParams)
349 preParams = [];
350 end
351
352 %post
353 postTypes = {ActivityPrecedenceType.POST_SEQ, ActivityPrecedenceType.POST_AND, ActivityPrecedenceType.POST_OR, ActivityPrecedenceType.POST_LOOP, ActivityPrecedenceType.POST_CACHE};
354 for m = 1:length(postTypes)
355 postType = postTypes{m};
356 postList = precElement.getElementsByTagName(ActivityPrecedenceType.toText(postType));
357 if postList.getLength() > 0
358 break
359 end
360 end
361
362 postElement = postList.item(0);
363 postActList = postElement.getElementsByTagName('activity');
364
365 % assumes that postType is a numeric precedence ID
366 % if strcmp(postType,ActivityPrecedenceType.toText(ActivityPrecedenceType.POST_OR))
367 if postType == ActivityPrecedenceType.POST_OR
368 postActs = cell(postActList.getLength(),1);
369 postParams = zeros(postActList.getLength(),1);
370 for m = 0:postActList.getLength()-1
371 postActElement = postActList.item(m);
372 postActs{m+1} = char(postActElement.getAttribute('name'));
373 postParams(m+1) = str2double(char(postActElement.getAttribute('prob')));
374 end
375 %elseif strcmp(postType,ActivityPrecedenceType.toText(ActivityPrecedenceType.POST_LOOP))
376 elseif postType == ActivityPrecedenceType.POST_LOOP
377 postActs = cell(postActList.getLength()+1,1);
378 postParams = zeros(postActList.getLength(),1);
379 for m = 0:postActList.getLength()-1
380 postActElement = postActList.item(m);
381 postActs{m+1} = char(postActElement.getAttribute('name'));
382 postParams(m+1) = str2double(char(postActElement.getAttribute('count')));
383
384 end
385 postActs{end} = char(postElement.getAttribute('end'));
386 else
387 postActs = cell(postActList.getLength(),1);
388 postParams = [];
389 for m = 0:postActList.getLength()-1
390 postActElement = postActList.item(m);
391 postActs{m+1} = char(postActElement.getAttribute('name'));
392 end
393 end
394 newPrec = ActivityPrecedence(preActs, postActs, preType, postType, preParams, postParams);
395 newTask = newTask.addPrecedence(newPrec);
396 end
397
398 %reply-entry
399 replyList = taskActsElement.getElementsByTagName('reply-entry');
400 for l = 0:replyList.getLength()-1
401 replyElement = replyList.item(l);
402 replyName = char(replyElement.getAttribute('name'));
403 replyIdx = findstring(entries(:,1), replyName);
404 replyActList = replyElement.getElementsByTagName('reply-activity');
405 for m = 0:replyActList.getLength()-1
406 replyActElement = replyActList.item(m);
407 replyActName = char(replyActElement.getAttribute('name'));
408 entryObj{replyIdx}.replyActivity{end+1} = replyActName;
409 end
410 end
411 end
412
413 tasks{end+1,1} = newTask.name;
414 tasks{end,2} = procID;
415 newProc = newProc.addTask(newTask);
416 taskID = taskID+1;
417 end
418
419 hosts{end+1,1} = newProc.name;
420 procID = procID+1;
421end
422end