1 package dol.parser.xml.mapschema;
3 import java.util.Stack;
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
8 import dol.datamodel.architecture.ArchiResource;
9 import dol.datamodel.architecture.Architecture;
10 import dol.datamodel.architecture.Processor;
11 import dol.datamodel.architecture.ReadPath;
12 import dol.datamodel.architecture.WritePath;
13 import dol.datamodel.mapping.Binding;
14 import dol.datamodel.mapping.CommunicationBinding;
15 import dol.datamodel.mapping.ComputationBinding;
16 import dol.datamodel.mapping.Configuration;
17 import dol.datamodel.mapping.Mapping;
18 import dol.datamodel.mapping.Schedule;
19 import dol.datamodel.mapping.ScheduleEntry;
20 import dol.datamodel.mapping.SchedulingPolicy;
21 import dol.datamodel.mapping.Variable;
22 import dol.datamodel.pn.Channel;
23 import dol.datamodel.pn.Process;
24 import dol.datamodel.pn.ProcessNetwork;
30 public class Xml2Map {
34 * @param pn the process network the mapping is refering to
35 * @param arch the architecture the mapping is refering to
37 public Xml2Map(ProcessNetwork pn, Architecture arch) {
43 * Process the start of the mapping tag in the XML.
45 * @param attributes attributes of the tag
46 * @return a Mapping object
48 public Mapping processMapping(Attributes attributes) {
49 String name = (String) attributes.getValue("name");
50 Mapping map = new Mapping(name);
57 * Process the end of the mapping tag in the XML.
61 public void processMapping(Stack<Object> stack) {
65 * Process the start of a binding tag in the XML.
67 * @param attributes The attributes of the tag.
68 * @return a Binding object.
70 public Binding processBinding(Attributes attributes) {
71 String name = (String) attributes.getValue("name");
72 String basename = (String) attributes.getValue("basename");
73 String type = (String) attributes.getValue(
74 "http://www.w3.org/2001/XMLSchema-instance", "type");
76 // Create ComputationBinding or CommunicationBinding object
77 if(type.equals(Binding.COMPUTATION)) {
78 b = new ComputationBinding(name);
79 } else if(type.equals(Binding.COMMUNICATION)) {
80 b = new CommunicationBinding(name);
83 if (basename != null) b.setBasename(basename);
89 * Process the end of a binding tag in the XML.
93 public void processBinding(Stack<Object> stack) throws SAXException
95 Binding bind = (Binding)stack.pop();
96 Mapping map = (Mapping)stack.peek();
97 bind.setParentResource(map);
99 if(bind instanceof ComputationBinding) {
100 ComputationBinding cb = (ComputationBinding)bind;
101 Process process = cb.getProcess();
102 Processor processor = cb.getProcessor();
103 process.setProcessor(processor);
104 processor.getProcessList().add(process);
106 map.getCompBindList().add(cb);
107 map.getProcessList().add(process);
108 if (!map.getProcessorList().contains(processor)) {
109 map.getProcessorList().add(processor);
113 map.getCommBindList().add((CommunicationBinding)bind);
118 * Process the start of a origin tag in the XML.
120 * @param attributes attributes of the tag
121 * @return a ScheduleEntry object
123 public ScheduleEntry processOrigin(Attributes attributes) {
124 String name = (String) attributes.getValue("name");
125 ScheduleEntry schedEntry = new ScheduleEntry(name);
127 // A scheduled resource is either a PN process
129 Process process = _pn.getProcess(name);
130 if(process != null) {
131 schedEntry.setConsumer(process);
133 Channel channel = _pn.getChannel(name);
134 schedEntry.setConsumer(channel);
140 * Process the end of a origin tag in the XML.
144 public void processOrigin(Stack<Object> stack) {
145 ScheduleEntry schedProcess = (ScheduleEntry) stack.pop();
146 Schedule sched = (Schedule) stack.peek();
147 sched.getEntryList().add(schedProcess);
148 schedProcess.setParentResource(sched);
152 * Process the start of a variable tag in the XML.
154 * @param attributes attributes of the tag
155 * @return a Variable object
157 public Variable processVariable(Attributes attributes) {
158 String name = (String) attributes.getValue("name");
159 String value = (String) attributes.getValue("value");
160 Variable variable = new Variable(name);
161 variable.setValue(Integer.parseInt(value));
166 * Process the end of a variable tag in the XML.
170 public void processVariable(Stack<Object> stack) {
171 Variable variable = (Variable) stack.pop();
172 Mapping map = (Mapping) stack.peek();
173 variable.setParentResource(map);
174 map.getVarList().add(variable);
178 * Process the start of a process tag in the XML.
180 * @param attributes attributes of the tag
181 * @return a Process object
183 public Process processProcess(Attributes attributes) {
184 String name = (String) attributes.getValue("name");
185 Process process = _pn.getProcess(name);
190 * Process the end of a process tag in the XML.
194 public void processProcess(Stack<Object> stack) {
195 Process p = (Process) stack.pop();
196 ComputationBinding b = (ComputationBinding) stack.peek();
201 * Process the start of a processor tag in the XML.
203 * @param attributes attributes of the tag
204 * @return a Processor object
206 public Processor processProcessor(Attributes attributes) {
207 String name = (String) attributes.getValue("name");
208 Processor processor = _arch.getProcessor(name);
213 * Process the end of a processor tag in the XML.
217 public void processProcessor(Stack<Object> stack) {
218 Processor p = (Processor) stack.pop();
219 ComputationBinding b = (ComputationBinding) stack.peek();
224 * Process the start of a channel tag in the XML.
226 * @param attributes attributes of the tag
227 * @return a Channel object
229 public Channel processChannel(Attributes attributes) {
230 String name = (String) attributes.getValue("name");
231 Channel channel = _pn.getChannel(name);
236 * Process the end of a channel tag in the XML.
240 public void processChannel(Stack<Object> stack) {
241 Channel chan = (Channel) stack.pop();
242 CommunicationBinding b = (CommunicationBinding) stack.peek();
247 * Process the start of a read path tag in the XML.
249 * @param attributes attributes of the tag
250 * @return a ReadPath object
252 public ReadPath processReadPath(Attributes attributes) {
253 String name = (String) attributes.getValue("name");
254 ReadPath path = _arch.getReadPath(name);
259 * Process the end of a read path tag in the XML.
263 public void processReadPath(Stack<Object> stack) {
264 ReadPath path = (ReadPath) stack.pop();
265 CommunicationBinding b = (CommunicationBinding) stack.peek();
270 * Process the start of a write path tag in the XML.
272 * @param attributes attributes of the tag
273 * @return a WritePath object
275 public WritePath processWritePath(Attributes attributes) {
276 String name = (String) attributes.getValue("name");
277 WritePath path = _arch.getWritePath(name);
282 * Process the end of a write path tag in the XML.
286 public void processWritePath(Stack<Object> stack) {
287 WritePath path = (WritePath) stack.pop();
288 CommunicationBinding b = (CommunicationBinding) stack.peek();
289 b.setWritePath(path);
293 * Process the start of a schedule tag in the XML.
295 * @param attributes attributes of the tag
296 * @return a Schedule object
298 public Schedule processSchedule(Attributes attributes) {
299 String name = (String) attributes.getValue("name");
300 String type = (String) attributes.getValue("type");
301 String basename = (String) attributes.getValue("basename");
303 Schedule schedule = new Schedule(name);
304 schedule.setSchedPolicy(SchedulingPolicy.fromString(type));
306 if (basename != null) schedule.setBasename(basename);
312 * Process the end of a schedule tag in the XML.
316 public void processSchedule(Stack<Object> stack) {
317 Schedule sched = (Schedule) stack.pop();
318 Mapping map = (Mapping) stack.peek();
319 map.getScheduleList().add(sched);
320 sched.setParentResource(map);
324 * Process the start of a resource tag in the XML.
326 * @param attributes attributes of the tag
327 * @return a ArchiResource object
329 public ArchiResource processResource(Attributes attributes) {
330 String name = (String) attributes.getValue("name");
331 // We are either looking for a HW channel or for a processor.
332 ArchiResource resource = null;
333 if((resource = _arch.getHWChannel(name)) == null) {
334 resource = _arch.getProcessor(name);
340 * Process the end of a resource tag in the XML.
344 public void processResource(Stack<Object> stack) {
345 ArchiResource resource = (ArchiResource) stack.pop();
346 Schedule schedule = (Schedule) stack.peek();
347 schedule.setResource(resource);
351 * Process the start of a configuration tag in the XML.
353 * @param attributes attributes of the tag
354 * @return a Configuration object
356 public Configuration processConfiguration(Attributes attributes) {
357 String name = (String) attributes.getValue("name");
358 String value = (String) attributes.getValue("value");
359 Configuration config = new Configuration(name, value);
364 * Process the end of a configuration tag in the XML.
368 public void processConfiguration(Stack<Object> stack) {
369 Configuration config = (Configuration) stack.pop();
370 if(stack.peek() instanceof ScheduleEntry) {
371 ScheduleEntry schedProc = (ScheduleEntry) stack.peek();
372 schedProc.getCfgList().add(config);
373 } else if(stack.peek() instanceof Schedule) {
374 Schedule sched = (Schedule) stack.peek();
375 sched.getCfgList().add(config);
379 protected ProcessNetwork _pn = null;
380 protected Architecture _arch = null;