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=e20c83e10b2ef021c1b8218e1ae7aab0e6ff6363;hb=954617febb489c628824343f08f41e1a0a047796;hp=82b7bfbb2a136177db43fcc08a5dbb3faf20b817;hpb=a51a513af1e519f4317b425529e8fb9bb35e3935;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 82b7bfb..e20c83e 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -25,13 +25,11 @@ 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; import org.simantics.utils.datastructures.BijectionMap; import org.simantics.utils.datastructures.MapList; @@ -50,6 +48,8 @@ import org.simantics.utils.datastructures.Pair; * */ public class GraphComparator { + + private static final boolean DEBUG = false; private Resource r1; private Resource r2; @@ -68,7 +68,7 @@ public class GraphComparator { private BijectionMap comparableStatements = new BijectionMap(); private BijectionMap comparableResources = new BijectionMap(); - + private Set processedResources = new HashSet(); private ResourceComparator comparator; @@ -160,12 +160,6 @@ public class GraphComparator { strong.addAll(rels); } - public void clearRels() { - traversed.clear(); - tested.clear(); - nonTraversed.clear(); - nonTested.clear(); - } public void test(ReadGraph g) throws DatabaseException { this.g = g; @@ -177,9 +171,6 @@ public class GraphComparator { objectsLeft.push(r1); objectsRight.push(r2); - - - Set unreliableLeft = new HashSet(); Set unreliableRight = new HashSet(); @@ -211,6 +202,58 @@ public class GraphComparator { } + } + + public void test(Session session) throws DatabaseException { + + comparator.setComparator(this); + + addComparable(r1, r2, false); + + 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() { + + @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); + } + } + }); + + + + } + for (Statement s : unreliableLeft) { + if (!comparableStatements.containsLeft(s)) + addDeletion(s); + } + for (Statement s : unreliableRight) { + if (!comparableStatements.containsRight(s)) + addAddition(s); + } + + } private void process(Stack objectsLeft, Stack objectsRight, Set unreliableLeft, Set unreliableRight) throws DatabaseException { @@ -223,15 +266,16 @@ public class GraphComparator { if (r1.equals(r2)) continue; - - if (comparableResources.contains(r1, r2)) { - //System.out.println("already tested " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); + + if (processedResources.contains(r1)) continue; - } - if (comparableResources.containsLeft(r1) || comparableResources.containsRight(r2)) { + processedResources.add(r1); + + + if((comparableResources.containsLeft(r1)||comparableResources.containsRight(r2)) && !comparableResources.contains(r1, 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); + addComparable(r1, r2, false); //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); compareProps(r1, r2); @@ -246,6 +290,7 @@ public class GraphComparator { ss1 = filterNonTested(ss1); ss2 = filterNonTested(ss2); + compareStatements(ss1, ss2, null, null,null,null); ss1.clear(); ss2.clear(); @@ -256,6 +301,8 @@ public class GraphComparator { ss2.addAll(g.getStatements(r2, rel)); ss1 = filterAsserted(r1, ss1); ss2 = filterAsserted(r2, ss2); + ss1 = filterNonTraversed(ss1); + ss2 = filterNonTraversed(ss2); compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight); ss1.clear(); ss2.clear(); @@ -264,7 +311,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(); @@ -299,7 +346,7 @@ public class GraphComparator { comparableResources.contains(leftS.getPredicate(), rightS.getPredicate())) { unreliableLeft.remove(leftS); unreliableRight.remove(rightS); - comparableStatements.map(leftS, rightS); + addComparable(leftS, rightS, false); } } } @@ -307,7 +354,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(); @@ -323,7 +370,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) @@ -334,6 +380,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) { @@ -402,9 +500,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); @@ -421,7 +519,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(); @@ -469,23 +567,29 @@ 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); + addComparable(ol, or, false); Collection statementsLeft = objectLeft.getValues(ol); Collection statementsRight = objectRight.getValues(or); unreliableLeft.removeAll(statementsLeft); unreliableRight.removeAll(statementsRight); - System.out.println(); - } else { - System.out.println(); - } + 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); + } + } + } } } @@ -493,7 +597,31 @@ public class GraphComparator { } - private void expand(Set paths) throws ManyObjectsForFunctionalRelationException, ServiceException { + 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 DatabaseException { Set stepPathsLeft = new HashSet(); if (paths.size() == 0) return; @@ -510,7 +638,7 @@ public class GraphComparator { } } - private Collection findComparableRight(Path leftPath, Resource beginRight) throws ManyObjectsForFunctionalRelationException, ServiceException { + private Collection findComparableRight(Path leftPath, Resource beginRight) throws DatabaseException { 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++) { @@ -541,18 +669,33 @@ public class GraphComparator { return comparableStatements; } + public BijectionMap getComparableResources() { + return comparableResources; + } + public GraphChanges getChanges() { 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); - if (!process) { - comparableResources.map(left.getObject(), right.getObject()); + //comparableResources.map(left.getObject(), right.getObject()); + } + + private void addComparable(Resource left, Resource right, boolean process) throws DatabaseException { + if(!comparableResources.contains(left, right)) { + if (comparableResources.containsLeft(left)||comparableResources.containsRight(right)) { + throw new DatabaseException("Comparator error: Trying to map " + left + " to " + right + " while mappings " + left + " to " + comparableResources.getRight(left) + " and " + comparableResources.getLeft(right) + " to " + right + " exist."); + } else { + if (DEBUG) System.out.println(left + " = " + right); + comparableResources.map(left, right); + } } + } - public List filterAsserted(Resource r, Collection in) throws ServiceException { + public List filterAsserted(Resource r, Collection in) throws DatabaseException { List out = new ArrayList(); for (Statement s : in) { if (!s.isAsserted(r)) @@ -561,20 +704,26 @@ public class GraphComparator { } return out; } + + - private String printStatement(ReadGraph graph, Statement s) throws ValidationException, ServiceException { + private String printStatement(ReadGraph graph, Statement s) throws DatabaseException { return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject()); } - private List filterTraversed(List in) throws ServiceException { + private List filterTraversed(List in) throws DatabaseException { return filter(traversed, in); } - private List filterNonTested(List in) throws ServiceException { + private List filterNonTested(List in) throws DatabaseException { return filter(nonTested, in); } - private List filter(Collection toFilter, List in) throws ServiceException { + private List filterNonTraversed(List in) throws DatabaseException { + return filter(nonTraversed, in); + } + + private List filter(Collection toFilter, List in) throws DatabaseException { if (toFilter.size() == 0) return in; List out = new ArrayList(); @@ -707,7 +856,7 @@ public class GraphComparator { break; } else { while (i2 < ss2.size()) { - System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i2))); + if (DEBUG) System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i2))); addAddition(ss2.get(i2)); i2++; @@ -716,7 +865,7 @@ public class GraphComparator { } } else if (i2 >= ss2.size()) { while (i1 < ss1.size()) { - System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i1))); + if (DEBUG) System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i1))); addDeletion(ss1.get(i1)); i1++; } @@ -731,13 +880,13 @@ public class GraphComparator { i2+=same2; } else if (c < 0) { for (int i = 0; i < same1; i++) { - System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i+i1))); + if (DEBUG) System.out.println("Compare Statements diff1 " + printStatement(g,ss1.get(i+i1))); addDeletion(ss1.get(i+i1)); } i1 += same1; } else { for (int i = 0; i < same2; i++) { - System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i+i2))); + if (DEBUG) System.out.println("Compare Statements diff2 " + printStatement(g,ss2.get(i+i2))); addAddition(ss2.get(i+i2)); } @@ -887,13 +1036,13 @@ public class GraphComparator { } for (int i1 = off1; i1 < off1 + len1; i1++) { if (!used1[i1-off1]) { - System.out.println("Compare Object diff1 " + printStatement(g,ss1.get(i1))); + if (DEBUG) System.out.println("Compare Object diff1 " + printStatement(g,ss1.get(i1))); addDeletion(ss1.get(i1)); } } for (int i2 = off2; i2 < off2 + len2; i2++) { if (!used2[i2-off2]) { - System.out.println("Compare Object diff2 " + printStatement(g,ss2.get(i2))); + if (DEBUG) System.out.println("Compare Object diff2 " + printStatement(g,ss2.get(i2))); addAddition(ss2.get(i2)); } } @@ -909,7 +1058,8 @@ 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 { + if (DEBUG) 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)); @@ -925,7 +1075,7 @@ public class GraphComparator { break; else { while (i2 < ss2.size()) { - System.out.println("Compare Prop diff2 " + printStatement(g,ss2.get(i2))); + if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,ss2.get(i2))); addAddition(ss2.get(i2)); i2++; } @@ -933,7 +1083,7 @@ public class GraphComparator { } } else if (i2 >= ss2.size()) { while (i1 < ss1.size()) { - System.out.println("Compare Prop diff1 " + printStatement(g,ss1.get(i1))); + if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,ss1.get(i1))); addDeletion(ss1.get(i1)); i1++; } @@ -941,6 +1091,11 @@ 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:{ @@ -956,7 +1111,8 @@ public class GraphComparator { addComparable(s1, s2, false); } } else { - compareProps(s1.getObject(), s2.getObject()); + if (!s1.getObject().equals(s1.getSubject()) && !s2.getObject().equals(s2.getSubject())) + compareProps(s1.getObject(), s2.getObject()); } } else { addModification(s1, s2); @@ -967,14 +1123,14 @@ public class GraphComparator { break; } case -1:{ - System.out.println("Compare Prop diff1s " + printStatement(g,s1)); + if (DEBUG) System.out.println("Compare Prop diff1s " + printStatement(g,s1)); addDeletion(s1); i1++; break; } case 1:{ - System.out.println("Compare Prop diff2s " + printStatement(g,s2)); + if (DEBUG) System.out.println("Compare Prop diff2s " + printStatement(g,s2)); addAddition(s2); i2++; break;