Program: 72556
Project: 72556
Buggy source:
/** 
 * Generate the code for the firing of actors. In this base class, it is attempted to fire all the actors once. In subclasses such as the adapters for SDF and Giotto directors, the firings of actors observe the associated schedule. In addition, some special handling is needed, e.g., the iteration limit in SDF and time advancement in Giotto.
 * @return The generated code.
 * @exception IllegalActionException If the adapter associated with an actor throws it while generating fire code for the actor.
 */
@Override public String generateFMIMA() throws IllegalActionException {
  StringBuffer code=new StringBuffer();
  NamedProgramCodeGeneratorAdapter adapter=(NamedProgramCodeGeneratorAdapter)getAdapter(getComponent());
  Iterator<?> actors=((CompositeActor)adapter.getComponent().getContainer()).deepEntityList().iterator();
  code.append(getCodeGenerator().comment("String_Node_Str" + _eol + "String_Node_Str"+ adapter.getComponent().getName()));
  CodeStream codeStream=_templateParser.getCodeStream();
  codeStream.clear();
  codeStream.appendCodeBlock("String_Node_Str");
  code.append(processCode(codeStream.toString()));
  HashMap<TypedIOPort,Node> actorNodeMap=new HashMap<TypedIOPort,Node>();
  HashMap<Node,TypedIOPort> actorPortMap=new HashMap<Node,TypedIOPort>();
  HashMap<String,Node> actorNodeNamesMap=new HashMap<String,Node>();
  DirectedGraph graph=new DirectedGraph();
  actors=((CompositeActor)adapter.getComponent().getContainer()).deepEntityList().iterator();
  while (actors.hasNext()) {
    ptolemy.actor.lib.fmi.FMUImport actor=(ptolemy.actor.lib.fmi.FMUImport)actors.next();
    for (    TypedIOPort input : actor.inputPortList()) {
      Node node=new Node(input);
      actorNodeMap.put(input,node);
      actorPortMap.put(node,input);
      actorNodeNamesMap.put(input.getFullName(),node);
      graph.addNode(node);
    }
    for (    TypedIOPort output : actor.outputPortList()) {
      Node node=new Node(output);
      actorNodeMap.put(output,node);
      actorPortMap.put(node,output);
      actorNodeNamesMap.put(output.getFullName(),node);
      graph.addNode(node);
    }
  }
  actors=((CompositeActor)adapter.getComponent().getContainer()).deepEntityList().iterator();
  while (actors.hasNext()) {
    ptolemy.actor.lib.fmi.FMUImport actor=(ptolemy.actor.lib.fmi.FMUImport)actors.next();
    for (    TypedIOPort input : actor.inputPortList()) {
      Node sinkNode=(Node)actorNodeMap.get(input);
      List<TypedIOPort> connected_ports=input.connectedPortList();
      for (int port_idx=0; port_idx < connected_ports.size(); port_idx++) {
        TypedIOPort output=(TypedIOPort)connected_ports.get(port_idx);
        Node sourceNode=(Node)actorNodeMap.get(output);
        graph.addEdge(sourceNode,sinkNode);
      }
    }
    for (    TypedIOPort output : actor.outputPortList()) {
      Node sinkNode=(Node)actorNodeMap.get(output);
      Set<String> inputPorts=actor.getInputDependencyList(output.getName());
      if (inputPorts != null) {
        Iterator<String> inputIterator=inputPorts.iterator();
        while (inputIterator.hasNext()) {
          String input=(String)inputIterator.next();
          Node sourceNode=(Node)actorNodeNamesMap.get(actor.getPort(input).getFullName());
          graph.addEdge(sourceNode,sinkNode);
        }
      }
    }
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str"+ actor.fmuFile.asFile().toString().replace("String_Node_Str","String_Node_Str")+ "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str"+ actor.fmuFile.asFile().toString().replace("String_Node_Str","String_Node_Str")+ "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str"+ actor.getName()+ "String_Node_Str");
  }
  Collection<Node> nodeCollection=graph.nodes();
  if (graph.isAcyclic() == false) {
    throw new IllegalActionException(adapter.getComponent(),"String_Node_Str");
  }
  List sortedGraph=graph.topologicalSort(nodeCollection);
  int connectionIndex=0;
  while (sortedGraph.size() > 1) {
    Node portSourceNode=(Node)sortedGraph.get(0);
    Node portSinkNode=(Node)sortedGraph.get(1);
    sortedGraph.remove(portSourceNode);
    sortedGraph.remove(portSinkNode);
    TypedIOPort outputPort=actorPortMap.get(portSourceNode);
    TypedIOPort inputPort=actorPortMap.get(portSinkNode);
    ptolemy.actor.lib.fmi.FMUImport sourceActor=(ptolemy.actor.lib.fmi.FMUImport)actorPortMap.get(portSourceNode).getContainer();
    ptolemy.actor.lib.fmi.FMUImport sinkActor=(ptolemy.actor.lib.fmi.FMUImport)actorPortMap.get(portSinkNode).getContainer();
    String fmuSourceName=sourceActor.getName();
    String fmuSinkName=sinkActor.getName();
    code.append("String_Node_Str" + connectionIndex + "String_Node_Str"+ fmuSourceName+ "String_Node_Str"+ "String_Node_Str"+ connectionIndex+ "String_Node_Str"+ fmuSourceName+ "String_Node_Str"+ sourceActor.getValueReference(outputPort.getName())+ "String_Node_Str"+ "String_Node_Str"+ connectionIndex+ "String_Node_Str"+ sourceActor.getTypeOfPort(outputPort.getName())+ "String_Node_Str"+ "String_Node_Str"+ connectionIndex+ "String_Node_Str"+ fmuSinkName+ "String_Node_Str");
    Long sinkActorInputValueReference=sinkActor.getValueReference(inputPort.getName());
    if (sinkActorInputValueReference < 0) {
      System.err.println("String_Node_Str" + inputPort.getName() + "String_Node_Str"+ sinkActor.getFullName()+ "String_Node_Str"+ sinkActorInputValueReference+ "String_Node_Str");
    }
 else {
      code.append("String_Node_Str" + connectionIndex + "String_Node_Str"+ fmuSinkName+ "String_Node_Str"+ sinkActorInputValueReference+ "String_Node_Str");
    }
    String sinkActorPortType=sinkActor.getTypeOfPort(inputPort.getName());
    if (sinkActorPortType.equals("String_Node_Str")) {
      System.err.println("String_Node_Str" + inputPort.getName() + "String_Node_Str"+ sinkActor.getFullName());
    }
 else {
      code.append("String_Node_Str" + connectionIndex + "String_Node_Str"+ sinkActorPortType+ "String_Node_Str");
    }
    connectionIndex++;
  }
  codeStream=_templateParser.getCodeStream();
  codeStream.clear();
  codeStream.appendCodeBlock("String_Node_Str");
  code.append(processCode(codeStream.toString()));
  code.append(getCodeGenerator().comment("String_Node_Str"));
  return code.toString();
}

