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);
123 taskObj{end+1,1} = newTask;
125 entryList = taskElement.getElementsByTagName(
'entry');
126 for k = 0:entryList.getLength()-1
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;
135 entryObj{end+1,1} = newEntry;
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'));
146 prob = str2double(probStr);
148 newEntry.forward(destName, prob);
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
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)
167 if hostDemandMean <= 0.0
168 hostDemand = Immediate.getInstance();
170 if hostDemandSCV <= 0.0
171 hostDemand = Det(hostDemandMean);
172 elseif hostDemandSCV == 1.0
173 hostDemand = Exp.fitMean(hostDemandMean);
175 hostDemand = APH.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
179 boundToEntry = newEntry.name;
183 callOrder = char(actElement.getAttribute(
'call-order'));
184 newAct = Activity(myLN, name{phase}, hostDemand, boundToEntry, callOrder);
186 % Parse activity think-time
187 actThinkTimeMean = str2double(
char(actElement.getAttribute(
'think-time')));
188 if ~isnan(actThinkTimeMean) && actThinkTimeMean > 0.0
189 newAct.setThinkTime(actThinkTimeMean);
192 actObj{end+1,1} = newAct;
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);
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);
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;
221 for l = 1:length(name)-1
222 newPrec = ActivityPrecedence(name(l), name(l+1));
223 newTask = newTask.addPrecedence(newPrec);
228 %newEntry.replyActivity{1} = name{1};
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;
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
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)
255 if hostDemandMean <= 0.0
256 hostDemand = Immediate.getInstance();
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);
265 hostDemand = HyperExp.fitMeanAndSCV(hostDemandMean, hostDemandSCV);
268 boundToEntry =
char(actElement.getAttribute('bound-to-entry'));
269 callOrder =
char(actElement.getAttribute('call-order'));
270 newAct = Activity(myLN, name, hostDemand, boundToEntry, callOrder);
272 % Parse activity think-time
273 actThinkTimeMean = str2double(
char(actElement.getAttribute('think-time')));
274 if ~isnan(actThinkTimeMean) && actThinkTimeMean > 0.0
275 newAct.setThinkTime(actThinkTimeMean);
278 actObj{end+1,1} = newAct;
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);
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);
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;
308 precList = taskActsElement.getElementsByTagName(
'precedence');
309 for l = 0:precList.getLength()-1
310 precElement = precList.item(l);
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
321 preElement = preList.item(0);
323 preActList = preElement.getElementsByTagName(
'activity');
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')));
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'));
342 preParams = str2double(
char(preElement.getAttribute(
'quorum')));
345 preActElement = preActList.item(0);
346 preActs{1} = char(preActElement.getAttribute(
'name'));
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
362 postElement = postList.item(0);
363 postActList = postElement.getElementsByTagName(
'activity');
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')));
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')));
385 postActs{end} = char(postElement.getAttribute(
'end'));
387 postActs = cell(postActList.getLength(),1);
389 for m = 0:postActList.getLength()-1
390 postActElement = postActList.item(m);
391 postActs{m+1} = char(postActElement.getAttribute(
'name'));
394 newPrec = ActivityPrecedence(preActs, postActs, preType, postType, preParams, postParams);
395 newTask = newTask.addPrecedence(newPrec);
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;
413 tasks{end+1,1} = newTask.name;
414 tasks{end,2} = procID;
415 newProc = newProc.addTask(newTask);
419 hosts{end+1,1} = newProc.name;