From 904106737f8068e02fe8e43dd258e93bae3a71c8 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Fri, 20 Oct 2017 15:29:08 +0300 Subject: [PATCH] Three-way comparison --- .../scl/Interop/Update.scl | 36 +++++ .../interop/update/model/ModelUpdate.java | 24 +++- .../interop/update/model/UpdateNode3.java | 131 ++++++++++++++++++ 3 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java diff --git a/org.simantics.interop.update/scl/Interop/Update.scl b/org.simantics.interop.update/scl/Interop/Update.scl index 06e7785..1498a86 100644 --- a/org.simantics.interop.update/scl/Interop/Update.scl +++ b/org.simantics.interop.update/scl/Interop/Update.scl @@ -20,6 +20,24 @@ importJava "org.simantics.interop.update.model.ModelUpdate" where @JavaName applySelected applySelectedUpdates :: ModelUpdate -> () + @JavaName getUpdateTree2 + getUpdateTree2 :: ModelUpdate -> UpdateTree + + @JavaName getUpdateList2 + getUpdateList2 :: ModelUpdate -> UpdateList + + @JavaName getChanges2 + getGraphChanges2 :: ModelUpdate -> GraphChanges + + @JavaName getUpdateTree3 + getUpdateTree3 :: ModelUpdate -> Maybe UpdateTree + + @JavaName getUpdateList + getUpdateList3 :: ModelUpdate -> Maybe UpdateList + + @JavaName getChanges + getGraphChanges3 :: ModelUpdate -> Maybe GraphChanges + importJava "org.simantics.interop.update.model.UpdateTree" where data UpdateTree @@ -114,6 +132,24 @@ importJava "org.simantics.interop.update.model.UpdateOp" where @JavaName isChange opIsChange :: UpdateOp -> Boolean +importJava "org.simantics.interop.update.model.UpdateNode3" where + data UpdateNode3 + + @JavaName getUn1 + getNode1 :: UpdateNode3 -> Maybe UpdateNode + + @JavaName getUn2 + getNode2 :: UpdateNode3 -> Maybe UpdateNode + + @JavaName getUn3 + getNode3 :: UpdateNode3 -> Maybe UpdateNode + + @JavaName getChildren + getNode3Children :: UpdateNode3 -> [UpdateNode3] + + @JavaName getCombinedTree + getCombinedTree :: ModelUpdate -> UpdateNode3 + importJava "org.simantics.utils.datastructures.Pair" where data Pair diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java index 0ddd4e4..2a263f1 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java @@ -25,10 +25,13 @@ public abstract class ModelUpdate { private UpdateList updateList; private GraphChanges changes2; - private GraphChanges changes3; private UpdateTree updateTree2; private UpdateList updateList2; + private GraphChanges changes3; + private UpdateTree updateTree3; + private UpdateList updateList3; + private List filters = new ArrayList(); boolean init = false; @@ -141,15 +144,28 @@ public abstract class ModelUpdate { public GraphChanges getChanges2() { return changes2; } - public GraphChanges getChanges3() { - return changes3; - } + public UpdateTree getUpdateTree2() { return updateTree2; } public UpdateList getUpdateList2() { return updateList2; } + + public GraphChanges getChanges3() { + return changes3; + } + + public UpdateTree getUpdateTree3() throws DatabaseException{ + if (updateTree3 == null && changes3 != null) + updateTree3 = getUpdateTree(changes3); + return updateTree3; + } + public UpdateList getUpdateList3() throws DatabaseException { + if (updateList3 == null && changes3 != null) + updateList3 = getUpdateList(changes3); + return updateList3; + } public void applyAll(WriteGraph graph) throws DatabaseException { 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; + } +} -- 2.45.2