From 624443b2a7c97a1808a42470dddf1437ed59cd21 Mon Sep 17 00:00:00 2001 From: luukkainen Date: Thu, 31 May 2012 13:47:21 +0000 Subject: [PATCH] Path comparisons are now much more reliable refs #3138 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@25085 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../interop/test/GraphComparator.java | 67 ++++++++++++++++--- .../src/org/simantics/interop/test/Path.java | 17 ++++- 2 files changed, 72 insertions(+), 12 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 71fc67e..58e2c05 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -309,7 +309,7 @@ public class GraphComparator { } } - private void processUnreliable(Set unreliableLeft, Set unreliableRight) { + private void processUnreliable(Set unreliableLeft, Set unreliableRight) throws DatabaseException { MapList subjectLeft = new MapList(); MapList subjectRight = new MapList(); MapList objectLeft = new MapList(); @@ -352,7 +352,7 @@ public class GraphComparator { } } - private void processUnreliable(Set unreliableLeft, Set unreliableRight, Stack objectsLeft, Stack objectsRight) { + private void processUnreliable(Set unreliableLeft, Set unreliableRight, Stack objectsLeft, Stack objectsRight) throws DatabaseException { MapList subjectLeft = new MapList(); MapList subjectRight = new MapList(); MapList objectLeft = new MapList(); @@ -498,9 +498,9 @@ public class GraphComparator { List right = matchingOR.getValues(or); Pair indices = matchingStatements.get(or); - comparableResources.map(ol, or); objectsLeft.add(ol); objectsRight.add(or); + addComparable(ol, or, false); for (int l = 0; l < left.size(); l++) { int r = indices.first[l]; Statement sl = left.get(l); @@ -517,7 +517,7 @@ public class GraphComparator { } - private void processUnreliableDeep(Set unreliableLeft, Set unreliableRight, Stack objectsLeft, Stack objectsRight) throws ManyObjectsForFunctionalRelationException, ServiceException { + private void processUnreliableDeep(Set unreliableLeft, Set unreliableRight, Stack objectsLeft, Stack objectsRight) throws DatabaseException { MapList subjectLeft = new MapList(); MapList subjectRight = new MapList(); MapList objectLeft = new MapList(); @@ -565,23 +565,31 @@ public class GraphComparator { for (Path leftPath : pathsLeft) { possiblePathsRight.addAll(findComparableRight(leftPath, or)); } - if (possiblePathsRight.size() == pathsLeft.size()) { + if (hasMatchingPaths(pathsLeft, possiblePathsRight)) { matchingPaths.put(or, possiblePathsRight); } } if (matchingPaths.size() > 0) { if (matchingPaths.size() == 1) { Resource or = matchingPaths.keySet().iterator().next(); + objectsLeft.add(ol); objectsRight.add(or); - comparableResources.map(ol, or); + addComparable(ol, or, false); Collection statementsLeft = objectLeft.getValues(ol); Collection statementsRight = objectRight.getValues(or); unreliableLeft.removeAll(statementsLeft); unreliableRight.removeAll(statementsRight); - System.out.println("Compare not implemented"); + BijectionMap map = getMatchingPaths(pathsLeft, matchingPaths.get(or)); + for (Path left : map.getLeftSet()) { + Path right = map.getRight(left); + for (int i = 0; i < left.getLength(); i++) { + addComparable(left.getStatements().get(i),right.getStatements().get(i),false); + } + } + //System.out.println("Compare not implemented"); } else { - System.out.println("Compare not implemented"); + //System.out.println("Compare not implemented"); } } } @@ -590,6 +598,31 @@ public class GraphComparator { } + private boolean hasMatchingPaths(Set leftPaths, Set rightPaths) { + if (leftPaths.size() != rightPaths.size()) + return false; + BijectionMap map = getMatchingPaths(leftPaths, rightPaths); + return map.size() == leftPaths.size(); + + } + + private BijectionMap getMatchingPaths(Set leftPaths, Set rightPaths) { + BijectionMap map = new BijectionMap(); + for (Path leftPath : leftPaths) { + for (Path rightPath : rightPaths) { + if (map.containsRight(rightPath)) + continue; + if (leftPath.getLength() != rightPath.getLength()) + continue; + if (comparableResources.contains(leftPath.getEnd(), rightPath.getEnd())) { + map.map(leftPath, rightPath); + break; + } + } + } + return map; + } + private void expand(Set paths) throws ManyObjectsForFunctionalRelationException, ServiceException { Set stepPathsLeft = new HashSet(); if (paths.size() == 0) @@ -642,11 +675,23 @@ public class GraphComparator { return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources); } - private void addComparable(Statement left, Statement right, boolean process) { + private void addComparable(Statement left, Statement right, boolean process) throws DatabaseException { + addComparable(left.getObject(), right.getObject(), process); comparableStatements.map(left, right); comparableResources.map(left.getObject(), right.getObject()); } + private void addComparable(Resource left, Resource right, boolean process) throws DatabaseException { + if(!comparableResources.contains(r1, r2)) { + if (comparableResources.containsLeft(r1)||comparableResources.containsRight(r2)) { + 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."); + } else { + comparableResources.map(left, right); + } + } + + } + public List filterAsserted(Resource r, Collection in) throws ServiceException { List out = new ArrayList(); for (Statement s : in) { @@ -934,6 +979,8 @@ public class GraphComparator { Statement s2 = ss2.get(i2+off2); if (objectsLeft != null) { + if (s1.getObject().getResourceId() == 52825217 || s1.getObject().getResourceId() == 52825127) + System.out.println(); objectsLeft.add(s1.getObject()); objectsRight.add(s2.getObject()); } @@ -1004,7 +1051,7 @@ public class GraphComparator { * @throws DoesNotContainValueException * @throws ValidationException */ - private void compareProps(Resource r1, Resource r2) throws ServiceException, DoesNotContainValueException, ValidationException { + private void compareProps(Resource r1, Resource r2) throws DatabaseException { System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2)); ArrayList ss1 = new ArrayList(); ArrayList ss2 = new ArrayList(); diff --git a/org.simantics.interop/src/org/simantics/interop/test/Path.java b/org.simantics.interop/src/org/simantics/interop/test/Path.java index b772267..4d4bb60 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/Path.java +++ b/org.simantics.interop/src/org/simantics/interop/test/Path.java @@ -86,14 +86,27 @@ public class Path { if (statements.size() != other.statements.size()) return false; for (int i = 0; i < statements.size(); i++) { - if (!statements.get(i).equals(other.statements.get(i))) + if (!statements.get(i).getPredicate().equals(other.statements.get(i).getPredicate())|| + !statements.get(i).getObject().equals(other.statements.get(i).getObject())) return false; } - return false; + return true; } @Override public int hashCode() { return begin.hashCode() + end.hashCode(); } + + @Override + public String toString() { + String s = ""; + for (int i = 0; i < statements.size() ; i++) { + s += statements.get(i).getSubject(); + s += "<" + statements.get(i).getPredicate() +">"; + if (i == statements.size() -1) + s += statements.get(i).getObject(); + } + return s; + } } -- 2.45.2