Program: 73267
Project: 73267
Buggy source:
/** 
 * Create a KIELER edge for a Ptolemy Diva edge object. The KEdge will be setup between either two ports or relation vertices or mixed. Hence the KEdge corresponds more likely to a Ptolemy link than a relation. Diva edges have no direction related to the flow of data in Ptolemy. However, KIELER uses a directed graph to perform layout and so a meaningful direction should be set in the KEdge. This direction will be approximated by doing a tree search beginning on both endpoints of the Diva edge. Whenever either of the endpoints is connected to a source port, this will be the source of the KEdge and determine its direction. The newly created edge is stored with the corresponding Diva edge in the global maps _ptolemyDiva2KielerEdges, _kieler2PtolemyDivaEdges, such that the   {@link #_applyLayout(KNode)} method will be able to reapply thelayout.
 * @param divaEdge The Ptolemy diva edge object for which to create a new KEdge.
 */
private void _createKEdge(Link divaEdge){
  KEdge kedge=KimlUtil.createInitializedEdge();
  Object source=_divaEdgeSource.get(divaEdge);
  if (source == null) {
    source=divaEdge.getTail();
  }
  Object target=_divaEdgeTarget.get(divaEdge);
  if (target == null) {
    target=divaEdge.getHead();
  }
  KPort kSourcePort=_getPort(source,PortType.OUTPUT,divaEdge.getRelation());
  if (kSourcePort != null) {
    kedge.setSourcePort(kSourcePort);
    kSourcePort.getEdges().add(kedge);
    kedge.setSource(kSourcePort.getNode());
  }
 else {
    kedge.setSource(_kieler2ptolemyDivaNodes.inverse().get(source));
  }
  KPort kTargetPort=_getPort(target,PortType.INPUT,divaEdge.getRelation());
  if (kTargetPort != null) {
    kedge.setTargetPort(kTargetPort);
    kTargetPort.getEdges().add(kedge);
    kedge.setTarget(kTargetPort.getNode());
  }
 else {
    kedge.setTarget(_kieler2ptolemyDivaNodes.inverse().get(target));
  }
  _edgeList.add(new Pair<KEdge,Link>(kedge,divaEdge));
  Object figure=getLayoutTarget().getVisualObject(divaEdge);
  if (figure instanceof AbstractConnector) {
    LabelFigure labelFigure=((AbstractConnector)figure).getLabelFigure();
    if (labelFigure != null) {
      KLabel label=KimlUtil.createInitializedLabel(kedge);
      label.setText(labelFigure.getString());
      KShapeLayout labelLayout=label.getData(KShapeLayout.class);
      labelLayout.setProperty(LayoutOptions.EDGE_LABEL_PLACEMENT,EdgeLabelPlacement.CENTER);
      Rectangle2D bounds=labelFigure.getBounds();
      labelLayout.setWidth((float)bounds.getWidth());
      labelLayout.setHeight((float)bounds.getHeight());
    }
  }
}

Fixed version (ground truth):
/** 
 * Create a KIELER edge for a Ptolemy Diva edge object. The KEdge will be setup between either two ports or relation vertices or mixed. Hence the KEdge corresponds more likely to a Ptolemy link than a relation. Diva edges have no direction related to the flow of data in Ptolemy. However, KIELER uses a directed graph to perform layout and so a meaningful direction should be set in the KEdge. This direction will be approximated by doing a tree search beginning on both endpoints of the Diva edge. Whenever either of the endpoints is connected to a source port, this will be the source of the KEdge and determine its direction. The newly created edge is stored with the corresponding Diva edge in the global maps _ptolemyDiva2KielerEdges, _kieler2PtolemyDivaEdges, such that the   {@link #_applyLayout(KNode)} method will be able to reapply thelayout.
 * @param divaEdge The Ptolemy diva edge object for which to create a new KEdge.
 */
private void _createKEdge(Link divaEdge){
  KEdge kedge=KimlUtil.createInitializedEdge();
  Object source=_divaEdgeSource.get(divaEdge);
  if (source == null) {
    source=divaEdge.getTail();
  }
  Object target=_divaEdgeTarget.get(divaEdge);
  if (target == null) {
    target=divaEdge.getHead();
  }
  KPort kSourcePort=_getPort(source,PortType.OUTPUT,divaEdge.getRelation());
  if (kSourcePort != null) {
    kedge.setSourcePort(kSourcePort);
    kSourcePort.getEdges().add(kedge);
    kedge.setSource(kSourcePort.getNode());
  }
 else {
    kedge.setSource(_kieler2ptolemyDivaNodes.inverse().get(source));
  }
  KPort kTargetPort=_getPort(target,PortType.INPUT,divaEdge.getRelation());
  if (kTargetPort != null) {
    kedge.setTargetPort(kTargetPort);
    kTargetPort.getEdges().add(kedge);
    kedge.setTarget(kTargetPort.getNode());
  }
 else {
    kedge.setTarget(_kieler2ptolemyDivaNodes.inverse().get(target));
  }
  KEdgeLayout edgeLayout=kedge.getData(KEdgeLayout.class);
  if (source instanceof Locatable) {
    double[] pos=((Locatable)source).getLocation();
    edgeLayout.getSourcePoint().setX((float)pos[0]);
    edgeLayout.getSourcePoint().setY((float)pos[1]);
  }
  if (target instanceof Locatable) {
    double[] pos=((Locatable)target).getLocation();
    edgeLayout.getTargetPoint().setX((float)pos[0]);
    edgeLayout.getTargetPoint().setY((float)pos[1]);
  }
  _edgeList.add(new Pair<KEdge,Link>(kedge,divaEdge));
  Object figure=getLayoutTarget().getVisualObject(divaEdge);
  if (figure instanceof AbstractConnector) {
    LabelFigure labelFigure=((AbstractConnector)figure).getLabelFigure();
    if (labelFigure != null) {
      KLabel label=KimlUtil.createInitializedLabel(kedge);
      label.setText(labelFigure.getString());
      KShapeLayout labelLayout=label.getData(KShapeLayout.class);
      labelLayout.setProperty(LayoutOptions.EDGE_LABEL_PLACEMENT,EdgeLabelPlacement.CENTER);
      Rectangle2D bounds=labelFigure.getBounds();
      labelLayout.setWidth((float)bounds.getWidth());
      labelLayout.setHeight((float)bounds.getHeight());
      labelLayout.setXpos((edgeLayout.getSourcePoint().getX() + edgeLayout.getTargetPoint().getX()) / 2);
      labelLayout.setYpos((edgeLayout.getSourcePoint().getY() + edgeLayout.getTargetPoint().getY()) / 2);
      kedge.getLabels().add(label);
    }
  }
}