X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.interop%2Fsrc%2Forg%2Fsimantics%2Finterop%2Ftest%2FGraphComparator.java;h=71fc67e8d272e8a2aeae384396b06542a89bf896;hb=de00fbb5944f623ba50d1a358b7b22e020ef3beb;hp=152d423e341ee9b28cee1c704f0a5c7d3f8a772c;hpb=499167aa0bcc2cab0066b87ef4a690c16ea92754;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 152d423..71fc67e 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -25,10 +25,13 @@ import java.util.Stack; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Session; import org.simantics.db.Statement; +import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.DoesNotContainValueException; +import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; import org.simantics.db.exception.ValidationException; import org.simantics.layer0.Layer0; @@ -67,7 +70,7 @@ public class GraphComparator { private BijectionMap comparableStatements = new BijectionMap(); private BijectionMap comparableResources = new BijectionMap(); - + private Set processedResources = new HashSet(); private ResourceComparator comparator; @@ -155,13 +158,11 @@ public class GraphComparator { strong.add(r); } - public void clearRels() { - traversed.clear(); - tested.clear(); - nonTraversed.clear(); - nonTested.clear(); + public void addStrong(Collection rels) { + strong.addAll(rels); } + public void test(ReadGraph g) throws DatabaseException { this.g = g; this.b = Layer0.getInstance(g); @@ -172,10 +173,6 @@ public class GraphComparator { objectsLeft.push(r1); objectsRight.push(r2); - - List ss1 = new ArrayList(); - List ss2 = new ArrayList(); - Set unreliableLeft = new HashSet(); Set unreliableRight = new HashSet(); @@ -183,47 +180,70 @@ public class GraphComparator { if (objectsLeft.isEmpty()) break; - while (!objectsLeft.isEmpty()) { - Resource r1 = objectsLeft.pop(); - Resource r2 = objectsRight.pop(); - if (comparableResources.contains(r1, r2)) { - //System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); - continue; - } - comparableResources.map(r1, r2); - - //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); - compareProps(r1, r2); - - for (Resource rel : tested) { - ss1.addAll(g.getStatements(r1, rel)); - ss2.addAll(g.getStatements(r2, rel)); - ss1 = filterAsserted(r1, ss1); - ss2 = filterAsserted(r2, ss2); - ss1 = filterTraversed(ss1); - ss2 = filterTraversed(ss2); - ss1 = filterNonTested(ss1); - ss2 = filterNonTested(ss2); - - compareStatements(ss1, ss2, null, null,null,null); - ss1.clear(); - ss2.clear(); - } + // 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) { + // comparison is ending, but we have still unprocessed unidentified resources left. + // These cases have longer path than one to identified objects. + processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight); + } + + } + for (Statement s : unreliableLeft) { + if (!comparableStatements.containsLeft(s)) + addDeletion(s); + } + for (Statement s : unreliableRight) { + if (!comparableStatements.containsRight(s)) + addAddition(s); + } + + + } + + public void test(Session session) throws DatabaseException { + + comparator.setComparator(this); + + comparableResources.map(r1, r2); + + final Stack objectsLeft = new Stack(); + final Stack objectsRight = new Stack(); + objectsLeft.push(r1); + objectsRight.push(r2); + + final Set unreliableLeft = new HashSet(); + final Set unreliableRight = new HashSet(); + + while (true) { + if (objectsLeft.isEmpty()) + break; + session.syncRequest(new ReadRequest() { - for (Resource rel : traversed) { - ss1.addAll(g.getStatements(r1, rel)); - ss2.addAll(g.getStatements(r2, rel)); - ss1 = filterAsserted(r1, ss1); - ss2 = filterAsserted(r2, ss2); - compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight); - ss1.clear(); - ss2.clear(); - + @Override + public void run(ReadGraph graph) throws DatabaseException { + g = graph; + b = Layer0.getInstance(graph); + // 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) { + // comparison is ending, but we have still unprocessed unidentified resources left. + // These cases have longer path than one to identified objects. + processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight); + } } - } + }); + - processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight); } for (Statement s : unreliableLeft) { @@ -238,6 +258,100 @@ public class GraphComparator { } + private void process(Stack objectsLeft, Stack objectsRight, Set unreliableLeft, Set unreliableRight) throws DatabaseException { + List ss1 = new ArrayList(); + List ss2 = new ArrayList(); + + while (!objectsLeft.isEmpty()) { + Resource r1 = objectsLeft.pop(); + Resource r2 = objectsRight.pop(); + + if (r1.equals(r2)) + continue; + + if (processedResources.contains(r1)) + continue; + processedResources.add(r1); + + + if(!comparableResources.contains(r1, r2) && (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."); + } + + //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); + compareProps(r1, r2); + + for (Resource rel : tested) { + ss1.addAll(g.getStatements(r1, rel)); + ss2.addAll(g.getStatements(r2, rel)); + ss1 = filterAsserted(r1, ss1); + ss2 = filterAsserted(r2, ss2); + ss1 = filterTraversed(ss1); + ss2 = filterTraversed(ss2); + ss1 = filterNonTested(ss1); + ss2 = filterNonTested(ss2); + + compareStatements(ss1, ss2, null, null,null,null); + ss1.clear(); + ss2.clear(); + } + + for (Resource rel : traversed) { + ss1.addAll(g.getStatements(r1, rel)); + ss2.addAll(g.getStatements(r2, rel)); + ss1 = filterAsserted(r1, ss1); + ss2 = filterAsserted(r2, ss2); + compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight); + ss1.clear(); + ss2.clear(); + + } + } + } + + 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); + addComparable(leftS, rightS, false); + } + } + } + + } + } + private void processUnreliable(Set unreliableLeft, Set unreliableRight, Stack objectsLeft, Stack objectsRight) { MapList subjectLeft = new MapList(); MapList subjectRight = new MapList(); @@ -254,7 +368,6 @@ public class GraphComparator { } for (Resource ol : objectLeft.getKeys()) { - // all statements to the left side object List left = objectLeft.getValues(ol); // all subjects that have statements to the left side object (ol) @@ -265,6 +378,58 @@ public class GraphComparator { sLeft.add(s.getSubject()); sRight.add(comparableResources.getRight(s.getSubject())); } + + // check if object left can be reliably identified by available statements + // if there are any objects on the left side with similar statements, object left cannot be mapped. + boolean hasSimilar = false; + MapList comparableOLeft = new MapList(); + for (Resource sl : sLeft) { + for (Statement s : subjectLeft.getValues(sl)) { + if (!s.getObject().equals(ol)) { + comparableOLeft.add(s.getObject(),s); + } + } + } + + for (Resource similarOl : comparableOLeft.getKeys()) { + List similarLeft = comparableOLeft.getValues(similarOl); + if (similarLeft.size() == left.size()) { + boolean useL[] = new boolean[left.size()]; + boolean useSL[] = new boolean[left.size()]; + for (int i = 0; i < left.size(); i++) { + useL[i] = false; + useSL[i] = false; + } + for (int i = 0; i < left.size(); i++) { + for (int j = 0; j < left.size(); j++) { + if (useSL[j]) + continue; + Resource pl = left.get(i).getPredicate(); + Resource psl = similarLeft.get(j).getPredicate(); + if (pl.equals(psl)) { + useL[i] = true; + useSL[j] = true; + break; + } + } + } + boolean diff = false; + for (int i = 0; i < left.size(); i++) { + if (!useL[i] || !useSL[i]) { + diff = true; + } + } + if (!diff) { + hasSimilar = true; + break; + } + } + } + + if (hasSimilar) + continue; + + // all objects that subjects on the right side point to. Object left has its matching resource among these, if it has matching resource MapList possibleOR = new MapList(); for (Resource sr : sRight) { @@ -333,14 +498,16 @@ public class GraphComparator { List right = matchingOR.getValues(or); Pair indices = matchingStatements.get(or); - //comparableResources.map(ol, or); + comparableResources.map(ol, or); objectsLeft.add(ol); 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); } } @@ -350,6 +517,123 @@ public class GraphComparator { } + private void processUnreliableDeep(Set unreliableLeft, Set unreliableRight, Stack objectsLeft, Stack objectsRight) throws ManyObjectsForFunctionalRelationException, ServiceException { + 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 ol : objectLeft.getKeys()) { + Set pathsLeft = new HashSet(); + for (Resource rel : traversed) { + pathsLeft.addAll(Path.create(g.getStatements(ol, rel))); + } + while (true) { + expand(pathsLeft); + if (pathsLeft.size() == 0) + break; + Collection endPaths = new ArrayList(1); + for (Path p : pathsLeft) { + if (comparableResources.containsLeft(p.getEnd())) { + endPaths.add(p); + } + } + if (endPaths.size() > 0) { + pathsLeft.clear(); + pathsLeft.addAll(endPaths); + break; + } + } + if (pathsLeft.size() > 0) { + Resource sl = objectLeft.getValues(ol).get(0).getSubject(); + Resource sr = comparableResources.getRight(sl); + Collection possibleOR = new ArrayList(); + for (Statement s : subjectRight.getValues(sr)) { + possibleOR.add(s.getObject()); + } + Map> matchingPaths = new HashMap>(); + for (Resource or : possibleOR) { + Set possiblePathsRight = new HashSet(); + for (Path leftPath : pathsLeft) { + possiblePathsRight.addAll(findComparableRight(leftPath, or)); + } + if (possiblePathsRight.size() == pathsLeft.size()) { + 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); + Collection statementsLeft = objectLeft.getValues(ol); + Collection statementsRight = objectRight.getValues(or); + unreliableLeft.removeAll(statementsLeft); + unreliableRight.removeAll(statementsRight); + System.out.println("Compare not implemented"); + } else { + System.out.println("Compare not implemented"); + } + } + } + + } + + } + + private void expand(Set paths) throws ManyObjectsForFunctionalRelationException, ServiceException { + Set stepPathsLeft = new HashSet(); + if (paths.size() == 0) + return; + int length = paths.iterator().next().getLength() + 1; + for (Path p : paths) { + for (Resource rel : traversed) { + stepPathsLeft.addAll(Path.expand(p,g.getStatements(p.getEnd(), rel))); + } + } + paths.clear(); + for (Path p : stepPathsLeft) { + if (p.getLength() == length) + paths.add(p); + } + } + + private Collection findComparableRight(Path leftPath, Resource beginRight) throws ManyObjectsForFunctionalRelationException, ServiceException { + Set rightPaths = new HashSet(); + rightPaths.addAll(Path.create(g.getStatements(beginRight, getRight(leftPath.getStatements().get(0).getPredicate())))); + for (int i = 1; i < leftPath.getLength(); i++) { + if (rightPaths.size() == 0) + return rightPaths; + Set stepPaths = new HashSet(); + for (Path p : rightPaths) { + stepPaths.addAll(Path.expand(p, g.getStatements(p.getEnd(), getRight(leftPath.getStatements().get(i).getPredicate())))); + } + rightPaths.clear(); + for (Path p : stepPaths) + if (p.getLength() == i+1) + rightPaths.add(p); + } + return rightPaths; + + } + + private Resource getRight(Resource r) { + if (comparableResources.containsLeft(r)) + return comparableResources.getRight(r); + return r; + } + + + public BijectionMap getComparableStatements() { return comparableStatements; } @@ -358,7 +642,12 @@ 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); + 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)) @@ -367,7 +656,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()); } @@ -498,7 +787,6 @@ public class GraphComparator { } } js++; - } } @@ -550,7 +838,6 @@ public class GraphComparator { i2 += same2; } - } } @@ -572,6 +859,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)) @@ -645,13 +934,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; } } @@ -719,13 +1005,12 @@ public class GraphComparator { * @throws ValidationException */ private void compareProps(Resource r1, Resource r2) throws ServiceException, DoesNotContainValueException, ValidationException { + System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2)); ArrayList ss1 = new ArrayList(); ArrayList ss2 = new ArrayList(); 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; @@ -752,49 +1037,52 @@ public class GraphComparator { } Statement s1 = ss1.get(i1); Statement s2 = ss2.get(i2); + if (s1.isAsserted(r1) && s2.isAsserted(r2)) { + i1++; + i2++; + continue; + } 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 { + if (!s1.getObject().equals(s1.getSubject()) && !s2.getObject().equals(s2.getSubject())) + 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(); @@ -802,7 +1090,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); @@ -823,7 +1110,6 @@ public class GraphComparator { } - public class PredicateComparator implements Comparator { @Override public int compare(Statement o1, Statement o2) { @@ -882,8 +1168,6 @@ public class GraphComparator { } } - - public class ResComparator implements Comparator { @Override public int compare(Resource o1, Resource o2) { @@ -896,6 +1180,5 @@ public class GraphComparator { return 0; } } - - + }