/** * (Re-)Adds the ports to the node of a component, having at least one port with a non-loop edge. * The ports are added as the direct neighbor of a port they are connected to via an edge. * * @param component The component whose ports have to get re-added. */ public void addComponentWithNonLoopEdges(final ConnectedSelfLoopComponent component) { // the node we are working on final LNode node = component.getNode(); // Set of all ports of the components that are hidden. Initially all ports that CAN be hidden. final Set<LPort> hiddenPorts = Sets.newHashSet(component.getHidablePorts()); // Iterator holding all ports that already have a portSide specified. Initially these are // all ports with an non-loop edge connected to them. While processing the elements, we will // add new ports to this Iterator. So we need a ListIterator. final ListIterator<LPort> portsWithSideIter = Lists.newLinkedList(component.getNonLoopPorts()).listIterator(); while (portsWithSideIter.hasNext()) { final LPort portWithSide = portsWithSideIter.next(); if (portWithSide.getOutgoingEdges().isEmpty()) { // inbound port for (final LEdge edgeWithHiddenPort : portWithSide.getIncomingEdges()) { final LPort hiddenPort = edgeWithHiddenPort.getSource(); if (hiddenPorts.contains(hiddenPort)) { final ListIterator<LPort> nodePortIter = node.getPorts().listIterator(); LPort portOnNode = nodePortIter.next(); // find the port with side ... while (!portOnNode.equals(portWithSide)) { portOnNode = nodePortIter.next(); } // ... and add the hidden port on it's right side nodePortIter.add(hiddenPort); portsWithSideIter.add(hiddenPort); setSideOfPort(hiddenPort, portWithSide.getSide()); // ensure the next element will be our new added element portsWithSideIter.previous(); portsWithSideIter.previous(); hiddenPorts.remove(hiddenPort); } } } else { // outbound port for (final LEdge edgeWithHiddenPort : portWithSide.getOutgoingEdges()) { final LPort hiddenPort = edgeWithHiddenPort.getTarget(); if (hiddenPorts.contains(hiddenPort)) { final ListIterator<LPort> nodePortIter = node.getPorts().listIterator(); LPort portOnNode = nodePortIter.next(); // find the port with side ... while (!portOnNode.equals(portWithSide)) { portOnNode = nodePortIter.next(); } // ... and add the hidden port on it's left side nodePortIter.previous(); nodePortIter.add(hiddenPort); portsWithSideIter.add(hiddenPort); setSideOfPort(hiddenPort, portWithSide.getSide()); // ensure the next element will be our new added element portsWithSideIter.previous(); portsWithSideIter.previous(); hiddenPorts.remove(hiddenPort); } } } } }
/** * Removes all sides from the list of available sides, that already have an port places on it. * Use to make sure no loops will be places on sides with ports containing non-loop edges. */ public void removeOccupiedSides() { for (final LPort port : node.getPorts()) { loopSides.removeSide(LoopSide.fromPortSides(port.getSide())); } }