]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/testcases/org/simantics/scenegraph/tests/SceneGraphComparator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph / testcases / org / simantics / scenegraph / tests / SceneGraphComparator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.tests;
13
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
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;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class SceneGraphComparator {
30
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>>();
34
35     private INode a;
36     private INode b;
37
38     public SceneGraphComparator(INode a, INode b) {
39         this.a = a;
40         this.b = b;
41     }
42
43     public void analyze() {
44         analyze(a.getParent(), a, b.getParent(), b);
45     }
46
47     public boolean equal() {
48         return aChildrenDiffer.isEmpty() && bChildrenDiffer.isEmpty() && different.isEmpty();
49     }
50
51     private void analyze(ParentNode<?> pa, INode a, ParentNode<?> pb, INode b) {
52         if (a == b)
53             return;
54         if (!equals(a, 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);
59             }
60             return;
61         }
62         if (a instanceof ParentNode<?>) {
63             ParentNode<?> ppa = (ParentNode<?>) a;
64             ParentNode<?> ppb = (ParentNode<?>) b;
65
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);
71             }
72
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]);
79                 }
80             } else {
81                 for (String id : aids) {
82                     INode ac = ppa.getNode(id);
83                     INode bc = ppb.getNode(id);
84                     analyze(ppa, ac, ppb, bc);
85                 }
86             }
87         }
88     }
89
90     private boolean equals(INode a, INode b) {
91         Class<?> clazz = a.getClass();
92         if (!clazz.equals(b.getClass()))
93             return false;
94
95         // TODO: use reflection to compare field by field
96         // ignore transient fields
97         // ignore Node.id field
98
99         return true;
100     }
101
102     @Override
103     public String toString() {
104         if (equal())
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");
110         }
111         return sb.toString();
112     }
113
114     public static void main(String[] args) {
115         G2DSceneGraph sg1 = new G2DSceneGraph();
116         G2DSceneGraph sg2 = new G2DSceneGraph();
117
118         SceneGraphComparator sgc = new SceneGraphComparator(sg1, sg1);
119         sgc.analyze();
120         System.out.println(sgc);
121         sgc = new SceneGraphComparator(sg1, sg2);
122         sgc.analyze();
123         System.out.println(sgc);
124     }
125
126 }