dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / mapschema / Xml2Map.java
1 package dol.parser.xml.mapschema;
2
3 import java.util.Stack;
4
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7
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;
25
26
27 /**
28  *
29  */
30 public class Xml2Map {
31
32     /**
33      * Constructor.
34      * @param pn the process network the mapping is refering to
35      * @param arch the architecture the mapping is refering to
36      */
37     public Xml2Map(ProcessNetwork pn, Architecture arch) {
38         _pn = pn;
39         _arch = arch;
40     }
41
42     /**
43      * Process the start of the mapping tag in the XML.
44      *
45      * @param attributes attributes of the tag
46      * @return a Mapping object
47      */
48     public Mapping processMapping(Attributes attributes) {
49         String name = (String) attributes.getValue("name");
50         Mapping map = new Mapping(name);
51         map.setArch(_arch);
52         map.setPN(_pn);
53         return map;
54     }
55
56     /**
57      * Process the end of the mapping tag in the XML.
58      *
59      * @param stack
60      */
61     public void processMapping(Stack<Object> stack) {
62     }
63
64     /**
65      * Process the start of a binding tag in the XML.
66      *
67      * @param  attributes The attributes of the tag.
68      * @return  a Binding object.
69      */
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");
75         Binding b = null;
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);
81         }
82
83         if (basename != null) b.setBasename(basename);
84
85         return b;
86     }
87
88     /**
89      * Process the end of a binding tag in the XML.
90      *
91      * @param stack
92      */
93     public void processBinding(Stack<Object> stack) throws SAXException
94     {
95         Binding bind = (Binding)stack.pop();
96         Mapping map = (Mapping)stack.peek();
97         bind.setParentResource(map);
98
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);
105
106             map.getCompBindList().add(cb);
107             map.getProcessList().add(process);
108             if (!map.getProcessorList().contains(processor)) {
109                 map.getProcessorList().add(processor);
110             }
111
112         } else {
113             map.getCommBindList().add((CommunicationBinding)bind);
114         }
115     }
116
117     /**
118      * Process the start of a origin tag in the XML.
119      *
120      * @param attributes attributes of the tag
121      * @return a ScheduleEntry object
122      */
123     public ScheduleEntry processOrigin(Attributes attributes) {
124         String name = (String) attributes.getValue("name");
125         ScheduleEntry schedEntry = new ScheduleEntry(name);
126
127         // A scheduled resource is either a PN process
128         // or a SW channel.
129         Process process = _pn.getProcess(name);
130         if(process != null) {
131             schedEntry.setConsumer(process);
132         } else {
133             Channel channel = _pn.getChannel(name);
134             schedEntry.setConsumer(channel);
135         }
136         return schedEntry;
137     }
138
139     /**
140      * Process the end of a origin tag in the XML.
141      *
142      * @param stack
143      */
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);
149     }
150
151     /**
152      * Process the start of a variable tag in the XML.
153      *
154      * @param attributes attributes of the tag
155      * @return a Variable object
156      */
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));
162         return variable;
163     }
164
165     /**
166      * Process the end of a variable tag in the XML.
167      *
168      * @param stack
169      */
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);
175     }
176
177     /**
178      * Process the start of a process tag in the XML.
179      *
180      * @param attributes attributes of the tag
181      * @return a Process object
182      */
183     public Process processProcess(Attributes attributes) {
184         String name = (String) attributes.getValue("name");
185         Process process = _pn.getProcess(name);
186         return process;
187     }
188
189     /**
190      * Process the end of a process tag in the XML.
191      *
192      * @param stack
193      */
194     public void processProcess(Stack<Object> stack) {
195         Process p = (Process) stack.pop();
196         ComputationBinding b = (ComputationBinding) stack.peek();
197         b.setProcess(p);
198     }
199
200     /**
201      * Process the start of a processor tag in the XML.
202      *
203      * @param attributes attributes of the tag
204      * @return a Processor object
205      */
206     public Processor processProcessor(Attributes attributes) {
207         String name = (String) attributes.getValue("name");
208         Processor processor = _arch.getProcessor(name);
209         return processor;
210     }
211
212     /**
213      * Process the end of a processor tag in the XML.
214      *
215      * @param stack
216      */
217     public void processProcessor(Stack<Object> stack) {
218         Processor p = (Processor) stack.pop();
219         ComputationBinding b = (ComputationBinding) stack.peek();
220         b.setProcessor(p);
221     }
222
223     /**
224      * Process the start of a channel tag in the XML.
225      *
226      * @param attributes attributes of the tag
227      * @return a Channel object
228      */
229     public Channel processChannel(Attributes attributes) {
230         String name = (String) attributes.getValue("name");
231         Channel channel = _pn.getChannel(name);
232         return channel;
233     }
234
235     /**
236      * Process the end of a channel tag in the XML.
237      *
238      * @param stack
239      */
240     public void processChannel(Stack<Object> stack) {
241         Channel chan = (Channel) stack.pop();
242         CommunicationBinding b = (CommunicationBinding) stack.peek();
243         b.setChannel(chan);
244     }
245
246     /**
247      * Process the start of a read path tag in the XML.
248      *
249      * @param attributes attributes of the tag
250      * @return a ReadPath object
251      */
252     public ReadPath processReadPath(Attributes attributes) {
253         String name = (String) attributes.getValue("name");
254         ReadPath path = _arch.getReadPath(name);
255         return path;
256     }
257
258     /**
259      * Process the end of a read path tag in the XML.
260      *
261      * @param stack
262      */
263     public void processReadPath(Stack<Object> stack) {
264         ReadPath path = (ReadPath) stack.pop();
265         CommunicationBinding b = (CommunicationBinding) stack.peek();
266         b.setReadPath(path);
267     }
268
269     /**
270      * Process the start of a write path tag in the XML.
271      *
272      * @param attributes attributes of the tag
273      * @return a WritePath object
274      */
275     public WritePath processWritePath(Attributes attributes) {
276         String name = (String) attributes.getValue("name");
277         WritePath path = _arch.getWritePath(name);
278         return path;
279     }
280
281     /**
282      * Process the end of a write path tag in the XML.
283      *
284      * @param stack
285      */
286     public void processWritePath(Stack<Object> stack) {
287         WritePath path = (WritePath) stack.pop();
288         CommunicationBinding b = (CommunicationBinding) stack.peek();
289         b.setWritePath(path);
290     }
291
292     /**
293      * Process the start of a schedule tag in the XML.
294      *
295      * @param attributes attributes of the tag
296      * @return a Schedule object
297      */
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");
302
303         Schedule schedule = new Schedule(name);
304         schedule.setSchedPolicy(SchedulingPolicy.fromString(type));
305
306         if (basename != null) schedule.setBasename(basename);
307
308         return schedule;
309     }
310
311     /**
312      * Process the end of a schedule tag in the XML.
313      *
314      * @param stack
315      */
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);
321     }
322
323     /**
324      * Process the start of a resource tag in the XML.
325      *
326      * @param attributes attributes of the tag
327      * @return a ArchiResource object
328      */
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);
335         }
336         return resource;
337     }
338
339     /**
340      * Process the end of a resource tag in the XML.
341      *
342      * @param stack
343      */
344     public void processResource(Stack<Object> stack) {
345         ArchiResource resource = (ArchiResource) stack.pop();
346         Schedule schedule = (Schedule) stack.peek();
347         schedule.setResource(resource);
348     }
349
350     /**
351      * Process the start of a configuration tag in the XML.
352      *
353      * @param attributes attributes of the tag
354      * @return a Configuration object
355      */
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);
360         return config;
361     }
362
363     /**
364      * Process the end of a configuration tag in the XML.
365      *
366      * @param stack
367      */
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);
376         }
377     }
378
379     protected ProcessNetwork _pn = null;
380     protected Architecture _arch = null;
381 }