Program: 74915
Project: 74915
Buggy source:
/** 
 * Sanity check the parameters; if the parameters are legal create a continuous-time subsystem that implement the model, preinitialize all the actors in the subsystem, and set the opaqueness of this actor to true. This method need the write access on the workspace.
 * @exception IllegalActionException If there is no CTDirector,or any contained actors throw it in its preinitialize() method.
 */
public void preinitialize() throws IllegalActionException {
  _checkParameters();
  ArrayToken stateNames=(ArrayToken)stateVariableNames.getToken();
  int n=stateNames.length();
  int m=inputPortList().size();
  int r=outputPortList().size();
  DoubleMatrixToken initial=(DoubleMatrixToken)initialStates.getToken();
  try {
    _workspace.getWriteAccess();
    removeAllEntities();
    removeAllRelations();
    Integrator[] integrators=new Integrator[n];
    String[] states=new String[n];
    IORelation[] stateRelations=new IORelation[n];
    Expression[] equations=new Expression[n];
    for (int i=0; i < n; i++) {
      states[i]=((StringToken)stateNames.getElement(i)).stringValue().trim();
      integrators[i]=new Integrator(this,states[i]);
      integrators[i].initialState.setExpression("String_Node_Str" + i + "String_Node_Str");
      stateRelations[i]=new TypedIORelation(this,"String_Node_Str" + states[i]);
      integrators[i].output.link(stateRelations[i]);
      equations[i]=new Expression(this,states[i] + "String_Node_Str");
      equations[i].expression.setExpression(((StringToken)((Parameter)getAttribute(states[i] + "String_Node_Str")).getToken()).stringValue());
      equations[i].output.setTypeEquals(BaseType.DOUBLE);
      connect(equations[i].output,integrators[i].input);
    }
    String[] inputs=new String[m];
    IORelation[] inputRelations=new IORelation[m];
    Iterator inputPorts=inputPortList().iterator();
    int inputIndex=0;
    while (inputPorts.hasNext()) {
      inputs[inputIndex]=((NamedObj)inputPorts.next()).getName();
      inputRelations[inputIndex]=new TypedIORelation(this,"String_Node_Str" + inputs[inputIndex]);
      getPort(inputs[inputIndex]).link(inputRelations[inputIndex]);
      inputIndex++;
    }
    String[] outputs=new String[r];
    Expression[] maps=new Expression[r];
    int outIndex=0;
    Iterator outputPorts=outputPortList().iterator();
    while (outputPorts.hasNext()) {
      outputs[outIndex]=((NamedObj)outputPorts.next()).getName();
      maps[outIndex]=new Expression(this,"String_Node_Str" + outputs[outIndex]);
      maps[outIndex].expression.setExpression(((StringToken)((Parameter)getAttribute(outputs[outIndex])).getToken()).stringValue());
      maps[outIndex].output.setTypeEquals(BaseType.DOUBLE);
      connect(maps[outIndex].output,(TypedIOPort)getPort(outputs[outIndex]));
      outIndex++;
    }
    for (int i=0; i < n; i++) {
      for (int k=0; k < n; k++) {
        TypedIOPort port=new TypedIOPort(equations[i],states[k],true,false);
        port.setTypeEquals(BaseType.DOUBLE);
        port.link(stateRelations[k]);
      }
      for (int k=0; k < m; k++) {
        TypedIOPort port=new TypedIOPort(equations[i],inputs[k],true,false);
        port.setTypeEquals(BaseType.DOUBLE);
        port.link(inputRelations[k]);
      }
    }
    for (int l=0; l < r; l++) {
      for (int k=0; k < n; k++) {
        TypedIOPort port=new TypedIOPort(maps[l],states[k],true,false);
        port.setTypeEquals(BaseType.DOUBLE);
        port.link(stateRelations[k]);
      }
    }
    _opaque=false;
    _workspace.incrVersion();
  }
 catch (  NameDuplicationException ex) {
    throw new InternalErrorException("String_Node_Str" + "String_Node_Str" + ex.getMessage());
  }
 finally {
    _workspace.doneWriting();
  }
  for (Iterator i=deepEntityList().iterator(); i.hasNext(); ) {
    Actor actor=(Actor)i.next();
    actor.preinitialize();
  }
}

