1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.tests;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
20 import org.simantics.scenegraph.INode;
21 import org.simantics.scenegraph.ParentNode;
22 import org.simantics.scenegraph.g2d.G2DParentNode;
23 import org.simantics.scenegraph.g2d.G2DSceneGraph;
24 import org.simantics.scenegraph.g2d.IG2DNode;
27 * @author Tuukka Lehtonen
29 public class SceneGraphComparator {
31 private Map<INode, INode> aChildrenDiffer = new HashMap<INode, INode>();
32 private Map<INode, INode> bChildrenDiffer = new HashMap<INode, INode>();
33 private Set<Pair<INode, INode>> different = new HashSet<Pair<INode, INode>>();
38 public SceneGraphComparator(INode a, INode b) {
43 public void analyze() {
44 analyze(a.getParent(), a, b.getParent(), b);
47 public boolean equal() {
48 return aChildrenDiffer.isEmpty() && bChildrenDiffer.isEmpty() && different.isEmpty();
51 private void analyze(ParentNode<?> pa, INode a, ParentNode<?> pb, INode b) {
55 different.add(Pair.<INode, INode>make(a, b));
56 if (pa != null && pb != null) {
57 aChildrenDiffer.put(pa, pb);
58 bChildrenDiffer.put(pb, pa);
62 if (a instanceof ParentNode<?>) {
63 ParentNode<?> ppa = (ParentNode<?>) a;
64 ParentNode<?> ppb = (ParentNode<?>) b;
66 Collection<String> aids = ppa.getNodeIds();
67 Collection<String> bids = ppb.getNodeIds();
68 if (!aids.equals(bids)) {
69 aChildrenDiffer.put(ppa, ppb);
70 bChildrenDiffer.put(ppb, ppa);
73 // Both nodes have an identically named set of child nodes.
74 if (a instanceof G2DParentNode) {
75 IG2DNode[] sac = ((G2DParentNode) a).getSortedNodes();
76 IG2DNode[] sbc = ((G2DParentNode) b).getSortedNodes();
77 for (int i = 0; i < sac.length; ++i) {
78 analyze(ppa, sac[i], ppb, sbc[i]);
81 for (String id : aids) {
82 INode ac = ppa.getNode(id);
83 INode bc = ppb.getNode(id);
84 analyze(ppa, ac, ppb, bc);
90 private boolean equals(INode a, INode b) {
91 Class<?> clazz = a.getClass();
92 if (!clazz.equals(b.getClass()))
95 // TODO: use reflection to compare field by field
96 // ignore transient fields
97 // ignore Node.id field
103 public String toString() {
105 return "no differences";
106 StringBuilder sb = new StringBuilder();
107 sb.append("Differences between nodes:\n");
108 for (Pair<INode, INode> p : different) {
109 sb.append(p.first.getClass().getSimpleName()).append(p.first).append("\nvs.\n").append(p.second.getClass().getSimpleName()).append(p.second).append("\n");
111 return sb.toString();
114 public static void main(String[] args) {
115 G2DSceneGraph sg1 = new G2DSceneGraph();
116 G2DSceneGraph sg2 = new G2DSceneGraph();
118 SceneGraphComparator sgc = new SceneGraphComparator(sg1, sg1);
120 System.out.println(sgc);
121 sgc = new SceneGraphComparator(sg1, sg2);
123 System.out.println(sgc);