From a51a513af1e519f4317b425529e8fb9bb35e3935 Mon Sep 17 00:00:00 2001 From: luukkainen Date: Mon, 24 Oct 2011 11:30:56 +0000 Subject: [PATCH] Comparator failed to process some of the statements when subject and object were matched. git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@22927 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../interop/test/GraphComparator.java | 81 ++++++++++++++++--- 1 file changed, 71 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 8fd1ae4..82b7bfb 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -187,8 +187,11 @@ public class GraphComparator { if (objectsLeft.isEmpty()) break; + // process compares objects that are identified and searches for more resources to process. process(objectsLeft, objectsRight, unreliableLeft, unreliableRight); + // process unreliable handles cases where unidentified statements subject and object have been identified + processUnreliable(unreliableLeft, unreliableRight); // process unreliable handles cases where unidentified resources have path of length one to identified resource processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight); if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) { @@ -217,11 +220,17 @@ public class GraphComparator { while (!objectsLeft.isEmpty()) { Resource r1 = objectsLeft.pop(); Resource r2 = objectsRight.pop(); + + if (r1.equals(r2)) + continue; if (comparableResources.contains(r1, r2)) { //System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); continue; } + 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."); + } comparableResources.map(r1, r2); //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); @@ -254,6 +263,50 @@ public class GraphComparator { } } } + + private void processUnreliable(Set unreliableLeft, Set unreliableRight) { + MapList subjectLeft = new MapList(); + MapList subjectRight = new MapList(); + MapList objectLeft = new MapList(); + MapList objectRight = new MapList(); + + for (Statement s : unreliableLeft) { + subjectLeft.add(s.getSubject(),s); + objectLeft.add(s.getObject(),s); + } + for (Statement s : unreliableRight) { + subjectRight.add(s.getSubject(),s); + objectRight.add(s.getObject(),s); + } + + for (Resource left : subjectLeft.getKeys()) { + if (!comparableResources.containsLeft(left)) + continue; + Resource right = comparableResources.getRight(left); + for (Statement leftS : subjectLeft.getValues(left)) { + Resource leftO = leftS.getObject(); + if (!comparableResources.containsLeft(leftO)) + continue; + if (!unreliableLeft.contains(leftS)) + continue; + Resource rightO = comparableResources.getRight(leftO); + for (Statement rightS : subjectRight.getValues(right)) { + if (!rightS.getObject().equals(rightO)) + continue; + if (!unreliableRight.contains(rightS)) + continue; + if (leftS.getPredicate().equals(rightS.getPredicate()) || + comparableResources.contains(leftS.getPredicate(), rightS.getPredicate())) { + unreliableLeft.remove(leftS); + unreliableRight.remove(rightS); + comparableStatements.map(leftS, rightS); + } + } + } + + } + } + private void processUnreliable(Set unreliableLeft, Set unreliableRight, Stack objectsLeft, Stack objectsRight) { MapList subjectLeft = new MapList(); MapList subjectRight = new MapList(); @@ -354,9 +407,11 @@ public class GraphComparator { objectsRight.add(or); for (int l = 0; l < left.size(); l++) { int r = indices.first[l]; - comparableStatements.map(left.get(l), right.get(r)); - unreliableLeft.remove(left.get(l)); - unreliableRight.remove(right.get(r)); + Statement sl = left.get(l); + Statement sr = right.get(r); + addComparable(sl, sr, true); + unreliableLeft.remove(sl); + unreliableRight.remove(sr); } } @@ -490,6 +545,13 @@ public class GraphComparator { return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources); } + private void addComparable(Statement left, Statement right, boolean process) { + comparableStatements.map(left, right); + if (!process) { + comparableResources.map(left.getObject(), right.getObject()); + } + } + public List filterAsserted(Resource r, Collection in) throws ServiceException { List out = new ArrayList(); for (Statement s : in) { @@ -499,7 +561,7 @@ public class GraphComparator { } return out; } - + private String printStatement(ReadGraph graph, Statement s) throws ValidationException, ServiceException { return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject()); } @@ -702,6 +764,8 @@ public class GraphComparator { } private int compareObject(Resource o1, Resource o2) throws DatabaseException { + if (o1.equals(o2)) + return -1; if (comparableResources.contains(o1, o2)) return (-1); if (comparableResources.containsLeft(o1)) @@ -778,8 +842,7 @@ public class GraphComparator { objectsLeft.add(s1.getObject()); objectsRight.add(s2.getObject()); } - comparableStatements.map(s1, s2); - //comparableResources.map(s1.getObject(), s2.getObject()); + addComparable(s1, s2, true); break; } } @@ -890,16 +953,14 @@ public class GraphComparator { boolean eq = compareValue(v1, v2); if (!eq) { addModification(s1, s2); - comparableStatements.map(s1, s2); - comparableResources.map(s1.getObject(),s2.getObject()); + addComparable(s1, s2, false); } } else { compareProps(s1.getObject(), s2.getObject()); } } else { addModification(s1, s2); - comparableStatements.map(s1, s2); - comparableResources.map(s1.getObject(),s2.getObject()); + addComparable(s1, s2, false); } i1++; i2++; -- 2.45.2