Program: 72368
Project: 72368
Buggy source:
/** 
 * Create a new input port if it does not already exist. The options argument can specify a "type", a "description", and/or a "value". If a type is given, and neither the port nor its corresponding parameter contain a TypeAttribute, then set the type as specified. Otherwise, leave the type unspecified so that it will be inferred. If a description is given, then create, append to, or modify the DocAttribute named "documentation" contained by this actor to include documentation of this output. If a value is given, then create a PortParameter instead of an ordinary port and set its default value, unless it already has a value that overrides the default. In that case, the prior value will be returned. Otherwise, null will be returned. If a Parameter already exists with the same name, then convert it to a PortParameter and preserve and return its value. The options can also include a field "visibility" with one of the values "none", "expert", "noteditable" or "full" (the default). This is a hint to restrict visibility that a user has of the port.
 * @param name The name of the port.
 * @param options The options, or null to accept the defaults.To give options, this argument must implement the Map interface.
 * @return The previous value of this input, if it has one, andnull otherwise.
 * @exception IllegalActionException If no name is given.
 * @exception NameDuplicationException If the name is a reserved word.
 */
public Token input(String name,Map<String,Object> options) throws IllegalActionException, NameDuplicationException {
  if (name == null) {
    throw new IllegalActionException(this,"String_Node_Str");
  }
  TypedIOPort port=(TypedIOPort)getPort(name);
  PortParameter parameter=null;
  Object token=null;
  Token previousValue=null;
  String previousExpression="String_Node_Str";
  boolean deletedPriorParameter=false;
  if (port == null) {
    Attribute previous=getAttribute(name);
    if (previous instanceof Parameter) {
      previousValue=((Parameter)previous).getToken();
      if (previousValue instanceof StringToken && ((StringToken)previousValue).stringValue().trim().equals("String_Node_Str")) {
        previousValue=null;
      }
      previous.setContainer(null);
      deletedPriorParameter=true;
    }
    if (options == null) {
      if (previousValue == null) {
        port=(TypedIOPort)newPort(name);
      }
 else {
        parameter=new PortParameter(this,name);
        port=parameter.getPort();
      }
    }
 else {
      Object value=((Map<String,Object>)options).get("String_Node_Str");
      if (value == null && previousValue == null) {
        port=(TypedIOPort)newPort(name);
      }
 else {
        parameter=new PortParameter(this,name);
        port=parameter.getPort();
      }
    }
  }
 else {
    if (port == script.getPort() || port == error) {
      throw new NameDuplicationException(this,"String_Node_Str" + name);
    }
    if (port instanceof ParameterPort) {
      parameter=((ParameterPort)port).getParameter();
      ((PortParameter)parameter).initialize();
      previousValue=parameter.getToken();
      previousExpression=parameter.getExpression();
    }
 else {
      if (options != null) {
        Object value=((Map<String,Object>)options).get("String_Node_Str");
        if (value != null) {
          port.setContainer(null);
          parameter=new PortParameter(this,name);
          port=parameter.getPort();
        }
      }
    }
  }
  if (options instanceof Map) {
    if (port.attributeList(TypeAttribute.class).isEmpty() && (parameter == null || parameter.attributeList(TypeAttribute.class).isEmpty())) {
      Object type=((Map<String,Object>)options).get("String_Node_Str");
      if (type instanceof String) {
        Type ptType=_typeAccessorToPtolemy((String)type,port);
        port.setTypeEquals(ptType);
        if (parameter != null) {
          parameter.setTypeEquals(ptType);
        }
      }
 else       if (type != null) {
        throw new IllegalActionException(this,"String_Node_Str" + type);
      }
    }
    _setOptionsForSelect(port,options);
    Object description=((Map<String,Object>)options).get("String_Node_Str");
    if (description != null) {
      _setPortDescription(port,description.toString());
    }
    Object value=((Map<String,Object>)options).get("String_Node_Str");
    if (value != null) {
      try {
        token=((Invocable)_engine).invokeFunction("String_Node_Str",value);
      }
 catch (      Exception e) {
        throw new IllegalActionException(this,e,"String_Node_Str" + value);
      }
      if (!(token instanceof Token)) {
        throw new IllegalActionException(this,"String_Node_Str" + value);
      }
    }
    _setPortVisibility(options,port,parameter);
  }
  Token result=null;
  if (parameter != null) {
    if (token != null && !parameter.isOverridden()) {
      if (parameter.getAttribute("String_Node_Str") != null && !(token instanceof StringToken)) {
        String json=TokenToJSON.constructJSON((Token)token);
        token=new StringToken(json);
      }
      parameter.setToken((Token)token);
      parameter.setDerivedLevel(1);
    }
    if (parameter.isOverridden()) {
      result=previousValue;
    }
    if (deletedPriorParameter && previousValue != null) {
      parameter.setExpression(previousExpression);
    }
  }
  port.setInput(true);
  return result;
}

