dol: initial dol commit
[jump.git] / dol / src / dol / visitor / rtems / RtemsVisitor.java
1 /* $Id: RtemsVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.rtems;
3
4 import java.io.File;
5 import java.util.HashMap;
6 import java.util.Vector;
7
8 import dol.datamodel.pn.Port;
9 import dol.datamodel.pn.Process;
10 import dol.datamodel.pn.ProcessNetwork;
11 import dol.util.Copier;
12 import dol.visitor.PNVisitor;
13
14 /**
15  * This class is a class for a visitor that is used to generate
16  * a RTEMS package.
17  */
18 public class RtemsVisitor extends PNVisitor {
19
20     /**
21      * Constructor.
22      *
23      * @param packageName name of the Rtems directory
24      */
25     public RtemsVisitor(String packageName) {
26         _packageName = packageName;
27     }
28
29     /**
30      * Visit process network.
31      *
32      * @param pn process network that needs to be rendered.
33      */
34     public void visitComponent(ProcessNetwork pn) {
35         try {
36             File dir = new File(_packageName);
37             dir.mkdirs();
38
39             //copy library files
40             File source = new File(_ui.getMySystemCLib().
41                     replaceAll("systemC", "rtems"));
42             new Copier().copy(source, dir);
43
44             //copy process source code
45             source = new File(_srcDirName);
46             new Copier().copy(source, dir);
47
48             createPortMap(pn);
49             createSinkMap(pn);
50             pn.accept(new RtemsMakefileVisitor(_packageName));
51             pn.accept(new RtemsProcessVisitor(_packageName,_portMap,_sinkMap));
52             pn.accept(new RtemsModuleVisitor(_packageName, _portMap));
53             pn.accept(new RtemsShaperVisitor(_packageName, _sinkMap));
54             pn.accept(new RtemsPropertiesVisitor(_packageName));
55         }
56         catch (Exception e) {
57             System.out.println("RtemsVisitor: exception occured: "
58                     + e.getMessage());
59             e.printStackTrace();
60         }
61     }
62
63     /**
64      * Create a hashmap which maps each port of the given process network
65      * to an integer. For each process, ports are numbered with integers
66      * starting from 0.
67      *
68      * @param pn process network for which the map should be generated
69      */
70     protected void createPortMap(ProcessNetwork pn) {
71         _portMap = new HashMap<Port, Integer>();
72
73         for (Process process : pn.getProcessList()) {
74             int portCount = 0;
75             Vector<Port> portList = process.getPortList();
76             Vector<String> portNameList = new Vector<String>();
77             portNameList.clear();
78             HashMap<String, Integer> portMap =
79                 new HashMap<String, Integer>();
80             portMap.clear();
81
82             for (int i = 0; i < portList.size(); i++) {
83                 //treat single ports differently than iterated ports
84                 String portName = portList.elementAt(i).getName();
85                 String baseName = portList.elementAt(i).getBasename();
86
87                 if (portName.equals(baseName)) {
88                     portNameList.add(portName);
89                     portMap.put(portName, portCount++);
90                 } else {
91                     String range_indices = portList.elementAt(i).getRange();
92                     Vector<Integer> range_indices_values = getIndex(range_indices, ";");
93
94                     String port_indices = portName;
95                     port_indices.replaceAll(baseName, "");
96                     Vector<Integer> port_indices_values = getIndex(port_indices, "_");
97
98                     if (!portNameList.contains(baseName)) {
99                         portNameList.add(baseName);
100                         portMap.put(baseName, portCount);
101
102                         int size = 1;
103                         for (int j = 0; j < range_indices_values.size(); j++) {
104                             size *= range_indices_values.elementAt(j);
105                         }
106                         portCount += size;
107                     }
108
109                     int portId = portMap.get(baseName);
110                     for (int j = 0; j < port_indices_values.size(); j++) {
111                         int weight = 1;
112                         for (int k = j + 1; k < range_indices_values.size(); k++) {
113                             weight *= range_indices_values.elementAt(k);
114                         }
115                         portId += port_indices_values.elementAt(j) * weight;
116                     }
117                     portMap.put(portName, portId);
118                 }
119             }
120
121             for (int i = 0; i < portList.size(); i++) {
122                 _portMap.put(portList.elementAt(i),
123                         portMap.get(portList.elementAt(i).getName()));
124             }
125         }
126     }
127
128
129     /**
130      * Create a hashmap which maps each sink of the given process network
131      * to an integer. For each process, sinks are numbered with integers
132      * starting from 1.
133      *
134      * @param pn process network for which the map should be generated
135      */
136     protected void createSinkMap(ProcessNetwork pn) {
137         _sinkMap = new HashMap<Process, Integer>();
138         int i = 1;
139
140         for (Process process : pn.getProcessList()) {
141             if (!process.hasOutPorts()) {
142                 _sinkMap.put(process, i);
143                 i++;
144             }
145         }
146     }
147
148     /**
149      * Gets vector of indices of a string, where the index must be
150      * separated by the specified separator.
151      * examples:
152      * getIndex("name_1_2", "_", 0) will return 1.
153      * getIndex("name_1_2", "_", 1) will return 2.
154      *
155      * @param range string to parse
156      * @param separator delimiter of indices
157      * @return vector of indices
158      */
159     protected Vector<Integer> getIndex(String range, String separator) {
160         Vector<Integer> indices = new Vector<Integer>();
161         String[] subranges = range.split(separator);
162         for (int i = 0; i < subranges.length; i++) {
163             try {
164                 int value = Integer.valueOf(subranges[i]);
165                 indices.add(value);
166             } catch (Exception e) {
167                 continue;
168             }
169         }
170         return indices;
171     }
172
173     protected HashMap<Port, Integer> _portMap;
174     protected HashMap<Process, Integer> _sinkMap;
175     protected String _packageName = null;
176
177     protected String _srcDir = "";
178     protected static String _srcDirName = "src";
179 }