Fixed version (ground truth):
/** 
 * Sanity check the parameters; if the parameters are legal create a continuous-time subsystem that implement the model, preinitialize all the actors in the subsystem, and set the opaqueness of this actor to true. This method need the write access on the workspace.
 * @exception IllegalActionException If there is no CTDirector,or any contained actors throw it in its preinitialize() method.
 */
public void preinitialize() throws IllegalActionException {
  _checkParameters();
  ArrayToken stateNames=(ArrayToken)stateVariableNames.getToken();
  int n=stateNames.length();
  int m=inputPortList().size();
  int r=outputPortList().size();
  try {
    _workspace.getWriteAccess();
    removeAllEntities();
    removeAllRelations();
    Integrator[] integrators=new Integrator[n];
    String[] states=new String[n];
    IORelation[] stateRelations=new IORelation[n];
    Expression[] equations=new Expression[n];
    for (int i=0; i < n; i++) {
      states[i]=((StringToken)stateNames.getElement(i)).stringValue().trim();
      integrators[i]=new Integrator(this,states[i]);
      integrators[i].initialState.setExpression("String_Node_Str" + i + "String_Node_Str");
      stateRelations[i]=new TypedIORelation(this,"String_Node_Str" + states[i]);
      integrators[i].output.link(stateRelations[i]);
      equations[i]=new Expression(this,states[i] + "String_Node_Str");
      equations[i].expression.setExpression(((StringToken)((Parameter)getAttribute(states[i] + "String_Node_Str")).getToken()).stringValue());
      equations[i].output.setTypeEquals(BaseType.DOUBLE);
      connect(equations[i].output,integrators[i].input);
    }
    String[] inputs=new String[m];
    IORelation[] inputRelations=new IORelation[m];
    Iterator inputPorts=inputPortList().iterator();
    int inputIndex=0;
    while (inputPorts.hasNext()) {
      inputs[inputIndex]=((NamedObj)inputPorts.next()).getName();
      inputRelations[inputIndex]=new TypedIORelation(this,"String_Node_Str" + inputs[inputIndex]);
      getPort(inputs[inputIndex]).link(inputRelations[inputIndex]);
      inputIndex++;
    }
    String[] outputs=new String[r];
    Expression[] maps=new Expression[r];
    int outIndex=0;
    Iterator outputPorts=outputPortList().iterator();
    while (outputPorts.hasNext()) {
      outputs[outIndex]=((NamedObj)outputPorts.next()).getName();
      maps[outIndex]=new Expression(this,"String_Node_Str" + outputs[outIndex]);
      maps[outIndex].expression.setExpression(((StringToken)((Parameter)getAttribute(outputs[outIndex])).getToken()).stringValue());
      maps[outIndex].output.setTypeEquals(BaseType.DOUBLE);
      connect(maps[outIndex].output,(TypedIOPort)getPort(outputs[outIndex]));
      outIndex++;
    }
    for (int i=0; i < n; i++) {
      for (int k=0; k < n; k++) {
        TypedIOPort port=new TypedIOPort(equations[i],states[k],true,false);
        port.setTypeEquals(BaseType.DOUBLE);
        port.link(stateRelations[k]);
      }
      for (int k=0; k < m; k++) {
        TypedIOPort port=new TypedIOPort(equations[i],inputs[k],true,false);
        port.setTypeEquals(BaseType.DOUBLE);
        port.link(inputRelations[k]);
      }
    }
    for (int l=0; l < r; l++) {
      for (int k=0; k < n; k++) {
        TypedIOPort port=new TypedIOPort(maps[l],states[k],true,false);
        port.setTypeEquals(BaseType.DOUBLE);
        port.link(stateRelations[k]);
      }
    }
    _opaque=false;
    _workspace.incrVersion();
  }
 catch (  NameDuplicationException ex) {
    throw new InternalErrorException("String_Node_Str" + "String_Node_Str" + ex.getMessage());
  }
 finally {
    _workspace.doneWriting();
  }
  for (Iterator i=deepEntityList().iterator(); i.hasNext(); ) {
    Actor actor=(Actor)i.next();
    actor.preinitialize();
  }
}