X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop%2Fsrc%2Forg%2Fsimantics%2Finterop%2Ftest%2FGraphComparator.java;h=82b7bfbb2a136177db43fcc08a5dbb3faf20b817;hb=a51a513af1e519f4317b425529e8fb9bb35e3935;hp=163dda4917b82e3a34303938a56ce07d2b2e30d3;hpb=15e18f8e20cc4c359781d2ba54418c4371cde4c8;p=simantics%2Finterop.git 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 163dda4..82b7bfb 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -156,6 +156,10 @@ public class GraphComparator { strong.add(r); } + public void addStrong(Collection rels) { + strong.addAll(rels); + } + public void clearRels() { traversed.clear(); tested.clear(); @@ -183,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) { @@ -213,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)); @@ -250,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(); @@ -350,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); } } @@ -486,7 +545,14 @@ public class GraphComparator { return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources); } - private List filterAsserted(Resource r, Collection in) throws ServiceException { + 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) { if (!s.isAsserted(r)) @@ -495,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()); } @@ -626,7 +692,6 @@ public class GraphComparator { } } js++; - } } @@ -678,7 +743,6 @@ public class GraphComparator { i2 += same2; } - } } @@ -700,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)) @@ -773,13 +839,10 @@ public class GraphComparator { Statement s2 = ss2.get(i2+off2); if (objectsLeft != null) { - - objectsLeft.add(s1.getObject()); - objectsRight.add(s2.getObject()); - + objectsLeft.add(s1.getObject()); + objectsRight.add(s2.getObject()); } - comparableStatements.map(s1, s2); - //comparableResources.map(s1.getObject(), s2.getObject()); + addComparable(s1, s2, true); break; } } @@ -852,8 +915,6 @@ public class GraphComparator { ss1.addAll(g.getStatements(r1, b.HasProperty)); ss2.addAll(g.getStatements(r2, b.HasProperty)); sortStatement(ss1, ss2); -// Collections.sort(ss1, scomp); -// Collections.sort(ss2, scomp); int i1 = 0; int i2 = 0; @@ -882,47 +943,44 @@ public class GraphComparator { Statement s2 = ss2.get(i2); int c = scomp.compare(s1, s2); switch (c) { - case 0:{ - boolean b1 = g.hasValue(s1.getObject()); - boolean b2 = g.hasValue(s2.getObject()); - if (b1 == b2) { - if (b1) { - Object v1 = g.getValue(s1.getObject()); - Object v2 = g.getValue(s2.getObject()); - boolean eq = compareValue(v1, v2); - if (!eq) { - addModification(s1, s2); - comparableStatements.map(s1, s2); - comparableResources.map(s1.getObject(),s2.getObject()); + case 0:{ + boolean b1 = g.hasValue(s1.getObject()); + boolean b2 = g.hasValue(s2.getObject()); + if (b1 == b2) { + if (b1) { + Object v1 = g.getValue(s1.getObject()); + Object v2 = g.getValue(s2.getObject()); + boolean eq = compareValue(v1, v2); + if (!eq) { + addModification(s1, s2); + addComparable(s1, s2, false); + } + } else { + compareProps(s1.getObject(), s2.getObject()); } } else { - compareProps(s1.getObject(), s2.getObject()); + addModification(s1, s2); + addComparable(s1, s2, false); } - } else { - addModification(s1, s2); - comparableStatements.map(s1, s2); - comparableResources.map(s1.getObject(),s2.getObject()); + i1++; + i2++; + break; + } + case -1:{ + System.out.println("Compare Prop diff1s " + printStatement(g,s1)); + addDeletion(s1); + i1++; + break; + } + + case 1:{ + System.out.println("Compare Prop diff2s " + printStatement(g,s2)); + addAddition(s2); + i2++; + break; } - i1++; - i2++; - break; - } - case -1:{ - System.out.println("Compare Prop diff1s " + printStatement(g,s1)); - addDeletion(s1); - i1++; - break; - } - - case 1:{ - System.out.println("Compare Prop diff2s " + printStatement(g,s2)); - addAddition(s2); - i2++; - break; - } } - - + } ss1.clear(); @@ -930,7 +988,6 @@ public class GraphComparator { } - public static boolean compareValue(Object v1, Object v2) { if (v1 instanceof Object[] && v2 instanceof Object[]) return Arrays.deepEquals((Object[])v1, (Object[])v2); @@ -951,7 +1008,6 @@ public class GraphComparator { } - public class PredicateComparator implements Comparator { @Override public int compare(Statement o1, Statement o2) { @@ -1010,8 +1066,6 @@ public class GraphComparator { } } - - public class ResComparator implements Comparator { @Override public int compare(Resource o1, Resource o2) { @@ -1024,6 +1078,5 @@ public class GraphComparator { return 0; } } - - + }