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 * VTT Technical Research Centre of Finland - Improvements to comparison.
12 * Semantum Oy - Improvements to comparison, various bug fixes.
13 *******************************************************************************/
14 package org.simantics.interop.test;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import java.util.Stack;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.NullProgressMonitor;
31 import org.simantics.databoard.Bindings;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.Session;
35 import org.simantics.db.Statement;
36 import org.simantics.db.common.request.ReadRequest;
37 import org.simantics.db.common.utils.NameUtils;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.request.Read;
40 import org.simantics.interop.test.GraphChanges.Modification;
41 import org.simantics.layer0.Layer0;
42 import org.simantics.utils.datastructures.BijectionMap;
43 import org.simantics.utils.datastructures.MapList;
44 import org.simantics.utils.datastructures.Pair;
47 * Compares two subgraphs and reports differences.
49 * Assumes that subgraphs (defined using traverse relations) are not cyclic.
51 * Assumes that properties can be used to identify objects, if relation type is not enough.
55 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
58 public class GraphComparator {
60 private static final boolean DEBUG = false;
64 private Set<Resource> strong = new HashSet<>(); // List of relations that identify object, if subject is already identified.
65 private List<Resource> traversed = new ArrayList<>(); // list of relations that are traversed (and tested)
66 private List<Resource> tested = new ArrayList<>(); // list of relations that are tested, but not traversed
67 private List<Resource> nonTraversed = new ArrayList<>(); // list of relations that are not traversed
68 private List<Resource> nonTested = new ArrayList<>(); // list of relations that are not tested
70 private List<Statement> changes1 = new ArrayList<>();
71 private List<Statement> changes2 = new ArrayList<>();
72 private List<Modification> modifications = new ArrayList<>();
73 private Set<Statement> changes1Set = new HashSet<>();
74 private Set<Statement> changes2Set = new HashSet<>();
75 private Set<Modification> modificationsSet = new HashSet<>();
77 private BijectionMap<Statement, Statement> comparableStatements = new BijectionMap<>();
78 private BijectionMap<Resource, Resource> comparableResources = new BijectionMap<>();
80 private Set<Resource> processedResources = new HashSet<Resource>();
82 private ResourceComparator comparator;
84 private Comparator<Statement> scomp = new PredicateComparator();
85 private Comparator<Resource> rcomp = new ResComparator();
87 private Set<Resource> nonMatchedLeft = new HashSet<Resource>();
88 private Set<Resource> nonMatchedRight = new HashSet<Resource>();
90 private long iter = 0;
91 private long maxIter = -1;
98 private boolean mapLiterals = true;
100 public GraphComparator(Resource r1, Resource r2) {
103 comparator = new TypeComparator();
106 public GraphComparator(Resource r1, Resource r2, ResourceComparator comparator) {
109 this.comparator = comparator;
112 ArrayList<Statement> ss1 = new ArrayList<Statement>();
113 ArrayList<Statement> ss2 = new ArrayList<Statement>();
116 public Comparator<Resource> getResourceComparator() {
120 public Comparator<Statement> getStatementComparator() {
124 public Resource getR1() {
128 public Resource getR2() {
132 public void addTraversed(Resource rel) {
136 public void addTraversed(Collection<Resource> rels) {
137 traversed.addAll(rels);
140 public void addNonTraversed(Resource rel) {
141 nonTraversed.add(rel);
144 public void addNonTraversed(Collection<Resource> rels) {
145 nonTraversed.addAll(rels);
148 public void addTested(Resource rel) {
152 public void addTested(Collection<Resource> rels) {
156 public void addNonTested(Resource rel) {
160 public void addNonTested(Collection<Resource> rels) {
161 nonTested.addAll(rels);
164 public void addComparableResources(Resource r1, Resource r2) {
166 System.out.println("Preset " + r1 + " = " + r2);
167 comparableResources.map(r1, r2);
170 public void addComparableResources(BijectionMap<Resource, Resource> matching) {
172 for (Entry<Resource, Resource> entry : matching.getEntries())
173 System.out.println("Preset " + entry.getKey() + " = " + entry.getValue());
175 comparableResources.addAll(matching);
178 public void addStrong(Resource r) {
182 public void addStrong(Collection<Resource> rels) {
186 public void addNonMatchedLeft(Resource r) {
187 nonMatchedLeft.add(r);
190 public void addNonMatchedRight(Resource r) {
191 nonMatchedRight.add(r);
194 public long getMaxIter() {
199 * Sets maximum iteration done on a single DB transaction. Affects only test(Session) methods.
200 * Default value is -1, which disables iteration limit.
201 * Note that using iteration limit causes comparison to repeat some work, thus total execution time will increase.
205 public void setMaxIter(long maxIter) {
206 this.maxIter = maxIter;
209 public boolean isMapLiterals() {
213 public void setMapLiterals(boolean mapLiterals) {
214 this.mapLiterals = mapLiterals;
217 private void clear() {
222 comparableResources.clear();
223 comparableStatements.clear();
224 modifications.clear();
225 modificationsSet.clear();
226 processedResources.clear();
229 public void dispose() {
234 comparableResources = null;
235 comparableStatements = null;
237 modifications = null;
238 modificationsSet = null;
239 processedResources = null;
240 nonMatchedLeft = null;
241 nonMatchedRight = null;
251 public void test(ReadGraph g) throws DatabaseException {
255 public void test(ReadGraph g, IProgressMonitor monitor) throws DatabaseException {
257 this.b = Layer0.getInstance(g);
258 comparator.setComparator(this);
259 comparator.initialize(g, r1, r2);
262 monitor = new NullProgressMonitor();
264 Stack<Resource> objectsLeft = new Stack<Resource>();
265 Stack<Resource> objectsRight = new Stack<Resource>();
266 objectsLeft.push(r1);
267 objectsRight.push(r2);
269 Set<Statement> unreliableLeft = new HashSet<Statement>();
270 Set<Statement> unreliableRight = new HashSet<Statement>();
272 boolean changed = false;
274 if (objectsLeft.isEmpty() && !changed)
276 if (monitor.isCanceled()) {
280 monitor.subTask(monitorTaskName(objectsLeft, objectsRight, unreliableLeft, unreliableRight));
283 // process compares objects that are identified and searches for more resources to process.
284 changed = compareIter(g, objectsLeft, objectsRight, unreliableLeft, unreliableRight);
288 for (Statement s : unreliableLeft) {
289 if (!comparableStatements.containsLeft(s)) {
290 if (DEBUG) System.out.println("Unreliable Object deletion " + printStatement(g,s));
294 for (Statement s : unreliableRight) {
295 if (!comparableStatements.containsRight(s)) {
296 if (DEBUG) System.out.println("Unreliable Object addition " + printStatement(g,s));
300 monitor.subTask(monitorTaskName(objectsLeft, objectsRight, unreliableLeft, unreliableRight) + " done.");
304 public void test(Session session) throws DatabaseException {
305 test(session, r1, r2, null);
308 public void test(Session session, IProgressMonitor monitor) throws DatabaseException {
309 test(session, r1, r2, monitor);
312 public void test(Session session, Resource r1, Resource r2) throws DatabaseException {
313 test(session, r1, r2, null);
316 public void test(Session session, Resource r1, Resource r2, IProgressMonitor monitor) throws DatabaseException {
318 comparator.setComparator(this);
321 monitor = new NullProgressMonitor();
323 session.syncRequest(new ReadRequest() {
326 public void run(ReadGraph graph) throws DatabaseException {
327 comparator.initialize(graph, r1, r2);
331 addComparable(r1, r2);
333 final Stack<Resource> objectsLeft = new Stack<Resource>();
334 final Stack<Resource> objectsRight = new Stack<Resource>();
335 objectsLeft.push(r1);
336 objectsRight.push(r2);
338 final Set<Statement> unreliableLeft = new HashSet<Statement>();
339 final Set<Statement> unreliableRight = new HashSet<Statement>();
341 boolean changed = false;
343 if (objectsLeft.isEmpty() && !changed)
345 if (monitor.isCanceled()) {
349 monitor.subTask(monitorTaskName(objectsLeft, objectsRight, unreliableLeft, unreliableRight));
350 changed = session.syncRequest(new Read<Boolean>() {
353 public Boolean perform(ReadGraph graph) throws DatabaseException {
354 //System.out.println("Iter " + monitorTaskName(objectsLeft, objectsRight, unreliableLeft, unreliableRight));
356 b = Layer0.getInstance(graph);
357 return compareIter(graph, objectsLeft, objectsRight, unreliableLeft, unreliableRight);
364 for (Statement s : unreliableLeft) {
365 if (!comparableStatements.containsLeft(s))
368 for (Statement s : unreliableRight) {
369 if (!comparableStatements.containsRight(s))
372 unreliableLeft.clear();
373 unreliableRight.clear();
375 monitor.subTask(monitorTaskName(objectsLeft, objectsRight, unreliableLeft, unreliableRight) + " done.");
379 private boolean compareIter(ReadGraph graph, Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
380 // process compares objects that are identified and searches for more resources to process.
382 boolean c = process(objectsLeft, objectsRight, unreliableLeft, unreliableRight);
383 if (objectsLeft.isEmpty()) {
384 // process unreliable handles cases where unidentified statements subject and object have been identified
385 c |= processUnreliable(unreliableLeft, unreliableRight);
386 // process unreliable handles cases where unidentified resources have path of length one to identified resource
388 c |= processUnreliable(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
389 if (!c && objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
390 c |= processUnreliable2(unreliableLeft, unreliableRight,objectsLeft,objectsRight);
392 if (!c && objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
393 // comparison is ending, but we have still unprocessed unidentified resources left.
394 // These cases have longer path than one to identified objects.
395 c |= processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
397 if (!c && objectsLeft.isEmpty() && unreliableLeft.size() > 0 && unreliableRight.size() > 0) {
398 // comparison is ending, but we have still unprocessed unidentified resources left.
399 // These cases have longer path than one to identified objects.
400 c |= processUnreliableDeep(unreliableLeft, unreliableRight, objectsLeft, objectsRight);
408 private String monitorTaskName(Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Set<Statement> unreliableLeft, Set<Statement> unreliableRight) {
409 int cr = comparableResources.size();
410 int ch = Math.max(changes1.size(), changes2.size());
411 return "Graph compare " + (cr + ch) + " / " + (cr+ch+(Math.max(objectsLeft.size(), objectsRight.size())+Math.max(unreliableLeft.size(), unreliableRight.size())));
414 private boolean process(Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
415 List<Statement> ss1 = new ArrayList<Statement>();
416 List<Statement> ss2 = new ArrayList<Statement>();
417 boolean didSomething = false;
418 while (!objectsLeft.isEmpty()&& (maxIter < 0 || iter < maxIter)) {
419 Resource r1 = objectsLeft.pop();
420 Resource r2 = objectsRight.pop();
424 if (processedResources.contains(r1))
426 processedResources.add(r1);
429 if((comparableResources.containsLeft(r1)||comparableResources.containsRight(r2)) && !comparableResources.contains(r1, r2)) {
430 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.");
432 addComparable(r1, r2);
434 //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));
435 compareProps(r1, r2);
437 for (Resource rel : tested) {
438 ss1.addAll(g.getStatements(r1, rel));
439 ss2.addAll(g.getStatements(r2, rel));
440 ss1 = filterAsserted(r1, ss1);
441 ss2 = filterAsserted(r2, ss2);
442 ss1 = filterTraversed(ss1);
443 ss2 = filterTraversed(ss2);
444 ss1 = filterNonTested(ss1);
445 ss2 = filterNonTested(ss2);
446 ss1 = filter(Collections.singletonList(b.HasProperty), ss1);
447 ss2 = filter(Collections.singletonList(b.HasProperty), ss2);
449 compareStatements(ss1, ss2, null, null,null,null);
454 for (Resource rel : traversed) {
455 ss1.addAll(g.getStatements(r1, rel));
456 ss2.addAll(g.getStatements(r2, rel));
457 ss1 = filterAsserted(r1, ss1);
458 ss2 = filterAsserted(r2, ss2);
459 ss1 = filterNonTraversed(ss1);
460 ss2 = filterNonTraversed(ss2);
461 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
471 private boolean processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
472 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
473 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
474 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
475 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
476 boolean didSomething = false;
477 for (Statement s : unreliableLeft) {
478 subjectLeft.add(s.getSubject(),s);
479 objectLeft.add(s.getObject(),s);
481 for (Statement s : unreliableRight) {
482 subjectRight.add(s.getSubject(),s);
483 objectRight.add(s.getObject(),s);
486 for (Resource left : subjectLeft.getKeys()) {
487 if (maxIter > 0 && iter > maxIter)
489 Resource right = comparableResources.getRight(left);
492 for (Statement leftS : subjectLeft.getValues(left)) {
493 Resource leftO = leftS.getObject();
494 if (!unreliableLeft.contains(leftS))
496 Resource rightO = comparableResources.getRight(leftO);
499 for (Statement rightS : subjectRight.getValues(right)) {
500 if (!rightS.getObject().equals(rightO))
502 if (!unreliableRight.contains(rightS))
504 if (leftS.getPredicate().equals(rightS.getPredicate()) ||
505 comparableResources.contains(leftS.getPredicate(), rightS.getPredicate())) {
506 unreliableLeft.remove(leftS);
507 unreliableRight.remove(rightS);
508 addComparable(leftS, rightS);
518 private boolean processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
519 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
520 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
521 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
522 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
523 boolean didSomething = false;
524 for (Statement s : unreliableLeft) {
525 subjectLeft.add(s.getSubject(),s);
526 objectLeft.add(s.getObject(),s);
528 for (Statement s : unreliableRight) {
529 subjectRight.add(s.getSubject(),s);
530 objectRight.add(s.getObject(),s);
532 for (Resource ol : objectLeft.getKeys()) {
533 if (maxIter > 0 && iter > maxIter)
535 // all statements to the left side object
536 List<Statement> left = objectLeft.getValues(ol);
537 // all subjects that have statements to the left side object (ol)
538 Set<Resource> sLeft = new HashSet<Resource>();
539 // all matching subjects on the right side
540 Set<Resource> sRight = new HashSet<Resource>();
541 for (Statement s : left) {
542 sLeft.add(s.getSubject());
543 sRight.add(comparableResources.getRight(s.getSubject()));
546 // check if object left can be reliably identified by available statements
547 // if there are any objects on the left side with similar statements, object left cannot be mapped.
548 boolean hasSimilar = false;
549 MapList<Resource, Statement> comparableOLeft = new MapList<Resource, Statement>();
550 for (Resource sl : sLeft) {
551 for (Statement s : subjectLeft.getValues(sl)) {
552 if (!s.getObject().equals(ol)) {
553 comparableOLeft.add(s.getObject(),s);
558 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
560 for (Resource similarOl : comparableOLeft.getKeys()) {
561 List<Statement> similarLeft = comparableOLeft.getValues(similarOl);
562 if (similarLeft.size() == left.size()) {
563 boolean useL[] = new boolean[left.size()];
564 boolean useSL[] = new boolean[left.size()];
565 for (int i = 0; i < left.size(); i++) {
569 for (int i = 0; i < left.size(); i++) {
570 for (int j = 0; j < left.size(); j++) {
573 // compare predicates
574 Resource pl = left.get(i).getPredicate();
575 Resource psl = similarLeft.get(j).getPredicate();
576 if (pl.equals(psl)) {
577 // compare objects (unreliable result is interpreted as positive match)
579 int comp = comparator.compare(g, left.get(i).getObject(), similarLeft.get(j).getObject(), true);
580 if (comp >= 0 && comp < ResourceComparator.NO_MATCH) {
588 boolean diff = false;
589 for (int i = 0; i < left.size(); i++) {
590 if (!useL[i] || !useSL[i]) {
605 // all objects that subjects on the right side point to. Object left has its matching resource among these, if it has matching resource
606 MapList<Resource,Statement> possibleOR = new MapList<Resource, Statement>();
607 for (Resource sr : sRight) {
608 for (Statement s : subjectRight.getValues(sr))
609 possibleOR.add(s.getObject(),s);
612 // filter possible right side objects to those that have same amount of statements as the left side object
613 for (Resource or : possibleOR.getKeys().toArray(new Resource[possibleOR.getKeys().size()])) {
614 List<Statement> right = possibleOR.getValues(or);
615 if (right.size() != left.size())
616 possibleOR.remove(or);
620 // check for matching statements (comparable subjects, matching predicates)
621 MapList<Resource,Statement> matchingOR = new MapList<Resource, Statement>(); // list of objects that have matching statements
622 Map<Resource,Pair<int[], int[]>> matchingStatements = new HashMap<Resource, Pair<int[], int[]>>(); // matching statements
623 for (Resource or : possibleOR.getKeys()) {
624 List<Statement> right = possibleOR.getValues(or);
625 int iLeft[] = new int[left.size()];
626 int iRight[] = new int[right.size()];
628 for (int i = 0; i < left.size(); i++) {
633 for (int l = 0; l < left.size(); l++) {
634 Statement ls = left.get(l);
635 for (int r = 0; r < right.size(); r++) {
638 Statement rs = right.get(r);
639 if (!comparableResources.contains(ls.getSubject(), rs.getSubject()))
641 if ((comparableResources.containsLeft(ls.getObject()) || comparableResources.containsRight(rs.getObject())) && !comparableResources.contains(ls.getObject(), rs.getObject()))
643 if (rcomp.compare(ls.getPredicate(),rs.getPredicate()) == 0) {
644 // compare objects (unreliable result is not accepted)
645 int comp = comparator.compare(g, ls.getObject(), rs.getObject());
646 if (comp > 0 && comp < Integer.MAX_VALUE) {
656 boolean success = true;
657 for (int i = 0; i < left.size(); i++) {
669 for (Statement s : right)
670 matchingOR.add(or,s);
671 matchingStatements.put(or, new Pair<int[], int[]>(iLeft, iRight));
674 // if there is only one matching right side object, we have found a match
675 if (matchingOR.getKeySize() == 1) {
676 Resource or = matchingOR.getKeys().iterator().next();
677 List<Statement> right = matchingOR.getValues(or);
678 Pair<int[], int[]> indices = matchingStatements.get(or);
681 objectsRight.add(or);
682 addComparable(ol, or);
684 for (int l = 0; l < left.size(); l++) {
685 int r = indices.first[l];
686 Statement sl = left.get(l);
687 Statement sr = right.get(r);
688 addComparable(sl, sr);
689 unreliableLeft.remove(sl);
690 unreliableRight.remove(sr);
704 private boolean processUnreliable2(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
705 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
706 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
707 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
708 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
709 boolean didSomething = false;
710 for (Statement s : unreliableLeft) {
711 subjectLeft.add(s.getSubject(),s);
712 objectLeft.add(s.getObject(),s);
714 for (Statement s : unreliableRight) {
715 subjectRight.add(s.getSubject(),s);
716 objectRight.add(s.getObject(),s);
718 Set<Pair<Resource, Resource>> processed = new HashSet<Pair<Resource,Resource>>();
719 for (Resource ol : objectLeft.getKeys()) {
720 if (maxIter > 0 && iter > maxIter)
722 // all statements to the left side object
723 List<Statement> left = objectLeft.getValues(ol);
724 // all subjects that have statements to the left side object (ol)
725 Set<Resource> sLeft = new HashSet<Resource>();
726 // all matching subjects on the right side
727 Set<Resource> sRight = new HashSet<Resource>();
728 for (Statement s : left) {
729 sLeft.add(s.getSubject());
730 sRight.add(comparableResources.getRight(s.getSubject()));
733 if (sLeft.size() == 1 && sRight.size() == 1) {
734 Resource sl = sLeft.iterator().next();
735 Resource sr = sRight.iterator().next();
736 Pair<Resource, Resource> p = new Pair<Resource, Resource>(sl, sr);
737 if (processed.contains(p))
740 List<Statement> ss1 = new ArrayList<Statement>(subjectLeft.getValuesSnapshot(sl));
741 List<Statement> ss2 = new ArrayList<Statement>(subjectRight.getValuesSnapshot(sr));
742 for (int i = ss1.size() -1; i >= 0; i--) {
743 if (!unreliableLeft.contains(ss1.get(i)))
746 for (int i = ss2.size() -1; i >= 0; i--) {
747 if (!unreliableRight.contains(ss2.get(i)))
751 int ccount = comparableStatements.size();
752 int lcount = changes1.size();
753 int rcount = changes2.size();
755 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
756 if (comparableStatements.size() > ccount) {
758 for (Entry<Statement, Statement> entry : comparableStatements.getEntries()) {
759 unreliableLeft.remove(entry.getKey());
760 unreliableRight.remove(entry.getValue());
764 if (changes1.size() > lcount) {
766 for (Statement stm : changes1) {
767 unreliableLeft.remove(stm);
771 if (changes2.size() > rcount) {
773 for (Statement stm : changes2) {
774 unreliableRight.remove(stm);
783 private boolean processUnreliableDeep(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
784 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
785 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
786 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
787 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
788 boolean didSomething = false;
789 for (Statement s : unreliableLeft) {
790 subjectLeft.add(s.getSubject(),s);
791 objectLeft.add(s.getObject(),s);
793 for (Statement s : unreliableRight) {
794 subjectRight.add(s.getSubject(),s);
795 objectRight.add(s.getObject(),s);
797 for (Resource ol : objectLeft.getKeys()) {
798 if (maxIter > 0 && iter > maxIter)
800 Set<Path> pathsLeft = new HashSet<Path>();
801 for (Resource rel : traversed) {
802 pathsLeft.addAll(Path.create(g.getStatements(ol, rel)));
806 if (pathsLeft.size() == 0)
808 Collection<Path> endPaths = new ArrayList<Path>(1);
809 for (Path p : pathsLeft) {
810 if (comparableResources.containsLeft(p.getEnd())) {
814 if (endPaths.size() > 0) {
816 pathsLeft.addAll(endPaths);
820 if (pathsLeft.size() > 0) {
821 Resource sl = objectLeft.getValues(ol).get(0).getSubject();
822 Resource sr = comparableResources.getRight(sl);
823 Collection<Resource> possibleOR = new ArrayList<Resource>();
824 for (Statement s : subjectRight.getValues(sr)) {
825 possibleOR.add(s.getObject());
827 Map<Resource,Set<Path>> matchingPaths = new HashMap<Resource, Set<Path>>();
828 for (Resource or : possibleOR) {
829 Set<Path> possiblePathsRight = new HashSet<Path>();
830 for (Path leftPath : pathsLeft) {
831 possiblePathsRight.addAll(findComparableRight(leftPath, or));
833 if (hasMatchingPaths(pathsLeft, possiblePathsRight)) {
834 matchingPaths.put(or, possiblePathsRight);
837 if (matchingPaths.size() > 0) {
838 if (matchingPaths.size() == 1) {
840 Resource or = matchingPaths.keySet().iterator().next();
843 objectsRight.add(or);
844 addComparable(ol, or);
845 Collection<Statement> statementsLeft = objectLeft.getValues(ol);
846 Collection<Statement> statementsRight = objectRight.getValues(or);
847 unreliableLeft.removeAll(statementsLeft);
848 unreliableRight.removeAll(statementsRight);
849 BijectionMap<Path,Path> map = getMatchingPaths(pathsLeft, matchingPaths.get(or));
850 for (Path left : map.getLeftSet()) {
851 Path right = map.getRight(left);
852 for (int i = 0; i < left.getLength(); i++) {
853 addComparable(left.getStatements().get(i),right.getStatements().get(i));
867 private boolean hasMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) throws DatabaseException {
868 if (leftPaths.size() != rightPaths.size())
870 BijectionMap<Path,Path> map = getMatchingPaths(leftPaths, rightPaths);
871 return map.size() == leftPaths.size();
874 private BijectionMap<Path,Path> getMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) throws DatabaseException {
875 BijectionMap<Path,Path> map = new BijectionMap<Path, Path>();
876 for (Path leftPath : leftPaths) {
877 for (Path rightPath : rightPaths) {
878 if (map.containsRight(rightPath))
880 if (leftPath.getLength() != rightPath.getLength())
882 if (comparableResources.contains(leftPath.getEnd(), rightPath.getEnd())) {
883 boolean match = true;
884 for (int i = 0; i < leftPath.getLength(); i++) {
885 Statement sl = leftPath.getStatements().get(i);
886 Statement sr = rightPath.getStatements().get(i);
887 if (!sl.getPredicate().equals(sr.getPredicate()) && !comparableResources.contains(sl.getPredicate(), sr.getPredicate())) {
891 if ((getComparableResources().containsLeft(sl.getObject()) || getComparableResources().containsRight(sr.getObject())) && !getComparableResources().contains(sl.getObject(), sr.getObject())) {
895 if (comparator.compare(g, sl.getObject(), sr.getObject()) == ResourceComparator.NO_MATCH) {
901 map.map(leftPath, rightPath);
910 private void expand(Set<Path> paths) throws DatabaseException {
911 Set<Path> stepPathsLeft = new HashSet<Path>();
912 if (paths.size() == 0)
914 int length = paths.iterator().next().getLength() + 1;
915 for (Path p : paths) {
916 for (Resource rel : traversed) {
917 stepPathsLeft.addAll(Path.expand(p,g.getStatements(p.getEnd(), rel)));
921 for (Path p : stepPathsLeft) {
922 if (p.getLength() == length)
927 private Collection<Path> findComparableRight(Path leftPath, Resource beginRight) throws DatabaseException {
928 Set<Path> rightPaths = new HashSet<Path>();
929 rightPaths.addAll(Path.create(g.getStatements(beginRight, getRight(leftPath.getStatements().get(0).getPredicate()))));
930 for (int i = 1; i < leftPath.getLength(); i++) {
931 if (rightPaths.size() == 0)
933 Set<Path> stepPaths = new HashSet<Path>();
934 for (Path p : rightPaths) {
935 stepPaths.addAll(Path.expand(p, g.getStatements(p.getEnd(), getRight(leftPath.getStatements().get(i).getPredicate()))));
938 for (Path p : stepPaths)
939 if (p.getLength() == i+1)
946 private Resource getRight(Resource r) {
947 if (comparableResources.containsLeft(r))
948 return comparableResources.getRight(r);
954 public BijectionMap<Statement, Statement> getComparableStatements() {
955 return comparableStatements;
958 public BijectionMap<Resource, Resource> getComparableResources() {
959 return comparableResources;
962 public GraphChanges getChanges() {
963 return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources);
966 public List<Statement> getChanges1() {
970 public List<Statement> getChanges2() {
974 private void addComparable(Statement left, Statement right) throws DatabaseException {
975 addComparable(left.getObject(), right.getObject());
976 comparableStatements.map(left, right);
979 private void addComparable(Resource left, Resource right) throws DatabaseException {
980 if(!comparableResources.contains(left, right)) {
981 if (comparableResources.containsLeft(left)||comparableResources.containsRight(right)) {
982 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.");
984 if (DEBUG) System.out.println(left + " = " + right);
985 comparableResources.map(left, right);
987 // if (comparableResources.size() % 1000 == 0)
994 public void printStats() {
995 System.out.println("Comp " + comparableResources.size() + " L " + changes1.size() + " R " + changes2.size() + " M " + modifications.size() + " P " + processedResources.size());
998 public List<Statement> filterAsserted(Resource r, Collection<Statement> in) throws DatabaseException {
999 List<Statement> out = new ArrayList<Statement>();
1000 for (Statement s : in) {
1001 if (!s.isAsserted(r))
1008 public List<Statement> filterAssertedDuplicates(Resource r, List<Statement> in) throws DatabaseException {
1009 List<Statement> out = new ArrayList<Statement>();
1010 for (int i = 0; i < in.size(); i++) {
1011 Statement s = in.get(i);
1012 if (!s.isAsserted(r))
1015 boolean has = false;
1016 if (i > 0 && in.get(i-1).getPredicate().equals(s.getPredicate()))
1018 else if (i < in.size()-1 && in.get(i+1).getPredicate().equals(s.getPredicate()))
1030 private String printStatement(ReadGraph graph, Statement s) throws DatabaseException {
1031 return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject());
1034 private List<Statement> filterTraversed(List<Statement> in) throws DatabaseException {
1035 return filter(traversed, in);
1038 private List<Statement> filterNonTested(List<Statement> in) throws DatabaseException {
1039 return filter(nonTested, in);
1042 private List<Statement> filterNonTraversed(List<Statement> in) throws DatabaseException {
1043 return filter(nonTraversed, in);
1046 private List<Statement> filter(Collection<Resource> toFilter, List<Statement> in) throws DatabaseException {
1047 if (toFilter.size() == 0)
1049 List<Statement> out = new ArrayList<Statement>();
1050 for (Statement s : in) {
1051 boolean usable = true;
1052 for (Resource r : toFilter) {
1053 if (g.isSubrelationOf(s.getPredicate(),r)) {
1067 private void addDeletion(Statement s) {
1068 if (!changes1Set.contains(s)) {
1069 if (DEBUG) System.out.println("- " +s.getSubject() + " " + s.getPredicate() + " " + s.getObject()) ;
1075 private void addAddition(Statement s) {
1076 if (!changes2Set.contains(s)) {
1078 if (DEBUG) System.out.println("+ " +s.getSubject() + " " + s.getPredicate() + " " + s.getObject()) ;
1083 private void addModification(Resource left, Statement leftstm, Resource right, Statement rightstm) {
1084 Modification mod = new Modification(left, right, leftstm, rightstm);
1085 if (!modificationsSet.contains(mod)) {
1086 modificationsSet.add(mod);
1087 modifications.add(mod);
1091 public void sortStatement(List<Statement> list1, List<Statement> list2) {
1092 sortStatement(list1, list2, scomp);
1095 public void sortStatement(List<Statement> list1, List<Statement> list2, Comparator<Statement> scomp) {
1096 Collections.sort(list1,scomp);
1097 Collections.sort(list2,scomp);
1099 List<Statement> sorted1 = new ArrayList<Statement>(list1.size());
1100 List<Statement> sorted2 = new ArrayList<Statement>(list2.size());
1101 sorted1.addAll(list1);
1102 sorted2.addAll(list2);
1106 for (int i = 0; i < list1.size(); ) {
1107 Statement s1 = list1.get(i);
1108 int same1 = sameRel(list1, i);
1109 for (int j = 0; j < list2.size(); j++) {
1110 Statement s2 = list2.get(j);
1111 if (scomp.compare(s1, s2) == 0) {
1112 int same2 = sameRel(list2, j);
1113 copy(sorted1,ss1,list1,i,same1);
1115 copy(sorted2,ss2,list2,j,same2);
1122 if (ss1 < sorted1.size()) {
1123 for (Statement s : list1) {
1124 if (!sorted1.contains(s)) {
1130 if (ss2 < sorted2.size()) {
1131 for (Statement s : list2) {
1132 if (!sorted2.contains(s)) {
1141 list1.addAll(sorted1);
1142 list2.addAll(sorted2);
1145 public <T> void copy(List<T> to, int toIndex, List<T> from, int fromIndex, int amount) {
1146 for (int i = 0; i < amount; i++) {
1147 to.set(toIndex + i, from.get(fromIndex+ i));
1151 public void sortResource(List<Resource> list1, List<Resource> list2) {
1152 Collections.sort(list1,rcomp);
1154 for (int i = 0; i < list1.size(); i++) {
1155 Resource s1 = list1.get(i);
1156 for (int j = js; j < list2.size(); j++) {
1157 Resource s2 = list2.get(j);
1158 if (rcomp.compare(s1, s2) == 0) {
1159 Resource t = list2.get(js);
1169 private void compareStatements(List<Statement> ss1, List<Statement> ss2, Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Collection<Statement> unreliableLeft, Collection<Statement> unreliableRight) throws DatabaseException {
1170 sortStatement(ss1, ss2);
1176 if (i1 >= ss1.size()) {
1177 if (i2 >= ss2.size()) {
1180 while (i2 < ss2.size()) {
1181 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i2)));
1183 addAddition(ss2.get(i2));
1188 } else if (i2 >= ss2.size()) {
1189 while (i1 < ss1.size()) {
1190 if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i1)));
1191 addDeletion(ss1.get(i1));
1196 int same1 = sameRel(ss1, i1);
1197 int same2 = sameRel(ss2, i2);
1198 int c = rcomp.compare(ss1.get(i1).getPredicate(),ss2.get(i2).getPredicate());
1200 compareStatements(ss1, i1, same1, ss2, i2, same2,objectsLeft,objectsRight,unreliableLeft,unreliableRight);
1204 for (int i = 0; i < same1; i++) {
1205 if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i+i1)));
1206 addDeletion(ss1.get(i+i1));
1210 for (int i = 0; i < same2; i++) {
1211 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i+i2)));
1212 addAddition(ss2.get(i+i2));
1222 private int sameRel(List<Statement> statements, int off) {
1223 if (statements.size() <= off)
1226 long id = statements.get(off).getPredicate().getResourceId();
1227 for (int i = off+1; i <statements.size(); i++) {
1228 if (statements.get(i).getPredicate().getResourceId() == id)
1237 private int compareObject(Resource o1, Resource o2) throws DatabaseException {
1240 if (comparableResources.contains(o1, o2))
1242 if (comparableResources.containsLeft(o1))
1243 return Integer.MAX_VALUE;
1244 if (comparableResources.containsRight(o2))
1245 return Integer.MAX_VALUE;
1246 if (nonMatchedLeft.contains(o1))
1247 return Integer.MAX_VALUE;
1248 if (nonMatchedRight.contains(o2))
1249 return Integer.MAX_VALUE;
1250 return comparator.compare(g, o1, o2);
1253 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 {
1254 boolean[] used1 = new boolean[len1];
1255 for (int i = 0; i < used1.length; i++) {
1259 boolean[] used2 = new boolean[len2];
1260 for (int i = 0; i < used2.length; i++) {
1264 // left, right, difference
1265 List<List<Integer>> differences = new ArrayList<List<Integer>>();
1266 for (int i1 = off1; i1 < off1 + len1; i1++) {
1267 Statement s1 = ss1.get(i1);
1268 List<Integer> diff = new ArrayList<Integer>();
1269 for (int i2 = off2; i2 < off2 + len2; i2++) {
1270 Statement s2 = ss2.get(i2);
1271 int d = compareObject(s1.getObject(), s2.getObject());
1273 for (Resource t : strong) {
1274 if (s1.getPredicate().equals(t) || g.isSubrelationOf(s1.getPredicate(), t)) {
1282 differences.add(diff);
1285 MapList<Integer, Integer> priorities = new MapList<Integer, Integer>();
1286 for (int i = 0; i < differences.size(); i++) {
1287 List<Integer> list = differences.get(i);
1288 for (int j = 0; j < list.size(); j++) {
1289 priorities.add(list.get(j), i);
1293 Integer[] pris = priorities.getKeys(new Integer[]{});
1295 boolean matchFail = priorityMatching(ss1, off1, len1, ss2, off2, len2, pris, differences, priorities, used1, used2, objectsLeft, objectsRight, false);
1297 matchFail = priorityMatching(ss1, off1, len1, ss2, off2, len2, pris, differences, priorities, used1, used2, objectsLeft, objectsRight, objectsLeft == null);
1298 if (unreliableLeft != null) {
1300 // With match failure, statement matching was aborted. We must move all unmatched statements to unreliable.
1301 for (Integer pri : pris) {
1302 if (pri == 0 || pri == Integer.MAX_VALUE)
1304 priorityProcessUnmatched(ss1, off1, len1, ss2, off2, len2, differences, priorities, used1, used2, unreliableLeft, unreliableRight, pri);
1307 // Zero means unreliable comparison result, move all unmatched to unreliable.
1308 if (org.simantics.utils.datastructures.Arrays.contains(pris, 0)) {
1309 priorityProcessUnmatched(ss1, off1, len1, ss2, off2, len2, differences, priorities, used1, used2, unreliableLeft, unreliableRight, 0);
1312 // Previous version processed 0 priority statements, even when unreliableLeft collection was null.
1313 // The problem was that property relations were not filtered in process() from "tested" statements.
1315 // if (org.simantics.utils.datastructures.Arrays.contains(pris, 0)) {
1316 // priorityProcessUnmatched(ss1, off1, len1, ss2, off2, len2, differences, priorities, used1, used2, unreliableLeft, unreliableRight, 0);
1319 // Report unmatched statements as changes.
1320 for (int i1 = off1; i1 < off1 + len1; i1++) {
1321 if (!used1[i1-off1]) {
1322 if (DEBUG) System.out.println("Compare Object deletion " + printStatement(g,ss1.get(i1)));
1323 addDeletion(ss1.get(i1));
1326 for (int i2 = off2; i2 < off2 + len2; i2++) {
1327 if (!used2[i2-off2]) {
1328 if (DEBUG) System.out.println("Compare Object addition " + printStatement(g,ss2.get(i2)));
1329 addAddition(ss2.get(i2));
1335 * Moves unmatched statements to unreliable collections.
1342 * @param differences
1346 * @param unreliableLeft
1347 * @param unreliableRight
1350 private void priorityProcessUnmatched(List<Statement> ss1, int off1, int len1, List<Statement> ss2, int off2, int len2,List<List<Integer>> differences,MapList<Integer, Integer> priorities,boolean used1[],boolean used2[], Collection<Statement> unreliableLeft, Collection<Statement> unreliableRight, int pri) {
1351 Set<Statement> s1s = new HashSet<Statement>();
1352 Set<Statement> s2s = new HashSet<Statement>();
1353 Set<Integer> s1i = new HashSet<Integer>();
1354 Set<Integer> s2i = new HashSet<Integer>();
1355 List<Integer> i1s = priorities.getValues(pri);
1356 for (Integer i1 : i1s) {
1359 List<Integer> i2diff = differences.get(i1);
1360 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1361 if (i2diff.get(i2) == pri) {
1364 Statement s1 = ss1.get(i1+off1);
1365 Statement s2 = ss2.get(i2+off2);
1373 if (unreliableLeft != null) {
1374 unreliableLeft.addAll(s1s);
1375 unreliableRight.addAll(s2s);
1377 for (Integer i : s1i)
1379 for (Integer i : s2i)
1383 * Matches left and right side statements.
1385 * When there are two or more equally good matching objects, the behaviour depends on force flag.
1386 * False: Matching is aborted and matchFail is returned (method return true).
1387 * True: equally good matches will be paired randomly. Method always returns false.
1395 * @param differences
1399 * @param objectsLeft
1400 * @param objectsRight
1403 * @throws DatabaseException
1405 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 {
1406 boolean matchFail = false;
1407 for (Integer pri : pris) {
1408 if (pri == Integer.MAX_VALUE) {
1410 } else if (pri == 0) {
1413 List<Integer> i1s = priorities.getValues(pri);
1415 for (Integer i1 : i1s) {
1418 List<Integer> i2diff = differences.get(i1);
1419 List<Integer> matches = new ArrayList<Integer>();
1420 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1421 if (i2diff.get(i2) == pri) {
1427 if (matches.size() == 1 || (force && matches.size() > 1)) {
1428 int i2 = matches.get(0);
1431 Statement s1 = ss1.get(i1+off1);
1432 Statement s2 = ss2.get(i2+off2);
1434 if (objectsLeft != null) {
1435 objectsLeft.add(s1.getObject());
1436 objectsRight.add(s2.getObject());
1438 addComparable(s1, s2);
1439 } else if (matches.size() > 1) {
1452 * compares properties, assumes functional relations
1455 * @throws DatabaseException
1457 private void compareProps(Resource r1, Resource r2) throws DatabaseException {
1458 if (DEBUG) System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2));
1459 List<Statement> ss1 = new ArrayList<Statement>();
1460 List<Statement> ss2 = new ArrayList<Statement>();
1461 ss1.addAll(g.getStatements(r1, b.HasProperty));
1462 ss2.addAll(g.getStatements(r2, b.HasProperty));
1463 ss1 = filterNonTested(ss1);
1464 ss2 = filterNonTested(ss2);
1465 sortStatement(ss1, ss2);
1466 // getStatements(r1, b.HasProperty) returns both instance and asserted statements for the same property relation.
1467 ss1 = filterAssertedDuplicates(r1, ss1);
1468 ss2 = filterAssertedDuplicates(r2, ss2);
1474 if (i1 >= ss1.size()) {
1475 if (i2 >= ss2.size())
1478 while (i2 < ss2.size()) {
1479 Statement s = ss2.get(i2);
1480 if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,s));
1481 if (!s.isAsserted(r2))
1487 } else if (i2 >= ss2.size()) {
1488 while (i1 < ss1.size()) {
1489 Statement s = ss1.get(i1);
1490 if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,s));
1491 if (!s.isAsserted(r1))
1497 Statement s1 = ss1.get(i1);
1498 Statement s2 = ss2.get(i2);
1499 if (s1.isAsserted(r1) && s2.isAsserted(r2)) {
1504 int c = scomp.compare(s1, s2);
1507 boolean b1 = g.hasValue(s1.getObject());
1508 boolean b2 = g.hasValue(s2.getObject());
1509 boolean a1 = s1.isAsserted(r1);
1510 boolean a2 = s2.isAsserted(r2);
1514 boolean eq = compareValue(g,b,s1.getObject(), s2.getObject());
1516 addModification(r1,s1,r2,s2);
1518 if (mapLiterals && !a1 && !a2)
1519 addComparable(s1, s2);
1521 // Non literal properties.
1522 int comp = comparator.compare(g, s1.getObject(), s2.getObject());
1523 if (comp == ResourceComparator.NO_MATCH) {
1524 addModification(r1,s1,r2,s2);
1525 } else if (comp != ResourceComparator.EXACT_MATCH) {
1526 if (!s1.getObject().equals(s1.getSubject()) && !s2.getObject().equals(s2.getSubject())) {
1528 // compare props matches objects, so we can call that only for non asserted statements
1529 compareProps(s1.getObject(), s2.getObject());
1530 } else if (a1 && a2) {
1531 // TODO : compare asserted statements?
1533 addModification(r1,s1,r2,s2);
1536 addModification(r1,s1,r2,s2);
1539 // Exact match, nothing to do.
1541 addComparable(s1, s2);
1545 addModification(r1,s1,r2,s2);
1552 if (DEBUG) System.out.println("Compare Prop diff1s " + printStatement(g,s1));
1553 // Use modification, since deletions do not support asserted statements
1554 addModification(r1,s1,r2,null);
1561 if (DEBUG) System.out.println("Compare Prop diff2s " + printStatement(g,s2));
1562 // Use modification, since additions do not support asserted statements
1563 addModification(r1,null,r2,s2);
1577 public static boolean compareValue(ReadGraph g, Layer0 b, Resource r1, Resource r2) throws DatabaseException {
1578 Resource t1 = g.getSingleType(r1);
1579 Resource t2 = g.getSingleType(r2);
1582 if (t1.equals(b.Integer)) {
1583 int v1 = g.getValue(r1, Bindings.INTEGER);
1584 int v2 = g.getValue(r2, Bindings.INTEGER);
1586 } else if (t1.equals(b.Float)) {
1587 float v1 = g.getValue(r1, Bindings.FLOAT);
1588 float v2 = g.getValue(r2, Bindings.FLOAT);
1590 } else if (t1.equals(b.Double)) {
1591 double v1 = g.getValue(r1, Bindings.DOUBLE);
1592 double v2 = g.getValue(r2, Bindings.DOUBLE);
1594 } else if (t1.equals(b.String)) {
1595 String v1 = g.getValue(r1, Bindings.STRING);
1596 String v2 = g.getValue(r2, Bindings.STRING);
1597 return v1.equals(v2);
1598 } else if (t1.equals(b.Boolean)) {
1599 boolean v1 = g.getValue(r1, Bindings.BOOLEAN);
1600 boolean v2 = g.getValue(r2, Bindings.BOOLEAN);
1602 } else if (t1.equals(b.Byte)) {
1603 byte v1 = g.getValue(r1, Bindings.BYTE);
1604 byte v2 = g.getValue(r2, Bindings.BYTE);
1606 } else if (t1.equals(b.Long)) {
1607 long v1 = g.getValue(r1, Bindings.LONG);
1608 long v2 = g.getValue(r2, Bindings.LONG);
1610 } else if (t1.equals(b.IntegerArray)) {
1611 int[] v1 = g.getValue(r1, Bindings.INT_ARRAY);
1612 int[] v2 = g.getValue(r2, Bindings.INT_ARRAY);
1613 return Arrays.equals(v1,v2);
1614 } else if (t1.equals(b.FloatArray)) {
1615 float[] v1 = g.getValue(r1, Bindings.FLOAT_ARRAY);
1616 float[] v2 = g.getValue(r2, Bindings.FLOAT_ARRAY);
1617 return Arrays.equals(v1,v2);
1618 } else if (t1.equals(b.DoubleArray)) {
1619 double[] v1 = g.getValue(r1, Bindings.DOUBLE_ARRAY);
1620 double[] v2 = g.getValue(r2, Bindings.DOUBLE_ARRAY);
1621 return Arrays.equals(v1,v2);
1622 } else if (t1.equals(b.StringArray)) {
1623 String[] v1 = g.getValue(r1, Bindings.STRING_ARRAY);
1624 String[] v2 = g.getValue(r2, Bindings.STRING_ARRAY);
1625 return Arrays.equals(v1,v2);
1626 } else if (t1.equals(b.BooleanArray)) {
1627 boolean[] v1 = g.getValue(r1, Bindings.BOOLEAN_ARRAY);
1628 boolean[] v2 = g.getValue(r2, Bindings.BOOLEAN_ARRAY);
1629 return Arrays.equals(v1,v2);
1630 } else if (t1.equals(b.ByteArray)) {
1631 byte[] v1 = g.getValue(r1, Bindings.BYTE_ARRAY);
1632 byte[] v2 = g.getValue(r2, Bindings.BYTE_ARRAY);
1633 return Arrays.equals(v1,v2);
1634 } else if (t1.equals(b.LongArray)) {
1635 long[] v1 = g.getValue(r1, Bindings.LONG_ARRAY);
1636 long[] v2 = g.getValue(r2, Bindings.LONG_ARRAY);
1637 return Arrays.equals(v1,v2);
1639 Object v1 = g.getValue(r1);
1640 Object v2 = g.getValue(r2);
1641 return compareValue(v1, v2);
1646 public static boolean compareValue(Object v1, Object v2) {
1647 if (v1 instanceof Object[] && v2 instanceof Object[])
1648 return Arrays.deepEquals((Object[])v1, (Object[])v2);
1649 else if (v1 instanceof int[] && v2 instanceof int[])
1650 return Arrays.equals((int[])v1, (int[])v2);
1651 else if (v1 instanceof float[] && v2 instanceof float[])
1652 return Arrays.equals((float[])v1, (float[])v2);
1653 else if (v1 instanceof double[] && v2 instanceof double[])
1654 return Arrays.equals((double[])v1, (double[])v2);
1655 else if (v1 instanceof long[] && v2 instanceof long[])
1656 return Arrays.equals((long[])v1, (long[])v2);
1657 else if (v1 instanceof byte[] && v2 instanceof byte[])
1658 return Arrays.equals((byte[])v1, (byte[])v2);
1659 else if (v1 instanceof boolean[] && v2 instanceof boolean[])
1660 return Arrays.equals((boolean[])v1, (boolean[])v2);
1662 return v1.equals(v2);
1666 public class PredicateComparator implements Comparator<Statement> {
1668 public int compare(Statement o1, Statement o2) {
1669 if (comparableResources.contains(o1.getPredicate(), o2.getPredicate()))
1671 if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1673 if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1679 public class SubjectComparator implements Comparator<Statement> {
1681 public int compare(Statement o1, Statement o2) {
1682 if (comparableResources.contains(o1.getSubject(), o2.getSubject()))
1684 if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1686 if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1692 public class ObjectComparator implements Comparator<Statement> {
1694 public int compare(Statement o1, Statement o2) {
1695 if (comparableResources.contains(o1.getObject(), o2.getObject()))
1697 if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1699 if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1705 public static class FullStatementComparator implements Comparator<Statement> {
1707 public int compare(Statement o1, Statement o2) {
1708 if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1710 if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1712 if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1714 if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1716 if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1718 if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1724 public class ResComparator implements Comparator<Resource> {
1726 public int compare(Resource o1, Resource o2) {
1727 if (comparableResources.contains(o1, o2))
1729 if (o1.getResourceId() < o2.getResourceId())
1731 if (o1.getResourceId() > o2.getResourceId())