+/* $Id: YapiModuleVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.yapi;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.Vector;
+
+import dol.datamodel.pn.Channel;
+import dol.datamodel.pn.Port;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.util.CodePrintStream;
+import dol.visitor.PNVisitor;
+
+/**
+ * This class is a class for a visitor that is used to generate
+ * the main program.
+ */
+public class YapiModuleVisitor extends PNVisitor {
+
+ /**
+ * Constructor.
+ */
+ public YapiModuleVisitor(String dir) {
+ _dir = dir;
+ }
+
+ /**
+ *
+ */
+ public void visitComponent(ProcessNetwork pn) {
+ try {
+ //create header file
+ String filename = _dir + _delimiter + "application.h";
+ OutputStream file = new FileOutputStream(filename);
+ _code = new CodePrintStream(file);
+
+ _code.println("#include \"yapi.h\"");
+ _code.println();
+
+ Vector<String> processList = new Vector<String>();
+ for (Process p : pn.getProcessList()) {
+ String basename = p.getBasename();
+ if (!processList.contains(basename)) {
+ processList.add(basename);
+ _code.println("#include \""
+ + p.getBasename() + "_wrapper.h\"");
+ }
+ }
+ _code.println();
+ _code.println("class Application : public ProcessNetwork");
+ _code.println("{");
+ _code.println("public:");
+ _code.println(" Application(const Id& n);");
+ _code.println(" const char* type() const;");
+ _code.println();
+ _code.println("private:");
+
+ for (Channel c : pn.getChannelList()) {
+ if (c.getType().equals("fifo")) {
+ _code.println(" Fifo<char> " + c.getName()
+ + ";");
+ } else if (c.getType().equals("wfifo")) {
+ System.out.println("Warning: YAPI visitor does not "
+ + "support windowed FIFOs.");
+ }
+ }
+ _code.println();
+
+ for (Process p : pn.getProcessList()) {
+ _code.println(" " + p.getBasename() + "_wrapper "
+ + p.getName() + ";");
+ }
+ _code.println("};");
+ file.close();
+
+
+ //create cpp file
+ filename = _dir + _delimiter + "application.cpp";
+ file = new FileOutputStream(filename);
+ _code = new CodePrintStream(file);
+
+ _code.println("#include \"application.h\"");
+ _code.println();
+ _code.println("Application::Application(const Id& n) :");
+ _code.print(" ProcessNetwork(n)");
+ for (Channel c : pn.getChannelList()) {
+ if (c.getType().equals("fifo")) {
+ _code.print(",\n " + c.getName() + "(id(\""
+ + c.getName() + "\"))");
+ } else if (c.getType().equals("wfifo")) {
+ System.out.println("Warning: YAPI visitor does not "
+ + "support windowed FIFOs.");
+ }
+ }
+ for (Process p : pn.getProcessList()) {
+ _code.print(",\n " + p.getName() + "(\""
+ + p.getName() + "\", id(\"" + p.getName()
+ + "\")");
+
+ for (Port port : p.getPortList()) {
+ _code.print(", " + port.getPeerResource().getName());
+ }
+ _code.print(")");
+ }
+ _code.println();
+ _code.println("{ }");
+ _code.println();
+ _code.println("const char* Application::type() const");
+ _code.println("{");
+ _code.println(" return \"Application\";");
+ _code.println("}");
+ }
+ catch (Exception e) {
+ System.out.println("YapiModuleVisitor: "
+ + "exception occured: " + e.getMessage());
+ e.printStackTrace();
+ }
+ }
+
+ protected CodePrintStream _code = null;
+ protected String _dir = null;
+ protected OutputStream _file;
+ protected CodePrintStream _pn;
+}
+