Program: 74087
Project: 74087
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) get 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, as Diva lacks certain concepts that are relevant for a proper layout, as for example exact port locations for considering port constraints in the model, supported by Kieler.
 * @param composite the GraphModel composite object to retrieve the model information from
 * @param boxLayoutNode Kieler subgraph to receive all unconnected model elements
 * @param hierarchicalLayoutNode Kieler subgraph to receive all connected model elements
 */
private void _createGraph(Object composite,KNode hierarchicalLayoutNode,KNode boxLayoutNode){
  _ptolemy2KielerNodes=new HashMap<Object,KNode>();
  _kieler2ptolemyDivaNodes=new HashMap<KNode,Object>();
  _kieler2ptolemyEntityNodes=new HashMap<KNode,NamedObj>();
  _ptolemyDiva2KielerEdges=new HashMap<Object,KEdge>();
  _kieler2PtolemyDivaEdges=new HashMap<KEdge,Object>();
  _ptolemy2KielerPorts=new HashMap<Port,List<KPort>>();
  _kieler2PtolemyPorts=new HashMap<KPort,Port>();
  _divaEdgeSource=new HashMap<Object,Object>();
  _divaEdgeTarget=new HashMap<Object,Object>();
  float globalX=Float.MAX_VALUE, globalY=Float.MAX_VALUE;
  LayoutTarget target=this.getLayoutTarget();
  GraphModel graph=target.getGraphModel();
  if (graph instanceof ActorGraphModel) {
    ActorGraphModel aGraph=(ActorGraphModel)graph;
    for (Iterator iterator=aGraph.nodes(composite); iterator.hasNext(); ) {
      Object node=iterator.next();
      Object semanticNode=aGraph.getSemanticObject(node);
      if (semanticNode instanceof Actor || semanticNode instanceof Attribute) {
        KNode knode=_createKNode(node,semanticNode);
        if (PtolemyModelUtil._isConnected((NamedObj)semanticNode)) {
          knode.setParent(hierarchicalLayoutNode);
          KShapeLayout layout=KimlLayoutUtil.getShapeLayout(knode);
          if (layout.getXpos() < globalX)           globalX=layout.getXpos();
          if (layout.getYpos() < globalY)           globalY=layout.getYpos();
        }
 else {
          knode.setParent(boxLayoutNode);
        }
        if (semanticNode instanceof Actor && semanticNode instanceof Entity) {
          Actor actor=(Actor)semanticNode;
          List<Port> inputs=actor.inputPortList();
          List<Port> outputs=actor.outputPortList();
          _createKPorts(knode,inputs,KPortType.INPUT);
          _createKPorts(knode,outputs,KPortType.OUTPUT);
        }
      }
 else       if (semanticNode instanceof Relation) {
        KNode kVertexNode=_createKNodeForVertex((Vertex)node);
        kVertexNode.setParent(hierarchicalLayoutNode);
        _ptolemy2KielerNodes.put(node,kVertexNode);
        _kieler2ptolemyDivaNodes.put(kVertexNode,node);
        _kieler2ptolemyEntityNodes.put(kVertexNode,(NamedObj)semanticNode);
      }
 else       if (semanticNode instanceof Port) {
        KNode kPortNode=_createKNodeForPort((Port)semanticNode);
        kPortNode.setParent(hierarchicalLayoutNode);
        _ptolemy2KielerNodes.put(node,kPortNode);
        _kieler2ptolemyDivaNodes.put(kPortNode,node);
        _kieler2ptolemyEntityNodes.put(kPortNode,(NamedObj)semanticNode);
      }
      Iterator portIter=null;
      if (semanticNode instanceof Relation) {
        List relations=new ArrayList();
        relations.add(node);
        portIter=relations.iterator();
      }
 else       if (semanticNode instanceof Actor) {
        portIter=aGraph.nodes(node);
      }
      if (portIter != null) {
        for (; portIter.hasNext(); ) {
          Object divaPort=portIter.next();
          for (Iterator iterator3=aGraph.outEdges(divaPort); iterator3.hasNext(); ) {
            Object divaEdge=iterator3.next();
            _ptolemyDiva2KielerEdges.put(divaEdge,null);
          }
        }
      }
    }
    _storeEndpoints();
    for (    Object divaEdge : _ptolemyDiva2KielerEdges.keySet()) {
      _createKEdge(divaEdge);
    }
  }
  KShapeLayout layout=KimlLayoutUtil.getShapeLayout(hierarchicalLayoutNode);
  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) get 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, as Diva lacks certain concepts that are relevant for a proper layout, as for example exact port locations for considering port constraints in the model, supported by Kieler.
 * @param composite the GraphModel composite object to retrieve the model information from
 * @param boxLayoutNode Kieler subgraph to receive all unconnected model elements
 * @param hierarchicalLayoutNode Kieler subgraph to receive all connected model elements
 */
