GModelUtils { public static List<GConnection> copyConnections(final Map<GNode, GNode> copies) { final Map<GConnection, GConnection> copiedConnections = new HashMap<>(); for (final GNode node : copies.keySet()) { final GNode copy = copies.get(node); for (final GConnector connector : node.getConnectors()) { final int connectorIndex = node.getConnectors().indexOf(connector); final GConnector copiedConnector = copy.getConnectors().get(connectorIndex); copiedConnector.getConnections().clear(); for (final GConnection connection : connector.getConnections()) { final GNode opposingNode = getOpposingNode(connector, connection); final boolean opposingNodePresent = copies.containsKey(opposingNode); if (opposingNodePresent) { final GConnection copiedConnection; if (!copiedConnections.containsKey(connection)) { copiedConnection = EcoreUtil.copy(connection); copiedConnections.put(connection, copiedConnection); } else { copiedConnection = copiedConnections.get(connection); } if (connection.getSource().equals(connector)) { copiedConnection.setSource(copiedConnector); } else { copiedConnection.setTarget(copiedConnector); } copiedConnector.getConnections().add(copiedConnection); } } } } return new ArrayList<GConnection>(copiedConnections.values()); } private  GModelUtils(); static List<GConnection> copyConnections(final Map<GNode, GNode> copies); static GNode getOpposingNode(final GConnector connector, final GConnection connection); static List<GJoint> getAllJoints(final GModel model);  }
@Test public void copyConnections() { final List<GNode> nodes = createNodes(); final List<GNode> copies = createNodes(); connect(nodes.get(0).getConnectors().get(1), nodes.get(1).getConnectors().get(0)); final Map<GNode, GNode> map = new HashMap<>(); for (int i = 0; i < 3; i++) { map.put(nodes.get(i), copies.get(i)); } final List<GConnection> connections = GModelUtils.copyConnections(map); assertTrue(connections.size() == 1); assertTrue(copies.get(0).getConnectors().get(1).getConnections().size() == 1); final GConnection newConnection = copies.get(0).getConnectors().get(1).getConnections().get(0); assertTrue(copies.get(1).getConnectors().get(0).getConnections().size() == 1); assertEquals(copies.get(1).getConnectors().get(0).getConnections().get(0), newConnection); assertEquals(copies.get(0).getConnectors().get(1), newConnection.getSource()); assertEquals(copies.get(1).getConnectors().get(0), newConnection.getTarget()); assertTrue(copies.get(0).getConnectors().get(0).getConnections().isEmpty()); assertTrue(copies.get(1).getConnectors().get(1).getConnections().isEmpty()); assertTrue(copies.get(2).getConnectors().get(0).getConnections().isEmpty()); assertTrue(copies.get(2).getConnectors().get(1).getConnections().isEmpty()); }