]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Path comparisons are now much more reliable
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Thu, 31 May 2012 13:47:21 +0000 (13:47 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 09:22:21 +0000 (11:22 +0200)
refs #3138

git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@25085 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java
org.simantics.interop/src/org/simantics/interop/test/Path.java

index 71fc67e8d272e8a2aeae384396b06542a89bf896..58e2c0531cb41bec456b451663450b6a6a030881 100644 (file)
@@ -309,7 +309,7 @@ public class GraphComparator {
                }\r
        }\r
        \r
-       private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight) {\r
+       private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {\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
@@ -352,7 +352,7 @@ public class GraphComparator {
                }\r
        }\r
        \r
-       private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) {\r
+       private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {\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
@@ -498,9 +498,9 @@ public class GraphComparator {
                                List<Statement> right = matchingOR.getValues(or);\r
                                Pair<int[], int[]> indices = matchingStatements.get(or);\r
                                \r
-                               comparableResources.map(ol, or);\r
                                objectsLeft.add(ol);\r
                                objectsRight.add(or);\r
+                               addComparable(ol, or, false);\r
                                for (int l = 0; l < left.size(); l++) {\r
                                        int r = indices.first[l];\r
                                        Statement sl = left.get(l);\r
@@ -517,7 +517,7 @@ public class GraphComparator {
                \r
        }\r
        \r
-       private void processUnreliableDeep(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
+       private void processUnreliableDeep(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {\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
@@ -565,23 +565,31 @@ public class GraphComparator {
                                        for (Path leftPath : pathsLeft) {\r
                                                possiblePathsRight.addAll(findComparableRight(leftPath, or));\r
                                        }\r
-                                       if (possiblePathsRight.size() == pathsLeft.size()) {\r
+                                       if (hasMatchingPaths(pathsLeft, possiblePathsRight)) {\r
                                                matchingPaths.put(or, possiblePathsRight);\r
                                        }\r
                                }\r
                                if (matchingPaths.size() > 0) {\r
                                        if (matchingPaths.size() == 1) {\r
                                                Resource or = matchingPaths.keySet().iterator().next();\r
+                                               \r
                                                objectsLeft.add(ol);\r
                                                objectsRight.add(or);\r
-                                               comparableResources.map(ol, or);\r
+                                               addComparable(ol, or, false);\r
                                                Collection<Statement> statementsLeft = objectLeft.getValues(ol);\r
                                                Collection<Statement> statementsRight = objectRight.getValues(or);\r
                                                unreliableLeft.removeAll(statementsLeft);\r
                                                unreliableRight.removeAll(statementsRight);\r
-                                               System.out.println("Compare not implemented");\r
+                                               BijectionMap<Path,Path> map = getMatchingPaths(pathsLeft, matchingPaths.get(or));\r
+                                               for (Path left : map.getLeftSet()) {\r
+                                                       Path right = map.getRight(left);\r
+                                                       for (int i = 0; i < left.getLength(); i++) {\r
+                                                               addComparable(left.getStatements().get(i),right.getStatements().get(i),false);\r
+                                                       }\r
+                                               }\r
+                                               //System.out.println("Compare not implemented");\r
                                        } else {\r
-                                               System.out.println("Compare not implemented");\r
+                                               //System.out.println("Compare not implemented");\r
                                        }\r
                                }\r
                        }\r
@@ -590,6 +598,31 @@ public class GraphComparator {
                \r
        }\r
        \r
+       private boolean hasMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {\r
+               if (leftPaths.size() != rightPaths.size())\r
+                       return false;\r
+               BijectionMap<Path,Path> map = getMatchingPaths(leftPaths, rightPaths);\r
+               return map.size() == leftPaths.size();\r
+                       \r
+       }\r
+       \r
+       private BijectionMap<Path,Path> getMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {\r
+               BijectionMap<Path,Path> map = new BijectionMap<Path, Path>();\r
+               for (Path leftPath : leftPaths) {\r
+                       for (Path rightPath : rightPaths) {\r
+                               if (map.containsRight(rightPath))\r
+                                       continue;\r
+                               if (leftPath.getLength() != rightPath.getLength())\r
+                                       continue;\r
+                               if (comparableResources.contains(leftPath.getEnd(), rightPath.getEnd())) {\r
+                                       map.map(leftPath, rightPath);\r
+                                       break;\r
+                               }\r
+                       }\r
+               }\r
+               return map;\r
+       }\r
+       \r
        private void expand(Set<Path> paths) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
                Set<Path> stepPathsLeft = new HashSet<Path>();\r
                if (paths.size() == 0)\r
@@ -642,11 +675,23 @@ public class GraphComparator {
                return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources);\r
        }\r
        \r
-       private void addComparable(Statement left, Statement right, boolean process) {\r
+       private void addComparable(Statement left, Statement right, boolean process) throws DatabaseException {\r
+               addComparable(left.getObject(), right.getObject(), process);\r
                comparableStatements.map(left, right);\r
                comparableResources.map(left.getObject(), right.getObject());\r
        }\r
        \r
+       private void addComparable(Resource left, Resource right, boolean process) throws DatabaseException {\r
+               if(!comparableResources.contains(r1, r2)) {\r
+                       if (comparableResources.containsLeft(r1)||comparableResources.containsRight(r2)) {\r
+                               throw new DatabaseException("Comparator error: Trying to map " + r1 + " to " + r2 + " while mappings " + r1 + " to " + comparableResources.getRight(r1) + " and " + comparableResources.getLeft(r2) + " to " + r2 + " exist.");\r
+                       } else {\r
+                               comparableResources.map(left, right);           \r
+                       }\r
+               }\r
+               \r
+       }\r
+       \r
        public List<Statement> filterAsserted(Resource r, Collection<Statement> in) throws ServiceException {\r
                List<Statement> out = new ArrayList<Statement>();\r
                for (Statement s : in) {\r
@@ -934,6 +979,8 @@ public class GraphComparator {
                                                        Statement s2  = ss2.get(i2+off2);\r
                                                        \r
                                                        if (objectsLeft != null) {\r
+                                                               if (s1.getObject().getResourceId() == 52825217 || s1.getObject().getResourceId() == 52825127)\r
+                                                                       System.out.println();\r
                                                                objectsLeft.add(s1.getObject());\r
                                                                objectsRight.add(s2.getObject());\r
                                                        } \r
@@ -1004,7 +1051,7 @@ public class GraphComparator {
         * @throws DoesNotContainValueException\r
         * @throws ValidationException \r
         */\r
-       private void compareProps(Resource r1, Resource r2) throws ServiceException, DoesNotContainValueException, ValidationException {\r
+       private void compareProps(Resource r1, Resource r2) throws DatabaseException {\r
                System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2));\r
                ArrayList<Statement> ss1 = new ArrayList<Statement>();\r
                ArrayList<Statement> ss2 = new ArrayList<Statement>();\r
index b772267caa349b0a218e2cda22c5a299d8f8c9c8..4d4bb60c2b8f8715391fd62ccd4df7b8e18edfc0 100644 (file)
@@ -86,14 +86,27 @@ public class Path {
                if (statements.size() != other.statements.size())\r
                        return false;\r
                for (int i = 0; i < statements.size(); i++) {\r
-                       if (!statements.get(i).equals(other.statements.get(i)))\r
+                       if (!statements.get(i).getPredicate().equals(other.statements.get(i).getPredicate())||\r
+                           !statements.get(i).getObject().equals(other.statements.get(i).getObject()))\r
                                return false;\r
                }\r
-               return false;\r
+               return true;\r
        }\r
        \r
        @Override\r
        public int hashCode() {\r
                return begin.hashCode() + end.hashCode();\r
        }\r
+       \r
+       @Override\r
+       public String toString() {\r
+               String s = "";\r
+               for (int i = 0; i < statements.size() ; i++) {\r
+                       s += statements.get(i).getSubject();\r
+                       s += "<" + statements.get(i).getPredicate() +">";\r
+                       if (i == statements.size() -1)\r
+                               s += statements.get(i).getObject();\r
+               }\r
+               return s;\r
+       }\r
 }\r