]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Improve matching logic for three way UpdateTree nodes. 39/3339/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 16 Oct 2019 09:54:29 +0000 (12:54 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 16 Oct 2019 09:54:29 +0000 (12:54 +0300)
gitlab #14

Change-Id: I87e14b14d548113c084882dd7e82939495aff92a

org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode3.java

index 9df0830cb3b3872dcfda1c0803ef52e9c003b354..a147647dea4c04967509f7e5b438e4544246dd39 100644 (file)
@@ -71,22 +71,40 @@ public class UpdateNode3 {
                return n3;
        }
        
+       private static void getComparable(UpdateNode n, GraphChanges gc1, GraphChanges gc2, GraphChanges gc3, Set<Resource> nodeR) {
+           nodeR.clear();
+           Resource r = n.getResource();
+           nodeR.add(r);
+           addNotNull(nodeR,gc1.getComparable().getLeft(r));
+           addNotNull(nodeR,gc1.getComparable().getRight(r));
+           addNotNull(nodeR,gc2.getComparable().getLeft(r));
+           addNotNull(nodeR,gc2.getComparable().getRight(r));
+           addNotNull(nodeR,gc3.getComparable().getLeft(r));
+           addNotNull(nodeR,gc3.getComparable().getRight(r));
+        
+       }
+       
+       public static <T> void addNotNull(Set<T> set, T t) {
+           if (t != null)
+               set.add(t);
+       }
+       
        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<>();
-               
+               Set<Resource> nodeR = new HashSet<>();
                if (un.getUn1() != null) {
                        for (UpdateNode n1 : un.getUn1().getChildren())  {
+                           getComparable(n1, gc1, gc2, gc3, nodeR);
                                p1.add(n1);
-                               UpdateNode n2 = null;
-                               if (un.getUn2() != null)
-                                       n2 = getMathcing(n1, un.getUn2().getChildren(), gc1);
+                               UpdateNode n2 = null;   
+                               if (un.getUn2() != null) {
+                                       n2 = getMatching(n1, un.getUn2().getChildren(), nodeR);
+                               }
                                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);
+                                       n3 = getMatching(n1, un.getUn3().getChildren(), nodeR); 
                                }
                                UpdateNode3 cn = new UpdateNode3(n1, n2, n3);
                                un.children.add(cn);
@@ -103,10 +121,10 @@ public class UpdateNode3 {
                                if (p2.contains(n2))
                                        continue;
                                p2.add(n2);
-                               
+                               getComparable(n2, gc1, gc2, gc3, nodeR);
                                UpdateNode n3 = null;
                                if (un.getUn3() != null) {
-                                       n3 = getMathcing(n2, un.getUn3().getChildren(), gc3);
+                                       n3 = getMatching(n2, un.getUn3().getChildren(), nodeR);
                                }
                                UpdateNode3 cn = new UpdateNode3(null, n2, n3);
                                un.children.add(cn);
@@ -128,7 +146,7 @@ public class UpdateNode3 {
                }
        }
        
-       private static UpdateNode getMathcing(UpdateNode n , Collection<UpdateNode> coll, GraphChanges gc) {
+       private static UpdateNode getMatching(UpdateNode n , Collection<UpdateNode> coll, Set<Resource> set) {
                if (n.getResource() == null) {
                        if (coll.size() != 1)
                                return null;
@@ -137,13 +155,9 @@ public class UpdateNode3 {
                                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()))
+                       if (set.contains(c.getResource()))
                                return c;
                }
                return null;