]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Use named Comparator return values for most common cases 30/3730/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 19 Dec 2019 14:53:40 +0000 (16:53 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 19 Dec 2019 14:53:40 +0000 (16:53 +0200)
gitlab #20

Change-Id: I3390167b14c673c6b587ae484d8d3d30635a1181

org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java
org.simantics.interop/src/org/simantics/interop/test/NameComparator.java
org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java
org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java

index 61d7128c5de031a9d3aa83f5e0756cb8b0ed2b28..052a9a9ee19ad7bce23cba188e2bc923fbd76a2d 100644 (file)
@@ -52,7 +52,7 @@ import org.simantics.utils.datastructures.Pair;
  */
 public class GraphComparator {
        
-       private static final boolean DEBUG = false;
+       private static final boolean DEBUG = true;
 
        private Resource r1;
        private Resource r2;
@@ -152,10 +152,16 @@ public class GraphComparator {
        }
        
        public void addComparableResources(Resource r1, Resource r2) {
+               if (DEBUG)
+                       System.out.println("Preset " + r1 + " = " + r2);
                comparableResources.map(r1, r2);
        }
        
        public void addComparableResources(BijectionMap<Resource, Resource> matching) {
+               if (DEBUG) {
+                       for (Entry<Resource, Resource> entry : matching.getEntries())
+                               System.out.println("Preset " + entry.getKey() + " = " + entry.getValue());
+               }
                comparableResources.addAll(matching);
        }
        
@@ -269,6 +275,11 @@ public class GraphComparator {
                                                // These cases have longer path than one to identified objects.
                                                processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
                                        }
+                                       if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
+                                               // comparison is ending, but we have still unprocessed unidentified resources left.
+                                               // These cases have longer path than one to identified objects.
+                                               processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
+                                       }
                                }
                        });
                        
@@ -277,8 +288,6 @@ public class GraphComparator {
                }
                for (Statement s : unreliableLeft) {
                        if (!comparableStatements.containsLeft(s))
-                               if (s.getObject().getResourceId() == 303248)
-                                       System.out.println();
                                addDeletion(s);
                }
                for (Statement s : unreliableRight) {
@@ -299,7 +308,8 @@ public class GraphComparator {
                        
                        if (r1.equals(r2))
                                continue;
-                       
+                       if (r1.getResourceId() == 610446L)
+                               System.out.println();
                        if (processedResources.contains(r1))
                                continue;
                        processedResources.add(r1);
@@ -779,7 +789,9 @@ public class GraphComparator {
                                throw new DatabaseException("Comparator error: Trying to map " + left + " to " + right + " while mappings " + left + " to " + comparableResources.getRight(left) + " and " + comparableResources.getLeft(right) + " to " + right + " exist.");
                        } else {
                                if (DEBUG) System.out.println(left + " = " + right);
-                               comparableResources.map(left, right);   
+                               if (left.getResourceId() == 610381L)
+                                       System.out.println();
+                               comparableResources.map(left, right);
                        }
                }
                
@@ -838,6 +850,8 @@ public class GraphComparator {
                if (!changes1Set.contains(s)) {
                        changes1Set.add(s);
                        changes1.add(s);
+                       if (s.getObject().getResourceId() == 532631L)
+                               System.out.println();
                }
        }
        
index 1467404b2808e9910bbf354c0fcbed10420ce180..e0172eb375f3302cb2521b7104f8949cab68475d 100644 (file)
@@ -22,14 +22,14 @@ public class NameComparator extends TypeComparator {
        public int compare(ReadGraph g, Resource o1, Resource o2)
                        throws DatabaseException {
                if (!compareType(g,o1, o2)) {
-                       return Integer.MAX_VALUE;
+                       return NO_MATCH;
                }
                if (hasName(g, o1, o2)) {
                        if(!compareName(g, o1, o2))
-                               return Integer.MAX_VALUE;
+                               return NO_MATCH;
                        return propsDiffCount(g,o1, o2);
                }
-               return 0;
+               return UNDECISIVE;
                
        }
        
index 8234998ad546067365c4439bf9c2431c97d5bc4a..f01d88d860b2bd34edc8b513740d6b6f353820f9 100644 (file)
@@ -11,6 +11,10 @@ import org.simantics.db.exception.DatabaseException;
  */
 public abstract class ResourceComparator {
        
+       public static int NO_MATCH = Integer.MAX_VALUE;   // Comparison result when Resources cannot match.
+       public static int UNDECISIVE = 0;                 // Comparison result when current available data is not enough to decide if the Resources match or not. 
+       public static int EXACT_MATCH = 1;                // Comparison result when Resources are exact match.
+       
        private GraphComparator comparator;
        
        void setComparator(GraphComparator comparator) {
index 6c85d284699db0e5db56231a48d9e96439774a6a..a0069c6e0e0b9218563fdd49f3a32d3c9919cd40 100644 (file)
@@ -27,7 +27,7 @@ public class TypeComparator extends ResourceComparator {
        @Override
        public int compare(ReadGraph g, Resource o1, Resource o2, boolean local) throws DatabaseException {
                if (o1.equals(o2)) {
-                       return 1;
+                       return EXACT_MATCH;
                }
                return compare(g, o1, o2);
        }
@@ -35,7 +35,7 @@ public class TypeComparator extends ResourceComparator {
        @Override
        public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException{
                if (!compareType(g,o1, o2)) {
-                       return (Integer.MAX_VALUE);
+                       return NO_MATCH;
                }
                return propsDiffCount(g,o1, o2);
        }