X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop.update%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fupdate%2Fmodel%2FUpdateNode3.java;fp=org.simantics.interop.update%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fupdate%2Fmodel%2FUpdateNode3.java;h=cdff0d8dadc33fa177684a032c3f4f63b9406f6d;hb=904106737f8068e02fe8e43dd258e93bae3a71c8;hp=0000000000000000000000000000000000000000;hpb=82a773fbfa81a3e40b10272526eee52324f2e8d2;p=simantics%2Finterop.git diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java new file mode 100644 index 0000000..cdff0d8 --- /dev/null +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java @@ -0,0 +1,131 @@ +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 children = new ArrayList(); + + + public Collection 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 p1 = new HashSet<>(); + Set p2 = new HashSet<>(); + Set 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 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; + } +}