--- /dev/null
+package org.simantics.interop.update.model;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.interop.test.GraphChanges;
+
+public class UpdateNode3 {
+
+ UpdateNode un1;
+ UpdateNode un2;
+ UpdateNode un3;
+
+ private Collection<UpdateNode3> children = new ArrayList<UpdateNode3>();
+
+
+ public Collection<UpdateNode3> getChildren() {
+ return children;
+ }
+
+ public UpdateNode getUn1() {
+ return un1;
+ }
+
+ public UpdateNode getUn2() {
+ return un2;
+ }
+
+ public UpdateNode getUn3() {
+ return un3;
+ }
+
+ public UpdateNode3(UpdateNode un1, UpdateNode un2, UpdateNode un3) {
+ this.un1 = un1;
+ this.un2 = un2;
+ this.un3 = un3;
+ }
+
+ public static UpdateNode3 getCombinedTree(ModelUpdate update) throws DatabaseException {
+ UpdateTree updateTree1 = update.getUpdateTree();
+ UpdateTree updateTree2 = update.getUpdateTree2();
+ UpdateTree updateTree3 = update.getUpdateTree3();
+
+ UpdateNode3 n3 = new UpdateNode3(updateTree1.getRootNode(), updateTree2.getRootNode(), updateTree3.getRootNode());
+
+ populate(n3, update.getChanges(), update.getChanges2(), update.getChanges3());
+ return n3;
+ }
+
+ private static void populate(UpdateNode3 un, GraphChanges gc1, GraphChanges gc2, GraphChanges gc3) {
+ Set<UpdateNode> p1 = new HashSet<>();
+ Set<UpdateNode> p2 = new HashSet<>();
+ Set<UpdateNode> p3 = new HashSet<>();
+
+ if (un.getUn1() != null) {
+ for (UpdateNode n1 : un.getUn1().getChildren()) {
+ p1.add(n1);
+ UpdateNode n2 = null;
+ if (un.getUn2() != null)
+ n2 = getMathcing(n1, un.getUn2().getChildren(), gc1);
+ UpdateNode n3 = null;
+ if (un.getUn3() != null) {
+ n3 = getMathcing(n1, un.getUn3().getChildren(), gc2);
+ if (n3 == null && n2 != null)
+ n3 = getMathcing(n2, un.getUn3().getChildren(), gc3);
+ }
+ UpdateNode3 cn = new UpdateNode3(n1, n2, n3);
+ un.children.add(cn);
+ populate(cn, gc1, gc2, gc3);
+
+ if (n2 != null)
+ p2.add(n2);
+ if (n3 != null)
+ p3.add(n3);
+ }
+ }
+ if (un.getUn2() != null) {
+ for (UpdateNode n2 : un.getUn2().getChildren()) {
+ if (p2.contains(n2))
+ continue;
+ p2.add(n2);
+
+ UpdateNode n3 = null;
+ if (un.getUn3() != null) {
+ n3 = getMathcing(n2, un.getUn3().getChildren(), gc3);
+ }
+ UpdateNode3 cn = new UpdateNode3(null, n2, n3);
+ un.children.add(cn);
+ populate(cn, gc1, gc2, gc3);
+
+ if (n3 != null)
+ p3.add(n3);
+ }
+ }
+ if (un.getUn3() != null) {
+ for (UpdateNode n3 : un.getUn3().getChildren()) {
+ if (p3.contains(n3))
+ continue;
+ p3.add(n3);
+ UpdateNode3 cn = new UpdateNode3(null, null, n3);
+ un.children.add(cn);
+ populate(cn, gc1, gc2, gc3);
+ }
+ }
+ }
+
+ private static UpdateNode getMathcing(UpdateNode n , Collection<UpdateNode> coll, GraphChanges gc) {
+ if (n.getResource() == null) {
+ if (coll.size() != 1)
+ return null;
+ UpdateNode n2 = coll.iterator().next();
+ if (n2.getClass() != n.getClass())
+ return null;
+ return n2;
+ }
+ Resource o = gc.getComparable().getLeft(n.getResource());
+ if (o == null)
+ o = gc.getComparable().getRight(n.getResource());
+ if (o == null)
+ o = n.getResource();
+ for (UpdateNode c : coll) {
+ if (o.equals(c.getResource()))
+ return c;
+ }
+ return null;
+ }
+}