1function writeXML(self,filename,useAbstractNames)
2% WRITEXML(SELF,FILENAME,USEAN)
4% USEAN:
if true replaces node names with abstract names eg E1, T1, ...
6% Copyright (c) 2012-2026, Imperial College London
10 useAbstractNames=
false;
12nodeHashMap = containers.Map;
18 for p = 1:length(self.hosts)
19 curProc = self.hosts{p};
20 nodeHashMap(curProc.name)=sprintf(
'P%d',p);
21 for t=1:length(curProc.tasks)
22 curTask = curProc.tasks(t);
24 nodeHashMap(curTask.name)=sprintf('T%d',tctr);
25 for e=1:length(curTask.entries)
26 curEntry = curTask.entries(e);
28 nodeHashMap(curEntry.name)=sprintf('E%d',ectr);
30 for a=1:length(curTask.activities)
31 curAct = curTask.activities(a);
33 nodeHashMap(curAct.name) = sprintf('A%d',actr);
38 for p = 1:length(self.hosts)
39 curProc = self.hosts{p};
40 nodeHashMap(curProc.name)=curProc.name;
41 for t=1:length(curProc.tasks)
42 curTask = curProc.tasks(t);
43 nodeHashMap(curTask.name)=curTask.name;
44 for e=1:length(curTask.entries)
45 curEntry = curTask.entries(e);
46 nodeHashMap(curEntry.name)=curEntry.name;
48 for a=1:length(curTask.activities)
49 curAct = curTask.activities(a);
50 nodeHashMap(curAct.name)=curAct.name;
56import javax.xml.parsers.DocumentBuilderFactory;
57import javax.xml.parsers.DocumentBuilder;
58import org.w3c.dom.Document;
59import org.w3c.dom.NodeList;
60import org.w3c.dom.Node;
61import org.w3c.dom.Element;
63import javax.xml.transform.Transformer;
64import javax.xml.transform.TransformerFactory;
65import javax.xml.transform.dom.DOMSource;
66import javax.xml.transform.stream.StreamResult;
67import javax.xml.transform.OutputKeys;
69precision = '%10.15e'; %precision for doubles
70docFactory = DocumentBuilderFactory.newInstance();
71docBuilder = docFactory.newDocumentBuilder();
72doc = docBuilder.newDocument();
75rootElement = doc.createElement('lqn-model');
76doc.appendChild(rootElement);
77rootElement.setAttribute('xmlns:xsi', 'http:
78rootElement.setAttribute('xsi:noNamespaceSchemaLocation', 'lqn.xsd');
79rootElement.setAttribute('name', getName(self));
81for p = 1:length(self.hosts)
83 curProc = self.hosts{p};
84 procElement = doc.createElement(
'processor');
85 rootElement.appendChild(procElement);
86 procElement.setAttribute(
'name', nodeHashMap(curProc.name));
87 procElement.setAttribute(
'scheduling', curProc.scheduling);
88 if curProc.replication>1
89 procElement.setAttribute(
'replication', num2str(curProc.replication));
91 if SchedStrategy.fromText(curProc.scheduling) ~= SchedStrategy.INF
92 mult = num2str(curProc.multiplicity);
93 if isinf(mult), mult=1; end
94 procElement.setAttribute(
'multiplicity', mult);
96 if SchedStrategy.fromText(curProc.scheduling) == SchedStrategy.PS || SchedStrategy.fromText(curProc.scheduling) == SchedStrategy.PSPRIO
97 procElement.setAttribute(
'quantum', num2str(curProc.quantum));
99 procElement.setAttribute(
'speed-factor', num2str(curProc.speedFactor));
100 for t=1:length(curProc.tasks)
101 curTask = curProc.tasks(t);
102 taskElement = doc.createElement('task');
103 procElement.appendChild(taskElement);
104 taskElement.setAttribute('name', nodeHashMap(curTask.name));
105 taskElement.setAttribute('scheduling', curTask.scheduling);
106 if curTask.replication>1
107 taskElement.setAttribute('replication', num2str(curTask.replication));
109 if SchedStrategy.fromText(curTask.scheduling) ~= SchedStrategy.INF
110 taskElement.setAttribute('multiplicity', num2str(curTask.multiplicity));
112 if SchedStrategy.fromText(curTask.scheduling) == SchedStrategy.REF
113 taskElement.setAttribute('think-time', num2str(curTask.thinkTimeMean));
115 for e=1:length(curTask.entries)
116 curEntry = curTask.entries(e);
117 entryElement = doc.createElement('entry');
118 taskElement.appendChild(entryElement);
119 entryElement.setAttribute('name', nodeHashMap(curEntry.name));
120 entryElement.setAttribute('type', 'NONE');
122 % Write forwarding calls
123 for fw=1:length(curEntry.forwardingDests)
124 fwdElement = doc.createElement('forwarding');
125 entryElement.appendChild(fwdElement);
126 fwdElement.setAttribute('dest', nodeHashMap(curEntry.forwardingDests{fw}));
127 fwdElement.setAttribute(
'prob', num2str(curEntry.forwardingProbs(fw)));
130 taskActsElement = doc.createElement(
'task-activities');
131 taskElement.appendChild(taskActsElement);
132 for a=1:length(curTask.activities)
133 curAct = curTask.activities(a);
134 actElement = doc.createElement('activity');
135 taskActsElement.appendChild(actElement);
136 actElement.setAttribute('host-demand-mean', num2str(curAct.hostDemandMean));
137 actElement.setAttribute('host-demand-cvsq', num2str(curAct.hostDemandSCV));
138 if ~isempty(curAct.boundToEntry)
139 actElement.setAttribute('bound-to-entry', nodeHashMap(curAct.boundToEntry));
141 actElement.setAttribute('call-order', curAct.callOrder);
142 actElement.setAttribute('name', nodeHashMap(curAct.name));
143 if curAct.thinkTimeMean > GlobalConstants.FineTol
144 actElement.setAttribute('think-time', num2str(curAct.thinkTimeMean));
147 for sc=1:length(curAct.syncCallDests)
148 syncCallElement = doc.createElement('synch-call');
149 actElement.appendChild(syncCallElement);
150 syncCallElement.setAttribute('dest', nodeHashMap(curAct.syncCallDests{sc}));
151 syncCallElement.setAttribute(
'calls-mean', num2str(curAct.syncCallMeans(sc)));
153 for ac=1:length(curAct.asyncCallDests)
154 asyncCallElement = doc.createElement('asynch-call');
155 actElement.appendChild(asyncCallElement);
156 asyncCallElement.setAttribute('dest', nodeHashMap(curAct.asyncCallDests{ac}));
157 asyncCallElement.setAttribute(
'calls-mean', num2str(curAct.asyncCallMeans(ac)));
160 for ap=1:length(curTask.precedences)
161 curActPrec = curTask.precedences(ap);
162 actPrecElement = doc.createElement('precedence');
163 taskActsElement.appendChild(actPrecElement);
165 preElement = doc.createElement(ActivityPrecedenceType.toText(curActPrec.preType));
166 actPrecElement.appendChild(preElement);
167 if curActPrec.preType== ActivityPrecedenceType.PRE_AND && ~isempty(curActPrec.preParams)
168 preElement.setAttribute('quorum', num2str(curActPrec.preParams(1)));
170 for pra = 1:length(curActPrec.preActs)
171 preActElement = doc.createElement('activity');
172 preElement.appendChild(preActElement);
173 preActElement.setAttribute('name', nodeHashMap(curActPrec.preActs{pra}));
176 postElement = doc.createElement(ActivityPrecedenceType.toText(curActPrec.postType));
177 actPrecElement.appendChild(postElement);
178 if curActPrec.postType==ActivityPrecedenceType.POST_OR
179 for poa = 1:length(curActPrec.postActs)
180 postActElement = doc.createElement('activity');
181 postElement.appendChild(postActElement);
182 postActElement.setAttribute('name', nodeHashMap(curActPrec.postActs{poa}));
183 postActElement.setAttribute(
'prob', num2str(curActPrec.postParams(poa)));
185 elseif curActPrec.postType==ActivityPrecedenceType.POST_LOOP
186 for poa = 1:length(curActPrec.postActs)-1
187 postActElement = doc.createElement(
'activity');
188 postElement.appendChild(postActElement);
189 postActElement.setAttribute(
'name', nodeHashMap(curActPrec.postActs{poa}));
190 postActElement.setAttribute(
'count', num2str(curActPrec.postParams(poa)));
192 postElement.setAttribute(
'end', curActPrec.postActs{end});
194 for poa = 1:length(curActPrec.postActs)
195 postActElement = doc.createElement('activity');
196 postElement.appendChild(postActElement);
197 postActElement.setAttribute('name', nodeHashMap(curActPrec.postActs{poa}));
201 if SchedStrategy.fromText(curTask.scheduling) ~= SchedStrategy.REF
202 for e=1:length(curTask.entries)
203 curEntry = curTask.entries(e);
204 if isempty(curEntry.replyActivity)
205 % the model was presumably loaded from a file without
206 % reply-activity specified by now lqns asks them so we
207 % need to calculate their location
208 lsn = self.getStruct();
209 eidx = find(cellfun(@(x) strcmp(x.name, curEntry.name), self.entries));
210 racts = find(lsn.replygraph(:,eidx));
211 curEntry.replyActivity{1,end+1} = nodeHashMap(lsn.names{lsn.ashift+ racts});
213 entryReplyElement = doc.createElement(
'reply-entry');
214 taskActsElement.appendChild(entryReplyElement);
215 entryReplyElement.setAttribute(
'name', nodeHashMap(curEntry.name));
216 for r=1:length(curEntry.replyActivity)
217 entryReplyActElement = doc.createElement('reply-activity');
218 entryReplyElement.appendChild(entryReplyActElement);
219 entryReplyActElement.setAttribute('name', nodeHashMap(curEntry.replyActivity{r}));
226%write the content into xml file
227transformerFactory = TransformerFactory.newInstance();
228transformer = transformerFactory.newTransformer();
229transformer.setOutputProperty(OutputKeys.INDENT,
'yes');
230transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount",
"2");
231transformer.setOutputProperty(OutputKeys.VERSION,
"1.0");
232transformer.setOutputProperty(OutputKeys.ENCODING,
"UTF-8");
233transformer.setOutputProperty(OutputKeys.STANDALONE,
"no");
234source = DOMSource(doc);
235if isempty(fileparts(filename))
236 filename=[lineRootFolder,filesep,
'workspace',filesep,filename];
239%
if self.options.verbose
240% line_printf(
'\nLQN model: %s\n', filename);
242result = StreamResult(File(filename));
243transformer.transform(source, result);