From 8b92927f87a7966d268ab7678d29992485752c21 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Thu, 19 Dec 2019 16:53:40 +0200 Subject: [PATCH] Use named Comparator return values for most common cases gitlab #20 Change-Id: I3390167b14c673c6b587ae484d8d3d30635a1181 --- .../interop/test/GraphComparator.java | 24 +++++++++++++++---- .../interop/test/NameComparator.java | 6 ++--- .../interop/test/ResourceComparator.java | 4 ++++ .../interop/test/TypeComparator.java | 4 ++-- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java index 61d7128..052a9a9 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -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 matching) { + if (DEBUG) { + for (Entry 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(); } } diff --git a/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java b/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java index 1467404..e0172eb 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/NameComparator.java @@ -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; } diff --git a/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java b/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java index 8234998..f01d88d 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/ResourceComparator.java @@ -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) { diff --git a/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java b/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java index 6c85d28..a0069c6 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java @@ -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); } -- 2.47.1