1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Foster Wheeler Energia Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.interop.test;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.Map.Entry;
25 import java.util.Stack;
27 import org.simantics.databoard.Bindings;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.Resource;
30 import org.simantics.db.Session;
31 import org.simantics.db.Statement;
32 import org.simantics.db.common.request.ReadRequest;
33 import org.simantics.db.common.utils.NameUtils;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.interop.test.GraphChanges.Modification;
36 import org.simantics.layer0.Layer0;
37 import org.simantics.utils.datastructures.BijectionMap;
38 import org.simantics.utils.datastructures.MapList;
39 import org.simantics.utils.datastructures.Pair;
42 * Compares two subgraphs and reports differences.
44 * Assumes that subgraphs (defined using traverse relations) are not cyclic.
46 * Assumes that properties can be used to identify objects, if relation type is not enough.
50 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
53 public class GraphComparator {
55 private static final boolean DEBUG = false;
59 private Set<Resource> strong = new HashSet<>(); // List of relations that identify object, if subject is already identified.
60 private List<Resource> traversed = new ArrayList<>(); // list of relations that are traversed (and tested)
61 private List<Resource> tested = new ArrayList<>(); // list of relations that are tested, but not traversed
62 private List<Resource> nonTraversed = new ArrayList<>(); // list of relations that are not traversed
63 private List<Resource> nonTested = new ArrayList<>(); // list of relations that are not tested
65 private List<Statement> changes1 = new ArrayList<>();
66 private List<Statement> changes2 = new ArrayList<>();
67 private List<Modification> modifications = new ArrayList<>();
68 private Set<Statement> changes1Set = new HashSet<>();
69 private Set<Statement> changes2Set = new HashSet<>();
70 private Set<Modification> modificationsSet = new HashSet<>();
72 private BijectionMap<Statement, Statement> comparableStatements = new BijectionMap<>();
73 private BijectionMap<Resource, Resource> comparableResources = new BijectionMap<>();
75 private Set<Resource> processedResources = new HashSet<Resource>();
77 private ResourceComparator comparator;
79 private Comparator<Statement> scomp = new PredicateComparator();
80 private Comparator<Resource> rcomp = new ResComparator();
82 private Set<Resource> nonMatchedLeft = new HashSet<Resource>();
83 private Set<Resource> nonMatchedRight = new HashSet<Resource>();
90 public GraphComparator(Resource r1, Resource r2) {
93 comparator = new TypeComparator();
96 public GraphComparator(Resource r1, Resource r2, ResourceComparator comparator) {
99 this.comparator = comparator;
102 ArrayList<Statement> ss1 = new ArrayList<Statement>();
103 ArrayList<Statement> ss2 = new ArrayList<Statement>();
106 public Comparator<Resource> getResourceComparator() {
110 public Comparator<Statement> getStatementComparator() {
114 public Resource getR1() {
118 public Resource getR2() {
122 public void addTraversed(Resource rel) {
126 public void addTraversed(Collection<Resource> rels) {
127 traversed.addAll(rels);
130 public void addNonTraversed(Resource rel) {
131 nonTraversed.add(rel);
134 public void addNonTraversed(Collection<Resource> rels) {
135 nonTraversed.addAll(rels);
138 public void addTested(Resource rel) {
142 public void addTested(Collection<Resource> rels) {
146 public void addNonTested(Resource rel) {
150 public void addNonTested(Collection<Resource> rels) {
151 nonTested.addAll(rels);
154 public void addComparableResources(Resource r1, Resource r2) {
156 System.out.println("Preset " + r1 + " = " + r2);
157 comparableResources.map(r1, r2);
160 public void addComparableResources(BijectionMap<Resource, Resource> matching) {
162 for (Entry<Resource, Resource> entry : matching.getEntries())
163 System.out.println("Preset " + entry.getKey() + " = " + entry.getValue());
165 comparableResources.addAll(matching);
168 public void addStrong(Resource r) {
172 public void addStrong(Collection<Resource> rels) {
176 public void addNonMatchedLeft(Resource r) {
177 nonMatchedLeft.add(r);
180 public void addNonMatchedRight(Resource r) {
181 nonMatchedRight.add(r);
184 public void test(ReadGraph g) throws DatabaseException {
186 this.b = Layer0.getInstance(g);
187 comparator.setComparator(this);
188 comparator.initialize(g, r1, r2);
190 Stack<Resource> objectsLeft = new Stack<Resource>();
191 Stack<Resource> objectsRight = new Stack<Resource>();
192 objectsLeft.push(r1);
193 objectsRight.push(r2);
195 Set<Statement> unreliableLeft = new HashSet<Statement>();
196 Set<Statement> unreliableRight = new HashSet<Statement>();
199 if (objectsLeft.isEmpty())
203 // process compares objects that are identified and searches for more resources to process.
204 process(objectsLeft, objectsRight, unreliableLeft, unreliableRight);
205 // process unreliable handles cases where unidentified statements subject and object have been identified
206 processUnreliable(unreliableLeft, unreliableRight);
207 // process unreliable handles cases where unidentified resources have path of length one to identified resource
208 processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
209 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
210 processUnreliable2(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
212 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
213 // comparison is ending, but we have still unprocessed unidentified resources left.
214 // These cases have longer path than one to identified objects.
215 processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
219 for (Statement s : unreliableLeft) {
220 if (!comparableStatements.containsLeft(s))
223 for (Statement s : unreliableRight) {
224 if (!comparableStatements.containsRight(s))
229 public void test(Session session) throws DatabaseException {
230 test(session, r1, r2);
233 public void test(Session session, Resource r1, Resource r2) throws DatabaseException {
235 comparator.setComparator(this);
237 session.syncRequest(new ReadRequest() {
240 public void run(ReadGraph graph) throws DatabaseException {
241 comparator.initialize(graph, r1, r2);
245 addComparable(r1, r2);
247 final Stack<Resource> objectsLeft = new Stack<Resource>();
248 final Stack<Resource> objectsRight = new Stack<Resource>();
249 objectsLeft.push(r1);
250 objectsRight.push(r2);
252 final Set<Statement> unreliableLeft = new HashSet<Statement>();
253 final Set<Statement> unreliableRight = new HashSet<Statement>();
256 if (objectsLeft.isEmpty())
258 session.syncRequest(new ReadRequest() {
261 public void run(ReadGraph graph) throws DatabaseException {
263 b = Layer0.getInstance(graph);
264 // process compares objects that are identified and searches for more resources to process.
265 process(objectsLeft, objectsRight, unreliableLeft, unreliableRight);
266 // process unreliable handles cases where unidentified statements subject and object have been identified
267 processUnreliable(unreliableLeft, unreliableRight);
268 // process unreliable handles cases where unidentified resources have path of length one to identified resource
269 processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
270 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
271 processUnreliable2(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
273 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
274 // comparison is ending, but we have still unprocessed unidentified resources left.
275 // These cases have longer path than one to identified objects.
276 processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
278 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
279 // comparison is ending, but we have still unprocessed unidentified resources left.
280 // These cases have longer path than one to identified objects.
281 processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
289 for (Statement s : unreliableLeft) {
290 if (!comparableStatements.containsLeft(s))
293 for (Statement s : unreliableRight) {
294 if (!comparableStatements.containsRight(s))
301 private void process(Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
302 List<Statement> ss1 = new ArrayList<Statement>();
303 List<Statement> ss2 = new ArrayList<Statement>();
305 while (!objectsLeft.isEmpty()) {
306 Resource r1 = objectsLeft.pop();
307 Resource r2 = objectsRight.pop();
311 if (processedResources.contains(r1))
313 processedResources.add(r1);
316 if((comparableResources.containsLeft(r1)||comparableResources.containsRight(r2)) && !comparableResources.contains(r1, r2)) {
317 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.");
319 addComparable(r1, r2);
321 //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));
322 compareProps(r1, r2);
324 for (Resource rel : tested) {
325 ss1.addAll(g.getStatements(r1, rel));
326 ss2.addAll(g.getStatements(r2, rel));
327 ss1 = filterAsserted(r1, ss1);
328 ss2 = filterAsserted(r2, ss2);
329 ss1 = filterTraversed(ss1);
330 ss2 = filterTraversed(ss2);
331 ss1 = filterNonTested(ss1);
332 ss2 = filterNonTested(ss2);
335 compareStatements(ss1, ss2, null, null,null,null);
340 for (Resource rel : traversed) {
341 ss1.addAll(g.getStatements(r1, rel));
342 ss2.addAll(g.getStatements(r2, rel));
343 ss1 = filterAsserted(r1, ss1);
344 ss2 = filterAsserted(r2, ss2);
345 ss1 = filterNonTraversed(ss1);
346 ss2 = filterNonTraversed(ss2);
347 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
355 private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
356 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
357 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
358 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
359 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
361 for (Statement s : unreliableLeft) {
362 subjectLeft.add(s.getSubject(),s);
363 objectLeft.add(s.getObject(),s);
365 for (Statement s : unreliableRight) {
366 subjectRight.add(s.getSubject(),s);
367 objectRight.add(s.getObject(),s);
370 for (Resource left : subjectLeft.getKeys()) {
371 Resource right = comparableResources.getRight(left);
374 for (Statement leftS : subjectLeft.getValues(left)) {
375 Resource leftO = leftS.getObject();
376 if (!unreliableLeft.contains(leftS))
378 Resource rightO = comparableResources.getRight(leftO);
381 for (Statement rightS : subjectRight.getValues(right)) {
382 if (!rightS.getObject().equals(rightO))
384 if (!unreliableRight.contains(rightS))
386 if (leftS.getPredicate().equals(rightS.getPredicate()) ||
387 comparableResources.contains(leftS.getPredicate(), rightS.getPredicate())) {
388 unreliableLeft.remove(leftS);
389 unreliableRight.remove(rightS);
390 addComparable(leftS, rightS);
397 private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
398 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
399 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
400 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
401 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
403 for (Statement s : unreliableLeft) {
404 subjectLeft.add(s.getSubject(),s);
405 objectLeft.add(s.getObject(),s);
407 for (Statement s : unreliableRight) {
408 subjectRight.add(s.getSubject(),s);
409 objectRight.add(s.getObject(),s);
412 for (Resource ol : objectLeft.getKeys()) {
413 // all statements to the left side object
414 List<Statement> left = objectLeft.getValues(ol);
415 // all subjects that have statements to the left side object (ol)
416 Set<Resource> sLeft = new HashSet<Resource>();
417 // all matching subjects on the right side
418 Set<Resource> sRight = new HashSet<Resource>();
419 for (Statement s : left) {
420 sLeft.add(s.getSubject());
421 sRight.add(comparableResources.getRight(s.getSubject()));
424 // check if object left can be reliably identified by available statements
425 // if there are any objects on the left side with similar statements, object left cannot be mapped.
426 boolean hasSimilar = false;
427 MapList<Resource, Statement> comparableOLeft = new MapList<Resource, Statement>();
428 for (Resource sl : sLeft) {
429 for (Statement s : subjectLeft.getValues(sl)) {
430 if (!s.getObject().equals(ol)) {
431 comparableOLeft.add(s.getObject(),s);
436 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
438 for (Resource similarOl : comparableOLeft.getKeys()) {
439 List<Statement> similarLeft = comparableOLeft.getValues(similarOl);
440 if (similarLeft.size() == left.size()) {
441 boolean useL[] = new boolean[left.size()];
442 boolean useSL[] = new boolean[left.size()];
443 for (int i = 0; i < left.size(); i++) {
447 for (int i = 0; i < left.size(); i++) {
448 for (int j = 0; j < left.size(); j++) {
451 // compare predicates
452 Resource pl = left.get(i).getPredicate();
453 Resource psl = similarLeft.get(j).getPredicate();
454 if (pl.equals(psl)) {
455 // compare objects (unreliable result is interpreted as positive match)
457 int comp = comparator.compare(g, left.get(i).getObject(), similarLeft.get(j).getObject(), true);
458 if (comp >= 0 && comp < ResourceComparator.NO_MATCH) {
466 boolean diff = false;
467 for (int i = 0; i < left.size(); i++) {
468 if (!useL[i] || !useSL[i]) {
483 // all objects that subjects on the right side point to. Object left has its matching resource among these, if it has matching resource
484 MapList<Resource,Statement> possibleOR = new MapList<Resource, Statement>();
485 for (Resource sr : sRight) {
486 for (Statement s : subjectRight.getValues(sr))
487 possibleOR.add(s.getObject(),s);
490 // filter possible right side objects to those that have same amount of statements as the left side object
491 for (Resource or : possibleOR.getKeys().toArray(new Resource[possibleOR.getKeys().size()])) {
492 List<Statement> right = possibleOR.getValues(or);
493 if (right.size() != left.size())
494 possibleOR.remove(or);
498 // check for matching statements (comparable subjects, matching predicates)
499 MapList<Resource,Statement> matchingOR = new MapList<Resource, Statement>(); // list of objects that have matching statements
500 Map<Resource,Pair<int[], int[]>> matchingStatements = new HashMap<Resource, Pair<int[], int[]>>(); // matching statements
501 for (Resource or : possibleOR.getKeys()) {
502 List<Statement> right = possibleOR.getValues(or);
503 int iLeft[] = new int[left.size()];
504 int iRight[] = new int[right.size()];
506 for (int i = 0; i < left.size(); i++) {
511 for (int l = 0; l < left.size(); l++) {
512 Statement ls = left.get(l);
513 for (int r = 0; r < right.size(); r++) {
516 Statement rs = right.get(r);
517 if (!comparableResources.contains(ls.getSubject(), rs.getSubject()))
519 if (rcomp.compare(ls.getPredicate(),rs.getPredicate()) == 0) {
520 // compare objects (unreliable result is not accepted)
521 int comp = comparator.compare(g, ls.getObject(), rs.getObject());
522 if (comp > 0 && comp < Integer.MAX_VALUE) {
532 boolean success = true;
533 for (int i = 0; i < left.size(); i++) {
545 for (Statement s : right)
546 matchingOR.add(or,s);
547 matchingStatements.put(or, new Pair<int[], int[]>(iLeft, iRight));
550 // if there is only one matching right side object, we have found a match
551 if (matchingOR.getKeySize() == 1) {
552 Resource or = matchingOR.getKeys().iterator().next();
553 List<Statement> right = matchingOR.getValues(or);
554 Pair<int[], int[]> indices = matchingStatements.get(or);
557 objectsRight.add(or);
558 addComparable(ol, or);
559 for (int l = 0; l < left.size(); l++) {
560 int r = indices.first[l];
561 Statement sl = left.get(l);
562 Statement sr = right.get(r);
563 addComparable(sl, sr);
564 unreliableLeft.remove(sl);
565 unreliableRight.remove(sr);
577 private void processUnreliable2(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
578 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
579 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
580 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
581 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
583 for (Statement s : unreliableLeft) {
584 subjectLeft.add(s.getSubject(),s);
585 objectLeft.add(s.getObject(),s);
587 for (Statement s : unreliableRight) {
588 subjectRight.add(s.getSubject(),s);
589 objectRight.add(s.getObject(),s);
592 for (Resource ol : objectLeft.getKeys()) {
593 // all statements to the left side object
594 List<Statement> left = objectLeft.getValues(ol);
595 // all subjects that have statements to the left side object (ol)
596 Set<Resource> sLeft = new HashSet<Resource>();
597 // all matching subjects on the right side
598 Set<Resource> sRight = new HashSet<Resource>();
599 for (Statement s : left) {
600 sLeft.add(s.getSubject());
601 sRight.add(comparableResources.getRight(s.getSubject()));
604 if (sLeft.size() == 1 && sRight.size() == 1) {
605 List<Statement> ss1 = new ArrayList<Statement>(subjectLeft.getValues(sLeft.iterator().next()));
606 List<Statement> ss2 = new ArrayList<Statement>(subjectRight.getValues(sRight.iterator().next()));
608 int count = comparableStatements.size();
609 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
610 if (comparableStatements.size() > count) {
611 for (Entry<Statement, Statement> entry : comparableStatements.getEntries()) {
612 unreliableLeft.remove(entry.getKey());
613 unreliableRight.remove(entry.getValue());
620 private void processUnreliableDeep(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
621 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
622 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
623 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
624 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
626 for (Statement s : unreliableLeft) {
627 subjectLeft.add(s.getSubject(),s);
628 objectLeft.add(s.getObject(),s);
630 for (Statement s : unreliableRight) {
631 subjectRight.add(s.getSubject(),s);
632 objectRight.add(s.getObject(),s);
634 for (Resource ol : objectLeft.getKeys()) {
635 Set<Path> pathsLeft = new HashSet<Path>();
636 for (Resource rel : traversed) {
637 pathsLeft.addAll(Path.create(g.getStatements(ol, rel)));
641 if (pathsLeft.size() == 0)
643 Collection<Path> endPaths = new ArrayList<Path>(1);
644 for (Path p : pathsLeft) {
645 if (comparableResources.containsLeft(p.getEnd())) {
649 if (endPaths.size() > 0) {
651 pathsLeft.addAll(endPaths);
655 if (pathsLeft.size() > 0) {
656 Resource sl = objectLeft.getValues(ol).get(0).getSubject();
657 Resource sr = comparableResources.getRight(sl);
658 Collection<Resource> possibleOR = new ArrayList<Resource>();
659 for (Statement s : subjectRight.getValues(sr)) {
660 possibleOR.add(s.getObject());
662 Map<Resource,Set<Path>> matchingPaths = new HashMap<Resource, Set<Path>>();
663 for (Resource or : possibleOR) {
664 Set<Path> possiblePathsRight = new HashSet<Path>();
665 for (Path leftPath : pathsLeft) {
666 possiblePathsRight.addAll(findComparableRight(leftPath, or));
668 if (hasMatchingPaths(pathsLeft, possiblePathsRight)) {
669 matchingPaths.put(or, possiblePathsRight);
672 if (matchingPaths.size() > 0) {
673 if (matchingPaths.size() == 1) {
674 Resource or = matchingPaths.keySet().iterator().next();
677 objectsRight.add(or);
678 addComparable(ol, or);
679 Collection<Statement> statementsLeft = objectLeft.getValues(ol);
680 Collection<Statement> statementsRight = objectRight.getValues(or);
681 unreliableLeft.removeAll(statementsLeft);
682 unreliableRight.removeAll(statementsRight);
683 BijectionMap<Path,Path> map = getMatchingPaths(pathsLeft, matchingPaths.get(or));
684 for (Path left : map.getLeftSet()) {
685 Path right = map.getRight(left);
686 for (int i = 0; i < left.getLength(); i++) {
687 addComparable(left.getStatements().get(i),right.getStatements().get(i));
698 private boolean hasMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) throws DatabaseException {
699 if (leftPaths.size() != rightPaths.size())
701 BijectionMap<Path,Path> map = getMatchingPaths(leftPaths, rightPaths);
702 return map.size() == leftPaths.size();
705 private BijectionMap<Path,Path> getMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) throws DatabaseException {
706 BijectionMap<Path,Path> map = new BijectionMap<Path, Path>();
707 for (Path leftPath : leftPaths) {
708 for (Path rightPath : rightPaths) {
709 if (map.containsRight(rightPath))
711 if (leftPath.getLength() != rightPath.getLength())
713 if (comparableResources.contains(leftPath.getEnd(), rightPath.getEnd())) {
714 boolean match = true;
715 for (int i = 0; i < leftPath.getLength(); i++) {
716 Statement sl = leftPath.getStatements().get(i);
717 Statement sr = rightPath.getStatements().get(i);
718 if (!sl.getPredicate().equals(sr.getPredicate()) && !comparableResources.contains(sl.getPredicate(), sr.getPredicate())) {
722 if ((getComparableResources().containsLeft(sl.getObject()) || getComparableResources().containsRight(sr.getObject())) && !getComparableResources().contains(sl.getObject(), sr.getObject())) {
726 if (comparator.compare(g, sl.getObject(), sr.getObject()) == ResourceComparator.NO_MATCH) {
732 map.map(leftPath, rightPath);
741 private void expand(Set<Path> paths) throws DatabaseException {
742 Set<Path> stepPathsLeft = new HashSet<Path>();
743 if (paths.size() == 0)
745 int length = paths.iterator().next().getLength() + 1;
746 for (Path p : paths) {
747 for (Resource rel : traversed) {
748 stepPathsLeft.addAll(Path.expand(p,g.getStatements(p.getEnd(), rel)));
752 for (Path p : stepPathsLeft) {
753 if (p.getLength() == length)
758 private Collection<Path> findComparableRight(Path leftPath, Resource beginRight) throws DatabaseException {
759 Set<Path> rightPaths = new HashSet<Path>();
760 rightPaths.addAll(Path.create(g.getStatements(beginRight, getRight(leftPath.getStatements().get(0).getPredicate()))));
761 for (int i = 1; i < leftPath.getLength(); i++) {
762 if (rightPaths.size() == 0)
764 Set<Path> stepPaths = new HashSet<Path>();
765 for (Path p : rightPaths) {
766 stepPaths.addAll(Path.expand(p, g.getStatements(p.getEnd(), getRight(leftPath.getStatements().get(i).getPredicate()))));
769 for (Path p : stepPaths)
770 if (p.getLength() == i+1)
777 private Resource getRight(Resource r) {
778 if (comparableResources.containsLeft(r))
779 return comparableResources.getRight(r);
785 public BijectionMap<Statement, Statement> getComparableStatements() {
786 return comparableStatements;
789 public BijectionMap<Resource, Resource> getComparableResources() {
790 return comparableResources;
793 public GraphChanges getChanges() {
794 return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources);
797 public List<Statement> getChanges1() {
801 public List<Statement> getChanges2() {
805 private void addComparable(Statement left, Statement right) throws DatabaseException {
806 addComparable(left.getObject(), right.getObject());
807 comparableStatements.map(left, right);
808 //comparableResources.map(left.getObject(), right.getObject());
811 private void addComparable(Resource left, Resource right) throws DatabaseException {
812 if(!comparableResources.contains(left, right)) {
813 if (comparableResources.containsLeft(left)||comparableResources.containsRight(right)) {
814 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.");
816 if (DEBUG) System.out.println(left + " = " + right);
817 comparableResources.map(left, right);
823 public List<Statement> filterAsserted(Resource r, Collection<Statement> in) throws DatabaseException {
824 List<Statement> out = new ArrayList<Statement>();
825 for (Statement s : in) {
826 if (!s.isAsserted(r))
833 public List<Statement> filterAssertedDuplicates(Resource r, List<Statement> in) throws DatabaseException {
834 List<Statement> out = new ArrayList<Statement>();
835 for (int i = 0; i < in.size(); i++) {
836 Statement s = in.get(i);
837 if (!s.isAsserted(r))
841 if (i > 0 && in.get(i-1).getPredicate().equals(s.getPredicate()))
843 else if (i < in.size()-1 && in.get(i+1).getPredicate().equals(s.getPredicate()))
855 private String printStatement(ReadGraph graph, Statement s) throws DatabaseException {
856 return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject());
859 private List<Statement> filterTraversed(List<Statement> in) throws DatabaseException {
860 return filter(traversed, in);
863 private List<Statement> filterNonTested(List<Statement> in) throws DatabaseException {
864 return filter(nonTested, in);
867 private List<Statement> filterNonTraversed(List<Statement> in) throws DatabaseException {
868 return filter(nonTraversed, in);
871 private List<Statement> filter(Collection<Resource> toFilter, List<Statement> in) throws DatabaseException {
872 if (toFilter.size() == 0)
874 List<Statement> out = new ArrayList<Statement>();
875 for (Statement s : in) {
876 boolean usable = true;
877 for (Resource r : toFilter) {
878 if (g.isSubrelationOf(s.getPredicate(),r)) {
892 private void addDeletion(Statement s) {
893 if (!changes1Set.contains(s)) {
899 private void addAddition(Statement s) {
900 if (!changes2Set.contains(s)) {
906 private void addModification(Resource left, Statement leftstm, Resource right, Statement rightstm) {
907 Modification mod = new Modification(left, right, leftstm, rightstm);
908 if (!modificationsSet.contains(mod)) {
909 modificationsSet.add(mod);
910 modifications.add(mod);
914 public void sortStatement(List<Statement> list1, List<Statement> list2) {
915 sortStatement(list1, list2, scomp);
918 public void sortStatement(List<Statement> list1, List<Statement> list2, Comparator<Statement> scomp) {
919 Collections.sort(list1,scomp);
920 Collections.sort(list2,scomp);
922 List<Statement> sorted1 = new ArrayList<Statement>(list1.size());
923 List<Statement> sorted2 = new ArrayList<Statement>(list2.size());
924 sorted1.addAll(list1);
925 sorted2.addAll(list2);
929 for (int i = 0; i < list1.size(); ) {
930 Statement s1 = list1.get(i);
931 int same1 = sameRel(list1, i);
932 for (int j = 0; j < list2.size(); j++) {
933 Statement s2 = list2.get(j);
934 if (scomp.compare(s1, s2) == 0) {
935 int same2 = sameRel(list2, j);
936 copy(sorted1,ss1,list1,i,same1);
938 copy(sorted2,ss2,list2,j,same2);
945 if (ss1 < sorted1.size()) {
946 for (Statement s : list1) {
947 if (!sorted1.contains(s)) {
953 if (ss2 < sorted2.size()) {
954 for (Statement s : list2) {
955 if (!sorted2.contains(s)) {
964 list1.addAll(sorted1);
965 list2.addAll(sorted2);
968 public <T> void copy(List<T> to, int toIndex, List<T> from, int fromIndex, int amount) {
969 for (int i = 0; i < amount; i++) {
970 to.set(toIndex + i, from.get(fromIndex+ i));
974 public void sortResource(List<Resource> list1, List<Resource> list2) {
975 Collections.sort(list1,rcomp);
977 for (int i = 0; i < list1.size(); i++) {
978 Resource s1 = list1.get(i);
979 for (int j = js; j < list2.size(); j++) {
980 Resource s2 = list2.get(j);
981 if (rcomp.compare(s1, s2) == 0) {
982 Resource t = list2.get(js);
992 private void compareStatements(List<Statement> ss1, List<Statement> ss2, Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Collection<Statement> unreliableLeft, Collection<Statement> unreliableRight) throws DatabaseException {
993 sortStatement(ss1, ss2);
999 if (i1 >= ss1.size()) {
1000 if (i2 >= ss2.size()) {
1003 while (i2 < ss2.size()) {
1004 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i2)));
1006 addAddition(ss2.get(i2));
1011 } else if (i2 >= ss2.size()) {
1012 while (i1 < ss1.size()) {
1013 if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i1)));
1014 addDeletion(ss1.get(i1));
1019 int same1 = sameRel(ss1, i1);
1020 int same2 = sameRel(ss2, i2);
1021 int c = rcomp.compare(ss1.get(i1).getPredicate(),ss2.get(i2).getPredicate());
1023 compareStatements(ss1, i1, same1, ss2, i2, same2,objectsLeft,objectsRight,unreliableLeft,unreliableRight);
1027 for (int i = 0; i < same1; i++) {
1028 if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i+i1)));
1029 addDeletion(ss1.get(i+i1));
1033 for (int i = 0; i < same2; i++) {
1034 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i+i2)));
1035 addAddition(ss2.get(i+i2));
1045 private int sameRel(List<Statement> statements, int off) {
1046 if (statements.size() <= off)
1049 long id = statements.get(off).getPredicate().getResourceId();
1050 for (int i = off+1; i <statements.size(); i++) {
1051 if (statements.get(i).getPredicate().getResourceId() == id)
1060 private int compareObject(Resource o1, Resource o2) throws DatabaseException {
1063 if (comparableResources.contains(o1, o2))
1065 if (comparableResources.containsLeft(o1))
1066 return Integer.MAX_VALUE;
1067 if (comparableResources.containsRight(o2))
1068 return Integer.MAX_VALUE;
1069 if (nonMatchedLeft.contains(o1))
1070 return Integer.MAX_VALUE;
1071 if (nonMatchedRight.contains(o2))
1072 return Integer.MAX_VALUE;
1073 return comparator.compare(g, o1, o2);
1076 private void compareStatements(List<Statement> ss1, int off1, int len1, List<Statement> ss2, int off2, int len2, Collection<Resource> objectsLeft, Collection<Resource> objectsRight, Collection<Statement> unreliableLeft, Collection<Statement> unreliableRight) throws DatabaseException {
1077 boolean[] used1 = new boolean[len1];
1078 for (int i = 0; i < used1.length; i++) {
1082 boolean[] used2 = new boolean[len2];
1083 for (int i = 0; i < used2.length; i++) {
1087 // left, right, difference
1088 List<List<Integer>> differences = new ArrayList<List<Integer>>();
1089 for (int i1 = off1; i1 < off1 + len1; i1++) {
1090 Statement s1 = ss1.get(i1);
1091 List<Integer> diff = new ArrayList<Integer>();
1092 for (int i2 = off2; i2 < off2 + len2; i2++) {
1093 Statement s2 = ss2.get(i2);
1094 int d = compareObject(s1.getObject(), s2.getObject());
1096 for (Resource t : strong) {
1097 if (s1.getPredicate().equals(t) || g.isSubrelationOf(s1.getPredicate(), t)) {
1105 differences.add(diff);
1108 MapList<Integer, Integer> priorities = new MapList<Integer, Integer>();
1109 for (int i = 0; i < differences.size(); i++) {
1110 List<Integer> list = differences.get(i);
1111 for (int j = 0; j < list.size(); j++) {
1112 priorities.add(list.get(j), i);
1116 Integer[] pris = priorities.getKeys(new Integer[]{});
1118 boolean matchFail = priorityMatching(ss1, off1, len1, ss2, off2, len2, pris, differences, priorities, used1, used2, objectsLeft, objectsRight, false);
1120 matchFail = priorityMatching(ss1, off1, len1, ss2, off2, len2, pris, differences, priorities, used1, used2, objectsLeft, objectsRight, objectsLeft == null);
1122 for (Integer pri : pris) {
1123 if (pri != 0 && !matchFail && unreliableLeft == null)
1125 Set<Statement> s1s = new HashSet<Statement>();
1126 Set<Statement> s2s = new HashSet<Statement>();
1127 Set<Integer> s1i = new HashSet<Integer>();
1128 Set<Integer> s2i = new HashSet<Integer>();
1129 List<Integer> i1s = priorities.getValues(pri);
1130 for (Integer i1 : i1s) {
1133 List<Integer> i2diff = differences.get(i1);
1134 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1135 if (i2diff.get(i2) == pri) {
1138 Statement s1 = ss1.get(i1+off1);
1139 Statement s2 = ss2.get(i2+off2);
1147 if (unreliableLeft != null) {
1148 unreliableLeft.addAll(s1s);
1149 unreliableRight.addAll(s2s);
1151 for (Integer i : s1i)
1153 for (Integer i : s2i)
1157 for (int i1 = off1; i1 < off1 + len1; i1++) {
1158 if (!used1[i1-off1]) {
1159 if (DEBUG) System.out.println("Compare Object deletion " + printStatement(g,ss1.get(i1)));
1160 addDeletion(ss1.get(i1));
1163 for (int i2 = off2; i2 < off2 + len2; i2++) {
1164 if (!used2[i2-off2]) {
1165 if (DEBUG) System.out.println("Compare Object addition " + printStatement(g,ss2.get(i2)));
1166 addAddition(ss2.get(i2));
1171 private boolean priorityMatching(List<Statement> ss1, int off1, int len1, List<Statement> ss2, int off2, int len2, Integer[] pris, List<List<Integer>> differences, MapList<Integer, Integer> priorities, boolean used1[],boolean used2[], Collection<Resource> objectsLeft, Collection<Resource> objectsRight, boolean force) throws DatabaseException {
1172 boolean matchFail = false;
1173 for (Integer pri : pris) {
1174 if (pri == Integer.MAX_VALUE) {
1176 } else if (pri == 0) {
1179 List<Integer> i1s = priorities.getValues(pri);
1181 for (Integer i1 : i1s) {
1184 List<Integer> i2diff = differences.get(i1);
1185 List<Integer> matches = new ArrayList<Integer>();
1186 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1187 if (i2diff.get(i2) == pri) {
1193 if (matches.size() == 1 || (force && matches.size() > 1)) {
1194 int i2 = matches.get(0);
1197 Statement s1 = ss1.get(i1+off1);
1198 Statement s2 = ss2.get(i2+off2);
1200 if (objectsLeft != null) {
1201 objectsLeft.add(s1.getObject());
1202 objectsRight.add(s2.getObject());
1204 addComparable(s1, s2);
1205 } else if (matches.size() > 1) {
1218 * compares properties, assumes functional relations
1221 * @throws ServiceException
1222 * @throws DoesNotContainValueException
1223 * @throws ValidationException
1225 private void compareProps(Resource r1, Resource r2) throws DatabaseException {
1226 if (DEBUG) System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2));
1227 List<Statement> ss1 = new ArrayList<Statement>();
1228 List<Statement> ss2 = new ArrayList<Statement>();
1229 ss1.addAll(g.getStatements(r1, b.HasProperty));
1230 ss2.addAll(g.getStatements(r2, b.HasProperty));
1231 ss1 = filterNonTested(ss1);
1232 ss2 = filterNonTested(ss2);
1233 sortStatement(ss1, ss2);
1234 // getStatements(r1, b.HasProperty) returns both instance and asserted statements for the same property relation.
1235 ss1 = filterAssertedDuplicates(r1, ss1);
1236 ss2 = filterAssertedDuplicates(r2, ss2);
1242 if (i1 >= ss1.size()) {
1243 if (i2 >= ss2.size())
1246 while (i2 < ss2.size()) {
1247 Statement s = ss2.get(i2);
1248 if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,s));
1249 if (!s.isAsserted(r2))
1255 } else if (i2 >= ss2.size()) {
1256 while (i1 < ss1.size()) {
1257 Statement s = ss1.get(i1);
1258 if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,s));
1259 if (!s.isAsserted(r1))
1265 Statement s1 = ss1.get(i1);
1266 Statement s2 = ss2.get(i2);
1267 if (s1.isAsserted(r1) && s2.isAsserted(r2)) {
1272 int c = scomp.compare(s1, s2);
1275 boolean b1 = g.hasValue(s1.getObject());
1276 boolean b2 = g.hasValue(s2.getObject());
1277 boolean a1 = s1.isAsserted(r1);
1278 boolean a2 = s2.isAsserted(r2);
1282 boolean eq = compareValue(g,b,s1.getObject(), s2.getObject());
1284 addModification(r1,s1,r2,s2);
1286 addComparable(s1, s2);
1289 // Non literal properties.
1290 int comp = comparator.compare(g, s1.getObject(), s2.getObject());
1291 if (comp == ResourceComparator.NO_MATCH) {
1292 addModification(r1,s1,r2,s2);
1293 } else if (comp != ResourceComparator.EXACT_MATCH) {
1294 if (!s1.getObject().equals(s1.getSubject()) && !s2.getObject().equals(s2.getSubject())) {
1296 // compare props matches objects, so we can call that only for non asserted statements
1297 compareProps(s1.getObject(), s2.getObject());
1298 } else if (a1 && a2) {
1299 // TODO : compare asserted statements?
1301 addModification(r1,s1,r2,s2);
1304 addModification(r1,s1,r2,s2);
1307 // Exact match, nothing to do.
1311 addModification(r1,s1,r2,s2);
1318 if (DEBUG) System.out.println("Compare Prop diff1s " + printStatement(g,s1));
1319 // Use modification, since deletions do not support asserted statements
1320 addModification(r1,s1,r2,null);
1327 if (DEBUG) System.out.println("Compare Prop diff2s " + printStatement(g,s2));
1328 // Use modification, since additions do not support asserted statements
1329 addModification(r1,null,r2,s2);
1343 public static boolean compareValue(ReadGraph g, Layer0 b, Resource r1, Resource r2) throws DatabaseException {
1344 Resource t1 = g.getSingleType(r1);
1345 Resource t2 = g.getSingleType(r2);
1348 if (t1.equals(b.Integer)) {
1349 int v1 = g.getValue(r1, Bindings.INTEGER);
1350 int v2 = g.getValue(r2, Bindings.INTEGER);
1352 } else if (t1.equals(b.Float)) {
1353 float v1 = g.getValue(r1, Bindings.FLOAT);
1354 float v2 = g.getValue(r2, Bindings.FLOAT);
1356 } else if (t1.equals(b.Double)) {
1357 double v1 = g.getValue(r1, Bindings.DOUBLE);
1358 double v2 = g.getValue(r2, Bindings.DOUBLE);
1360 } else if (t1.equals(b.String)) {
1361 String v1 = g.getValue(r1, Bindings.STRING);
1362 String v2 = g.getValue(r2, Bindings.STRING);
1363 return v1.equals(v2);
1364 } else if (t1.equals(b.Boolean)) {
1365 boolean v1 = g.getValue(r1, Bindings.BOOLEAN);
1366 boolean v2 = g.getValue(r2, Bindings.BOOLEAN);
1368 } else if (t1.equals(b.Byte)) {
1369 byte v1 = g.getValue(r1, Bindings.BYTE);
1370 byte v2 = g.getValue(r2, Bindings.BYTE);
1372 } else if (t1.equals(b.Long)) {
1373 long v1 = g.getValue(r1, Bindings.LONG);
1374 long v2 = g.getValue(r2, Bindings.LONG);
1376 } else if (t1.equals(b.IntegerArray)) {
1377 int[] v1 = g.getValue(r1, Bindings.INT_ARRAY);
1378 int[] v2 = g.getValue(r2, Bindings.INT_ARRAY);
1379 return Arrays.equals(v1,v2);
1380 } else if (t1.equals(b.FloatArray)) {
1381 float[] v1 = g.getValue(r1, Bindings.FLOAT_ARRAY);
1382 float[] v2 = g.getValue(r2, Bindings.FLOAT_ARRAY);
1383 return Arrays.equals(v1,v2);
1384 } else if (t1.equals(b.DoubleArray)) {
1385 double[] v1 = g.getValue(r1, Bindings.DOUBLE_ARRAY);
1386 double[] v2 = g.getValue(r2, Bindings.DOUBLE_ARRAY);
1387 return Arrays.equals(v1,v2);
1388 } else if (t1.equals(b.StringArray)) {
1389 String[] v1 = g.getValue(r1, Bindings.STRING_ARRAY);
1390 String[] v2 = g.getValue(r2, Bindings.STRING_ARRAY);
1391 return Arrays.equals(v1,v2);
1392 } else if (t1.equals(b.BooleanArray)) {
1393 boolean[] v1 = g.getValue(r1, Bindings.BOOLEAN_ARRAY);
1394 boolean[] v2 = g.getValue(r2, Bindings.BOOLEAN_ARRAY);
1395 return Arrays.equals(v1,v2);
1396 } else if (t1.equals(b.ByteArray)) {
1397 byte[] v1 = g.getValue(r1, Bindings.BYTE_ARRAY);
1398 byte[] v2 = g.getValue(r2, Bindings.BYTE_ARRAY);
1399 return Arrays.equals(v1,v2);
1400 } else if (t1.equals(b.LongArray)) {
1401 long[] v1 = g.getValue(r1, Bindings.LONG_ARRAY);
1402 long[] v2 = g.getValue(r2, Bindings.LONG_ARRAY);
1403 return Arrays.equals(v1,v2);
1405 Object v1 = g.getValue(r1);
1406 Object v2 = g.getValue(r2);
1407 return compareValue(v1, v2);
1412 public static boolean compareValue(Object v1, Object v2) {
1413 if (v1 instanceof Object[] && v2 instanceof Object[])
1414 return Arrays.deepEquals((Object[])v1, (Object[])v2);
1415 else if (v1 instanceof int[] && v2 instanceof int[])
1416 return Arrays.equals((int[])v1, (int[])v2);
1417 else if (v1 instanceof float[] && v2 instanceof float[])
1418 return Arrays.equals((float[])v1, (float[])v2);
1419 else if (v1 instanceof double[] && v2 instanceof double[])
1420 return Arrays.equals((double[])v1, (double[])v2);
1421 else if (v1 instanceof long[] && v2 instanceof long[])
1422 return Arrays.equals((long[])v1, (long[])v2);
1423 else if (v1 instanceof byte[] && v2 instanceof byte[])
1424 return Arrays.equals((byte[])v1, (byte[])v2);
1425 else if (v1 instanceof boolean[] && v2 instanceof boolean[])
1426 return Arrays.equals((boolean[])v1, (boolean[])v2);
1428 return v1.equals(v2);
1432 public class PredicateComparator implements Comparator<Statement> {
1434 public int compare(Statement o1, Statement o2) {
1435 if (comparableResources.contains(o1.getPredicate(), o2.getPredicate()))
1437 if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1439 if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1445 public class SubjectComparator implements Comparator<Statement> {
1447 public int compare(Statement o1, Statement o2) {
1448 if (comparableResources.contains(o1.getSubject(), o2.getSubject()))
1450 if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1452 if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1458 public class ObjectComparator implements Comparator<Statement> {
1460 public int compare(Statement o1, Statement o2) {
1461 if (comparableResources.contains(o1.getObject(), o2.getObject()))
1463 if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1465 if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1471 public static class FullStatementComparator implements Comparator<Statement> {
1473 public int compare(Statement o1, Statement o2) {
1474 if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1476 if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1478 if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1480 if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1482 if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1484 if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1490 public class ResComparator implements Comparator<Resource> {
1492 public int compare(Resource o1, Resource o2) {
1493 if (comparableResources.contains(o1, o2))
1495 if (o1.getResourceId() < o2.getResourceId())
1497 if (o1.getResourceId() > o2.getResourceId())