private void _createGraph(Object composite,KNode hierarchicalLayoutNode,KNode boxLayoutNode){
  _ptolemy2KielerNodes=new HashMap<Object,KNode>();
  _kieler2ptolemyDivaNodes=new HashMap<KNode,Object>();
  _kieler2ptolemyEntityNodes=new HashMap<KNode,NamedObj>();
  _ptolemyDiva2KielerEdges=new HashMap<Object,KEdge>();
  _kieler2PtolemyDivaEdges=new HashMap<KEdge,Object>();
  _ptolemy2KielerPorts=new HashMap<Port,List<KPort>>();
  _kieler2PtolemyPorts=new HashMap<KPort,Port>();
  _divaEdgeSource=new HashMap<Object,Object>();
  _divaEdgeTarget=new HashMap<Object,Object>();
  float globalX=Float.MAX_VALUE, globalY=Float.MAX_VALUE;
  LayoutTarget target=this.getLayoutTarget();
  GraphModel graph=target.getGraphModel();
  if (graph instanceof ActorGraphModel) {
    ActorGraphModel aGraph=(ActorGraphModel)graph;
    for (Iterator iterator=aGraph.nodes(composite); iterator.hasNext(); ) {
      Object node=iterator.next();
      Object semanticNode=aGraph.getSemanticObject(node);
      if (semanticNode instanceof Actor || semanticNode instanceof Attribute) {
        KNode knode=_createKNode(node,semanticNode);
        if (PtolemyModelUtil._isConnected((NamedObj)semanticNode)) {
          knode.setParent(hierarchicalLayoutNode);
          KShapeLayout layout=KimlLayoutUtil.getShapeLayout(knode);
          if (layout.getXpos() < globalX)           globalX=layout.getXpos();
          if (layout.getYpos() < globalY)           globalY=layout.getYpos();
        }
 else {
          knode.setParent(boxLayoutNode);
        }
        if (semanticNode instanceof Actor && semanticNode instanceof Entity) {
          Actor actor=(Actor)semanticNode;
          List<Port> inputs=actor.inputPortList();
          List<Port> outputs=actor.outputPortList();
          _createKPorts(knode,inputs,KPortType.INPUT);
          _createKPorts(knode,outputs,KPortType.OUTPUT);
        }
      }
 else       if (semanticNode instanceof Relation) {
        KNode kVertexNode=_createKNodeForVertex((Vertex)node);
        kVertexNode.setParent(hierarchicalLayoutNode);
        _ptolemy2KielerNodes.put(node,kVertexNode);
        _kieler2ptolemyDivaNodes.put(kVertexNode,node);
        _kieler2ptolemyEntityNodes.put(kVertexNode,(NamedObj)semanticNode);
      }
 else       if (semanticNode instanceof Port) {
        KNode kPortNode=_createKNodeForPort((Port)semanticNode);
        kPortNode.setParent(hierarchicalLayoutNode);
        _ptolemy2KielerNodes.put(node,kPortNode);
        _kieler2ptolemyDivaNodes.put(kPortNode,node);
        _kieler2ptolemyEntityNodes.put(kPortNode,(NamedObj)semanticNode);
      }
      Iterator portIter=null;
      List portList=new ArrayList();
      if (semanticNode instanceof Relation) {
        portList.add(node);
        portIter=portList.iterator();
      }
 else       if (semanticNode instanceof Actor) {
        portIter=aGraph.nodes(node);
      }
 else       if (semanticNode instanceof Port) {
        portList.add(node);
        portIter=portList.iterator();
      }
      if (portIter != null) {
        for (; portIter.hasNext(); ) {
          Object divaPort=portIter.next();
          Iterator edgeIterator=aGraph.outEdges(divaPort);
          if (semanticNode instanceof Port) {
            edgeIterator=aGraph.getExternalPortModel().outEdges(node);
          }
          for (; edgeIterator.hasNext(); ) {
            Object divaEdge=edgeIterator.next();
            _ptolemyDiva2KielerEdges.put(divaEdge,null);
          }
        }
      }
    }
    _storeEndpoints();
    for (    Object divaEdge : _ptolemyDiva2KielerEdges.keySet()) {
      _createKEdge(divaEdge);
    }
  }
  KShapeLayout layout=KimlLayoutUtil.getShapeLayout(hierarchicalLayoutNode);
  layout.setXpos(globalX);
  layout.setYpos(globalY);
}