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) {
155 comparableResources.map(r1, r2);
158 public void addComparableResources(BijectionMap<Resource, Resource> matching) {
159 comparableResources.addAll(matching);
162 public void addStrong(Resource r) {
166 public void addStrong(Collection<Resource> rels) {
170 public void addNonMatchedLeft(Resource r) {
171 nonMatchedLeft.add(r);
174 public void addNonMatchedRight(Resource r) {
175 nonMatchedRight.add(r);
178 public void test(ReadGraph g) throws DatabaseException {
180 this.b = Layer0.getInstance(g);
181 comparator.setComparator(this);
182 comparator.initialize(g, r1, r2);
184 Stack<Resource> objectsLeft = new Stack<Resource>();
185 Stack<Resource> objectsRight = new Stack<Resource>();
186 objectsLeft.push(r1);
187 objectsRight.push(r2);
189 Set<Statement> unreliableLeft = new HashSet<Statement>();
190 Set<Statement> unreliableRight = new HashSet<Statement>();
193 if (objectsLeft.isEmpty())
197 // process compares objects that are identified and searches for more resources to process.
198 process(objectsLeft, objectsRight, unreliableLeft, unreliableRight);
199 // process unreliable handles cases where unidentified statements subject and object have been identified
200 processUnreliable(unreliableLeft, unreliableRight);
201 // process unreliable handles cases where unidentified resources have path of length one to identified resource
202 processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
203 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
204 processUnreliable2(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
206 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
207 // comparison is ending, but we have still unprocessed unidentified resources left.
208 // These cases have longer path than one to identified objects.
209 processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
213 for (Statement s : unreliableLeft) {
214 if (!comparableStatements.containsLeft(s))
217 for (Statement s : unreliableRight) {
218 if (!comparableStatements.containsRight(s))
223 public void test(Session session) throws DatabaseException {
224 test(session, r1, r2);
227 public void test(Session session, Resource r1, Resource r2) throws DatabaseException {
229 comparator.setComparator(this);
231 session.syncRequest(new ReadRequest() {
234 public void run(ReadGraph graph) throws DatabaseException {
235 comparator.initialize(graph, r1, r2);
239 addComparable(r1, r2);
241 final Stack<Resource> objectsLeft = new Stack<Resource>();
242 final Stack<Resource> objectsRight = new Stack<Resource>();
243 objectsLeft.push(r1);
244 objectsRight.push(r2);
246 final Set<Statement> unreliableLeft = new HashSet<Statement>();
247 final Set<Statement> unreliableRight = new HashSet<Statement>();
250 if (objectsLeft.isEmpty())
252 session.syncRequest(new ReadRequest() {
255 public void run(ReadGraph graph) throws DatabaseException {
257 b = Layer0.getInstance(graph);
258 // process compares objects that are identified and searches for more resources to process.
259 process(objectsLeft, objectsRight, unreliableLeft, unreliableRight);
260 // process unreliable handles cases where unidentified statements subject and object have been identified
261 processUnreliable(unreliableLeft, unreliableRight);
262 // process unreliable handles cases where unidentified resources have path of length one to identified resource
263 processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
264 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
265 processUnreliable2(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
267 if (objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
268 // comparison is ending, but we have still unprocessed unidentified resources left.
269 // These cases have longer path than one to identified objects.
270 processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
278 for (Statement s : unreliableLeft) {
279 if (!comparableStatements.containsLeft(s))
280 if (s.getObject().getResourceId() == 303248)
281 System.out.println();
284 for (Statement s : unreliableRight) {
285 if (!comparableStatements.containsRight(s))
292 private void process(Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
293 List<Statement> ss1 = new ArrayList<Statement>();
294 List<Statement> ss2 = new ArrayList<Statement>();
296 while (!objectsLeft.isEmpty()) {
297 Resource r1 = objectsLeft.pop();
298 Resource r2 = objectsRight.pop();
303 if (processedResources.contains(r1))
305 processedResources.add(r1);
308 if((comparableResources.containsLeft(r1)||comparableResources.containsRight(r2)) && !comparableResources.contains(r1, r2)) {
309 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.");
311 addComparable(r1, r2);
313 //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));
314 compareProps(r1, r2);
316 for (Resource rel : tested) {
317 ss1.addAll(g.getStatements(r1, rel));
318 ss2.addAll(g.getStatements(r2, rel));
319 ss1 = filterAsserted(r1, ss1);
320 ss2 = filterAsserted(r2, ss2);
321 ss1 = filterTraversed(ss1);
322 ss2 = filterTraversed(ss2);
323 ss1 = filterNonTested(ss1);
324 ss2 = filterNonTested(ss2);
327 compareStatements(ss1, ss2, null, null,null,null);
332 for (Resource rel : traversed) {
333 ss1.addAll(g.getStatements(r1, rel));
334 ss2.addAll(g.getStatements(r2, rel));
335 ss1 = filterAsserted(r1, ss1);
336 ss2 = filterAsserted(r2, ss2);
337 ss1 = filterNonTraversed(ss1);
338 ss2 = filterNonTraversed(ss2);
339 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
347 private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
348 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
349 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
350 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
351 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
353 for (Statement s : unreliableLeft) {
354 subjectLeft.add(s.getSubject(),s);
355 objectLeft.add(s.getObject(),s);
357 for (Statement s : unreliableRight) {
358 subjectRight.add(s.getSubject(),s);
359 objectRight.add(s.getObject(),s);
362 for (Resource left : subjectLeft.getKeys()) {
363 Resource right = comparableResources.getRight(left);
366 for (Statement leftS : subjectLeft.getValues(left)) {
367 Resource leftO = leftS.getObject();
368 if (!unreliableLeft.contains(leftS))
370 Resource rightO = comparableResources.getRight(leftO);
373 for (Statement rightS : subjectRight.getValues(right)) {
374 if (!rightS.getObject().equals(rightO))
376 if (!unreliableRight.contains(rightS))
378 if (leftS.getPredicate().equals(rightS.getPredicate()) ||
379 comparableResources.contains(leftS.getPredicate(), rightS.getPredicate())) {
380 unreliableLeft.remove(leftS);
381 unreliableRight.remove(rightS);
382 addComparable(leftS, rightS);
389 private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
390 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
391 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
392 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
393 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
395 for (Statement s : unreliableLeft) {
396 subjectLeft.add(s.getSubject(),s);
397 objectLeft.add(s.getObject(),s);
399 for (Statement s : unreliableRight) {
400 subjectRight.add(s.getSubject(),s);
401 objectRight.add(s.getObject(),s);
404 for (Resource ol : objectLeft.getKeys()) {
405 // all statements to the left side object
406 List<Statement> left = objectLeft.getValues(ol);
407 // all subjects that have statements to the left side object (ol)
408 Set<Resource> sLeft = new HashSet<Resource>();
409 // all matching subjects on the right side
410 Set<Resource> sRight = new HashSet<Resource>();
411 for (Statement s : left) {
412 sLeft.add(s.getSubject());
413 sRight.add(comparableResources.getRight(s.getSubject()));
416 // check if object left can be reliably identified by available statements
417 // if there are any objects on the left side with similar statements, object left cannot be mapped.
418 boolean hasSimilar = false;
419 MapList<Resource, Statement> comparableOLeft = new MapList<Resource, Statement>();
420 for (Resource sl : sLeft) {
421 for (Statement s : subjectLeft.getValues(sl)) {
422 if (!s.getObject().equals(ol)) {
423 comparableOLeft.add(s.getObject(),s);
428 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
430 for (Resource similarOl : comparableOLeft.getKeys()) {
431 List<Statement> similarLeft = comparableOLeft.getValues(similarOl);
432 if (similarLeft.size() == left.size()) {
433 boolean useL[] = new boolean[left.size()];
434 boolean useSL[] = new boolean[left.size()];
435 for (int i = 0; i < left.size(); i++) {
439 for (int i = 0; i < left.size(); i++) {
440 for (int j = 0; j < left.size(); j++) {
443 // compare predicates
444 Resource pl = left.get(i).getPredicate();
445 Resource psl = similarLeft.get(j).getPredicate();
446 if (pl.equals(psl)) {
447 // compare objects (unreliable result is interpreted as positive match)
449 int comp = comparator.compare(g, left.get(i).getObject(), similarLeft.get(j).getObject(), true);
450 if (comp >= 0 && comp < Integer.MAX_VALUE) {
458 boolean diff = false;
459 for (int i = 0; i < left.size(); i++) {
460 if (!useL[i] || !useSL[i]) {
475 // all objects that subjects on the right side point to. Object left has its matching resource among these, if it has matching resource
476 MapList<Resource,Statement> possibleOR = new MapList<Resource, Statement>();
477 for (Resource sr : sRight) {
478 for (Statement s : subjectRight.getValues(sr))
479 possibleOR.add(s.getObject(),s);
482 // filter possible right side objects to those that have same amount of statements as the left side object
483 for (Resource or : possibleOR.getKeys().toArray(new Resource[possibleOR.getKeys().size()])) {
484 List<Statement> right = possibleOR.getValues(or);
485 if (right.size() != left.size())
486 possibleOR.remove(or);
490 // check for matching statements (comparable subjects, matching predicates)
491 MapList<Resource,Statement> matchingOR = new MapList<Resource, Statement>(); // list of objects that have matching statements
492 Map<Resource,Pair<int[], int[]>> matchingStatements = new HashMap<Resource, Pair<int[], int[]>>(); // matching statements
493 for (Resource or : possibleOR.getKeys()) {
494 List<Statement> right = possibleOR.getValues(or);
495 int iLeft[] = new int[left.size()];
496 int iRight[] = new int[right.size()];
498 for (int i = 0; i < left.size(); i++) {
503 for (int l = 0; l < left.size(); l++) {
504 Statement ls = left.get(l);
505 for (int r = 0; r < right.size(); r++) {
508 Statement rs = right.get(r);
509 if (!comparableResources.contains(ls.getSubject(), rs.getSubject()))
511 if (rcomp.compare(ls.getPredicate(),rs.getPredicate()) == 0) {
512 // compare objects (unreliable result is not accepted)
513 int comp = comparator.compare(g, ls.getObject(), rs.getObject());
514 if (comp > 0 && comp < Integer.MAX_VALUE) {
524 boolean success = true;
525 for (int i = 0; i < left.size(); i++) {
537 for (Statement s : right)
538 matchingOR.add(or,s);
539 matchingStatements.put(or, new Pair<int[], int[]>(iLeft, iRight));
542 // if there is only one matching right side object, we have found a match
543 if (matchingOR.getKeySize() == 1) {
544 Resource or = matchingOR.getKeys().iterator().next();
545 List<Statement> right = matchingOR.getValues(or);
546 Pair<int[], int[]> indices = matchingStatements.get(or);
549 objectsRight.add(or);
550 addComparable(ol, or);
551 for (int l = 0; l < left.size(); l++) {
552 int r = indices.first[l];
553 Statement sl = left.get(l);
554 Statement sr = right.get(r);
555 addComparable(sl, sr);
556 unreliableLeft.remove(sl);
557 unreliableRight.remove(sr);
569 private void processUnreliable2(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
570 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
571 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
572 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
573 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
575 for (Statement s : unreliableLeft) {
576 subjectLeft.add(s.getSubject(),s);
577 objectLeft.add(s.getObject(),s);
579 for (Statement s : unreliableRight) {
580 subjectRight.add(s.getSubject(),s);
581 objectRight.add(s.getObject(),s);
584 for (Resource ol : objectLeft.getKeys()) {
585 // all statements to the left side object
586 List<Statement> left = objectLeft.getValues(ol);
587 // all subjects that have statements to the left side object (ol)
588 Set<Resource> sLeft = new HashSet<Resource>();
589 // all matching subjects on the right side
590 Set<Resource> sRight = new HashSet<Resource>();
591 for (Statement s : left) {
592 sLeft.add(s.getSubject());
593 sRight.add(comparableResources.getRight(s.getSubject()));
596 if (sLeft.size() == 1 && sRight.size() == 1) {
597 List<Statement> ss1 = new ArrayList<Statement>(subjectLeft.getValues(sLeft.iterator().next()));
598 List<Statement> ss2 = new ArrayList<Statement>(subjectRight.getValues(sRight.iterator().next()));
600 int count = comparableStatements.size();
601 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
602 if (comparableStatements.size() > count) {
603 for (Entry<Statement, Statement> entry : comparableStatements.getEntries()) {
604 unreliableLeft.remove(entry.getKey());
605 unreliableRight.remove(entry.getValue());
612 private void processUnreliableDeep(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
613 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
614 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
615 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
616 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
618 for (Statement s : unreliableLeft) {
619 subjectLeft.add(s.getSubject(),s);
620 objectLeft.add(s.getObject(),s);
622 for (Statement s : unreliableRight) {
623 subjectRight.add(s.getSubject(),s);
624 objectRight.add(s.getObject(),s);
626 for (Resource ol : objectLeft.getKeys()) {
627 Set<Path> pathsLeft = new HashSet<Path>();
628 for (Resource rel : traversed) {
629 pathsLeft.addAll(Path.create(g.getStatements(ol, rel)));
633 if (pathsLeft.size() == 0)
635 Collection<Path> endPaths = new ArrayList<Path>(1);
636 for (Path p : pathsLeft) {
637 if (comparableResources.containsLeft(p.getEnd())) {
641 if (endPaths.size() > 0) {
643 pathsLeft.addAll(endPaths);
647 if (pathsLeft.size() > 0) {
648 Resource sl = objectLeft.getValues(ol).get(0).getSubject();
649 Resource sr = comparableResources.getRight(sl);
650 Collection<Resource> possibleOR = new ArrayList<Resource>();
651 for (Statement s : subjectRight.getValues(sr)) {
652 possibleOR.add(s.getObject());
654 Map<Resource,Set<Path>> matchingPaths = new HashMap<Resource, Set<Path>>();
655 for (Resource or : possibleOR) {
656 Set<Path> possiblePathsRight = new HashSet<Path>();
657 for (Path leftPath : pathsLeft) {
658 possiblePathsRight.addAll(findComparableRight(leftPath, or));
660 if (hasMatchingPaths(pathsLeft, possiblePathsRight)) {
661 matchingPaths.put(or, possiblePathsRight);
664 if (matchingPaths.size() > 0) {
665 if (matchingPaths.size() == 1) {
666 Resource or = matchingPaths.keySet().iterator().next();
669 objectsRight.add(or);
670 addComparable(ol, or);
671 Collection<Statement> statementsLeft = objectLeft.getValues(ol);
672 Collection<Statement> statementsRight = objectRight.getValues(or);
673 unreliableLeft.removeAll(statementsLeft);
674 unreliableRight.removeAll(statementsRight);
675 BijectionMap<Path,Path> map = getMatchingPaths(pathsLeft, matchingPaths.get(or));
676 for (Path left : map.getLeftSet()) {
677 Path right = map.getRight(left);
678 for (int i = 0; i < left.getLength(); i++) {
679 addComparable(left.getStatements().get(i),right.getStatements().get(i));
690 private boolean hasMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {
691 if (leftPaths.size() != rightPaths.size())
693 BijectionMap<Path,Path> map = getMatchingPaths(leftPaths, rightPaths);
694 return map.size() == leftPaths.size();
697 private BijectionMap<Path,Path> getMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {
698 BijectionMap<Path,Path> map = new BijectionMap<Path, Path>();
699 for (Path leftPath : leftPaths) {
700 for (Path rightPath : rightPaths) {
701 if (map.containsRight(rightPath))
703 if (leftPath.getLength() != rightPath.getLength())
705 if (comparableResources.contains(leftPath.getEnd(), rightPath.getEnd())) {
706 map.map(leftPath, rightPath);
714 private void expand(Set<Path> paths) throws DatabaseException {
715 Set<Path> stepPathsLeft = new HashSet<Path>();
716 if (paths.size() == 0)
718 int length = paths.iterator().next().getLength() + 1;
719 for (Path p : paths) {
720 for (Resource rel : traversed) {
721 stepPathsLeft.addAll(Path.expand(p,g.getStatements(p.getEnd(), rel)));
725 for (Path p : stepPathsLeft) {
726 if (p.getLength() == length)
731 private Collection<Path> findComparableRight(Path leftPath, Resource beginRight) throws DatabaseException {
732 Set<Path> rightPaths = new HashSet<Path>();
733 rightPaths.addAll(Path.create(g.getStatements(beginRight, getRight(leftPath.getStatements().get(0).getPredicate()))));
734 for (int i = 1; i < leftPath.getLength(); i++) {
735 if (rightPaths.size() == 0)
737 Set<Path> stepPaths = new HashSet<Path>();
738 for (Path p : rightPaths) {
739 stepPaths.addAll(Path.expand(p, g.getStatements(p.getEnd(), getRight(leftPath.getStatements().get(i).getPredicate()))));
742 for (Path p : stepPaths)
743 if (p.getLength() == i+1)
750 private Resource getRight(Resource r) {
751 if (comparableResources.containsLeft(r))
752 return comparableResources.getRight(r);
758 public BijectionMap<Statement, Statement> getComparableStatements() {
759 return comparableStatements;
762 public BijectionMap<Resource, Resource> getComparableResources() {
763 return comparableResources;
766 public GraphChanges getChanges() {
767 return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources);
770 private void addComparable(Statement left, Statement right) throws DatabaseException {
771 addComparable(left.getObject(), right.getObject());
772 comparableStatements.map(left, right);
773 //comparableResources.map(left.getObject(), right.getObject());
776 private void addComparable(Resource left, Resource right) throws DatabaseException {
777 if(!comparableResources.contains(left, right)) {
778 if (comparableResources.containsLeft(left)||comparableResources.containsRight(right)) {
779 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.");
781 if (DEBUG) System.out.println(left + " = " + right);
782 comparableResources.map(left, right);
788 public List<Statement> filterAsserted(Resource r, Collection<Statement> in) throws DatabaseException {
789 List<Statement> out = new ArrayList<Statement>();
790 for (Statement s : in) {
791 if (!s.isAsserted(r))
800 private String printStatement(ReadGraph graph, Statement s) throws DatabaseException {
801 return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject());
804 private List<Statement> filterTraversed(List<Statement> in) throws DatabaseException {
805 return filter(traversed, in);
808 private List<Statement> filterNonTested(List<Statement> in) throws DatabaseException {
809 return filter(nonTested, in);
812 private List<Statement> filterNonTraversed(List<Statement> in) throws DatabaseException {
813 return filter(nonTraversed, in);
816 private List<Statement> filter(Collection<Resource> toFilter, List<Statement> in) throws DatabaseException {
817 if (toFilter.size() == 0)
819 List<Statement> out = new ArrayList<Statement>();
820 for (Statement s : in) {
821 boolean usable = true;
822 for (Resource r : toFilter) {
823 if (g.isSubrelationOf(s.getPredicate(),r)) {
837 private void addDeletion(Statement s) {
838 if (!changes1Set.contains(s)) {
844 private void addAddition(Statement s) {
845 if (!changes2Set.contains(s)) {
851 private void addModification(Resource sub1, Statement s1, Resource sub2, Statement s2) {
852 Modification mod = new Modification(sub1, sub2, s1, s2);
853 if (!modificationsSet.contains(mod)) {
854 modificationsSet.add(mod);
855 modifications.add(mod);
859 public void sortStatement(List<Statement> list1, List<Statement> list2) {
860 sortStatement(list1, list2, scomp);
863 public void sortStatement(List<Statement> list1, List<Statement> list2, Comparator<Statement> scomp) {
864 Collections.sort(list1,scomp);
865 Collections.sort(list2,scomp);
867 List<Statement> sorted1 = new ArrayList<Statement>(list1.size());
868 List<Statement> sorted2 = new ArrayList<Statement>(list2.size());
869 sorted1.addAll(list1);
870 sorted2.addAll(list2);
874 for (int i = 0; i < list1.size(); ) {
875 Statement s1 = list1.get(i);
876 int same1 = sameRel(list1, i);
877 for (int j = 0; j < list2.size(); j++) {
878 Statement s2 = list2.get(j);
879 if (scomp.compare(s1, s2) == 0) {
880 int same2 = sameRel(list2, j);
881 copy(sorted1,ss1,list1,i,same1);
883 copy(sorted2,ss2,list2,j,same2);
890 if (ss1 < sorted1.size()) {
891 for (Statement s : list1) {
892 if (!sorted1.contains(s)) {
898 if (ss2 < sorted2.size()) {
899 for (Statement s : list2) {
900 if (!sorted2.contains(s)) {
909 list1.addAll(sorted1);
910 list2.addAll(sorted2);
913 public <T> void copy(List<T> to, int toIndex, List<T> from, int fromIndex, int amount) {
914 for (int i = 0; i < amount; i++) {
915 to.set(toIndex + i, from.get(fromIndex+ i));
919 public void sortResource(List<Resource> list1, List<Resource> list2) {
920 Collections.sort(list1,rcomp);
922 for (int i = 0; i < list1.size(); i++) {
923 Resource s1 = list1.get(i);
924 for (int j = js; j < list2.size(); j++) {
925 Resource s2 = list2.get(j);
926 if (rcomp.compare(s1, s2) == 0) {
927 Resource t = list2.get(js);
937 private void compareStatements(List<Statement> ss1, List<Statement> ss2, Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Collection<Statement> unreliableLeft, Collection<Statement> unreliableRight) throws DatabaseException {
938 sortStatement(ss1, ss2);
944 if (i1 >= ss1.size()) {
945 if (i2 >= ss2.size()) {
948 while (i2 < ss2.size()) {
949 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i2)));
951 addAddition(ss2.get(i2));
956 } else if (i2 >= ss2.size()) {
957 while (i1 < ss1.size()) {
958 if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i1)));
959 addDeletion(ss1.get(i1));
964 int same1 = sameRel(ss1, i1);
965 int same2 = sameRel(ss2, i2);
966 int c = rcomp.compare(ss1.get(i1).getPredicate(),ss2.get(i2).getPredicate());
968 compareStatements(ss1, i1, same1, ss2, i2, same2,objectsLeft,objectsRight,unreliableLeft,unreliableRight);
972 for (int i = 0; i < same1; i++) {
973 if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i+i1)));
974 addDeletion(ss1.get(i+i1));
978 for (int i = 0; i < same2; i++) {
979 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i+i2)));
980 addAddition(ss2.get(i+i2));
990 private int sameRel(List<Statement> statements, int off) {
991 if (statements.size() <= off)
994 long id = statements.get(off).getPredicate().getResourceId();
995 for (int i = off+1; i <statements.size(); i++) {
996 if (statements.get(i).getPredicate().getResourceId() == id)
1005 private int compareObject(Resource o1, Resource o2) throws DatabaseException {
1008 if (comparableResources.contains(o1, o2))
1010 if (comparableResources.containsLeft(o1))
1011 return Integer.MAX_VALUE;
1012 if (comparableResources.containsRight(o2))
1013 return Integer.MAX_VALUE;
1014 if (nonMatchedLeft.contains(o1))
1015 return Integer.MAX_VALUE;
1016 if (nonMatchedRight.contains(o2))
1017 return Integer.MAX_VALUE;
1018 return comparator.compare(g, o1, o2);
1021 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 {
1022 boolean[] used1 = new boolean[len1];
1023 for (int i = 0; i < used1.length; i++) {
1027 boolean[] used2 = new boolean[len2];
1028 for (int i = 0; i < used2.length; i++) {
1032 // left, right, difference
1033 List<List<Integer>> differences = new ArrayList<List<Integer>>();
1034 for (int i1 = off1; i1 < off1 + len1; i1++) {
1035 Statement s1 = ss1.get(i1);
1036 List<Integer> diff = new ArrayList<Integer>();
1037 for (int i2 = off2; i2 < off2 + len2; i2++) {
1038 Statement s2 = ss2.get(i2);
1039 int d = compareObject(s1.getObject(), s2.getObject());
1041 for (Resource t : strong) {
1042 if (s1.getPredicate().equals(t) || g.isSubrelationOf(s1.getPredicate(), t)) {
1050 differences.add(diff);
1053 MapList<Integer, Integer> priorities = new MapList<Integer, Integer>();
1054 for (int i = 0; i < differences.size(); i++) {
1055 List<Integer> list = differences.get(i);
1056 for (int j = 0; j < list.size(); j++) {
1057 priorities.add(list.get(j), i);
1061 Integer[] pris = priorities.getKeys(new Integer[]{});
1064 for (Integer pri : pris) {
1065 if (pri == Integer.MAX_VALUE) {
1067 } else if (pri == 0) {
1070 List<Integer> i1s = priorities.getValues(pri);
1071 for (Integer i1 : i1s) {
1074 List<Integer> i2diff = differences.get(i1);
1075 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1076 if (i2diff.get(i2) == pri) {
1081 Statement s1 = ss1.get(i1+off1);
1082 Statement s2 = ss2.get(i2+off2);
1084 if (objectsLeft != null) {
1085 objectsLeft.add(s1.getObject());
1086 objectsRight.add(s2.getObject());
1088 addComparable(s1, s2);
1096 for (Integer pri : pris) {
1099 Set<Statement> s1s = new HashSet<Statement>();
1100 Set<Statement> s2s = new HashSet<Statement>();
1101 Set<Integer> s1i = new HashSet<Integer>();
1102 Set<Integer> s2i = new HashSet<Integer>();
1103 List<Integer> i1s = priorities.getValues(pri);
1104 for (Integer i1 : i1s) {
1107 List<Integer> i2diff = differences.get(i1);
1108 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1109 if (i2diff.get(i2) == pri) {
1112 Statement s1 = ss1.get(i1+off1);
1113 Statement s2 = ss2.get(i2+off2);
1121 if (unreliableLeft != null) {
1122 unreliableLeft.addAll(s1s);
1123 unreliableRight.addAll(s2s);
1125 for (Integer i : s1i)
1127 for (Integer i : s2i)
1131 for (int i1 = off1; i1 < off1 + len1; i1++) {
1132 if (!used1[i1-off1]) {
1133 if (DEBUG) System.out.println("Compare Object deletion " + printStatement(g,ss1.get(i1)));
1134 addDeletion(ss1.get(i1));
1137 for (int i2 = off2; i2 < off2 + len2; i2++) {
1138 if (!used2[i2-off2]) {
1139 if (DEBUG) System.out.println("Compare Object addition " + printStatement(g,ss2.get(i2)));
1140 addAddition(ss2.get(i2));
1148 * compares properties, assumes functional relations
1151 * @throws ServiceException
1152 * @throws DoesNotContainValueException
1153 * @throws ValidationException
1155 private void compareProps(Resource r1, Resource r2) throws DatabaseException {
1156 if (DEBUG) System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2));
1157 List<Statement> ss1 = new ArrayList<Statement>();
1158 List<Statement> ss2 = new ArrayList<Statement>();
1159 ss1.addAll(g.getStatements(r1, b.HasProperty));
1160 ss2.addAll(g.getStatements(r2, b.HasProperty));
1161 ss1 = filterNonTested(ss1);
1162 ss2 = filterNonTested(ss2);
1163 sortStatement(ss1, ss2);
1169 if (i1 >= ss1.size()) {
1170 if (i2 >= ss2.size())
1173 while (i2 < ss2.size()) {
1174 if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,ss2.get(i2)));
1175 addAddition(ss2.get(i2));
1180 } else if (i2 >= ss2.size()) {
1181 while (i1 < ss1.size()) {
1182 if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,ss1.get(i1)));
1183 addDeletion(ss1.get(i1));
1188 Statement s1 = ss1.get(i1);
1189 Statement s2 = ss2.get(i2);
1190 if (s1.isAsserted(r1) && s2.isAsserted(r2)) {
1195 int c = scomp.compare(s1, s2);
1198 boolean b1 = g.hasValue(s1.getObject());
1199 boolean b2 = g.hasValue(s2.getObject());
1202 // Object v1 = g.getValue(s1.getObject());
1203 // Object v2 = g.getValue(s2.getObject());
1204 // boolean eq = compareValue(v1, v2);
1205 boolean eq = compareValue(g,b,s1.getObject(), s2.getObject());
1207 addModification(r1,s1,r2,s2);
1208 if (!s1.isAsserted(r1) && !s2.isAsserted(r2))
1209 addComparable(s1, s2);
1212 if (!s1.getObject().equals(s1.getSubject()) && !s2.getObject().equals(s2.getSubject()))
1213 compareProps(s1.getObject(), s2.getObject());
1216 addModification(r1,s1,r2,s2);
1217 if (!s1.isAsserted(r1) && !s2.isAsserted(r2))
1218 addComparable(s1, s2);
1225 if (DEBUG) System.out.println("Compare Prop diff1s " + printStatement(g,s1));
1232 if (DEBUG) System.out.println("Compare Prop diff2s " + printStatement(g,s2));
1246 public static boolean compareValue(ReadGraph g, Layer0 b, Resource r1, Resource r2) throws DatabaseException {
1247 Resource t1 = g.getSingleType(r1);
1248 Resource t2 = g.getSingleType(r2);
1251 if (t1.equals(b.Integer)) {
1252 int v1 = g.getValue(r1, Bindings.INTEGER);
1253 int v2 = g.getValue(r2, Bindings.INTEGER);
1255 } else if (t1.equals(b.Float)) {
1256 float v1 = g.getValue(r1, Bindings.FLOAT);
1257 float v2 = g.getValue(r2, Bindings.FLOAT);
1259 } else if (t1.equals(b.Double)) {
1260 double v1 = g.getValue(r1, Bindings.DOUBLE);
1261 double v2 = g.getValue(r2, Bindings.DOUBLE);
1263 } else if (t1.equals(b.String)) {
1264 String v1 = g.getValue(r1, Bindings.STRING);
1265 String v2 = g.getValue(r2, Bindings.STRING);
1266 return v1.equals(v2);
1267 } else if (t1.equals(b.Boolean)) {
1268 boolean v1 = g.getValue(r1, Bindings.BOOLEAN);
1269 boolean v2 = g.getValue(r2, Bindings.BOOLEAN);
1271 } else if (t1.equals(b.Byte)) {
1272 byte v1 = g.getValue(r1, Bindings.BYTE);
1273 byte v2 = g.getValue(r2, Bindings.BYTE);
1275 } else if (t1.equals(b.Long)) {
1276 long v1 = g.getValue(r1, Bindings.LONG);
1277 long v2 = g.getValue(r2, Bindings.LONG);
1279 } else if (t1.equals(b.IntegerArray)) {
1280 int[] v1 = g.getValue(r1, Bindings.INT_ARRAY);
1281 int[] v2 = g.getValue(r2, Bindings.INT_ARRAY);
1282 return Arrays.equals(v1,v2);
1283 } else if (t1.equals(b.FloatArray)) {
1284 float[] v1 = g.getValue(r1, Bindings.FLOAT_ARRAY);
1285 float[] v2 = g.getValue(r2, Bindings.FLOAT_ARRAY);
1286 return Arrays.equals(v1,v2);
1287 } else if (t1.equals(b.DoubleArray)) {
1288 double[] v1 = g.getValue(r1, Bindings.DOUBLE_ARRAY);
1289 double[] v2 = g.getValue(r2, Bindings.DOUBLE_ARRAY);
1290 return Arrays.equals(v1,v2);
1291 } else if (t1.equals(b.StringArray)) {
1292 String[] v1 = g.getValue(r1, Bindings.STRING_ARRAY);
1293 String[] v2 = g.getValue(r2, Bindings.STRING_ARRAY);
1294 return Arrays.equals(v1,v2);
1295 } else if (t1.equals(b.BooleanArray)) {
1296 boolean[] v1 = g.getValue(r1, Bindings.BOOLEAN_ARRAY);
1297 boolean[] v2 = g.getValue(r2, Bindings.BOOLEAN_ARRAY);
1298 return Arrays.equals(v1,v2);
1299 } else if (t1.equals(b.ByteArray)) {
1300 byte[] v1 = g.getValue(r1, Bindings.BYTE_ARRAY);
1301 byte[] v2 = g.getValue(r2, Bindings.BYTE_ARRAY);
1302 return Arrays.equals(v1,v2);
1303 } else if (t1.equals(b.LongArray)) {
1304 long[] v1 = g.getValue(r1, Bindings.LONG_ARRAY);
1305 long[] v2 = g.getValue(r2, Bindings.LONG_ARRAY);
1306 return Arrays.equals(v1,v2);
1308 Object v1 = g.getValue(r1);
1309 Object v2 = g.getValue(r2);
1310 return compareValue(v1, v2);
1315 public static boolean compareValue(Object v1, Object v2) {
1316 if (v1 instanceof Object[] && v2 instanceof Object[])
1317 return Arrays.deepEquals((Object[])v1, (Object[])v2);
1318 else if (v1 instanceof int[] && v2 instanceof int[])
1319 return Arrays.equals((int[])v1, (int[])v2);
1320 else if (v1 instanceof float[] && v2 instanceof float[])
1321 return Arrays.equals((float[])v1, (float[])v2);
1322 else if (v1 instanceof double[] && v2 instanceof double[])
1323 return Arrays.equals((double[])v1, (double[])v2);
1324 else if (v1 instanceof long[] && v2 instanceof long[])
1325 return Arrays.equals((long[])v1, (long[])v2);
1326 else if (v1 instanceof byte[] && v2 instanceof byte[])
1327 return Arrays.equals((byte[])v1, (byte[])v2);
1328 else if (v1 instanceof boolean[] && v2 instanceof boolean[])
1329 return Arrays.equals((boolean[])v1, (boolean[])v2);
1331 return v1.equals(v2);
1335 public class PredicateComparator implements Comparator<Statement> {
1337 public int compare(Statement o1, Statement o2) {
1338 if (comparableResources.contains(o1.getPredicate(), o2.getPredicate()))
1340 if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1342 if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1348 public class SubjectComparator implements Comparator<Statement> {
1350 public int compare(Statement o1, Statement o2) {
1351 if (comparableResources.contains(o1.getSubject(), o2.getSubject()))
1353 if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1355 if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1361 public class ObjectComparator implements Comparator<Statement> {
1363 public int compare(Statement o1, Statement o2) {
1364 if (comparableResources.contains(o1.getObject(), o2.getObject()))
1366 if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1368 if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1374 public static class FullStatementComparator implements Comparator<Statement> {
1376 public int compare(Statement o1, Statement o2) {
1377 if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1379 if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1381 if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1383 if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1385 if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1387 if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1393 public class ResComparator implements Comparator<Resource> {
1395 public int compare(Resource o1, Resource o2) {
1396 if (comparableResources.contains(o1, o2))
1398 if (o1.getResourceId() < o2.getResourceId())
1400 if (o1.getResourceId() > o2.getResourceId())