Program: 74065
Project: 74065
Buggy source:
/** 
 * Remove all unnecessary relations within a composite actor. Unnecessary means that a relation is connected only with 0, 1 or 2 objects. In such case a relation can be either simply removed or replaced by a direct link between the objects. Iterate all relations in the parent actor and for all unnecessary relations with vertices, remove them and if required reestablish the links such that the semantics keeps the same.
 * @param parent The composite actor in which to look for unnecessary relations.
 */
public static void _removeUnnecessaryRelations(CompositeActor parent){
  PtolemyModelUtil util=new PtolemyModelUtil();
  for (Iterator containedIterator=parent.containedObjectsIterator(); containedIterator.hasNext(); ) {
    Object containedElement=containedIterator.next();
    if (containedElement instanceof Relation) {
      Relation relation=(Relation)containedElement;
      List linkedObjects=relation.linkedObjectsList();
      if (linkedObjects.size() == 0 || linkedObjects.size() == 1) {
        util._removeRelation(relation,parent);
        util._performChangeRequest(parent);
      }
 else {
        List<Vertex> vertices=relation.attributeList(ptolemy.moml.Vertex.class);
        if (!vertices.isEmpty()) {
          if (linkedObjects.size() == 2) {
            Object o1=linkedObjects.get(0);
            Object o2=linkedObjects.get(1);
            if (o1 instanceof Port && o2 instanceof Port) {
              util._removeRelationVertex(relation);
            }
 else             if (o1 instanceof Relation && o2 instanceof Relation) {
              util._link("String_Node_Str",((Relation)o1).getName(),"String_Node_Str",((Relation)o2).getName());
              util._removeRelation(relation,parent);
              util._performChangeRequest(parent);
            }
 else {
              Port connectedPort=null;
              Relation connectedRelation=null;
              if (o1 instanceof Port && o2 instanceof Relation) {
                connectedPort=(Port)o1;
                connectedRelation=(Relation)o2;
              }
 else               if (o2 instanceof Port && o1 instanceof Relation) {
                connectedPort=(Port)o2;
                connectedRelation=(Relation)o1;
              }
              if (connectedPort != null && connectedRelation != null) {
                int index=connectedPort.linkedRelationList().indexOf(relation);
                if (index >= 0) {
                  util._linkPort(connectedPort.getName(parent),"String_Node_Str",connectedRelation.getName(),index);
                  util._removeRelation(relation,parent);
                  util._performChangeRequest(parent);
                }
              }
            }
          }
        }
      }
    }
  }
  util._performChangeRequest(parent);
}

Fixed version (ground truth):
/** 
 * Remove all unnecessary relations within a composite actor. Unnecessary means that a relation is connected only with 0, 1 or 2 objects. In such case a relation can be either simply removed or replaced by a direct link between the objects. Iterate all relations in the parent actor and for all unnecessary relations with vertices, remove them and if required reestablish the links such that the semantics keeps the same.
 * @param parent The composite actor in which to look for unnecessary relations.
 */
public static void _removeUnnecessaryRelations(CompositeActor parent){
  PtolemyModelUtil util=new PtolemyModelUtil();
  for (Iterator containedIterator=parent.containedObjectsIterator(); containedIterator.hasNext(); ) {
    Object containedElement=containedIterator.next();
    if (containedElement instanceof Relation) {
      Relation relation=(Relation)containedElement;
      List linkedObjects=relation.linkedObjectsList();
      if (linkedObjects.size() == 0 || linkedObjects.size() == 1) {
        util._removeRelation(relation,parent);
        util._performChangeRequest(parent);
      }
 else {
        List<Vertex> vertices=relation.attributeList(ptolemy.moml.Vertex.class);
        if (!vertices.isEmpty()) {
          if (linkedObjects.size() == 2) {
            Object o1=linkedObjects.get(0);
            Object o2=linkedObjects.get(1);
            if (o1 instanceof Port && o2 instanceof Port) {
              util._removeRelationVertex(relation);
            }
 else             if (o1 instanceof Relation && o2 instanceof Relation) {
              util._link("String_Node_Str",((Relation)o1).getName(),"String_Node_Str",((Relation)o2).getName());
              util._removeRelation(relation,parent);
              util._performChangeRequest(parent);
            }
 else {
              Port connectedPort=null;
              Relation connectedRelation=null;
              if (o1 instanceof Port && o2 instanceof Relation) {
                connectedPort=(Port)o1;
                connectedRelation=(Relation)o2;
              }
 else               if (o2 instanceof Port && o1 instanceof Relation) {
                connectedPort=(Port)o2;
                connectedRelation=(Relation)o1;
              }
              if (connectedPort != null && connectedRelation != null) {
                int index=connectedPort.linkedRelationList().indexOf(relation);
                if (index >= 0) {
                  util._removeRelation(relation,parent);
                  util._performChangeRequest(parent);
                  util._linkPort(connectedPort.getName(parent),"String_Node_Str",connectedRelation.getName(),index);
                  util._performChangeRequest(parent);
                }
              }
            }
          }
        }
      }
    }
  }
  util._performChangeRequest(parent);
}