return createDiff(a, b, pairs);
}
private static InputGraph createDiff(InputGraph a, InputGraph b, Set<Pair> pairs) {
Group g = new Group();
g.setMethod(a.getGroup().getMethod());
g.setAssembly(a.getGroup().getAssembly());
g.getProperties().setProperty("name", "Difference");
InputGraph graph = new InputGraph(g, null);
graph.setName(a.getName() + ", " + b.getName());
graph.setIsDifferenceGraph(true);
Set<InputNode> nodesA = new HashSet<InputNode>(a.getNodes());
Set<InputNode> nodesB = new HashSet<InputNode>(b.getNodes());
Map<InputNode, InputNode> inputNodeMap = new HashMap<InputNode, InputNode>();
for (Pair p : pairs) {
InputNode n = p.getN1();
assert nodesA.contains(n);
InputNode nB = p.getN2();
assert nodesB.contains(nB);
nodesA.remove(n);
nodesB.remove(nB);
InputNode n2 = new InputNode(n);
inputNodeMap.put(n, n2);
inputNodeMap.put(nB, n2);
graph.addNode(n2);
markAsChanged(n2, n, nB);
}
for (InputNode n : nodesA) {
InputNode n2 = new InputNode(n);
graph.addNode(n2);
markAsNew(n2);
inputNodeMap.put(n, n2);
}
for (InputNode n : nodesB) {
InputNode n2 = new InputNode(n);
n2.setId(-n2.getId());
graph.addNode(n2);
markAsDeleted(n2);
inputNodeMap.put(n, n2);
}
Collection<InputEdge> edgesA = a.getEdges();
Collection<InputEdge> edgesB = b.getEdges();
Set<InputEdge> newEdges = new HashSet<InputEdge>();
for (InputEdge e : edgesA) {
int from = e.getFrom();
int to = e.getTo();
InputNode nodeFrom = inputNodeMap.get(a.getNode(from));
InputNode nodeTo = inputNodeMap.get(a.getNode(to));
char index = e.getToIndex();
InputEdge newEdge = new InputEdge(index, nodeFrom.getId(), nodeTo.getId());
if (!newEdges.contains(newEdge)) {
markAsNew(newEdge);
newEdges.add(newEdge);
graph.addEdge(newEdge);
}
}
for (InputEdge e : edgesB) {
int from = e.getFrom();
int to = e.getTo();
InputNode nodeFrom = inputNodeMap.get(b.getNode(from));
InputNode nodeTo = inputNodeMap.get(b.getNode(to));
char index = e.getToIndex();
InputEdge newEdge = new InputEdge(index, nodeFrom.getId(), nodeTo.getId());
if (!newEdges.contains(newEdge)) {
markAsDeleted(newEdge);
newEdges.add(newEdge);
graph.addEdge(newEdge);
} else {
newEdges.remove(newEdge);
graph.removeEdge(newEdge);
markAsSame(newEdge);
newEdges.add(newEdge);
graph.addEdge(newEdge);
}
}
g.addGraph(graph);
return graph;
}