Program: 74064
Project: 74064
Buggy source:
/** 
 * Create a Kieler KPort corresponding to a Ptolemy Port. Set the size and position (relative to parent) and the direction of the port in the KPort layout information. As Kieler does not explicitly support multiports as Ptolemy, this gets emulated by creating multiple distinct ports with a little offset each. Create only one node. For multiports call this method multiple times with changed parameters. The newly created port is stored with the corresponding ptolemy port in the global maps _kieler2PtolemyPorts, _ptolemy2KielerPorts, such that the  {@link #_applyLayout(KNode)} method will be able to reapply the layout.
 * @param knode The parent KNode of the new port
 * @param portType The port Type, either input or output
 * @param port The corresponding Ptolemy port (might be a multiport)
 * @param rank The rank of the new port which is an ordering index. If thisis not set, Kieler will try to infer the ranks automatically from the port's position.
 * @param index Index of the KPort corresponding to a multiport
 * @param maxIndex Width of the multiport, i.e. the number of connected edges tothat port.
 * @param size Custom size (same for width and height) for a port that willbe used instead of the real Ptolemy port size. If this value is negative, the original Ptolemy sizes are used.
 */
private void _createKPort(KNode knode,KPortType portType,Port port,int rank,int index,int maxIndex,float size){
  KPort kport=KimlLayoutUtil.createInitializedPort();
  KShapeLayout kportlayout=KimlLayoutUtil.getShapeLayout(kport);
  kportlayout.setXpos(0);
  kportlayout.setYpos(0);
  kportlayout.setHeight(5);
  kportlayout.setWidth(5);
  knode.getPorts().add(kport);
  kport.setType(portType);
  if (rank != NO_RANK) {
    LayoutOptions.setPortRank(kportlayout,rank);
  }
  float offsetX=0, offsetY=0;
  int direction=IOPortController.getDirection(IOPortController.getCardinality(port));
switch (direction) {
case SwingConstants.NORTH:
    LayoutOptions.setPortSide(kportlayout,PortSide.NORTH);
  offsetX=-((maxIndex - index) * MULTIPORT_OFFSET);
break;
case SwingConstants.EAST:
LayoutOptions.setPortSide(kportlayout,PortSide.EAST);
offsetY=index * MULTIPORT_OFFSET;
break;
case SwingConstants.SOUTH:
LayoutOptions.setPortSide(kportlayout,PortSide.SOUTH);
offsetX=-(index * MULTIPORT_OFFSET);
break;
default :
LayoutOptions.setPortSide(kportlayout,PortSide.WEST);
offsetY=-((maxIndex - index) * MULTIPORT_OFFSET);
break;
}
Object portObject=this.getLayoutTarget().getVisualObject(port);
if (portObject instanceof PortTerminal) {
PortTerminal portFigure=(PortTerminal)portObject;
Rectangle2D portBounds=portFigure.getBounds();
CanvasComponent parent=portFigure.getParent();
if (parent instanceof CompositeFigure) {
CompositeFigure parentFigure=(CompositeFigure)parent;
AffineTransform parentTransform=parentFigure.getTransformContext().getTransform();
Point2D.Double portLocation=new Point2D.Double(portBounds.getMinX(),portBounds.getMinY());
Point2D.Double transformedLocation=new Point2D.Double();
parentTransform.transform(portLocation,transformedLocation);
double w=portBounds.getWidth();
double h=portBounds.getHeight();
double x=transformedLocation.getX() - parentFigure.getBounds().getMinX() + offsetX;
double y=transformedLocation.getY() - parentFigure.getBounds().getMinY() + offsetY;
kportlayout.setXpos((float)x);
kportlayout.setYpos((float)y);
if (size < 0) {
kportlayout.setWidth((float)w);
kportlayout.setHeight((float)h);
}
 else {
Rectangle2D newPortBounds=new Rectangle2D.Double();
newPortBounds.setRect(x,y,w,h);
Rectangle2D shrunkPortBounds=new Rectangle2D.Double();
shrunkPortBounds.setRect(x,y,size,size);
Point2D shrunkenLocation=KielerGraphUtil._shrinkCoordinates(newPortBounds,shrunkPortBounds,direction);
kportlayout.setXpos((float)shrunkenLocation.getX());
kportlayout.setYpos((float)shrunkenLocation.getY());
kportlayout.setWidth(size);
kportlayout.setHeight(size);
}
}
}
_kieler2PtolemyPorts.put(kport,port);
List<KPort> kports=_ptolemy2KielerPorts.get(port);
if (kports == null) {
kports=new ArrayList<KPort>();
_ptolemy2KielerPorts.put(port,kports);
}
kports.add(kport);
}

