X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop%2Fsrc%2Forg%2Fsimantics%2Finterop%2Ftest%2FGraphComparator.java;h=58e2c0531cb41bec456b451663450b6a6a030881;hb=624443b2a7c97a1808a42470dddf1437ed59cd21;hp=ab9c67943d481fce82f8f64a73217790341c7ae8;hpb=240c4a4d8efbb302feedccac76edd5c28d6d7150;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 ab9c679..58e2c05 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java @@ -25,7 +25,9 @@ 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; @@ -68,7 +70,7 @@ public class GraphComparator { private BijectionMap comparableStatements = new BijectionMap(); private BijectionMap comparableResources = new BijectionMap(); - + private Set processedResources = new HashSet(); private ResourceComparator comparator; @@ -160,12 +162,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 +173,6 @@ public class GraphComparator { objectsLeft.push(r1); objectsRight.push(r2); - - - Set unreliableLeft = new HashSet(); Set unreliableRight = new HashSet(); @@ -211,6 +204,58 @@ public class GraphComparator { } + } + + 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() { + + @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 { @@ -221,23 +266,17 @@ public class GraphComparator { Resource r1 = objectsLeft.pop(); Resource r2 = objectsRight.pop(); -// if (r1.getResourceId() == 42543612 || -// r1.getResourceId() == 42729524 || -// r1.getResourceId() == 42729506) { -// System.out.println("test " + r1 + " " + r2); -// } - 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.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."); } - comparableResources.map(r1, r2); //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2)); compareProps(r1, r2); @@ -270,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(); @@ -305,7 +344,7 @@ public class GraphComparator { comparableResources.contains(leftS.getPredicate(), rightS.getPredicate())) { unreliableLeft.remove(leftS); unreliableRight.remove(rightS); - comparableStatements.map(leftS, rightS); + addComparable(leftS, rightS, false); } } } @@ -313,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(); @@ -329,9 +368,6 @@ public class GraphComparator { } for (Resource ol : objectLeft.getKeys()) { -// if (ol.getResourceId() == 42729506) { -// System.out.println(); -// } // all statements to the left side object List left = objectLeft.getValues(ol); // all subjects that have statements to the left side object (ol) @@ -462,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); @@ -481,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(); @@ -529,22 +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); + addComparable(ol, or, false); Collection statementsLeft = objectLeft.getValues(ol); Collection statementsRight = objectRight.getValues(or); unreliableLeft.removeAll(statementsLeft); unreliableRight.removeAll(statementsRight); - 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); + } + } + //System.out.println("Compare not implemented"); } else { - System.out.println(); + //System.out.println("Compare not implemented"); } } } @@ -553,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) @@ -605,16 +675,21 @@ public class GraphComparator { return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources); } - private void addComparable(Statement left, Statement right, boolean process) { -// if (left.getObject().getResourceId() == 42543612 || -// left.getObject().getResourceId() == 42729524 || -// left.getObject().getResourceId() == 42729506) { -// System.out.println("test " + r1 + " " + r2); -// } + 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(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 { @@ -904,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()); } @@ -974,7 +1051,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 { + 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)); @@ -1006,6 +1084,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:{ @@ -1021,7 +1104,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);