Fixed version (ground truth):
/** 
 * Create a new input port if it does not already exist. The options argument can specify a "type", a "description", and/or a "value". If a type is given, and neither the port nor its corresponding parameter contain a TypeAttribute, then set the type as specified. Otherwise, leave the type unspecified so that it will be inferred. If a description is given, then create, append to, or modify the DocAttribute named "documentation" contained by this actor to include documentation of this output. If a value is given, then create a PortParameter instead of an ordinary port and set its default value, unless it already has a value that overrides the default. In that case, the prior value will be returned. Otherwise, null will be returned. If a Parameter already exists with the same name, then convert it to a PortParameter and preserve and return its value. The options can also include a field "visibility" with one of the values "none", "expert", "noteditable" or "full" (the default). This is a hint to restrict visibility that a user has of the port.
 * @param name The name of the port.
 * @param options The options, or null to accept the defaults.To give options, this argument must implement the Map interface.
 * @return The previous value of this input, if it has one, andnull otherwise.
 * @exception IllegalActionException If no name is given.
 * @exception NameDuplicationException If the name is a reserved word.
 */
public Token input(String name,Map<String,Object> options) throws IllegalActionException, NameDuplicationException {
  if (name == null) {
    throw new IllegalActionException(this,"String_Node_Str");
  }
  TypedIOPort port=(TypedIOPort)getPort(name);
  PortParameter parameter=null;
  Object token=null;
  Token previousValue=null;
  String previousExpression="String_Node_Str";
  boolean deletedPriorParameter=false;
  if (port == null) {
    Attribute previous=getAttribute(name);
    if (previous instanceof Parameter) {
      previousValue=((Parameter)previous).getToken();
      if (previousValue instanceof StringToken && ((StringToken)previousValue).stringValue().trim().equals("String_Node_Str")) {
        previousValue=null;
      }
      previous.setContainer(null);
      deletedPriorParameter=true;
    }
    if (options == null) {
      if (previousValue == null) {
        port=(TypedIOPort)newPort(name);
      }
 else {
        parameter=new PortParameter(this,name);
        port=parameter.getPort();
      }
    }
 else {
      Object value=((Map<String,Object>)options).get("String_Node_Str");
      if (value == null && previousValue == null) {
        port=(TypedIOPort)newPort(name);
      }
 else {
        parameter=new PortParameter(this,name);
        port=parameter.getPort();
      }
    }
  }
 else {
    if (port == script.getPort() || port == error) {
      throw new NameDuplicationException(this,"String_Node_Str" + name);
    }
    if (port instanceof ParameterPort) {
      parameter=((ParameterPort)port).getParameter();
      ((PortParameter)parameter).initialize();
      previousValue=parameter.getToken();
      previousExpression=parameter.getExpression();
    }
 else {
      if (options != null) {
        Object value=((Map<String,Object>)options).get("String_Node_Str");
        if (value != null) {
          port.setContainer(null);
          parameter=new PortParameter(this,name);
          port=parameter.getPort();
        }
      }
    }
  }
  if (options != null) {
    if (port.attributeList(TypeAttribute.class).isEmpty() && (parameter == null || parameter.attributeList(TypeAttribute.class).isEmpty())) {
      Object type=((Map<String,Object>)options).get("String_Node_Str");
      if (type instanceof String) {
        Type ptType=_typeAccessorToPtolemy((String)type,port);
        port.setTypeEquals(ptType);
        if (parameter != null) {
          parameter.setTypeEquals(ptType);
        }
      }
 else       if (type != null) {
        throw new IllegalActionException(this,"String_Node_Str" + type);
      }
    }
    _setOptionsForSelect(port,options);
    Object description=((Map<String,Object>)options).get("String_Node_Str");
    if (description != null) {
      _setPortDescription(port,description.toString());
    }
    Object value=((Map<String,Object>)options).get("String_Node_Str");
    if (value != null) {
      try {
        token=((Invocable)_engine).invokeFunction("String_Node_Str",value);
      }
 catch (      Exception e) {
        throw new IllegalActionException(this,e,"String_Node_Str" + value);
      }
      if (!(token instanceof Token)) {
        throw new IllegalActionException(this,"String_Node_Str" + value);
      }
    }
    _setPortVisibility(options,port,parameter);
  }
  Token result=null;
  if (parameter != null) {
    if (token != null && !parameter.isOverridden()) {
      if (parameter.getAttribute("String_Node_Str") != null && !(token instanceof StringToken)) {
        String json=TokenToJSON.constructJSON((Token)token);
        token=new StringToken(json);
      }
      parameter.setToken((Token)token);
      parameter.setDerivedLevel(1);
    }
    if (parameter.isOverridden()) {
      result=previousValue;
    }
    if (deletedPriorParameter && previousValue != null) {
      parameter.setExpression(previousExpression);
    }
  }
  port.setInput(true);
  return result;
}