+package dol.visitor.hdsd;
+
+import dol.datamodel.architecture.Processor;
+import dol.datamodel.mapping.Mapping;
+import dol.datamodel.pn.Channel;
+import dol.datamodel.pn.Port;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.Resource;
+import dol.util.CodePrintStream;
+import dol.visitor.PNVisitor;
+
+/**
+ * Visitor that generates the main program thread of a process
+ * and the process network in the constructor.
+ */
+public class HdsdModulePNVisitor extends PNVisitor {
+
+ /**
+ * Constructor.
+ */
+ public HdsdModulePNVisitor(Processor processor, CodePrintStream stream) {
+ _processor = processor;
+ _mainPS = stream;
+ }
+
+ /**
+ * Generates the thread for this process.
+ *
+ * @param x process that needs to be rendered
+ */
+ public void visitComponent(Process x) {
+ _mainPS.printPrefixln("void thread_" + x.getName() + "()");
+ _mainPS.printLeftBracket();
+ _mainPS.printPrefixln("while (!" + x.getName()
+ + "_ins.isDetached())");
+ _mainPS.printLeftBracket();
+
+ /* begin of profiling: start event */
+ _mainPS.printPrefixln("#ifdef INCLUDE_PROFILER");
+ _mainPS.printPrefixln("if (profiler_output_file != NULL) fprintf(profiler_output_file, \"%u "+ x.getName() + " started.\\n\", profiler_event_counter++);");
+ _mainPS.printPrefixln("#endif");
+ /* end of profiling */
+
+ _mainPS.printPrefixln(x.getName() + "_ins.fire();");
+
+ /* begin of profiling: stop event */
+ _mainPS.printPrefixln("#ifdef INCLUDE_PROFILER");
+ _mainPS.printPrefixln("if (profiler_output_file != NULL) fprintf(profiler_output_file, \"%u "+ x.getName() + " stopped.\\n\", profiler_event_counter++);");
+ _mainPS.printPrefixln("#endif");
+ /* end of profiling */
+
+ _mainPS.printPrefixln("eventList.push_back(&" + x.getName()
+ + "_event);");
+ _mainPS.printPrefixln("sched_event.notify(SC_ZERO_TIME);");
+ _mainPS.printPrefixln("wait(" + x.getName() + "_event);");
+
+ _mainPS.printRightBracket();
+ _mainPS.printRightBracket();
+ }
+
+ /**
+ * Generates the process network. This is the channel content
+ * in the model constructor.
+ * @param x channel that needs to be rendered
+ */
+ public void visitComponent(Channel x) {
+ for (Port p : x.getPortList()) {
+ Port peerPort = (Port)(p.getPeerPort());
+ Resource peerResource = p.getPeerResource();
+
+ if (!_processor.hasProcess(peerResource.getName()))
+ continue;
+
+ if (peerPort.getRange() != null) {
+ if (p.isOutPort()) {
+ _mainPS.printPrefixln(peerResource.getName()
+ + "_ins.INPORT_"
+ + peerPort.getBasename()
+ + peerPort.getName().replace(
+ peerPort.getBasename(), "").replaceAll(
+ "_([0-9]+)", "[$1]") + "(" + x.getName()
+ + "_ins);");
+ }
+ else if (p.isInPort()) {
+ _mainPS.printPrefixln(peerResource.getName()
+ + "_ins.OUTPORT_"
+ + peerPort.getBasename()
+ + peerPort.getName().replace(
+ peerPort.getBasename(), "").replaceAll(
+ "_([0-9]+)", "[$1]") + "(" + x.getName()
+ + "_ins);");
+ }
+ }
+ else {
+ if (p.isOutPort()) {
+ _mainPS.printPrefixln(peerResource.getName()
+ + "_ins.INPORT_" + peerPort.getName()
+ + "(" + x.getName() + "_ins);");
+ }
+ else if (p.isInPort()) {
+ _mainPS.printPrefixln(peerResource.getName()
+ + "_ins.OUTPORT_" + peerPort.getName()
+ + "(" + x.getName() + "_ins);");
+ }
+ }
+ }
+ }
+
+ protected CodePrintStream _mainPS = null;
+ protected String _dir = null;
+ protected Mapping _map = null;
+ protected Processor _processor = null;
+}