dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / mapschema / Xml2Map.java
diff --git a/dol/src/dol/parser/xml/mapschema/Xml2Map.java b/dol/src/dol/parser/xml/mapschema/Xml2Map.java
new file mode 100644 (file)
index 0000000..4d15666
--- /dev/null
@@ -0,0 +1,381 @@
+package dol.parser.xml.mapschema;
+
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+import dol.datamodel.architecture.ArchiResource;
+import dol.datamodel.architecture.Architecture;
+import dol.datamodel.architecture.Processor;
+import dol.datamodel.architecture.ReadPath;
+import dol.datamodel.architecture.WritePath;
+import dol.datamodel.mapping.Binding;
+import dol.datamodel.mapping.CommunicationBinding;
+import dol.datamodel.mapping.ComputationBinding;
+import dol.datamodel.mapping.Configuration;
+import dol.datamodel.mapping.Mapping;
+import dol.datamodel.mapping.Schedule;
+import dol.datamodel.mapping.ScheduleEntry;
+import dol.datamodel.mapping.SchedulingPolicy;
+import dol.datamodel.mapping.Variable;
+import dol.datamodel.pn.Channel;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+
+
+/**
+ *
+ */
+public class Xml2Map {
+
+    /**
+     * Constructor.
+     * @param pn the process network the mapping is refering to
+     * @param arch the architecture the mapping is refering to
+     */
+    public Xml2Map(ProcessNetwork pn, Architecture arch) {
+        _pn = pn;
+        _arch = arch;
+    }
+
+    /**
+     * Process the start of the mapping tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Mapping object
+     */
+    public Mapping processMapping(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        Mapping map = new Mapping(name);
+        map.setArch(_arch);
+        map.setPN(_pn);
+        return map;
+    }
+
+    /**
+     * Process the end of the mapping tag in the XML.
+     *
+     * @param stack
+     */
+    public void processMapping(Stack<Object> stack) {
+    }
+
+    /**
+     * Process the start of a binding tag in the XML.
+     *
+     * @param  attributes The attributes of the tag.
+     * @return  a Binding object.
+     */
+    public Binding processBinding(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String basename = (String) attributes.getValue("basename");
+        String type = (String) attributes.getValue(
+                "http://www.w3.org/2001/XMLSchema-instance", "type");
+        Binding b = null;
+        // Create ComputationBinding or CommunicationBinding object
+        if(type.equals(Binding.COMPUTATION)) {
+            b = new ComputationBinding(name);
+        } else if(type.equals(Binding.COMMUNICATION)) {
+            b = new CommunicationBinding(name);
+        }
+
+        if (basename != null) b.setBasename(basename);
+
+        return b;
+    }
+
+    /**
+     * Process the end of a binding tag in the XML.
+     *
+     * @param stack
+     */
+    public void processBinding(Stack<Object> stack) throws SAXException
+    {
+        Binding bind = (Binding)stack.pop();
+        Mapping map = (Mapping)stack.peek();
+        bind.setParentResource(map);
+
+        if(bind instanceof ComputationBinding) {
+            ComputationBinding cb = (ComputationBinding)bind;
+            Process process = cb.getProcess();
+            Processor processor = cb.getProcessor();
+            process.setProcessor(processor);
+            processor.getProcessList().add(process);
+
+            map.getCompBindList().add(cb);
+            map.getProcessList().add(process);
+            if (!map.getProcessorList().contains(processor)) {
+                map.getProcessorList().add(processor);
+            }
+
+        } else {
+            map.getCommBindList().add((CommunicationBinding)bind);
+        }
+    }
+
+    /**
+     * Process the start of a origin tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a ScheduleEntry object
+     */
+    public ScheduleEntry processOrigin(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        ScheduleEntry schedEntry = new ScheduleEntry(name);
+
+        // A scheduled resource is either a PN process
+        // or a SW channel.
+        Process process = _pn.getProcess(name);
+        if(process != null) {
+            schedEntry.setConsumer(process);
+        } else {
+            Channel channel = _pn.getChannel(name);
+            schedEntry.setConsumer(channel);
+        }
+        return schedEntry;
+    }
+
+    /**
+     * Process the end of a origin tag in the XML.
+     *
+     * @param stack
+     */
+    public void processOrigin(Stack<Object> stack) {
+        ScheduleEntry schedProcess = (ScheduleEntry) stack.pop();
+        Schedule sched = (Schedule) stack.peek();
+        sched.getEntryList().add(schedProcess);
+        schedProcess.setParentResource(sched);
+    }
+
+    /**
+     * Process the start of a variable tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Variable object
+     */
+    public Variable processVariable(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String value = (String) attributes.getValue("value");
+        Variable variable = new Variable(name);
+        variable.setValue(Integer.parseInt(value));
+        return variable;
+    }
+
+    /**
+     * Process the end of a variable tag in the XML.
+     *
+     * @param stack
+     */
+    public void processVariable(Stack<Object> stack) {
+        Variable variable = (Variable) stack.pop();
+        Mapping map = (Mapping) stack.peek();
+        variable.setParentResource(map);
+        map.getVarList().add(variable);
+    }
+
+    /**
+     * Process the start of a process tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Process object
+     */
+    public Process processProcess(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        Process process = _pn.getProcess(name);
+        return process;
+    }
+
+    /**
+     * Process the end of a process tag in the XML.
+     *
+     * @param stack
+     */
+    public void processProcess(Stack<Object> stack) {
+        Process p = (Process) stack.pop();
+        ComputationBinding b = (ComputationBinding) stack.peek();
+        b.setProcess(p);
+    }
+
+    /**
+     * Process the start of a processor tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Processor object
+     */
+    public Processor processProcessor(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        Processor processor = _arch.getProcessor(name);
+        return processor;
+    }
+
+    /**
+     * Process the end of a processor tag in the XML.
+     *
+     * @param stack
+     */
+    public void processProcessor(Stack<Object> stack) {
+        Processor p = (Processor) stack.pop();
+        ComputationBinding b = (ComputationBinding) stack.peek();
+        b.setProcessor(p);
+    }
+
+    /**
+     * Process the start of a channel tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Channel object
+     */
+    public Channel processChannel(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        Channel channel = _pn.getChannel(name);
+        return channel;
+    }
+
+    /**
+     * Process the end of a channel tag in the XML.
+     *
+     * @param stack
+     */
+    public void processChannel(Stack<Object> stack) {
+        Channel chan = (Channel) stack.pop();
+        CommunicationBinding b = (CommunicationBinding) stack.peek();
+        b.setChannel(chan);
+    }
+
+    /**
+     * Process the start of a read path tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a ReadPath object
+     */
+    public ReadPath processReadPath(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        ReadPath path = _arch.getReadPath(name);
+        return path;
+    }
+
+    /**
+     * Process the end of a read path tag in the XML.
+     *
+     * @param stack
+     */
+    public void processReadPath(Stack<Object> stack) {
+        ReadPath path = (ReadPath) stack.pop();
+        CommunicationBinding b = (CommunicationBinding) stack.peek();
+        b.setReadPath(path);
+    }
+
+    /**
+     * Process the start of a write path tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a WritePath object
+     */
+    public WritePath processWritePath(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        WritePath path = _arch.getWritePath(name);
+        return path;
+    }
+
+    /**
+     * Process the end of a write path tag in the XML.
+     *
+     * @param stack
+     */
+    public void processWritePath(Stack<Object> stack) {
+        WritePath path = (WritePath) stack.pop();
+        CommunicationBinding b = (CommunicationBinding) stack.peek();
+        b.setWritePath(path);
+    }
+
+    /**
+     * Process the start of a schedule tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Schedule object
+     */
+    public Schedule processSchedule(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String type = (String) attributes.getValue("type");
+        String basename = (String) attributes.getValue("basename");
+
+        Schedule schedule = new Schedule(name);
+        schedule.setSchedPolicy(SchedulingPolicy.fromString(type));
+
+        if (basename != null) schedule.setBasename(basename);
+
+        return schedule;
+    }
+
+    /**
+     * Process the end of a schedule tag in the XML.
+     *
+     * @param stack
+     */
+    public void processSchedule(Stack<Object> stack) {
+        Schedule sched = (Schedule) stack.pop();
+        Mapping map = (Mapping) stack.peek();
+        map.getScheduleList().add(sched);
+        sched.setParentResource(map);
+    }
+
+    /**
+     * Process the start of a resource tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a ArchiResource object
+     */
+    public ArchiResource processResource(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        // We are either looking for a HW channel or for a processor.
+        ArchiResource resource = null;
+        if((resource = _arch.getHWChannel(name)) == null) {
+             resource = _arch.getProcessor(name);
+        }
+        return resource;
+    }
+
+    /**
+     * Process the end of a resource tag in the XML.
+     *
+     * @param stack
+     */
+    public void processResource(Stack<Object> stack) {
+        ArchiResource resource = (ArchiResource) stack.pop();
+        Schedule schedule = (Schedule) stack.peek();
+        schedule.setResource(resource);
+    }
+
+    /**
+     * Process the start of a configuration tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Configuration object
+     */
+    public Configuration processConfiguration(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String value = (String) attributes.getValue("value");
+        Configuration config = new Configuration(name, value);
+        return config;
+    }
+
+    /**
+     * Process the end of a configuration tag in the XML.
+     *
+     * @param stack
+     */
+    public void processConfiguration(Stack<Object> stack) {
+        Configuration config = (Configuration) stack.pop();
+        if(stack.peek() instanceof ScheduleEntry) {
+            ScheduleEntry schedProc = (ScheduleEntry) stack.peek();
+            schedProc.getCfgList().add(config);
+        } else if(stack.peek() instanceof Schedule) {
+            Schedule sched = (Schedule) stack.peek();
+            sched.getCfgList().add(config);
+        }
+    }
+
+    protected ProcessNetwork _pn = null;
+    protected Architecture _arch = null;
+}