--- /dev/null
+/* $Id: ProtothreadModuleVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.protothread;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.StringTokenizer;
+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 ProtothreadModuleVisitor extends PNVisitor {
+
+ /**
+ * Constructor.
+ */
+ public ProtothreadModuleVisitor(String dir) {
+ _dir = dir;
+ }
+
+ public void visitComponent(ProcessNetwork x) {
+ try {
+ String filename = _dir + _delimiter + "sc_application.cpp";
+ OutputStream file = new FileOutputStream(filename);
+ _code = new CodePrintStream(file);
+
+ _code.printPrefixln("#include \"pt.h\"");
+ _code.printPrefixln("#include \"Fifo.h\"");
+ _code.printPrefixln("#include \"WindowedFifo.h\"");
+ for (String basename : x.getProcessBasenames()) {
+ _code.printPrefixln("#include \"" + basename
+ + "Wrapper.h\"");
+ }
+
+ _code.println();
+ _code.printPrefixln("int main(void)");
+ _code.printLeftBracket();
+
+ //instantiate channels
+ for (Channel c : x.getChannelList()) {
+ if (c.getType().equals("fifo")) {
+ _code.printPrefixln("Fifo " + c.getName() + "("
+ + c.getSize() * c.getTokenSize() + ");");
+ } else if (c.getType().equals("wfifo")) {
+ _code.printPrefixln("WindowedFifo " + c.getName() + "("
+ + c.getSize() * c.getTokenSize() + ");");
+ }
+ }
+ _code.println();
+
+ //instantiate processes
+ for (Process p : x.getProcessList()) {
+ _code.printPrefix("int " + p.getName()
+ + "Indices[] = { ");
+ Vector<Integer> iteratorIndex =
+ p.getIteratorIndices();
+ if (iteratorIndex.size() < 4) {
+ while (iteratorIndex.size() < 4) {
+ iteratorIndex.add(-1);
+ }
+ } else if (iteratorIndex.size() > 4) {
+ new RuntimeException("Error: Currently not more than "
+ + "4 iterator dimensions are supported."
+ + "Consider revising " + p.getBasename()
+ + ".");
+ }
+ for (int i = 0; i < 4; i++) {
+ if (i < 3) {
+ _code.print(iteratorIndex.elementAt(i) + ", ");
+ } else {
+ _code.println(iteratorIndex.elementAt(i) + " };");
+ }
+ }
+ _code.printPrefixln(p.getBasename() + "Wrapper *"
+ + p.getName() + " = new "
+ + p.getBasename() + "Wrapper(\""
+ + p.getName() + "\", "
+ + p.getName() + "Indices);");
+ }
+ _code.println();
+
+ //connect the network
+ for (Process p : x.getProcessList()) {
+ for (Port port : p.getPortList()) {
+ if (port.getName().equals(port.getBasename())) {
+ _code.printPrefixln(p.getName()
+ + "->_port" + port.getName() + "Fifo = &"
+ + port.getPeerResource().getName() + ";");
+ } else {
+ _code.printPrefix(p.getName()
+ + "->_port" + port.getBasename()
+ + "Fifo");
+ StringTokenizer tokenizer =
+ new StringTokenizer(port.getName().
+ replaceFirst(port.getBasename(), ""), "_");
+ while (tokenizer.hasMoreTokens()) {
+ _code.print("[" + tokenizer.nextToken()
+ + "]");
+ }
+ _code.println(" = &"
+ + port.getPeerResource().getName() + ";");
+ }
+ }
+ }
+ _code.println();
+
+ //initialize processes
+ for (Process p : x.getProcessList()) {
+ _code.printPrefixln(p.getName() + "->init();");
+ }
+ _code.println();
+
+ /*
+ _code.printPrefix("while(");
+ int counter = 0;
+ for (Process p : x.getProcessList()) {
+ if (counter > 0) {
+ _code.printPrefix(" ");
+ }
+ _code.print("!" + p.getName() + "->isDetached()");
+ if (counter++ < x.getProcessList().size() - 1) {
+ _code.println(" ||");
+ } else {
+ _code.println(")");
+ }
+ }
+ */
+ _code.printPrefixln("bool allBlocked = false;");
+ _code.printPrefixln("while(!allBlocked)");
+ _code.printLeftBracket();
+ _code.printPrefixln("allBlocked = true;");
+ for (Process p : x.getProcessList()) {
+ _code.printPrefixln("if (!" + p.getName()
+ + "->isDetached()) {");
+ //_code.printPrefixln(" " + p.getName() + "->fire();");
+ _code.printPrefixln(" if (" + p.getName()
+ + "->fire() == PT_ENDED) {");
+ _code.printPrefixln(" allBlocked = false;");
+ _code.printPrefixln(" }");
+ _code.printPrefixln("}");
+ }
+ _code.printRightBracket();
+ _code.println();
+
+ for (Process p : x.getProcessList()) {
+ _code.printPrefixln("delete " + p.getName() + ";");
+ }
+ _code.println();
+ _code.printPrefixln("return 0;");
+ _code.printRightBracket();
+ }
+ catch (Exception e) {
+ System.out.println("ProtothreadModuleVisitor: "
+ + "exception occured: " + e.getMessage());
+ e.printStackTrace();
+ }
+ }
+
+ protected CodePrintStream _code = null;
+ protected String _dir = null;
+}
+