1function myLN = parseXML(filename, verbose)
2% MYLN = PARSEXML(FILENAME, VERBOSE)
4% Copyright (c) 2012-2026, Imperial College London
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;
16import LayeredNetwork.*;
19myLN = LayeredNetwork(strrep(filename,
'_',
'\_'));
21if nargin<2%~exist(
'verbose',
'var')
25% init Java XML parser and load file
26dbFactory = DocumentBuilderFactory.newInstance();
27dBuilder = dbFactory.newDocumentBuilder();
29fid=fopen(filename,'r');
31 line_error(mfilename,'File cannot be found. Verify the current directory and the specified filename.');
36if isempty(fileparts(filename))
37 doc = dBuilder.parse(which(filename));
39 doc = dBuilder.parse(filename);
41doc.getDocumentElement().normalize();
43 line_printf(['Parsing LQN file: ',filename]);
44 line_printf(['Root element :',
char(doc.getDocumentElement().getNodeName())]);
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
60procList = doc.getElementsByTagName('processor');
61for i = 0:procList.getLength()-1
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')));
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');
77 elseif isnan(multiplicity)
80 quantum = str2double(
char(procElement.getAttribute('quantum')));
84 speedFactor = str2double(
char(procElement.getAttribute('speed-factor')));
88 newProc = Processor(myLN, name, multiplicity, SchedStrategy.fromText(scheduling), quantum, speedFactor);
89 newProc.setReplication(replication);
90 procObj{end+1,1} = newProc;
92 taskList = procElement.getElementsByTagName(
'task');
93 for j = 0:taskList.getLength()-1
95 taskElement = taskList.item(j);
96 name = char(taskElement.getAttribute(
'name'));
97 scheduling = char(taskElement.getAttribute(
'scheduling'));
98 replication = str2double(
char(taskElement.getAttribute(
'replication')));
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');
109 elseif isnan(multiplicity)
112 thinkTimeMean = str2double(
char(taskElement.getAttribute('think-time')));
113 if isnan(thinkTimeMean)
116 if thinkTimeMean <= 0.0
117 thinkTime = Immediate.getInstance();
119 thinkTime = Exp.fitMean(thinkTimeMean);
121 newTask = Task(myLN, name, multiplicity, SchedStrategy.fromText(scheduling), thinkTime);
122 newTask.setReplication(replication);
124 % Parse priority attribute if present
125 priorityStr =
char(taskElement.getAttribute('priority'));
126 if ~isempty(priorityStr)
127 priority = str2double(priorityStr);
129 newTask.setPriority(priority);
133 % Parse fan-in element if present (used for replication load distribution)
134 fanInList = taskElement.getElementsByTagName('fan-in');
135 if fanInList.getLength() > 0
136 fanInElement = fanInList.item(0);
137 source =
char(fanInElement.getAttribute('source'));
138 valueStr =
char(fanInElement.getAttribute('value'));
139 if ~isempty(source) && ~isempty(valueStr)
140 value = str2double(valueStr);
142 newTask.setFanIn(source, value);
147 % Parse fan-out elements if present (used for replication load distribution)
148 fanOutList = taskElement.getElementsByTagName('fan-out');
149 for fo = 0:fanOutList.getLength()-1
150 fanOutElement = fanOutList.item(fo);
151 dest =
char(fanOutElement.getAttribute('dest'));
152 valueStr =
char(fanOutElement.getAttribute('value'));
153 if ~isempty(dest) && ~isempty(valueStr)
154 value = str2double(valueStr);
156 newTask.setFanOut(dest, value);
161 taskObj{end+1,1} = newTask;
163 entryList = taskElement.getElementsByTagName(
'entry');
164 for k = 0:entryList.getLength()-1
166 entryElement = entryList.item(k);
167 name = char(entryElement.getAttribute(
'name'));
168 newEntry = Entry(myLN, name);
169 openArrivalRate = str2double(
char(entryElement.getAttribute(
'open-arrival-rate')));
170 if ~isnan(openArrivalRate)
171 newEntry.openArrivalRate = openArrivalRate;
174 % Parse entry type attribute
175 eType = char(entryElement.getAttribute(
'type'));
177 newEntry.setType(eType);
180 entryObj{end+1,1} = newEntry;
182 % Parse forwarding calls
183 forwardingList = entryElement.getElementsByTagName(
'forwarding');
184 for fw = 0:forwardingList.getLength()-1
185 fwdElement = forwardingList.item(fw);
186 destName = char(fwdElement.getAttribute(
'dest'));
187 probStr = char(fwdElement.getAttribute(
'prob'));
191 prob = str2double(probStr);
193 newEntry.forward(destName, prob);
196 %entry-phase-activities
197 entryPhaseActsList = entryElement.getElementsByTagName(
'entry-phase-activities');
198 if entryPhaseActsList.getLength > 0
199 entryPhaseActsElement = entryPhaseActsList.item(0);
200 actList = entryPhaseActsElement.getElementsByTagName(
'activity');
201 name = cell(actList.getLength(),1);
202 for l = 0:actList.getLength()-1
204 actElement = actList.item(l);
205 phase = str2double(
char(actElement.getAttribute(
'phase')));
206 name{phase} = char(actElement.getAttribute(
'name'));
207 hostDemandMean = str2double(
char(actElement.getAttribute(
'host-demand-mean')));
208 hostDemandSCV = str2double(
char(actElement.getAttribute(
'host-demand-cvsq')));
209 if isnan(hostDemandSCV)
212 if hostDemandMean <= 0.0
213 hostDemand = Immediate.getInstance();
215 if hostDemandSCV <= 0.0
216 hostDemand = Det(hostDemandMean);
217 elseif hostDemandSCV == 1.0
218 hostDemand = Exp.fitMean(hostDemandMean);
220 hostDemand = APH.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
224 boundToEntry = newEntry.name;
228 callOrder = char(actElement.getAttribute(
'call-order'));
229 newAct = Activity(myLN, name{phase}, hostDemand, boundToEntry, callOrder);
230 newAct.phase = phase; % Store the phase number
232 % Parse activity think-time
233 actThinkTimeMean = str2double(
char(actElement.getAttribute(
'think-time')));
234 if ~isnan(actThinkTimeMean) && actThinkTimeMean > 0.0
235 newAct.setThinkTime(actThinkTimeMean);
238 actObj{end+1,1} = newAct;
241 synchCalls = actElement.getElementsByTagName(
'synch-call');
242 for m = 0:synchCalls.getLength()-1
243 callElement = synchCalls.item(m);
244 dest = char(callElement.getAttribute(
'dest'));
245 mean = str2double(
char(callElement.getAttribute(
'calls-mean')));
246 newAct = newAct.synchCall(dest,mean);
250 asynchCalls = actElement.getElementsByTagName(
'asynch-call');
251 for m = 0:asynchCalls.getLength()-1
252 callElement = asynchCalls.item(m);
253 dest = char(callElement.getAttribute(
'dest'));
254 mean = str2double(
char(callElement.getAttribute(
'calls-mean')));
255 newAct = newAct.asynchCall(dest,mean);
258 activities{end+1,1} = newAct.name;
259 activities{end,2} = taskID;
260 activities{end,3} = procID;
261 newTask = newTask.addActivity(newAct);
262 newAct.parent = newTask;
267 for l = 1:length(name)-1
268 newPrec = ActivityPrecedence(name(l), name(l+1));
269 newTask = newTask.addPrecedence(newPrec);
272 %reply-entry: For entry-phase-activities, phase-1 activities reply implicitly
273 % Find the last phase-1 activity and set it as the reply activity
274 if ~isempty(name) && ~isempty(name{1})
275 % The last phase-1 activity (name{1}) should reply to the entry
276 newEntry.replyActivity{end+1} = name{1};
280 entries{end+1,1} = newEntry.name;
281 entries{end,2} = taskID;
282 entries{end,3} = procID;
283 newTask = newTask.addEntry(newEntry);
284 newEntry.parent = newTask;
289 taskActsList = taskElement.getElementsByTagName(
'task-activities');
290 if taskActsList.getLength > 0
291 taskActsElement = taskActsList.item(0);
292 actList = taskActsElement.getElementsByTagName(
'activity');
293 for l = 0:actList.getLength()-1
295 actElement = actList.item(l);
296 if strcmp(
char(actElement.getParentNode().getNodeName()),
'task-activities')
297 name =
char(actElement.getAttribute('name'));
298 hostDemandMean = str2double(
char(actElement.getAttribute('host-demand-mean')));
299 hostDemandSCV = str2double(
char(actElement.getAttribute('host-demand-cvsq')));
300 if isnan(hostDemandSCV)
303 if hostDemandMean <= 0.0
304 hostDemand = Immediate.getInstance();
306 if hostDemandSCV <= 0.0
307 hostDemand = Det(hostDemandMean);
308 elseif hostDemandSCV < 1.0
309 hostDemand = APH.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
310 elseif hostDemandSCV == 1.0
311 hostDemand = Exp.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
313 hostDemand = HyperExp.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
316 boundToEntry =
char(actElement.getAttribute('bound-to-entry'));
317 callOrder =
char(actElement.getAttribute('call-order'));
318 newAct = Activity(myLN, name, hostDemand, boundToEntry, callOrder);
320 % Parse activity think-time
321 actThinkTimeMean = str2double(
char(actElement.getAttribute('think-time')));
322 if ~isnan(actThinkTimeMean) && actThinkTimeMean > 0.0
323 newAct.setThinkTime(actThinkTimeMean);
326 actObj{end+1,1} = newAct;
329 synchCalls = actElement.getElementsByTagName(
'synch-call');
330 for m = 0:synchCalls.getLength()-1
331 callElement = synchCalls.item(m);
332 dest = char(callElement.getAttribute(
'dest'));
333 mean = str2double(
char(callElement.getAttribute(
'calls-mean')));
334 newAct = newAct.synchCall(dest,mean);
338 asynchCalls = actElement.getElementsByTagName(
'asynch-call');
339 for m = 0:asynchCalls.getLength()-1
340 callElement = asynchCalls.item(m);
341 dest = char(callElement.getAttribute(
'dest'));
342 mean = str2double(
char(callElement.getAttribute(
'calls-mean')));
343 newAct = newAct.asynchCall(dest,mean);
346 activities{end+1,1} = newAct.name;
347 activities{end,2} = taskID;
348 activities{end,3} = procID;
349 newTask = newTask.addActivity(newAct);
350 newAct.parent = newTask;
356 precList = taskActsElement.getElementsByTagName(
'precedence');
357 for l = 0:precList.getLength()-1
358 precElement = precList.item(l);
361 preTypes = {ActivityPrecedenceType.PRE_SEQ,ActivityPrecedenceType.PRE_AND,ActivityPrecedenceType.PRE_OR};
362 for m = 1:length(preTypes)
363 preType = preTypes{m};
364 preList = precElement.getElementsByTagName(ActivityPrecedenceType.toText(preType));
365 if preList.getLength() > 0
369 preElement = preList.item(0);
371 preActList = preElement.getElementsByTagName(
'activity');
373 % assumes that preType
is a numeric precedence ID
374 %
if strcmp(preType,ActivityPrecedenceType.toText(ActivityPrecedenceType.PRE_OR))
375 if preType == ActivityPrecedenceType.PRE_OR
376 preActs = cell(preActList.getLength(),1);
377 preParams = zeros(postActList.getLength(),1);
378 for m = 0:preActList.getLength()-1
379 preActElement = preActList.item(m);
380 preActs{m+1} = char(preActElement.getAttribute(
'name'));
381 preParams(m+1) = str2double(
char(preActElement.getAttribute(
'prob')));
383 % elseif strcmp(preType,ActivityPrecedenceType.toText(ActivityPrecedenceType.PRE_AND))
384 elseif preType == ActivityPrecedenceType.PRE_AND
385 preActs = cell(preActList.getLength(),1);
386 for m = 0:preActList.getLength()-1
387 preActElement = preActList.item(m);
388 preActs{m+1} = char(preActElement.getAttribute(
'name'));
390 preParams = str2double(
char(preElement.getAttribute(
'quorum')));
393 preActElement = preActList.item(0);
394 preActs{1} = char(preActElement.getAttribute(
'name'));
401 postTypes = {ActivityPrecedenceType.POST_SEQ, ActivityPrecedenceType.POST_AND, ActivityPrecedenceType.POST_OR, ActivityPrecedenceType.POST_LOOP, ActivityPrecedenceType.POST_CACHE};
402 for m = 1:length(postTypes)
403 postType = postTypes{m};
404 postList = precElement.getElementsByTagName(ActivityPrecedenceType.toText(postType));
405 if postList.getLength() > 0
410 postElement = postList.item(0);
411 postActList = postElement.getElementsByTagName(
'activity');
413 % assumes that postType
is a numeric precedence ID
414 %
if strcmp(postType,ActivityPrecedenceType.toText(ActivityPrecedenceType.POST_OR))
415 if postType == ActivityPrecedenceType.POST_OR
416 postActs = cell(postActList.getLength(),1);
417 postParams = zeros(postActList.getLength(),1);
418 for m = 0:postActList.getLength()-1
419 postActElement = postActList.item(m);
420 postActs{m+1} = char(postActElement.getAttribute(
'name'));
421 postParams(m+1) = str2double(
char(postActElement.getAttribute(
'prob')));
423 %elseif strcmp(postType,ActivityPrecedenceType.toText(ActivityPrecedenceType.POST_LOOP))
424 elseif postType == ActivityPrecedenceType.POST_LOOP
425 postActs = cell(postActList.getLength()+1,1);
426 postParams = zeros(postActList.getLength(),1);
427 for m = 0:postActList.getLength()-1
428 postActElement = postActList.item(m);
429 postActs{m+1} = char(postActElement.getAttribute(
'name'));
430 postParams(m+1) = str2double(
char(postActElement.getAttribute(
'count')));
433 postActs{end} = char(postElement.getAttribute(
'end'));
435 postActs = cell(postActList.getLength(),1);
437 for m = 0:postActList.getLength()-1
438 postActElement = postActList.item(m);
439 postActs{m+1} = char(postActElement.getAttribute(
'name'));
442 newPrec = ActivityPrecedence(preActs, postActs, preType, postType, preParams, postParams);
443 newTask = newTask.addPrecedence(newPrec);
447 replyList = taskActsElement.getElementsByTagName(
'reply-entry');
448 for l = 0:replyList.getLength()-1
449 replyElement = replyList.item(l);
450 replyName = char(replyElement.getAttribute(
'name'));
451 replyIdx = findstring(entries(:,1), replyName);
452 replyActList = replyElement.getElementsByTagName(
'reply-activity');
453 for m = 0:replyActList.getLength()-1
454 replyActElement = replyActList.item(m);
455 replyActName = char(replyActElement.getAttribute(
'name'));
456 entryObj{replyIdx}.replyActivity{end+1} = replyActName;
461 tasks{end+1,1} = newTask.name;
462 tasks{end,2} = procID;
463 newProc = newProc.addTask(newTask);
467 hosts{end+1,1} = newProc.name;