]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Improved handling of unidentified objects.
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 27 Sep 2011 14:49:42 +0000 (14:49 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 09:22:16 +0000 (11:22 +0200)
git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@22442 ac1ea38d-2e2b-0410-8846-a27921b304fc

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 e52a3bf296f485d11b88f362bf07ada14b566a41..152d423e341ee9b28cee1c704f0a5c7d3f8a772c 100644 (file)
@@ -52,6 +52,7 @@ public class GraphComparator {
 \r
        private Resource r1;\r
        private Resource r2;\r
+       private Set<Resource> strong = new HashSet<Resource>();       // List of relations that identify object, if subject is already identified. \r
        private List<Resource> traversed = new ArrayList<Resource>(); // list of relations that are traversed (and tested)\r
        private List<Resource> tested = new ArrayList<Resource>();    // list of relations that are tested, but not traversed\r
        private List<Resource> nonTraversed = new ArrayList<Resource>(); // list of relations that are not traversed\r
@@ -150,6 +151,10 @@ public class GraphComparator {
                comparableResources.addAll(matching);\r
        }\r
        \r
+       public void addStrong(Resource r) {\r
+               strong.add(r);\r
+       }\r
+       \r
        public void clearRels() {\r
                traversed.clear();\r
                tested.clear();\r
@@ -174,45 +179,66 @@ public class GraphComparator {
                Set<Statement> unreliableLeft = new HashSet<Statement>();\r
                Set<Statement> unreliableRight = new HashSet<Statement>();\r
                \r
-               while (!objectsLeft.isEmpty()) {\r
-                       Resource r1 = objectsLeft.pop();\r
-                       Resource r2 = objectsRight.pop();\r
-               \r
-                       if (comparableResources.contains(r1, r2)) {\r
-                               //System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));\r
-                               continue;\r
-                       }\r
-                       comparableResources.map(r1, r2);\r
+               while (true) {\r
+                       if (objectsLeft.isEmpty())\r
+                               break;\r
                        \r
-                       //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));\r
-                       compareProps(r1, r2);\r
+                       while (!objectsLeft.isEmpty()) {\r
+                               Resource r1 = objectsLeft.pop();\r
+                               Resource r2 = objectsRight.pop();\r
                        \r
-                       for (Resource rel : tested) {\r
-                               ss1.addAll(g.getStatements(r1, rel));\r
-                               ss2.addAll(g.getStatements(r2, rel));\r
-                               ss1 = filterAsserted(r1, ss1);\r
-                               ss2 = filterAsserted(r2, ss2);\r
-                               ss1 = filterTraversed(ss1);\r
-                               ss2 = filterTraversed(ss2);\r
-                               ss1 = filterNonTested(ss1);\r
-                               ss2 = filterNonTested(ss2);\r
+                               if (comparableResources.contains(r1, r2)) {\r
+                                       //System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));\r
+                                       continue;\r
+                               }\r
+                               comparableResources.map(r1, r2);\r
                                \r
-                               compareStatements(ss1, ss2, null, null,null,null);\r
-                               ss1.clear();\r
-                               ss2.clear();\r
-                       }\r
-                       \r
-                       for (Resource rel : traversed) {\r
-                               ss1.addAll(g.getStatements(r1, rel));\r
-                               ss2.addAll(g.getStatements(r2, rel));\r
-                               ss1 = filterAsserted(r1, ss1);\r
-                               ss2 = filterAsserted(r2, ss2);\r
-                               compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);\r
-                               ss1.clear();\r
-                               ss2.clear();\r
+                               //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));\r
+                               compareProps(r1, r2);\r
                                \r
+                               for (Resource rel : tested) {\r
+                                       ss1.addAll(g.getStatements(r1, rel));\r
+                                       ss2.addAll(g.getStatements(r2, rel));\r
+                                       ss1 = filterAsserted(r1, ss1);\r
+                                       ss2 = filterAsserted(r2, ss2);\r
+                                       ss1 = filterTraversed(ss1);\r
+                                       ss2 = filterTraversed(ss2);\r
+                                       ss1 = filterNonTested(ss1);\r
+                                       ss2 = filterNonTested(ss2);\r
+                                       \r
+                                       compareStatements(ss1, ss2, null, null,null,null);\r
+                                       ss1.clear();\r
+                                       ss2.clear();\r
+                               }\r
+                               \r
+                               for (Resource rel : traversed) {\r
+                                       ss1.addAll(g.getStatements(r1, rel));\r
+                                       ss2.addAll(g.getStatements(r2, rel));\r
+                                       ss1 = filterAsserted(r1, ss1);\r
+                                       ss2 = filterAsserted(r2, ss2);\r
+                                       compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);\r
+                                       ss1.clear();\r
+                                       ss2.clear();\r
+                                       \r
+                               }\r
                        }\r
+                       \r
+                       processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight);\r
+                       \r
                }\r
+               for (Statement s : unreliableLeft) {\r
+                       if (!comparableStatements.containsLeft(s))\r
+                               addDeletion(s);\r
+               }\r
+               for (Statement s : unreliableRight) {\r
+                       if (!comparableStatements.containsRight(s))\r
+                               addAddition(s);\r
+               }\r
+               \r
+               \r
+       }\r
+       \r
+       private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) {\r
                MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();\r
                MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();\r
                MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();\r
@@ -307,29 +333,22 @@ public class GraphComparator {
                                List<Statement> right = matchingOR.getValues(or);\r
                                Pair<int[], int[]> indices = matchingStatements.get(or);\r
                                \r
-                               comparableResources.map(ol, or);\r
+                               //comparableResources.map(ol, or);\r
+                               objectsLeft.add(ol);\r
+                               objectsRight.add(or);\r
                                for (int l = 0; l < left.size(); l++) {\r
                                        int r = indices.first[l];\r
                                        comparableStatements.map(left.get(l), right.get(r));\r
+                                       unreliableLeft.remove(left.get(l));\r
+                                       unreliableRight.remove(right.get(r));\r
                                }\r
                                \r
                        }\r
 \r
                }\r
                \r
-               for (Statement s : unreliableLeft) {\r
-                       if (!comparableStatements.containsLeft(s))\r
-                               addDeletion(s);\r
-               }\r
-               for (Statement s : unreliableRight) {\r
-                       if (!comparableStatements.containsRight(s))\r
-                               addAddition(s);\r
-               }\r
-               \r
                \r
-       }\r
-       \r
-       \r
+       }\r
        \r
        public BijectionMap<Statement, Statement> getComparableStatements() {\r
                return comparableStatements;\r
@@ -581,6 +600,14 @@ public class GraphComparator {
                        for (int i2 = off2; i2 < off2 + len2; i2++) {\r
                                Statement s2 = ss2.get(i2);\r
                                int d = compareObject(s1.getObject(), s2.getObject());\r
+                               if (d == 0) {\r
+                                       for (Resource t : strong) {\r
+                                                if (s1.getPredicate().equals(t) || g.isSubrelationOf(s1.getPredicate(), t)) {\r
+                                                        d = 1;\r
+                                                        break;\r
+                                                }\r
+                                       }\r
+                               }\r
                                diff.add(d);\r
                        }\r
                        differences.add(diff);\r
index 5bdb3f041f55f954f271059ee93864185b7bfa00..125d05912b6d443a7687f8635b9d2023b7759bf0 100644 (file)
@@ -8,7 +8,9 @@ import org.simantics.db.exception.ServiceException;
 import org.simantics.layer0.Layer0;\r
 \r
 /**\r
- * * Object comparator that uses type and name of objects to check if objects are comparable. \r
+ * Object comparator that uses type and name of objects to check if objects are comparable.\r
+ * If objects have no name, compare returns unreliable as result.\r
+ *  \r
  * \r
  * Difference value is amount of properties that have different values.\r
  * \r
@@ -23,10 +25,13 @@ public class NameComparator extends TypeComparator {
                if (!compareType(g,o1, o2)) {\r
                        return Integer.MAX_VALUE;\r
                }\r
-               if(!compareName(g, o1, o2))\r
-                       return Integer.MAX_VALUE;\r
+               if (hasName(g, o1, o2)) {\r
+                       if(!compareName(g, o1, o2))\r
+                               return Integer.MAX_VALUE;\r
+                       return propsDiffCount(g,o1, o2);\r
+               }\r
+               return 0;\r
                \r
-               return propsDiffCount(g,o1, o2);\r
        }\r
        \r
        protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
@@ -40,5 +45,15 @@ public class NameComparator extends TypeComparator {
                return false;\r
                        \r
        }\r
+       \r
+       protected boolean hasName(ReadGraph g, Resource o1, Resource o2) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
+               Layer0 l0 = Layer0.getInstance(g);\r
+               String n1 = g.getPossibleRelatedValue(o1, l0.HasName);\r
+               String n2 = g.getPossibleRelatedValue(o2, l0.HasName);\r
+               if (n1 != null && n2 != null)\r
+                       return true;\r
+               return false;\r
+                       \r
+       }\r
 \r
 }\r
index 3d31d22b2568c8fe2280ea477d9d28464d3d7763..0b0a7c1a53d983933319bfa901f70eca84ff1ce8 100644 (file)
@@ -27,6 +27,7 @@ public abstract class ResourceComparator {
         * Special values:\r
         * Integer.MAX_VALUE: Objects are not comparable.\r
         * 0 (zero): Object comparison is not reliable. \r
+        * \r
         * @param g\r
         * @param o1\r
         * @param o2\r
index 96ef6101c3c91087a936c47fe989eeaa28f9c9a0..1417588f10f7ac828386176d8edaace5d55a1196 100644 (file)
@@ -16,7 +16,8 @@ import org.simantics.layer0.Layer0;
 /**\r
  * Object comparator that uses type of objects to check if objects are comparable. \r
  * \r
- * Difference value is amount of properties that have different values.\r
+ * Difference value is amount of properties that have different values. If objects have no properties, the result is 0 (unreliable).\r
+ * \r
  * \r
  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
  *\r