Program: 73268
Project: 73268
Buggy source:
/** 
 * Creates a graph for the KIELER API from a Ptolemy model. Will traverse the low level GraphModel given by the composite and record all found elements in the mapping fields of this object that keep a mapping between Ptolemy/Diva objects and KIELER objects. New KIELER objects (KEdge, KNode, KPort) are created for their respective Ptolemy counterparts and initialized with the initial sizes and positions and are put in a composite KNode (the graph KIELER will perform the layout on later). To obtain the right mappings, multiple abstraction levels of Ptolemy are considered here: Diva, as this was the intended original way to do automatic layout (e.g. by GlobalAbstractLayout) and Ptolemy, since Diva lacks certain concepts that are relevant for a proper layout, such as exact port locations for considering port constraints in the model, which are supported by KIELER.
 * @param composite The GraphModel composite object to retrieve the modelinformation from
 * @param parentNode KIELER subgraph to receive all connectedmodel elements
 */
private void _createGraph(Object composite,KNode parentNode){
  _kieler2ptolemyDivaNodes=HashBiMap.create();
  _ptolemy2KielerPorts=LinkedListMultimap.create();
  _divaEdgeSource=Maps.newHashMap();
  _divaEdgeTarget=Maps.newHashMap();
  _edgeList=Lists.newLinkedList();
  boolean doBoxLayout=_layoutOptions.getProperty(Options.DECORATIONS);
  float globalX=Float.MAX_VALUE, globalY=Float.MAX_VALUE;
  GraphModel graphModel=getLayoutTarget().getGraphModel();
  ExternalPortModel externalPortModel=null;
  if (graphModel instanceof ActorGraphModel) {
    externalPortModel=((ActorGraphModel)graphModel).getExternalPortModel();
  }
  List<Link> unprocessedEdges=new LinkedList<Link>();
  List<NamedObj> unprocessedRelatives=new LinkedList<NamedObj>();
  for (Iterator iterator=graphModel.nodes(composite); iterator.hasNext(); ) {
    Object node=iterator.next();
    if (!(node instanceof Locatable)) {
      continue;
    }
    Iterator portIter=null;
    NamedObj semanticNode=(NamedObj)graphModel.getSemanticObject(node);
    if (doBoxLayout || PtolemyModelUtil._isConnected(semanticNode)) {
      KNode knode=null;
      if (semanticNode instanceof Actor || semanticNode instanceof Attribute) {
        knode=_createKNode(node,semanticNode);
        if (semanticNode instanceof Actor && semanticNode instanceof Entity) {
          Actor actor=(Actor)semanticNode;
          List<Port> inputs=actor.inputPortList();
          List<Port> outputs=actor.outputPortList();
          _createKPorts(knode,inputs,PortType.INPUT);
          _createKPorts(knode,outputs,PortType.OUTPUT);
          portIter=graphModel.nodes(node);
        }
 else         if (semanticNode instanceof RelativeLocatable) {
          unprocessedRelatives.add(semanticNode);
        }
      }
 else       if (semanticNode instanceof Relation) {
        knode=_createKNodeForVertex((Vertex)node);
        portIter=Iterators.singletonIterator(node);
      }
 else       if (semanticNode instanceof ComponentPort) {
        knode=_createKNodeForPort(node,(ComponentPort)semanticNode);
        portIter=Iterators.singletonIterator(node);
      }
 else       if (semanticNode instanceof State) {
        knode=_createKNodeForState(node,(State)semanticNode);
        portIter=Iterators.singletonIterator(node);
      }
      if (knode != null) {
        knode.setParent(parentNode);
        KShapeLayout layout=knode.getData(KShapeLayout.class);
        if (layout.getXpos() < globalX) {
          globalX=layout.getXpos();
        }
        if (layout.getYpos() < globalY) {
          globalY=layout.getYpos();
        }
        _kieler2ptolemyDivaNodes.put(knode,(Locatable)node);
      }
    }
    if (portIter != null) {
      while (portIter.hasNext()) {
        Object divaPort=portIter.next();
        Iterator edgeIterator;
        if (semanticNode instanceof Port && externalPortModel != null) {
          edgeIterator=externalPortModel.outEdges(divaPort);
        }
 else {
          edgeIterator=graphModel.outEdges(divaPort);
        }
        while (edgeIterator.hasNext()) {
          Object next=edgeIterator.next();
          if (next instanceof Link) {
            unprocessedEdges.add((Link)next);
          }
        }
      }
    }
  }
  if (graphModel instanceof ActorGraphModel) {
    _storeEndpoints(unprocessedEdges);
  }
  for (  Link divaEdge : unprocessedEdges) {
    _createKEdge(divaEdge);
  }
  for (  NamedObj relativeObj : unprocessedRelatives) {
    Locatable source=PtolemyModelUtil._getLocation(relativeObj);
    if (source instanceof RelativeLocation) {
      NamedObj referenceObj=PtolemyModelUtil._getReferencedObj((RelativeLocation)source);
      if (referenceObj != null) {
        Locatable target=PtolemyModelUtil._getLocation(referenceObj);
        _createKEdgeForAttribute(source,target);
      }
    }
  }
  KShapeLayout layout=parentNode.getData(KShapeLayout.class);
  layout.setXpos(globalX);
  layout.setYpos(globalY);
}

