]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java
Added object checks for graph path comparison
[simantics/interop.git] / org.simantics.interop / src / org / simantics / interop / test / GraphComparator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Foster Wheeler Energia Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.interop.test;
13
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;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
25 import java.util.Stack;
26
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;
40
41 /**
42  * Compares two subgraphs and reports differences.
43  * 
44  * Assumes that subgraphs (defined using traverse relations) are not cyclic.
45  * 
46  * Assumes that properties can be used to identify objects, if relation type is not enough.
47  * 
48  * 
49  * 
50  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
51  *
52  */
53 public class GraphComparator {
54         
55         private static final boolean DEBUG = false;
56
57         private Resource r1;
58         private Resource r2;
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
64         
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<>();
71
72         private BijectionMap<Statement, Statement> comparableStatements = new BijectionMap<>();
73         private BijectionMap<Resource, Resource> comparableResources = new BijectionMap<>();
74         
75         private Set<Resource> processedResources = new HashSet<Resource>();
76         
77         private ResourceComparator comparator;
78         
79         private Comparator<Statement> scomp = new PredicateComparator();
80         private Comparator<Resource> rcomp = new ResComparator();
81         
82         private Set<Resource> nonMatchedLeft = new HashSet<Resource>();
83         private Set<Resource> nonMatchedRight = new HashSet<Resource>();
84         
85         // runtime attributes
86         
87         private ReadGraph g;
88         private Layer0 b;
89         
90         public GraphComparator(Resource r1, Resource r2) {
91                 this.r1 = r1;
92                 this.r2 = r2;
93                 comparator = new TypeComparator();
94         }
95         
96         public GraphComparator(Resource r1, Resource r2, ResourceComparator comparator) {
97                 this.r1 = r1;
98                 this.r2 = r2;
99                 this.comparator = comparator;
100         }
101         
102         ArrayList<Statement> ss1 = new ArrayList<Statement>();
103         ArrayList<Statement> ss2 = new ArrayList<Statement>();
104         
105         
106         public Comparator<Resource> getResourceComparator() {
107                 return rcomp;
108         }
109         
110         public Comparator<Statement> getStatementComparator() {
111                 return scomp;
112         }
113         
114         public Resource getR1() {
115                 return r1;
116         }
117         
118         public Resource getR2() {
119                 return r2;
120         }
121         
122         public void addTraversed(Resource rel) {
123                 traversed.add(rel);
124         }
125         
126         public void addTraversed(Collection<Resource> rels) {
127                 traversed.addAll(rels);
128         }
129         
130         public void addNonTraversed(Resource rel) {
131                 nonTraversed.add(rel);
132         }
133         
134         public void addNonTraversed(Collection<Resource> rels) {
135                 nonTraversed.addAll(rels);
136         }
137         
138         public void addTested(Resource rel) {
139                 tested.add(rel);
140         }
141         
142         public void addTested(Collection<Resource> rels) {
143                 tested.addAll(rels);
144         }
145         
146         public void addNonTested(Resource rel) {
147                 nonTested.add(rel);
148         }
149         
150         public void addNonTested(Collection<Resource> rels) {
151                 nonTested.addAll(rels);
152         }
153         
154         public void addComparableResources(Resource r1, Resource r2) {
155                 if (DEBUG)
156                         System.out.println("Preset " + r1 + " = " + r2);
157                 comparableResources.map(r1, r2);
158         }
159         
160         public void addComparableResources(BijectionMap<Resource, Resource> matching) {
161                 if (DEBUG) {
162                         for (Entry<Resource, Resource> entry : matching.getEntries())
163                                 System.out.println("Preset " + entry.getKey() + " = " + entry.getValue());
164                 }
165                 comparableResources.addAll(matching);
166         }
167         
168         public void addStrong(Resource r) {
169                 strong.add(r);
170         }
171         
172         public void addStrong(Collection<Resource> rels) {
173                 strong.addAll(rels);
174         }
175         
176         public void addNonMatchedLeft(Resource r) {
177                 nonMatchedLeft.add(r);
178         }
179         
180         public void addNonMatchedRight(Resource r) {
181                 nonMatchedRight.add(r);
182         }
183         
184         public void test(ReadGraph g) throws DatabaseException {
185                 this.g = g;
186                 this.b = Layer0.getInstance(g);
187                 comparator.setComparator(this);
188                 comparator.initialize(g, r1, r2);
189                 
190                 Stack<Resource> objectsLeft = new Stack<Resource>();
191                 Stack<Resource> objectsRight = new Stack<Resource>();
192                 objectsLeft.push(r1);
193                 objectsRight.push(r2);
194                 
195                 Set<Statement> unreliableLeft = new HashSet<Statement>();
196                 Set<Statement> unreliableRight = new HashSet<Statement>();
197                 
198                 while (true) {
199                         if (objectsLeft.isEmpty())
200                                 break;
201                         
202                         
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);
211                         }
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);
216                         }
217                         
218                 }
219                 for (Statement s : unreliableLeft) {
220                         if (!comparableStatements.containsLeft(s))
221                                 addDeletion(s);
222                 }
223                 for (Statement s : unreliableRight) {
224                         if (!comparableStatements.containsRight(s))
225                                 addAddition(s);
226                 }
227         }
228         
229         public void test(Session session) throws DatabaseException {
230                 test(session, r1, r2);
231         }
232         
233         public void test(Session session, Resource r1, Resource r2) throws DatabaseException {
234                 
235                 comparator.setComparator(this);
236                 
237                 session.syncRequest(new ReadRequest() {
238             
239             @Override
240             public void run(ReadGraph graph) throws DatabaseException {
241                 comparator.initialize(graph, r1, r2);
242             }
243         });
244                 
245                 addComparable(r1, r2);
246                 
247                 final Stack<Resource> objectsLeft = new Stack<Resource>();
248                 final Stack<Resource> objectsRight = new Stack<Resource>();
249                 objectsLeft.push(r1);
250                 objectsRight.push(r2);
251                 
252                 final Set<Statement> unreliableLeft = new HashSet<Statement>();
253                 final Set<Statement> unreliableRight = new HashSet<Statement>();
254                 
255                 while (true) {
256                         if (objectsLeft.isEmpty())
257                                 break;
258                         session.syncRequest(new ReadRequest() {
259                                 
260                                 @Override
261                                 public void run(ReadGraph graph) throws DatabaseException {
262                                         g = graph;
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);
272                                         }
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);
277                                         }
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);
282                                         }
283                                 }
284                         });
285                         
286                         
287                         
288                 }
289                 for (Statement s : unreliableLeft) {
290                         if (!comparableStatements.containsLeft(s))
291                                 addDeletion(s);
292                 }
293                 for (Statement s : unreliableRight) {
294                         if (!comparableStatements.containsRight(s))
295                                 addAddition(s);
296                 }
297                 
298                 
299         }
300         
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>();
304                 
305                 while (!objectsLeft.isEmpty()) {
306                         Resource r1 = objectsLeft.pop();
307                         Resource r2 = objectsRight.pop();
308                         
309                         if (r1.equals(r2))
310                                 continue;
311                         if (processedResources.contains(r1))
312                                 continue;
313                         processedResources.add(r1);
314                         
315                 
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.");
318                         }
319                         addComparable(r1, r2);
320                         
321                         //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));
322                         compareProps(r1, r2);
323                         
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);
333                                 
334                                 
335                                 compareStatements(ss1, ss2, null, null,null,null);
336                                 ss1.clear();
337                                 ss2.clear();
338                         }
339                         
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);
348                                 ss1.clear();
349                                 ss2.clear();
350                                 
351                         }
352                 }
353         }
354         
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>();
360                 
361                 for (Statement s : unreliableLeft) {
362                         subjectLeft.add(s.getSubject(),s);
363                         objectLeft.add(s.getObject(),s);
364                 }
365                 for (Statement s : unreliableRight) {
366                         subjectRight.add(s.getSubject(),s);
367                         objectRight.add(s.getObject(),s);
368                 }
369                 
370                 for (Resource left : subjectLeft.getKeys()) {
371                         Resource right = comparableResources.getRight(left);
372                         if (right == null)
373                                 continue;
374                         for (Statement leftS : subjectLeft.getValues(left)) {
375                                 Resource leftO = leftS.getObject();
376                                 if (!unreliableLeft.contains(leftS))
377                                         continue;
378                                 Resource rightO = comparableResources.getRight(leftO);
379                                 if (rightO == null)
380                                         continue;
381                                 for (Statement rightS : subjectRight.getValues(right)) {
382                                         if (!rightS.getObject().equals(rightO))
383                                                 continue;
384                                         if (!unreliableRight.contains(rightS))
385                                                 continue;
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);
391                                         }
392                                 }
393                         }               
394                 }
395         }
396         
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>();
402                 
403                 for (Statement s : unreliableLeft) {
404                         subjectLeft.add(s.getSubject(),s);
405                         objectLeft.add(s.getObject(),s);
406                 }
407                 for (Statement s : unreliableRight) {
408                         subjectRight.add(s.getSubject(),s);
409                         objectRight.add(s.getObject(),s);
410                 }
411                 
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()));
422                         }
423                         
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);
432                                         }
433                                 }
434                         }
435                         
436                         compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
437                         
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++) {
444                                                 useL[i] = false;
445                                                 useSL[i] = false;
446                                         }
447                                         for (int i = 0; i < left.size(); i++) {
448                                                 for (int j = 0; j < left.size(); j++) {
449                                                         if (useSL[j])
450                                                                 continue;
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)
456
457                                                                 int comp = comparator.compare(g, left.get(i).getObject(), similarLeft.get(j).getObject(), true);
458                                                                 if (comp >= 0 && comp < Integer.MAX_VALUE) {
459                                                                         useL[i] = true;
460                                                                         useSL[j] = true;
461                                                                         break;
462                                                                 }
463                                                         }
464                                                 }
465                                         }
466                                         boolean diff = false;
467                                         for (int i = 0; i < left.size(); i++) {
468                                                 if (!useL[i] || !useSL[i]) {
469                                                         diff = true;
470                                                 }
471                                         }
472                                         if (!diff) {
473                                                 hasSimilar = true;
474                                                 break;
475                                         }
476                                 }
477                         }
478                         
479                         if (hasSimilar)
480                                 continue;
481                                 
482                         
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);
488                         }
489                         
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);
495                                         
496                         }
497                         
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()];
505                                 
506                                 for (int i = 0; i < left.size(); i++) {
507                                         iLeft[i] = -1;
508                                         iRight[i] = -1;
509                                 }
510                                 
511                                 for (int l = 0; l < left.size(); l++) {
512                                         Statement ls = left.get(l);
513                                         for (int r = 0; r < right.size(); r++) {
514                                                 if (iRight[r] >= 0)
515                                                         continue;
516                                                 Statement rs = right.get(r);
517                                                 if (!comparableResources.contains(ls.getSubject(), rs.getSubject()))
518                                                         continue;
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) {
523                                                                 iLeft[l] = r;
524                                                                 iRight[r] = l;
525                                                                 break;
526                                                         }
527                                                         break;
528                                                 }
529                                         }
530                                         
531                                 }
532                                 boolean success = true;
533                                 for (int i = 0; i < left.size(); i++) {
534                                         if (iLeft[i] < 0) {
535                                                 success = false;
536                                                 break;
537                                         }
538                                         if (iRight[i] < 0) {
539                                                 success = false;
540                                                 break;
541                                         }
542                                                 
543                                 }
544                                 if (success) {
545                                         for (Statement s : right) 
546                                                 matchingOR.add(or,s);
547                                         matchingStatements.put(or, new Pair<int[], int[]>(iLeft, iRight));
548                                 }
549                         }
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);
555                                 
556                                 objectsLeft.add(ol);
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);
566                                 }
567                                 
568                         } else {
569                                 
570                         }
571
572                 }
573                 
574                 
575         }
576         
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>();
582                 
583                 for (Statement s : unreliableLeft) {
584                         subjectLeft.add(s.getSubject(),s);
585                         objectLeft.add(s.getObject(),s);
586                 }
587                 for (Statement s : unreliableRight) {
588                         subjectRight.add(s.getSubject(),s);
589                         objectRight.add(s.getObject(),s);
590                 }
591                 
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()));
602                         }
603                         
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()));
607                                 
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());
614                                         }
615                                 }
616                         }
617                 }
618         }
619         
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>();
625                 
626                 for (Statement s : unreliableLeft) {
627                         subjectLeft.add(s.getSubject(),s);
628                         objectLeft.add(s.getObject(),s);
629                 }
630                 for (Statement s : unreliableRight) {
631                         subjectRight.add(s.getSubject(),s);
632                         objectRight.add(s.getObject(),s);
633                 }
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)));
638                         }
639                         while (true) {
640                                 expand(pathsLeft);
641                                 if (pathsLeft.size() == 0)
642                                         break;
643                                 Collection<Path> endPaths = new ArrayList<Path>(1);
644                                 for (Path p : pathsLeft) {
645                                         if (comparableResources.containsLeft(p.getEnd())) {
646                                                 endPaths.add(p);
647                                         }
648                                 }
649                                 if (endPaths.size() > 0) {
650                                         pathsLeft.clear();
651                                         pathsLeft.addAll(endPaths);
652                                         break;
653                                 }       
654                         }
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());
661                                 }
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));
667                                         }
668                                         if (hasMatchingPaths(pathsLeft, possiblePathsRight)) {
669                                                 matchingPaths.put(or, possiblePathsRight);
670                                         }
671                                 }
672                                 if (matchingPaths.size() > 0) {
673                                         if (matchingPaths.size() == 1) {
674                                                 Resource or = matchingPaths.keySet().iterator().next();
675                                                 
676                                                 objectsLeft.add(ol);
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));
688                                                         }
689                                                 }
690                                         } 
691                                 }
692                         }
693                         
694                 }
695                 
696         }
697         
698         private boolean hasMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {
699                 if (leftPaths.size() != rightPaths.size())
700                         return false;
701                 BijectionMap<Path,Path> map = getMatchingPaths(leftPaths, rightPaths);
702                 return map.size() == leftPaths.size();
703         }
704         
705         private BijectionMap<Path,Path> getMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {
706                 BijectionMap<Path,Path> map = new BijectionMap<Path, Path>();
707                 for (Path leftPath : leftPaths) {
708                         for (Path rightPath : rightPaths) {
709                                 if (map.containsRight(rightPath))
710                                         continue;
711                                 if (leftPath.getLength() != rightPath.getLength())
712                                         continue;
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())) {
719                                                         match = false;
720                                                         break;
721                                                 }
722                                                 if ((getComparableResources().containsLeft(sl.getObject()) || getComparableResources().containsRight(sr.getObject())) && !getComparableResources().contains(sl.getObject(), sr.getObject())) {
723                                                         match = false;
724                                                         break;
725                                                 }
726                                         }
727                                         if (match) {
728                                                 map.map(leftPath, rightPath);
729                                                 break;
730                                         }
731                                 }
732                         }
733                 }
734                 return map;
735         }
736         
737         private void expand(Set<Path> paths) throws DatabaseException {
738                 Set<Path> stepPathsLeft = new HashSet<Path>();
739                 if (paths.size() == 0)
740                         return;
741                 int length = paths.iterator().next().getLength() + 1;
742                 for (Path p : paths) {
743                         for (Resource rel : traversed) {
744                                 stepPathsLeft.addAll(Path.expand(p,g.getStatements(p.getEnd(), rel)));
745                         }
746                 }
747                 paths.clear();
748                 for (Path p : stepPathsLeft) {
749                         if (p.getLength() == length)
750                                 paths.add(p);
751                 }
752         }
753         
754         private Collection<Path> findComparableRight(Path leftPath, Resource beginRight) throws DatabaseException {
755                 Set<Path> rightPaths = new HashSet<Path>();
756                 rightPaths.addAll(Path.create(g.getStatements(beginRight, getRight(leftPath.getStatements().get(0).getPredicate()))));
757                 for (int i = 1; i < leftPath.getLength(); i++) {
758                         if (rightPaths.size() == 0)
759                                 return rightPaths;
760                         Set<Path> stepPaths = new HashSet<Path>();
761                         for (Path p : rightPaths) {
762                                 stepPaths.addAll(Path.expand(p, g.getStatements(p.getEnd(), getRight(leftPath.getStatements().get(i).getPredicate()))));
763                         }
764                         rightPaths.clear();
765                         for (Path p : stepPaths)
766                                 if (p.getLength() == i+1) 
767                                         rightPaths.add(p);
768                 }
769                 return rightPaths;
770                 
771         }
772         
773         private Resource getRight(Resource r) {
774                 if (comparableResources.containsLeft(r))
775                         return comparableResources.getRight(r);
776                 return r;
777         }
778         
779
780         
781         public BijectionMap<Statement, Statement> getComparableStatements() {
782                 return comparableStatements;
783         }
784         
785         public BijectionMap<Resource, Resource> getComparableResources() {
786                 return comparableResources;
787         }
788         
789         public GraphChanges getChanges() {
790                 return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources);
791         }
792         
793         private void addComparable(Statement left, Statement right) throws DatabaseException {
794                 addComparable(left.getObject(), right.getObject());
795                 comparableStatements.map(left, right);
796                 //comparableResources.map(left.getObject(), right.getObject());
797         }
798         
799         private void addComparable(Resource left, Resource right) throws DatabaseException {
800                 if(!comparableResources.contains(left, right)) {
801                         if (comparableResources.containsLeft(left)||comparableResources.containsRight(right)) {
802                                 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.");
803                         } else {
804                                 if (DEBUG) System.out.println(left + " = " + right);
805                                 comparableResources.map(left, right);
806                         }
807                 }
808                 
809         }
810         
811         public List<Statement> filterAsserted(Resource r, Collection<Statement> in) throws DatabaseException {
812                 List<Statement> out = new ArrayList<Statement>();
813                 for (Statement s : in) {
814                         if (!s.isAsserted(r))
815                                 out.add(s);
816                         
817                 }
818                 return out;
819         }
820         
821         public List<Statement> filterAssertedDuplicates(Resource r, List<Statement> in) throws DatabaseException {
822                 List<Statement> out = new ArrayList<Statement>();
823                 for (int i = 0; i < in.size(); i++) {
824                         Statement s = in.get(i);
825                         if (!s.isAsserted(r))
826                                 out.add(s);
827                         else {
828                                 boolean has = false;
829                                 if (i > 1 && in.get(i-1).getPredicate().equals(s.getPredicate()))
830                                         has = true;
831                                 else if (i < in.size()-1 && in.get(i+1).getPredicate().equals(s.getPredicate()))
832                                         has = true;
833                                 if (!has)
834                                         out.add(s);
835                         }
836                         
837                 }
838                 for (Statement s : in) {
839                         if (!s.isAsserted(r))
840                                 out.add(s);
841                         
842                 }
843                 return out;
844         }
845         
846         
847
848         private String printStatement(ReadGraph graph, Statement s) throws DatabaseException {
849                 return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject());
850         }
851         
852         private List<Statement> filterTraversed(List<Statement> in) throws DatabaseException {
853                 return filter(traversed, in);
854         }
855         
856         private List<Statement> filterNonTested(List<Statement> in) throws DatabaseException {
857                 return filter(nonTested, in);
858         }
859         
860         private List<Statement> filterNonTraversed(List<Statement> in) throws DatabaseException {
861                 return filter(nonTraversed, in);
862         }
863         
864         private List<Statement> filter(Collection<Resource> toFilter, List<Statement> in) throws DatabaseException {
865                 if (toFilter.size() == 0)
866                         return in;
867                 List<Statement> out = new ArrayList<Statement>();
868                 for (Statement s : in) {
869                         boolean usable = true;
870                         for (Resource r : toFilter) {
871                                 if (g.isSubrelationOf(s.getPredicate(),r)) {
872                                         usable = false;
873                                         break;
874                                 }
875                         }
876                         if (usable) {
877                                 out.add(s);
878                         }
879                         
880                 }
881                 return out;
882         }
883         
884         
885         private void addDeletion(Statement s) {
886                 if (!changes1Set.contains(s)) {
887                         changes1Set.add(s);
888                         changes1.add(s);
889                 }
890         }
891         
892         private void addAddition(Statement s) {
893                 if (!changes2Set.contains(s)) {
894                         changes2Set.add(s);
895                         changes2.add(s);
896                 }
897         }
898         
899         private void addModification(Resource sub1, Statement s1, Resource sub2, Statement s2) {
900                 Modification mod = new Modification(sub1, sub2, s1, s2);
901                 if (!modificationsSet.contains(mod)) {
902                         modificationsSet.add(mod);
903                         modifications.add(mod);
904                 }
905         }
906         
907         public void sortStatement(List<Statement> list1, List<Statement> list2) {
908                 sortStatement(list1, list2, scomp);
909         }
910         
911         public void sortStatement(List<Statement> list1, List<Statement> list2, Comparator<Statement> scomp) {
912                 Collections.sort(list1,scomp);
913                 Collections.sort(list2,scomp);
914                 
915                 List<Statement> sorted1 = new ArrayList<Statement>(list1.size());
916                 List<Statement> sorted2 = new ArrayList<Statement>(list2.size());
917                 sorted1.addAll(list1);
918                 sorted2.addAll(list2);
919                 
920                 int ss1 = 0;
921                 int ss2 = 0;
922                 for (int i = 0; i < list1.size(); ) {
923                         Statement s1 = list1.get(i);
924                         int same1 = sameRel(list1, i);  
925                         for (int j = 0; j < list2.size(); j++) {
926                                 Statement s2 = list2.get(j);
927                                 if (scomp.compare(s1, s2) == 0) {
928                                         int same2 = sameRel(list2, j);
929                                         copy(sorted1,ss1,list1,i,same1);
930                                         ss1 += same1;
931                                         copy(sorted2,ss2,list2,j,same2);
932                                         ss2 += same2;
933                                         break;
934                                 }
935                         }
936                         i+= same1;
937                 }
938                 if (ss1 < sorted1.size()) {
939                         for (Statement s : list1) {
940                                 if (!sorted1.contains(s)) {
941                                         sorted1.set(ss1,s);
942                                         ss1++;
943                                 }
944                         }
945                 }
946                 if (ss2 < sorted2.size()) {
947                         for (Statement s : list2) {
948                                 if (!sorted2.contains(s)) {
949                                         sorted2.set(ss2,s);
950                                         ss2++;
951                                 }
952                         }
953                 }
954                 
955                 list1.clear();
956                 list2.clear();
957                 list1.addAll(sorted1);
958                 list2.addAll(sorted2);
959         }
960         
961         public <T> void copy(List<T> to, int toIndex, List<T> from, int fromIndex, int amount) {
962                 for (int i = 0; i <  amount; i++) {
963                         to.set(toIndex + i, from.get(fromIndex+ i));
964                 }
965         }
966         
967         public void sortResource(List<Resource> list1, List<Resource> list2) {
968                 Collections.sort(list1,rcomp);
969                 int js = 0;
970                 for (int i = 0; i < list1.size(); i++) {
971                         Resource s1 = list1.get(i);
972                         for (int j = js; j < list2.size(); j++) {
973                                 Resource s2 = list2.get(j);
974                                 if (rcomp.compare(s1, s2) == 0) {
975                                         Resource t = list2.get(js);
976                                         list2.set(js, s2);
977                                         list2.set(j, t);
978                                         break;
979                                 }
980                         }
981                         js++;
982                 }
983         }
984         
985         private void compareStatements(List<Statement> ss1, List<Statement> ss2, Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Collection<Statement> unreliableLeft, Collection<Statement> unreliableRight) throws DatabaseException {
986                 sortStatement(ss1, ss2);
987                 
988                 int i1 = 0;
989                 int i2 = 0;
990                 
991                 while (true) {
992                         if (i1 >= ss1.size()) {
993                                 if (i2 >= ss2.size()) {
994                                         break;
995                                 } else {
996                                         while (i2 < ss2.size()) {
997                                                 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i2)));
998                                                 
999                                                 addAddition(ss2.get(i2));
1000                                                 i2++;
1001                                         }
1002                                         break;
1003                                 }
1004                         } else if (i2 >= ss2.size()) {
1005                                 while (i1 < ss1.size()) {
1006                                         if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i1)));
1007                                         addDeletion(ss1.get(i1));
1008                                         i1++;
1009                                 }
1010                                 break;
1011                         }
1012                         int same1 = sameRel(ss1, i1);
1013                         int same2 = sameRel(ss2, i2);
1014                         int c = rcomp.compare(ss1.get(i1).getPredicate(),ss2.get(i2).getPredicate());
1015                         if (c == 0) {
1016                                 compareStatements(ss1, i1, same1, ss2, i2, same2,objectsLeft,objectsRight,unreliableLeft,unreliableRight);
1017                                 i1+=same1;
1018                                 i2+=same2;
1019                         } else if (c < 0) {
1020                                 for (int i = 0; i < same1; i++) {
1021                                         if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i+i1)));
1022                                         addDeletion(ss1.get(i+i1));
1023                                 }
1024                                 i1 += same1;
1025                         } else {
1026                                 for (int i = 0; i < same2; i++) {
1027                                         if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i+i2)));
1028                                         addAddition(ss2.get(i+i2));
1029                                 }
1030                                 
1031                                 i2 += same2;
1032                         }
1033                 }
1034         }
1035         
1036
1037         
1038         private int sameRel(List<Statement> statements, int off) {
1039                 if (statements.size() <= off)
1040                         return 0;
1041                 int same = 1;
1042                 long id = statements.get(off).getPredicate().getResourceId();
1043                 for (int i = off+1; i <statements.size(); i++) {
1044                         if (statements.get(i).getPredicate().getResourceId() == id)
1045                                 same++;
1046                         else 
1047                                 break;
1048                 }
1049                 return same;
1050                 
1051         }
1052
1053         private int compareObject(Resource o1, Resource o2) throws DatabaseException {
1054                 if (o1.equals(o2))
1055                         return -1;
1056                 if (comparableResources.contains(o1, o2))
1057                         return (-1);
1058                 if (comparableResources.containsLeft(o1))
1059                         return Integer.MAX_VALUE;
1060                 if (comparableResources.containsRight(o2))
1061                         return Integer.MAX_VALUE;
1062                 if (nonMatchedLeft.contains(o1))
1063                         return Integer.MAX_VALUE;
1064                 if (nonMatchedRight.contains(o2))
1065                         return Integer.MAX_VALUE;
1066                 return comparator.compare(g, o1, o2);
1067         }
1068         
1069         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 {
1070                 boolean[] used1 = new boolean[len1];
1071                 for (int i = 0; i < used1.length; i++) {
1072                         used1[i] = false;
1073                 }
1074                 
1075                 boolean[] used2 = new boolean[len2];
1076                 for (int i = 0; i < used2.length; i++) {
1077                         used2[i] = false;
1078                 }
1079                 
1080                 // left, right, difference
1081                 List<List<Integer>> differences = new ArrayList<List<Integer>>();
1082                 for (int i1 = off1; i1 < off1 + len1; i1++) {
1083                         Statement s1 = ss1.get(i1);
1084                         List<Integer> diff = new ArrayList<Integer>();
1085                         for (int i2 = off2; i2 < off2 + len2; i2++) {
1086                                 Statement s2 = ss2.get(i2);
1087                                 int d = compareObject(s1.getObject(), s2.getObject());
1088                                 if (d == 0) {
1089                                         for (Resource t : strong) {
1090                                                  if (s1.getPredicate().equals(t) || g.isSubrelationOf(s1.getPredicate(), t)) {
1091                                                          d = 1;
1092                                                          break;
1093                                                  }
1094                                         }
1095                                 }
1096                                 diff.add(d);
1097                         }
1098                         differences.add(diff);
1099                 }
1100                 // difference, left
1101                 MapList<Integer, Integer> priorities = new MapList<Integer, Integer>();
1102                 for (int i = 0; i < differences.size(); i++) {
1103                         List<Integer> list = differences.get(i);
1104                         for (int j = 0; j < list.size(); j++) {
1105                                 priorities.add(list.get(j), i);
1106                         }
1107                 }
1108                 
1109                 Integer[] pris = priorities.getKeys(new Integer[]{});
1110                 Arrays.sort(pris);
1111                 
1112                 for (Integer pri : pris) {
1113                         if (pri == Integer.MAX_VALUE) {
1114
1115                         } else if (pri == 0) {
1116                                 
1117                         } else {
1118                                 List<Integer> i1s = priorities.getValues(pri);
1119                                 for (Integer i1 : i1s) {
1120                                         if (used1[i1])
1121                                                 continue;
1122                                         List<Integer> i2diff = differences.get(i1);
1123                                         for (int i2 = 0; i2 < i2diff.size(); i2++) {
1124                                                 if (i2diff.get(i2) == pri) {
1125                                                         if (used2[i2])
1126                                                                 continue;
1127                                                         used1[i1] = true;
1128                                                         used2[i2] = true;
1129                                                         Statement s1  = ss1.get(i1+off1);
1130                                                         Statement s2  = ss2.get(i2+off2);
1131                                                         
1132                                                         if (objectsLeft != null) {
1133                                                                 objectsLeft.add(s1.getObject());
1134                                                                 objectsRight.add(s2.getObject());
1135                                                         } 
1136                                                         addComparable(s1, s2);
1137                                                         break;
1138                                                 }
1139                                         }
1140                                 }
1141                         }
1142                 }
1143                 
1144                 for (Integer pri : pris) {
1145                         if (pri != 0)
1146                                 continue;
1147                         Set<Statement> s1s = new HashSet<Statement>();
1148                         Set<Statement> s2s = new HashSet<Statement>();
1149                         Set<Integer> s1i = new HashSet<Integer>();
1150                         Set<Integer> s2i = new HashSet<Integer>();
1151                         List<Integer> i1s = priorities.getValues(pri);
1152                         for (Integer i1 : i1s) {
1153                                 if (used1[i1])
1154                                         continue;
1155                                 List<Integer> i2diff = differences.get(i1);
1156                                 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1157                                         if (i2diff.get(i2) == pri) {
1158                                                 if (used2[i2])
1159                                                         continue;
1160                                                 Statement s1  = ss1.get(i1+off1);
1161                                                 Statement s2  = ss2.get(i2+off2);
1162                                                 s1s.add(s1);
1163                                                 s2s.add(s2);
1164                                                 s1i.add(i1);
1165                                                 s2i.add(i2);
1166                                         }
1167                                 }
1168                         }
1169                         if (unreliableLeft != null) {
1170                                 unreliableLeft.addAll(s1s);
1171                                 unreliableRight.addAll(s2s);
1172                         }
1173                         for (Integer i : s1i)
1174                                 used1[i] = true;
1175                         for (Integer i : s2i)
1176                                 used2[i] = true;
1177
1178                 }
1179                 for (int i1 = off1; i1 < off1 + len1; i1++) {
1180                         if (!used1[i1-off1]) {
1181                                 if (DEBUG) System.out.println("Compare Object deletion " + printStatement(g,ss1.get(i1)));
1182                                 addDeletion(ss1.get(i1));
1183                         }
1184                 }
1185                 for (int i2 = off2; i2 < off2 + len2; i2++) {
1186                         if (!used2[i2-off2]) {
1187                                 if (DEBUG) System.out.println("Compare Object addition " + printStatement(g,ss2.get(i2)));
1188                                 addAddition(ss2.get(i2));
1189                         }
1190                 }
1191         }
1192         
1193         
1194         
1195         /**
1196          * compares properties, assumes functional relations
1197          * @param r1
1198          * @param r2
1199          * @throws ServiceException
1200          * @throws DoesNotContainValueException
1201          * @throws ValidationException 
1202          */
1203         private void compareProps(Resource r1, Resource r2) throws DatabaseException {
1204                 if (DEBUG) System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2));
1205                 List<Statement> ss1 = new ArrayList<Statement>();
1206                 List<Statement> ss2 = new ArrayList<Statement>();
1207                 ss1.addAll(g.getStatements(r1, b.HasProperty));
1208                 ss2.addAll(g.getStatements(r2, b.HasProperty));
1209                 ss1 = filterNonTested(ss1);
1210                 ss2 = filterNonTested(ss2);
1211                 sortStatement(ss1, ss2);
1212                 // getStatements(r1, b.HasProperty) returns both instance and asserted statements for the same property relation. 
1213                 ss1 = filterAssertedDuplicates(r1, ss1);
1214                 ss2 = filterAssertedDuplicates(r2, ss2);
1215                 
1216                 int i1 = 0; 
1217                 int i2 = 0;
1218                 
1219                 while (true) {
1220                         if (i1 >= ss1.size()) {
1221                                 if (i2 >= ss2.size())
1222                                         break;
1223                                 else {
1224                                         while (i2 < ss2.size()) {
1225                                                 Statement s = ss2.get(i2);
1226                                                 if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,s));
1227                                                 if (!s.isAsserted(r2))
1228                                                         addAddition(s);
1229                                                 i2++;
1230                                         }
1231                                         break;
1232                                 }
1233                         } else if (i2 >= ss2.size()) {
1234                                 while (i1 < ss1.size()) {
1235                                         Statement s = ss1.get(i1);
1236                                         if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,s));
1237                                         if (!s.isAsserted(r1))
1238                                                 addDeletion(s);
1239                                         i1++;
1240                                 }
1241                                 break;
1242                         }
1243                         Statement s1 = ss1.get(i1);
1244                         Statement s2 = ss2.get(i2);
1245                         if (s1.isAsserted(r1) && s2.isAsserted(r2)) {
1246                                 i1++;
1247                                 i2++;
1248                                 continue;
1249                         }
1250                         int c = scomp.compare(s1, s2);
1251                         switch (c) {
1252                                 case 0:{
1253                                         boolean b1 = g.hasValue(s1.getObject());
1254                                         boolean b2 = g.hasValue(s2.getObject());
1255                                         if (b1 == b2) {
1256                                                 if (b1) {
1257 //                                                      Object v1 = g.getValue(s1.getObject());
1258 //                                                      Object v2 = g.getValue(s2.getObject());
1259 //                                                      boolean eq = compareValue(v1, v2);
1260                                                         boolean eq = compareValue(g,b,s1.getObject(), s2.getObject());
1261                                                         if (!eq) {
1262                                                                 addModification(r1,s1,r2,s2);
1263                                                                 if (!s1.isAsserted(r1) && !s2.isAsserted(r2))
1264                                                                         addComparable(s1, s2);
1265                                                         }
1266                                                 } else {
1267                                                         if (!s1.getObject().equals(s1.getSubject()) && !s2.getObject().equals(s2.getSubject()))
1268                                                                 compareProps(s1.getObject(), s2.getObject());
1269                                                 }
1270                                         } else {
1271                                                 addModification(r1,s1,r2,s2);
1272                                                 if (!s1.isAsserted(r1) && !s2.isAsserted(r2))
1273                                                         addComparable(s1, s2);
1274                                         }
1275                                         i1++;
1276                                         i2++;
1277                                         break;
1278                                 }
1279                                 case -1:{
1280                                         if (DEBUG) System.out.println("Compare Prop diff1s " + printStatement(g,s1));
1281                                         addDeletion(s1);
1282                                         i1++;
1283                                         break;
1284                                 }
1285                                         
1286                                 case 1:{
1287                                         if (DEBUG) System.out.println("Compare Prop diff2s " + printStatement(g,s2));
1288                                         addAddition(s2);
1289                                         i2++;
1290                                         break;
1291                                 }
1292                         }
1293
1294                 }
1295                 
1296                 ss1.clear();
1297                 ss2.clear();
1298                 
1299         }
1300         
1301         public static boolean compareValue(ReadGraph g, Layer0 b, Resource r1, Resource r2) throws DatabaseException {
1302                 Resource t1 = g.getSingleType(r1);
1303                 Resource t2 = g.getSingleType(r2);
1304                 if (!t1.equals(t2))
1305                         return false;
1306                 if (t1.equals(b.Integer)) {
1307                         int v1 = g.getValue(r1, Bindings.INTEGER);
1308                         int v2 = g.getValue(r2, Bindings.INTEGER);
1309                         return v1 == v2;
1310                 } else if (t1.equals(b.Float)) {
1311                         float v1 = g.getValue(r1, Bindings.FLOAT);
1312                         float v2 = g.getValue(r2, Bindings.FLOAT);
1313                         return v1 == v2;
1314                 } else if (t1.equals(b.Double)) {
1315                         double v1 = g.getValue(r1, Bindings.DOUBLE);
1316                         double v2 = g.getValue(r2, Bindings.DOUBLE);
1317                         return v1 == v2;
1318                 } else if (t1.equals(b.String)) {
1319                         String v1 = g.getValue(r1, Bindings.STRING);
1320                         String v2 = g.getValue(r2, Bindings.STRING);
1321                         return v1.equals(v2);
1322                 } else if (t1.equals(b.Boolean)) {
1323                         boolean v1 = g.getValue(r1, Bindings.BOOLEAN);
1324                         boolean v2 = g.getValue(r2, Bindings.BOOLEAN);
1325                         return v1 == v2;
1326                 } else if (t1.equals(b.Byte)) {
1327                         byte v1 = g.getValue(r1, Bindings.BYTE);
1328                         byte v2 = g.getValue(r2, Bindings.BYTE);
1329                         return v1 == v2;
1330                 } else if (t1.equals(b.Long)) {
1331                         long v1 = g.getValue(r1, Bindings.LONG);
1332                         long v2 = g.getValue(r2, Bindings.LONG);
1333                         return v1 == v2;
1334                 } else if (t1.equals(b.IntegerArray)) {
1335                         int[] v1 = g.getValue(r1, Bindings.INT_ARRAY);
1336                         int[] v2 = g.getValue(r2, Bindings.INT_ARRAY);
1337                         return Arrays.equals(v1,v2);
1338                 } else if (t1.equals(b.FloatArray)) {
1339                         float[] v1 = g.getValue(r1, Bindings.FLOAT_ARRAY);
1340                         float[] v2 = g.getValue(r2, Bindings.FLOAT_ARRAY);
1341                         return Arrays.equals(v1,v2);
1342                 } else if (t1.equals(b.DoubleArray)) {
1343                         double[] v1 = g.getValue(r1, Bindings.DOUBLE_ARRAY);
1344                         double[] v2 = g.getValue(r2, Bindings.DOUBLE_ARRAY);
1345                         return Arrays.equals(v1,v2);
1346                 } else if (t1.equals(b.StringArray)) {
1347                         String[] v1 = g.getValue(r1, Bindings.STRING_ARRAY);
1348                         String[] v2 = g.getValue(r2, Bindings.STRING_ARRAY);
1349                         return Arrays.equals(v1,v2);
1350                 } else if (t1.equals(b.BooleanArray)) {
1351                         boolean[] v1 = g.getValue(r1, Bindings.BOOLEAN_ARRAY);
1352                         boolean[] v2 = g.getValue(r2, Bindings.BOOLEAN_ARRAY);
1353                         return Arrays.equals(v1,v2);
1354                 } else if (t1.equals(b.ByteArray)) {
1355                         byte[] v1 = g.getValue(r1, Bindings.BYTE_ARRAY);
1356                         byte[] v2 = g.getValue(r2, Bindings.BYTE_ARRAY);
1357                         return Arrays.equals(v1,v2);
1358                 } else if (t1.equals(b.LongArray)) {
1359                         long[] v1 = g.getValue(r1, Bindings.LONG_ARRAY);
1360                         long[] v2 = g.getValue(r2, Bindings.LONG_ARRAY);
1361                         return Arrays.equals(v1,v2);
1362                 } else {
1363                         Object v1 = g.getValue(r1);
1364                         Object v2 = g.getValue(r2);
1365                         return compareValue(v1, v2);
1366                 }
1367                 
1368         }
1369         
1370         public static boolean compareValue(Object v1, Object v2) {
1371                 if (v1 instanceof Object[] && v2 instanceof Object[])
1372                         return Arrays.deepEquals((Object[])v1, (Object[])v2);
1373                 else if (v1 instanceof int[] && v2 instanceof int[]) 
1374                         return Arrays.equals((int[])v1, (int[])v2);
1375                 else if (v1 instanceof float[] && v2 instanceof float[]) 
1376                         return Arrays.equals((float[])v1, (float[])v2);
1377                 else if (v1 instanceof double[] && v2 instanceof double[]) 
1378                         return Arrays.equals((double[])v1, (double[])v2);
1379                 else if (v1 instanceof long[] && v2 instanceof long[]) 
1380                         return  Arrays.equals((long[])v1, (long[])v2);
1381                 else if (v1 instanceof byte[] && v2 instanceof byte[]) 
1382                         return Arrays.equals((byte[])v1, (byte[])v2);
1383                 else if (v1 instanceof boolean[] && v2 instanceof boolean[]) 
1384                         return Arrays.equals((boolean[])v1, (boolean[])v2);
1385                 else
1386                         return v1.equals(v2);
1387         }
1388
1389         
1390         public class PredicateComparator implements Comparator<Statement> {
1391                 @Override
1392                 public int compare(Statement o1, Statement o2) {
1393                         if (comparableResources.contains(o1.getPredicate(), o2.getPredicate()))
1394                                 return 0;
1395                         if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1396                                 return -1;
1397                         if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1398                                 return 1;
1399                         return 0;
1400                 }
1401         }
1402         
1403         public class SubjectComparator implements Comparator<Statement> {
1404                 @Override
1405                 public int compare(Statement o1, Statement o2) {
1406                         if (comparableResources.contains(o1.getSubject(), o2.getSubject()))
1407                                 return 0;
1408                         if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1409                                 return -1;
1410                         if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1411                                 return 1;
1412                         return 0;
1413                 }
1414         }
1415         
1416         public class ObjectComparator implements Comparator<Statement> {
1417                 @Override
1418                 public int compare(Statement o1, Statement o2) {
1419                         if (comparableResources.contains(o1.getObject(), o2.getObject()))
1420                                 return 0;
1421                         if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1422                                 return -1;
1423                         if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1424                                 return 1;
1425                         return 0;
1426                 }
1427         }
1428         
1429         public static class FullStatementComparator implements Comparator<Statement> {
1430                 @Override
1431                 public int compare(Statement o1, Statement o2) {
1432                         if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1433                                 return -1;
1434                         if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1435                                 return 1;
1436                         if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1437                                 return -1;
1438                         if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1439                                 return 1;
1440                         if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1441                                 return -1;
1442                         if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1443                                 return 1;
1444                         return 0;
1445                 }
1446         }
1447         
1448         public class ResComparator implements Comparator<Resource> {
1449                 @Override
1450                 public int compare(Resource o1, Resource o2) {
1451                         if (comparableResources.contains(o1, o2))
1452                                 return 0;
1453                         if (o1.getResourceId() < o2.getResourceId())
1454                                 return -1;
1455                         if (o1.getResourceId() > o2.getResourceId())
1456                                 return 1;
1457                         return 0;
1458                 }
1459         }
1460
1461 }