Fixed version (ground truth):
/** 
 * Generate the code for the firing of actors. In this base class, it is attempted to fire all the actors once. In subclasses such as the adapters for SDF and Giotto directors, the firings of actors observe the associated schedule. In addition, some special handling is needed, e.g., the iteration limit in SDF and time advancement in Giotto.
 * @return The generated code.
 * @exception IllegalActionException If the adapter associated with an actor throws it while generating fire code for the actor.
 */
@Override public String generateFMIMA() throws IllegalActionException {
  StringBuffer code=new StringBuffer();
  NamedProgramCodeGeneratorAdapter adapter=(NamedProgramCodeGeneratorAdapter)getAdapter(getComponent());
  Iterator<?> actors=((CompositeActor)adapter.getComponent().getContainer()).deepEntityList().iterator();
  code.append(getCodeGenerator().comment("String_Node_Str" + _eol + "String_Node_Str"+ adapter.getComponent().getName()));
  CodeStream codeStream=_templateParser.getCodeStream();
  codeStream.clear();
  codeStream.appendCodeBlock("String_Node_Str");
  code.append(processCode(codeStream.toString()));
  HashMap<TypedIOPort,Node> actorNodeMap=new HashMap<TypedIOPort,Node>();
  HashMap<Node,TypedIOPort> actorPortMap=new HashMap<Node,TypedIOPort>();
  HashMap<String,Node> actorNodeNamesMap=new HashMap<String,Node>();
  DirectedGraph graph=new DirectedGraph();
  actors=((CompositeActor)adapter.getComponent().getContainer()).deepEntityList().iterator();
  while (actors.hasNext()) {
    ptolemy.actor.lib.fmi.FMUImport actor=(ptolemy.actor.lib.fmi.FMUImport)actors.next();
    for (    TypedIOPort input : actor.inputPortList()) {
      Node node=new Node(input);
      actorNodeMap.put(input,node);
      actorPortMap.put(node,input);
      actorNodeNamesMap.put(input.getFullName(),node);
      graph.addNode(node);
    }
    for (    TypedIOPort output : actor.outputPortList()) {
      Node node=new Node(output);
      actorNodeMap.put(output,node);
      actorPortMap.put(node,output);
      actorNodeNamesMap.put(output.getFullName(),node);
      graph.addNode(node);
    }
  }
  actors=((CompositeActor)adapter.getComponent().getContainer()).deepEntityList().iterator();
  int connectionNumber=0;
  while (actors.hasNext()) {
    ptolemy.actor.lib.fmi.FMUImport actor=(ptolemy.actor.lib.fmi.FMUImport)actors.next();
    for (    TypedIOPort input : actor.inputPortList()) {
      Node sinkNode=(Node)actorNodeMap.get(input);
      List<TypedIOPort> connected_ports=input.connectedPortList();
      for (int port_idx=0; port_idx < connected_ports.size(); port_idx++) {
        TypedIOPort output=(TypedIOPort)connected_ports.get(port_idx);
        Node sourceNode=(Node)actorNodeMap.get(output);
        graph.addEdge(sourceNode,sinkNode);
        connectionNumber++;
      }
    }
    for (    TypedIOPort output : actor.outputPortList()) {
      Node sinkNode=(Node)actorNodeMap.get(output);
      Set<String> inputPorts=actor.getInputDependencyList(output.getName());
      if (inputPorts != null) {
        Iterator<String> inputIterator=inputPorts.iterator();
        while (inputIterator.hasNext()) {
          String input=(String)inputIterator.next();
          Node sourceNode=(Node)actorNodeNamesMap.get(actor.getPort(input).getFullName());
          graph.addEdge(sourceNode,sinkNode);
        }
      }
    }
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str"+ actor.fmuFile.asFile().toString().replace("String_Node_Str","String_Node_Str")+ "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str"+ actor.fmuFile.asFile().toString().replace("String_Node_Str","String_Node_Str")+ "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str");
    code.append("String_Node_Str" + actor.getName() + "String_Node_Str"+ actor.getName()+ "String_Node_Str");
  }
  Collection<Node> nodeCollection=graph.nodes();
  if (graph.isAcyclic() == false) {
    throw new IllegalActionException(adapter.getComponent(),"String_Node_Str");
  }
  List sortedGraph=graph.topologicalSort(nodeCollection);
  int connectionIndex=0;
  for (int i=0; i < connectionNumber; i++) {
    Node portSourceNode=(Node)sortedGraph.get(0);
    Node portSinkNode=(Node)sortedGraph.get(1);
    sortedGraph.remove(portSourceNode);
    sortedGraph.remove(portSinkNode);
    TypedIOPort outputPort=actorPortMap.get(portSourceNode);
    TypedIOPort inputPort=actorPortMap.get(portSinkNode);
    ptolemy.actor.lib.fmi.FMUImport sourceActor=(ptolemy.actor.lib.fmi.FMUImport)actorPortMap.get(portSourceNode).getContainer();
    ptolemy.actor.lib.fmi.FMUImport sinkActor=(ptolemy.actor.lib.fmi.FMUImport)actorPortMap.get(portSinkNode).getContainer();
    String fmuSourceName=sourceActor.getName();
    String fmuSinkName=sinkActor.getName();
    code.append("String_Node_Str" + connectionIndex + "String_Node_Str"+ fmuSourceName+ "String_Node_Str"+ "String_Node_Str"+ connectionIndex+ "String_Node_Str"+ fmuSourceName+ "String_Node_Str"+ sourceActor.getValueReference(outputPort.getName())+ "String_Node_Str"+ "String_Node_Str"+ connectionIndex+ "String_Node_Str"+ sourceActor.getTypeOfPort(outputPort.getName())+ "String_Node_Str"+ "String_Node_Str"+ connectionIndex+ "String_Node_Str"+ fmuSinkName+ "String_Node_Str");
    Long sinkActorInputValueReference=sinkActor.getValueReference(inputPort.getName());
    if (sinkActorInputValueReference < 0) {
      System.err.println("String_Node_Str" + inputPort.getName() + "String_Node_Str"+ sinkActor.getFullName()+ "String_Node_Str"+ sinkActorInputValueReference+ "String_Node_Str");
    }
 else {
      code.append("String_Node_Str" + connectionIndex + "String_Node_Str"+ fmuSinkName+ "String_Node_Str"+ sinkActorInputValueReference+ "String_Node_Str");
    }
    String sinkActorPortType=sinkActor.getTypeOfPort(inputPort.getName());
    if (sinkActorPortType.equals("String_Node_Str")) {
      System.err.println("String_Node_Str" + inputPort.getName() + "String_Node_Str"+ sinkActor.getFullName());
    }
 else {
      code.append("String_Node_Str" + connectionIndex + "String_Node_Str"+ sinkActorPortType+ "String_Node_Str");
    }
    connectionIndex++;
  }
  codeStream=_templateParser.getCodeStream();
  codeStream.clear();
  codeStream.appendCodeBlock("String_Node_Str");
  code.append(processCode(codeStream.toString()));
  code.append(getCodeGenerator().comment("String_Node_Str"));
  return code.toString();
}