Fixed version (ground truth):
/** 
 * Create a Kieler KPort corresponding to a Ptolemy Port. Set the size and position (relative to parent) and the direction of the port in the KPort layout information. As Kieler does not explicitly support multiports as Ptolemy, this gets emulated by creating multiple distinct ports with a little offset each. Create only one node. For multiports call this method multiple times with changed parameters. The newly created port is stored with the corresponding ptolemy port in the global maps _kieler2PtolemyPorts, _ptolemy2KielerPorts, such that the  {@link #_applyLayout(KNode)} method will be able to reapply the layout.
 * @param knode The parent KNode of the new port
 * @param portType The port Type, either input or output
 * @param port The corresponding Ptolemy port (might be a multiport)
 * @param rank The rank of the new port which is an ordering index. If thisis not set, Kieler will try to infer the ranks automatically from the port's position.
 * @param index Index of the KPort corresponding to a multiport
 * @param maxIndex Width of the multiport, i.e. the number of connected edges tothat port.
 * @param size Custom size (same for width and height) for a port that willbe used instead of the real Ptolemy port size. If this value is negative, the original Ptolemy sizes are used.
 */
private void _createKPort(KNode knode,KPortType portType,Port port,int rank,int index,int maxIndex,float size){
  KPort kport=KimlLayoutUtil.createInitializedPort();
  KShapeLayout kportlayout=KimlLayoutUtil.getShapeLayout(kport);
  kportlayout.setXpos(0);
  kportlayout.setYpos(0);
  kportlayout.setHeight(5);
  kportlayout.setWidth(5);
  knode.getPorts().add(kport);
  kport.setType(portType);
  if (rank != NO_RANK) {
    LayoutOptions.setPortRank(kportlayout,rank);
  }
  float offsetX=0, offsetY=0;
  int direction=IOPortController.getDirection(IOPortController.getCardinality(port));
switch (direction) {
case SwingConstants.NORTH:
    LayoutOptions.setPortSide(kportlayout,PortSide.NORTH);
  offsetX=-((maxIndex - index) * MULTIPORT_OFFSET);
break;
case SwingConstants.EAST:
LayoutOptions.setPortSide(kportlayout,PortSide.EAST);
offsetY=index * MULTIPORT_OFFSET;
break;
case SwingConstants.SOUTH:
LayoutOptions.setPortSide(kportlayout,PortSide.SOUTH);
offsetX=(index * MULTIPORT_OFFSET);
break;
default :
LayoutOptions.setPortSide(kportlayout,PortSide.WEST);
offsetY=-((maxIndex - index) * MULTIPORT_OFFSET);
break;
}
Object portObject=this.getLayoutTarget().getVisualObject(port);
if (portObject instanceof PortTerminal) {
PortTerminal portFigure=(PortTerminal)portObject;
Rectangle2D portBounds=portFigure.getBounds();
CanvasComponent parent=portFigure.getParent();
if (parent instanceof CompositeFigure) {
CompositeFigure parentFigure=(CompositeFigure)parent;
AffineTransform parentTransform=parentFigure.getTransformContext().getTransform();
Point2D.Double portLocation=new Point2D.Double(portBounds.getMinX(),portBounds.getMinY());
Point2D.Double transformedLocation=new Point2D.Double();
parentTransform.transform(portLocation,transformedLocation);
double w=portBounds.getWidth();
double h=portBounds.getHeight();
double x=transformedLocation.getX() - parentFigure.getBounds().getMinX() + offsetX;
double y=transformedLocation.getY() - parentFigure.getBounds().getMinY() + offsetY;
kportlayout.setXpos((float)x);
kportlayout.setYpos((float)y);
if (size < 0) {
kportlayout.setWidth((float)w);
kportlayout.setHeight((float)h);
}
 else {
Rectangle2D newPortBounds=new Rectangle2D.Double();
newPortBounds.setRect(x,y,w,h);
Rectangle2D shrunkPortBounds=new Rectangle2D.Double();
shrunkPortBounds.setRect(x,y,size,size);
Point2D shrunkenLocation=KielerGraphUtil._shrinkCoordinates(newPortBounds,shrunkPortBounds,direction,MULTIPORT_BOTTOM);
kportlayout.setXpos((float)shrunkenLocation.getX());
kportlayout.setYpos((float)shrunkenLocation.getY());
kportlayout.setWidth(size);
kportlayout.setHeight(size);
}
}
}
_kieler2PtolemyPorts.put(kport,port);
List<KPort> kports=_ptolemy2KielerPorts.get(port);
if (kports == null) {
kports=new ArrayList<KPort>();
_ptolemy2KielerPorts.put(port,kports);
}
kports.add(kport);
}