dol: initial dol commit
[jump.git] / dol / src / dol / visitor / yapi / YapiModuleVisitor.java
1 /* $Id: YapiModuleVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.yapi;
3
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.util.Vector;
7
8 import dol.datamodel.pn.Channel;
9 import dol.datamodel.pn.Port;
10 import dol.datamodel.pn.Process;
11 import dol.datamodel.pn.ProcessNetwork;
12 import dol.util.CodePrintStream;
13 import dol.visitor.PNVisitor;
14
15 /**
16  * This class is a class for a visitor that is used to generate
17  * the main program.
18  */
19 public class YapiModuleVisitor extends PNVisitor {
20
21     /**
22      * Constructor.
23      */
24     public YapiModuleVisitor(String dir) {
25         _dir = dir;
26     }
27
28     /**
29      * 
30      */
31     public void visitComponent(ProcessNetwork pn) {
32         try {
33             //create header file
34             String filename = _dir + _delimiter + "application.h";
35             OutputStream file = new FileOutputStream(filename);
36             _code = new CodePrintStream(file);
37
38             _code.println("#include \"yapi.h\"");
39             _code.println();
40
41             Vector<String> processList = new Vector<String>();
42             for (Process p : pn.getProcessList()) {
43                 String basename = p.getBasename();
44                 if (!processList.contains(basename)) {
45                     processList.add(basename);
46                     _code.println("#include \""
47                             + p.getBasename() + "_wrapper.h\"");
48                 }
49             }
50             _code.println();
51             _code.println("class Application : public ProcessNetwork");
52             _code.println("{");
53             _code.println("public:");
54             _code.println("  Application(const Id& n);");
55             _code.println("  const char* type() const;");
56             _code.println();
57             _code.println("private:");
58
59             for (Channel c : pn.getChannelList()) {
60                 if (c.getType().equals("fifo")) {
61                     _code.println("  Fifo<char> " + c.getName()
62                         + ";");
63                 } else if (c.getType().equals("wfifo")) {
64                     System.out.println("Warning: YAPI visitor does not "
65                         + "support windowed FIFOs.");
66                 }
67             }
68             _code.println();
69
70             for (Process p : pn.getProcessList()) {
71                 _code.println("  " + p.getBasename() + "_wrapper "
72                         + p.getName() + ";");
73             }
74             _code.println("};");
75             file.close();
76
77
78             //create cpp file
79             filename = _dir + _delimiter + "application.cpp";
80             file = new FileOutputStream(filename);
81             _code = new CodePrintStream(file);
82
83             _code.println("#include \"application.h\"");
84             _code.println();
85             _code.println("Application::Application(const Id& n) :");
86             _code.print("  ProcessNetwork(n)");
87             for (Channel c : pn.getChannelList()) {
88                 if (c.getType().equals("fifo")) {
89                     _code.print(",\n  " + c.getName() + "(id(\""
90                         + c.getName() + "\"))");
91                 } else if (c.getType().equals("wfifo")) {
92                     System.out.println("Warning: YAPI visitor does not "
93                         + "support windowed FIFOs.");
94                 }
95             }
96             for (Process p : pn.getProcessList()) {
97                 _code.print(",\n  " + p.getName() + "(\""
98                         + p.getName() + "\", id(\"" + p.getName()
99                         + "\")");
100
101                 for (Port port : p.getPortList()) {
102                     _code.print(", " + port.getPeerResource().getName());
103                 }
104                 _code.print(")");
105             }
106             _code.println();
107             _code.println("{ }");
108             _code.println();
109             _code.println("const char* Application::type() const");
110             _code.println("{");
111             _code.println("  return \"Application\";");
112             _code.println("}");
113         }
114         catch (Exception e) {
115             System.out.println("YapiModuleVisitor: "
116                     + "exception occured: " + e.getMessage());
117             e.printStackTrace();
118         }
119     }
120
121     protected CodePrintStream _code = null;
122     protected String _dir = null;
123     protected OutputStream _file;
124     protected CodePrintStream _pn;
125 }
126