Fixed version (ground truth):
/** 
 * Creates a graph for the KIELER API from a Ptolemy model. Will traverse the low level GraphModel given by the composite and record all found elements in the mapping fields of this object that keep a mapping between Ptolemy/Diva objects and KIELER objects. New KIELER objects (KEdge, KNode, KPort) are created for their respective Ptolemy counterparts and initialized with the initial sizes and positions and are put in a composite KNode (the graph KIELER will perform the layout on later). To obtain the right mappings, multiple abstraction levels of Ptolemy are considered here: Diva, as this was the intended original way to do automatic layout (e.g. by GlobalAbstractLayout) and Ptolemy, since Diva lacks certain concepts that are relevant for a proper layout, such as exact port locations for considering port constraints in the model, which are supported by KIELER.
 * @param composite The GraphModel composite object to retrieve the modelinformation from
 * @param parentNode KIELER subgraph to receive all connectedmodel elements
 */
private void _createGraph(Object composite,KNode parentNode){
  _kieler2ptolemyDivaNodes=HashBiMap.create();
  _ptolemy2KielerPorts=LinkedListMultimap.create();
  _divaEdgeSource=Maps.newHashMap();
  _divaEdgeTarget=Maps.newHashMap();
  _edgeList=Lists.newLinkedList();
  boolean doBoxLayout=_layoutOptions.getProperty(Options.DECORATIONS);
  float globalX=Float.MAX_VALUE, globalY=Float.MAX_VALUE;
  GraphModel graphModel=getLayoutTarget().getGraphModel();
  ExternalPortModel externalPortModel=null;
  if (graphModel instanceof ActorGraphModel) {
    externalPortModel=((ActorGraphModel)graphModel).getExternalPortModel();
  }
  List<Link> unprocessedEdges=new LinkedList<Link>();
  List<NamedObj> unprocessedRelatives=new LinkedList<NamedObj>();
  for (Iterator iterator=graphModel.nodes(composite); iterator.hasNext(); ) {
    Object node=iterator.next();
    if (!(node instanceof Locatable)) {
      continue;
    }
    Iterator portIter=null;
    NamedObj semanticNode=(NamedObj)graphModel.getSemanticObject(node);
    if (doBoxLayout || PtolemyModelUtil._isConnected(semanticNode)) {
      KNode knode=null;
      if (semanticNode instanceof Actor || semanticNode instanceof Attribute) {
        knode=_createKNode(node,semanticNode);
        if (semanticNode instanceof Actor && semanticNode instanceof Entity) {
          Actor actor=(Actor)semanticNode;
          List<Port> inputs=actor.inputPortList();
          List<Port> outputs=actor.outputPortList();
          _createKPorts(knode,inputs,PortType.INPUT);
          _createKPorts(knode,outputs,PortType.OUTPUT);
          portIter=graphModel.nodes(node);
        }
 else         if (semanticNode instanceof RelativeLocatable) {
          unprocessedRelatives.add(semanticNode);
        }
      }
 else       if (semanticNode instanceof Relation) {
        knode=_createKNodeForVertex((Vertex)node);
        portIter=Iterators.singletonIterator(node);
      }
 else       if (semanticNode instanceof ComponentPort) {
        knode=_createKNodeForPort(node,(ComponentPort)semanticNode);
        portIter=Iterators.singletonIterator(node);
      }
 else       if (semanticNode instanceof State) {
        knode=_createKNodeForState(node,(State)semanticNode);
        portIter=Iterators.singletonIterator(node);
      }
      if (knode != null) {
        knode.setParent(parentNode);
        KShapeLayout layout=knode.getData(KShapeLayout.class);
        if (layout.getXpos() < globalX) {
          globalX=layout.getXpos();
        }
        if (layout.getYpos() < globalY) {
          globalY=layout.getYpos();
        }
        _kieler2ptolemyDivaNodes.put(knode,(Locatable)node);
      }
    }
    if (portIter != null) {
      while (portIter.hasNext()) {
        Object divaPort=portIter.next();
        Iterator edgeIterator;
        if (semanticNode instanceof Port && externalPortModel != null) {
          edgeIterator=externalPortModel.outEdges(divaPort);
        }
 else {
          edgeIterator=graphModel.outEdges(divaPort);
        }
        while (edgeIterator.hasNext()) {
          Object next=edgeIterator.next();
          if (next instanceof Link) {
            unprocessedEdges.add((Link)next);
          }
        }
      }
    }
  }
  if (graphModel instanceof ActorGraphModel) {
    _storeEndpoints(unprocessedEdges);
  }
  for (  Link divaEdge : unprocessedEdges) {
    _createKEdge(divaEdge);
  }
  for (  NamedObj relativeObj : unprocessedRelatives) {
    _createKEdgeForAttribute(relativeObj);
  }
  KShapeLayout layout=parentNode.getData(KShapeLayout.class);
  layout.setXpos(globalX);
  layout.setYpos(globalY);
}