]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java
052a9a9ee19ad7bce23cba188e2bc923fbd76a2d
[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 = true;
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 (r1.getResourceId() == 610446L)
312                                 System.out.println();
313                         if (processedResources.contains(r1))
314                                 continue;
315                         processedResources.add(r1);
316                         
317                 
318                         if((comparableResources.containsLeft(r1)||comparableResources.containsRight(r2)) && !comparableResources.contains(r1, r2)) {
319                                 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.");
320                         }
321                         addComparable(r1, r2);
322                         
323                         //System.out.println("test " + NameUtils.getSafeName(g, r1) + " " + NameUtils.getSafeName(g, r2));
324                         compareProps(r1, r2);
325                         
326                         for (Resource rel : tested) {
327                                 ss1.addAll(g.getStatements(r1, rel));
328                                 ss2.addAll(g.getStatements(r2, rel));
329                                 ss1 = filterAsserted(r1, ss1);
330                                 ss2 = filterAsserted(r2, ss2);
331                                 ss1 = filterTraversed(ss1);
332                                 ss2 = filterTraversed(ss2);
333                                 ss1 = filterNonTested(ss1);
334                                 ss2 = filterNonTested(ss2);
335                                 
336                                 
337                                 compareStatements(ss1, ss2, null, null,null,null);
338                                 ss1.clear();
339                                 ss2.clear();
340                         }
341                         
342                         for (Resource rel : traversed) {
343                                 ss1.addAll(g.getStatements(r1, rel));
344                                 ss2.addAll(g.getStatements(r2, rel));
345                                 ss1 = filterAsserted(r1, ss1);
346                                 ss2 = filterAsserted(r2, ss2);
347                                 ss1 = filterNonTraversed(ss1);
348                                 ss2 = filterNonTraversed(ss2);
349                                 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
350                                 ss1.clear();
351                                 ss2.clear();
352                                 
353                         }
354                 }
355         }
356         
357         private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight) throws DatabaseException {
358                 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
359                 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
360                 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
361                 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
362                 
363                 for (Statement s : unreliableLeft) {
364                         subjectLeft.add(s.getSubject(),s);
365                         objectLeft.add(s.getObject(),s);
366                 }
367                 for (Statement s : unreliableRight) {
368                         subjectRight.add(s.getSubject(),s);
369                         objectRight.add(s.getObject(),s);
370                 }
371                 
372                 for (Resource left : subjectLeft.getKeys()) {
373                         Resource right = comparableResources.getRight(left);
374                         if (right == null)
375                                 continue;
376                         for (Statement leftS : subjectLeft.getValues(left)) {
377                                 Resource leftO = leftS.getObject();
378                                 if (!unreliableLeft.contains(leftS))
379                                         continue;
380                                 Resource rightO = comparableResources.getRight(leftO);
381                                 if (rightO == null)
382                                         continue;
383                                 for (Statement rightS : subjectRight.getValues(right)) {
384                                         if (!rightS.getObject().equals(rightO))
385                                                 continue;
386                                         if (!unreliableRight.contains(rightS))
387                                                 continue;
388                                         if (leftS.getPredicate().equals(rightS.getPredicate()) ||
389                                                 comparableResources.contains(leftS.getPredicate(), rightS.getPredicate())) {
390                                                 unreliableLeft.remove(leftS);
391                                                 unreliableRight.remove(rightS);
392                                                 addComparable(leftS, rightS);
393                                         }
394                                 }
395                         }               
396                 }
397         }
398         
399         private void processUnreliable(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
400                 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
401                 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
402                 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
403                 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
404                 
405                 for (Statement s : unreliableLeft) {
406                         subjectLeft.add(s.getSubject(),s);
407                         objectLeft.add(s.getObject(),s);
408                 }
409                 for (Statement s : unreliableRight) {
410                         subjectRight.add(s.getSubject(),s);
411                         objectRight.add(s.getObject(),s);
412                 }
413                 
414                 for (Resource ol : objectLeft.getKeys()) {
415                         // all statements to the left side object
416                         List<Statement> left = objectLeft.getValues(ol);
417                         // all subjects that have statements to the left side object (ol)
418                         Set<Resource> sLeft = new HashSet<Resource>();
419                         // all matching subjects on the right side
420                         Set<Resource> sRight = new HashSet<Resource>();
421                         for (Statement s : left) {
422                                 sLeft.add(s.getSubject());
423                                 sRight.add(comparableResources.getRight(s.getSubject()));
424                         }
425                         
426                         // check if object left can be reliably identified by available statements
427                         // if there are any objects on the left side with similar statements, object left cannot be mapped.
428                         boolean hasSimilar = false;
429                         MapList<Resource, Statement> comparableOLeft = new MapList<Resource, Statement>();
430                         for (Resource sl : sLeft) {
431                                 for (Statement s : subjectLeft.getValues(sl)) {
432                                         if (!s.getObject().equals(ol)) {
433                                                 comparableOLeft.add(s.getObject(),s);
434                                         }
435                                 }
436                         }
437                         
438                         compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
439                         
440                         for (Resource similarOl : comparableOLeft.getKeys()) {
441                                 List<Statement> similarLeft = comparableOLeft.getValues(similarOl);
442                                 if (similarLeft.size() == left.size()) {
443                                         boolean useL[] = new boolean[left.size()];
444                                         boolean useSL[] = new boolean[left.size()];
445                                         for (int i = 0; i < left.size(); i++) {
446                                                 useL[i] = false;
447                                                 useSL[i] = false;
448                                         }
449                                         for (int i = 0; i < left.size(); i++) {
450                                                 for (int j = 0; j < left.size(); j++) {
451                                                         if (useSL[j])
452                                                                 continue;
453                                                         // compare predicates
454                                                         Resource pl = left.get(i).getPredicate();
455                                                         Resource psl = similarLeft.get(j).getPredicate();
456                                                         if (pl.equals(psl)) {
457                                                                 // compare objects (unreliable result is interpreted as positive match)
458
459                                                                 int comp = comparator.compare(g, left.get(i).getObject(), similarLeft.get(j).getObject(), true);
460                                                                 if (comp >= 0 && comp < Integer.MAX_VALUE) {
461                                                                         useL[i] = true;
462                                                                         useSL[j] = true;
463                                                                         break;
464                                                                 }
465                                                         }
466                                                 }
467                                         }
468                                         boolean diff = false;
469                                         for (int i = 0; i < left.size(); i++) {
470                                                 if (!useL[i] || !useSL[i]) {
471                                                         diff = true;
472                                                 }
473                                         }
474                                         if (!diff) {
475                                                 hasSimilar = true;
476                                                 break;
477                                         }
478                                 }
479                         }
480                         
481                         if (hasSimilar)
482                                 continue;
483                                 
484                         
485                         // all objects that subjects on the right side point to. Object left has its matching resource among these, if it has matching resource
486                         MapList<Resource,Statement> possibleOR = new MapList<Resource, Statement>();
487                         for (Resource sr : sRight) {
488                                 for (Statement s : subjectRight.getValues(sr))
489                                         possibleOR.add(s.getObject(),s);
490                         }
491                         
492                         // filter possible right side objects to those that have same amount of statements as the left side object
493                         for (Resource or : possibleOR.getKeys().toArray(new Resource[possibleOR.getKeys().size()])) {
494                                 List<Statement> right = possibleOR.getValues(or);
495                                 if (right.size() != left.size())
496                                         possibleOR.remove(or);
497                                         
498                         }
499                         
500                         // check for matching statements (comparable subjects, matching predicates)
501                         MapList<Resource,Statement> matchingOR = new MapList<Resource, Statement>(); // list of objects that have matching statements
502                         Map<Resource,Pair<int[], int[]>> matchingStatements = new HashMap<Resource, Pair<int[], int[]>>(); // matching statements
503                         for (Resource or : possibleOR.getKeys()) {
504                                 List<Statement> right = possibleOR.getValues(or);
505                                 int iLeft[] = new int[left.size()];
506                                 int iRight[] = new int[right.size()];
507                                 
508                                 for (int i = 0; i < left.size(); i++) {
509                                         iLeft[i] = -1;
510                                         iRight[i] = -1;
511                                 }
512                                 
513                                 for (int l = 0; l < left.size(); l++) {
514                                         Statement ls = left.get(l);
515                                         for (int r = 0; r < right.size(); r++) {
516                                                 if (iRight[r] >= 0)
517                                                         continue;
518                                                 Statement rs = right.get(r);
519                                                 if (!comparableResources.contains(ls.getSubject(), rs.getSubject()))
520                                                         continue;
521                                                 if (rcomp.compare(ls.getPredicate(),rs.getPredicate()) == 0) {
522                                                         // compare objects (unreliable result is not accepted)
523                                                         int comp = comparator.compare(g, ls.getObject(), rs.getObject());
524                                                         if (comp > 0 && comp < Integer.MAX_VALUE) {
525                                                                 iLeft[l] = r;
526                                                                 iRight[r] = l;
527                                                                 break;
528                                                         }
529                                                         break;
530                                                 }
531                                         }
532                                         
533                                 }
534                                 boolean success = true;
535                                 for (int i = 0; i < left.size(); i++) {
536                                         if (iLeft[i] < 0) {
537                                                 success = false;
538                                                 break;
539                                         }
540                                         if (iRight[i] < 0) {
541                                                 success = false;
542                                                 break;
543                                         }
544                                                 
545                                 }
546                                 if (success) {
547                                         for (Statement s : right) 
548                                                 matchingOR.add(or,s);
549                                         matchingStatements.put(or, new Pair<int[], int[]>(iLeft, iRight));
550                                 }
551                         }
552                         // if there is only one matching right side object, we have found a match 
553                         if (matchingOR.getKeySize() == 1) {
554                                 Resource or = matchingOR.getKeys().iterator().next();
555                                 List<Statement> right = matchingOR.getValues(or);
556                                 Pair<int[], int[]> indices = matchingStatements.get(or);
557                                 
558                                 objectsLeft.add(ol);
559                                 objectsRight.add(or);
560                                 addComparable(ol, or);
561                                 for (int l = 0; l < left.size(); l++) {
562                                         int r = indices.first[l];
563                                         Statement sl = left.get(l);
564                                         Statement sr = right.get(r);
565                                         addComparable(sl, sr);
566                                         unreliableLeft.remove(sl);
567                                         unreliableRight.remove(sr);
568                                 }
569                                 
570                         } else {
571                                 
572                         }
573
574                 }
575                 
576                 
577         }
578         
579         private void processUnreliable2(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
580                 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
581                 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
582                 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
583                 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
584                 
585                 for (Statement s : unreliableLeft) {
586                         subjectLeft.add(s.getSubject(),s);
587                         objectLeft.add(s.getObject(),s);
588                 }
589                 for (Statement s : unreliableRight) {
590                         subjectRight.add(s.getSubject(),s);
591                         objectRight.add(s.getObject(),s);
592                 }
593                 
594                 for (Resource ol : objectLeft.getKeys()) {
595                         // all statements to the left side object
596                         List<Statement> left = objectLeft.getValues(ol);
597                         // all subjects that have statements to the left side object (ol)
598                         Set<Resource> sLeft = new HashSet<Resource>();
599                         // all matching subjects on the right side
600                         Set<Resource> sRight = new HashSet<Resource>();
601                         for (Statement s : left) {
602                                 sLeft.add(s.getSubject());
603                                 sRight.add(comparableResources.getRight(s.getSubject()));
604                         }
605                         
606                         if (sLeft.size() == 1 && sRight.size() == 1) {
607                                 List<Statement> ss1 = new ArrayList<Statement>(subjectLeft.getValues(sLeft.iterator().next()));
608                                 List<Statement> ss2 = new ArrayList<Statement>(subjectRight.getValues(sRight.iterator().next()));
609                                 
610                                 int count = comparableStatements.size();
611                                 compareStatements(ss1, ss2, objectsLeft, objectsRight,unreliableLeft,unreliableRight);
612                                 if (comparableStatements.size() > count) {
613                                         for (Entry<Statement, Statement> entry : comparableStatements.getEntries()) {
614                                                 unreliableLeft.remove(entry.getKey());
615                                                 unreliableRight.remove(entry.getValue());
616                                         }
617                                 }
618                         }
619                 }
620         }
621         
622         private void processUnreliableDeep(Set<Statement> unreliableLeft, Set<Statement> unreliableRight, Stack<Resource> objectsLeft, Stack<Resource> objectsRight) throws DatabaseException {
623                 MapList<Resource,Statement> subjectLeft = new MapList<Resource, Statement>();
624                 MapList<Resource,Statement> subjectRight = new MapList<Resource, Statement>();
625                 MapList<Resource,Statement> objectLeft = new MapList<Resource, Statement>();
626                 MapList<Resource,Statement> objectRight = new MapList<Resource, Statement>();
627                 
628                 for (Statement s : unreliableLeft) {
629                         subjectLeft.add(s.getSubject(),s);
630                         objectLeft.add(s.getObject(),s);
631                 }
632                 for (Statement s : unreliableRight) {
633                         subjectRight.add(s.getSubject(),s);
634                         objectRight.add(s.getObject(),s);
635                 }
636                 for (Resource ol : objectLeft.getKeys()) {
637                         Set<Path> pathsLeft = new HashSet<Path>();
638                         for (Resource rel : traversed) {
639                                 pathsLeft.addAll(Path.create(g.getStatements(ol, rel)));
640                         }
641                         while (true) {
642                                 expand(pathsLeft);
643                                 if (pathsLeft.size() == 0)
644                                         break;
645                                 Collection<Path> endPaths = new ArrayList<Path>(1);
646                                 for (Path p : pathsLeft) {
647                                         if (comparableResources.containsLeft(p.getEnd())) {
648                                                 endPaths.add(p);
649                                         }
650                                 }
651                                 if (endPaths.size() > 0) {
652                                         pathsLeft.clear();
653                                         pathsLeft.addAll(endPaths);
654                                         break;
655                                 }       
656                         }
657                         if (pathsLeft.size() > 0) {
658                                 Resource sl = objectLeft.getValues(ol).get(0).getSubject();
659                                 Resource sr = comparableResources.getRight(sl);
660                                 Collection<Resource> possibleOR = new ArrayList<Resource>();
661                                 for (Statement s : subjectRight.getValues(sr)) {
662                                         possibleOR.add(s.getObject());
663                                 }
664                                 Map<Resource,Set<Path>> matchingPaths = new HashMap<Resource, Set<Path>>();
665                                 for (Resource or : possibleOR) {
666                                         Set<Path> possiblePathsRight = new HashSet<Path>();
667                                         for (Path leftPath : pathsLeft) {
668                                                 possiblePathsRight.addAll(findComparableRight(leftPath, or));
669                                         }
670                                         if (hasMatchingPaths(pathsLeft, possiblePathsRight)) {
671                                                 matchingPaths.put(or, possiblePathsRight);
672                                         }
673                                 }
674                                 if (matchingPaths.size() > 0) {
675                                         if (matchingPaths.size() == 1) {
676                                                 Resource or = matchingPaths.keySet().iterator().next();
677                                                 
678                                                 objectsLeft.add(ol);
679                                                 objectsRight.add(or);
680                                                 addComparable(ol, or);
681                                                 Collection<Statement> statementsLeft = objectLeft.getValues(ol);
682                                                 Collection<Statement> statementsRight = objectRight.getValues(or);
683                                                 unreliableLeft.removeAll(statementsLeft);
684                                                 unreliableRight.removeAll(statementsRight);
685                                                 BijectionMap<Path,Path> map = getMatchingPaths(pathsLeft, matchingPaths.get(or));
686                                                 for (Path left : map.getLeftSet()) {
687                                                         Path right = map.getRight(left);
688                                                         for (int i = 0; i < left.getLength(); i++) {
689                                                                 addComparable(left.getStatements().get(i),right.getStatements().get(i));
690                                                         }
691                                                 }
692                                         } 
693                                 }
694                         }
695                         
696                 }
697                 
698         }
699         
700         private boolean hasMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {
701                 if (leftPaths.size() != rightPaths.size())
702                         return false;
703                 BijectionMap<Path,Path> map = getMatchingPaths(leftPaths, rightPaths);
704                 return map.size() == leftPaths.size();
705         }
706         
707         private BijectionMap<Path,Path> getMatchingPaths(Set<Path> leftPaths, Set<Path> rightPaths) {
708                 BijectionMap<Path,Path> map = new BijectionMap<Path, Path>();
709                 for (Path leftPath : leftPaths) {
710                         for (Path rightPath : rightPaths) {
711                                 if (map.containsRight(rightPath))
712                                         continue;
713                                 if (leftPath.getLength() != rightPath.getLength())
714                                         continue;
715                                 if (comparableResources.contains(leftPath.getEnd(), rightPath.getEnd())) {
716                                         map.map(leftPath, rightPath);
717                                         break;
718                                 }
719                         }
720                 }
721                 return map;
722         }
723         
724         private void expand(Set<Path> paths) throws DatabaseException {
725                 Set<Path> stepPathsLeft = new HashSet<Path>();
726                 if (paths.size() == 0)
727                         return;
728                 int length = paths.iterator().next().getLength() + 1;
729                 for (Path p : paths) {
730                         for (Resource rel : traversed) {
731                                 stepPathsLeft.addAll(Path.expand(p,g.getStatements(p.getEnd(), rel)));
732                         }
733                 }
734                 paths.clear();
735                 for (Path p : stepPathsLeft) {
736                         if (p.getLength() == length)
737                                 paths.add(p);
738                 }
739         }
740         
741         private Collection<Path> findComparableRight(Path leftPath, Resource beginRight) throws DatabaseException {
742                 Set<Path> rightPaths = new HashSet<Path>();
743                 rightPaths.addAll(Path.create(g.getStatements(beginRight, getRight(leftPath.getStatements().get(0).getPredicate()))));
744                 for (int i = 1; i < leftPath.getLength(); i++) {
745                         if (rightPaths.size() == 0)
746                                 return rightPaths;
747                         Set<Path> stepPaths = new HashSet<Path>();
748                         for (Path p : rightPaths) {
749                                 stepPaths.addAll(Path.expand(p, g.getStatements(p.getEnd(), getRight(leftPath.getStatements().get(i).getPredicate()))));
750                         }
751                         rightPaths.clear();
752                         for (Path p : stepPaths)
753                                 if (p.getLength() == i+1) 
754                                         rightPaths.add(p);
755                 }
756                 return rightPaths;
757                 
758         }
759         
760         private Resource getRight(Resource r) {
761                 if (comparableResources.containsLeft(r))
762                         return comparableResources.getRight(r);
763                 return r;
764         }
765         
766
767         
768         public BijectionMap<Statement, Statement> getComparableStatements() {
769                 return comparableStatements;
770         }
771         
772         public BijectionMap<Resource, Resource> getComparableResources() {
773                 return comparableResources;
774         }
775         
776         public GraphChanges getChanges() {
777                 return new GraphChanges(r1,r2,changes1,changes2,modifications,comparableResources);
778         }
779         
780         private void addComparable(Statement left, Statement right) throws DatabaseException {
781                 addComparable(left.getObject(), right.getObject());
782                 comparableStatements.map(left, right);
783                 //comparableResources.map(left.getObject(), right.getObject());
784         }
785         
786         private void addComparable(Resource left, Resource right) throws DatabaseException {
787                 if(!comparableResources.contains(left, right)) {
788                         if (comparableResources.containsLeft(left)||comparableResources.containsRight(right)) {
789                                 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.");
790                         } else {
791                                 if (DEBUG) System.out.println(left + " = " + right);
792                                 if (left.getResourceId() == 610381L)
793                                         System.out.println();
794                                 comparableResources.map(left, right);
795                         }
796                 }
797                 
798         }
799         
800         public List<Statement> filterAsserted(Resource r, Collection<Statement> in) throws DatabaseException {
801                 List<Statement> out = new ArrayList<Statement>();
802                 for (Statement s : in) {
803                         if (!s.isAsserted(r))
804                                 out.add(s);
805                         
806                 }
807                 return out;
808         }
809         
810         
811
812         private String printStatement(ReadGraph graph, Statement s) throws DatabaseException {
813                 return NameUtils.getSafeName(graph, s.getSubject()) + " " + NameUtils.getSafeName(graph, s.getPredicate()) + " " + NameUtils.getSafeName(graph, s.getObject());
814         }
815         
816         private List<Statement> filterTraversed(List<Statement> in) throws DatabaseException {
817                 return filter(traversed, in);
818         }
819         
820         private List<Statement> filterNonTested(List<Statement> in) throws DatabaseException {
821                 return filter(nonTested, in);
822         }
823         
824         private List<Statement> filterNonTraversed(List<Statement> in) throws DatabaseException {
825                 return filter(nonTraversed, in);
826         }
827         
828         private List<Statement> filter(Collection<Resource> toFilter, List<Statement> in) throws DatabaseException {
829                 if (toFilter.size() == 0)
830                         return in;
831                 List<Statement> out = new ArrayList<Statement>();
832                 for (Statement s : in) {
833                         boolean usable = true;
834                         for (Resource r : toFilter) {
835                                 if (g.isSubrelationOf(s.getPredicate(),r)) {
836                                         usable = false;
837                                         break;
838                                 }
839                         }
840                         if (usable) {
841                                 out.add(s);
842                         }
843                         
844                 }
845                 return out;
846         }
847         
848         
849         private void addDeletion(Statement s) {
850                 if (!changes1Set.contains(s)) {
851                         changes1Set.add(s);
852                         changes1.add(s);
853                         if (s.getObject().getResourceId() == 532631L)
854                                 System.out.println();
855                 }
856         }
857         
858         private void addAddition(Statement s) {
859                 if (!changes2Set.contains(s)) {
860                         changes2Set.add(s);
861                         changes2.add(s);
862                 }
863         }
864         
865         private void addModification(Resource sub1, Statement s1, Resource sub2, Statement s2) {
866                 Modification mod = new Modification(sub1, sub2, s1, s2);
867                 if (!modificationsSet.contains(mod)) {
868                         modificationsSet.add(mod);
869                         modifications.add(mod);
870                 }
871         }
872         
873         public void sortStatement(List<Statement> list1, List<Statement> list2) {
874                 sortStatement(list1, list2, scomp);
875         }
876         
877         public void sortStatement(List<Statement> list1, List<Statement> list2, Comparator<Statement> scomp) {
878                 Collections.sort(list1,scomp);
879                 Collections.sort(list2,scomp);
880                 
881                 List<Statement> sorted1 = new ArrayList<Statement>(list1.size());
882                 List<Statement> sorted2 = new ArrayList<Statement>(list2.size());
883                 sorted1.addAll(list1);
884                 sorted2.addAll(list2);
885                 
886                 int ss1 = 0;
887                 int ss2 = 0;
888                 for (int i = 0; i < list1.size(); ) {
889                         Statement s1 = list1.get(i);
890                         int same1 = sameRel(list1, i);  
891                         for (int j = 0; j < list2.size(); j++) {
892                                 Statement s2 = list2.get(j);
893                                 if (scomp.compare(s1, s2) == 0) {
894                                         int same2 = sameRel(list2, j);
895                                         copy(sorted1,ss1,list1,i,same1);
896                                         ss1 += same1;
897                                         copy(sorted2,ss2,list2,j,same2);
898                                         ss2 += same2;
899                                         break;
900                                 }
901                         }
902                         i+= same1;
903                 }
904                 if (ss1 < sorted1.size()) {
905                         for (Statement s : list1) {
906                                 if (!sorted1.contains(s)) {
907                                         sorted1.set(ss1,s);
908                                         ss1++;
909                                 }
910                         }
911                 }
912                 if (ss2 < sorted2.size()) {
913                         for (Statement s : list2) {
914                                 if (!sorted2.contains(s)) {
915                                         sorted2.set(ss2,s);
916                                         ss2++;
917                                 }
918                         }
919                 }
920                 
921                 list1.clear();
922                 list2.clear();
923                 list1.addAll(sorted1);
924                 list2.addAll(sorted2);
925         }
926         
927         public <T> void copy(List<T> to, int toIndex, List<T> from, int fromIndex, int amount) {
928                 for (int i = 0; i <  amount; i++) {
929                         to.set(toIndex + i, from.get(fromIndex+ i));
930                 }
931         }
932         
933         public void sortResource(List<Resource> list1, List<Resource> list2) {
934                 Collections.sort(list1,rcomp);
935                 int js = 0;
936                 for (int i = 0; i < list1.size(); i++) {
937                         Resource s1 = list1.get(i);
938                         for (int j = js; j < list2.size(); j++) {
939                                 Resource s2 = list2.get(j);
940                                 if (rcomp.compare(s1, s2) == 0) {
941                                         Resource t = list2.get(js);
942                                         list2.set(js, s2);
943                                         list2.set(j, t);
944                                         break;
945                                 }
946                         }
947                         js++;
948                 }
949         }
950         
951         private void compareStatements(List<Statement> ss1, List<Statement> ss2, Stack<Resource> objectsLeft, Stack<Resource> objectsRight, Collection<Statement> unreliableLeft, Collection<Statement> unreliableRight) throws DatabaseException {
952                 sortStatement(ss1, ss2);
953                 
954                 int i1 = 0;
955                 int i2 = 0;
956                 
957                 while (true) {
958                         if (i1 >= ss1.size()) {
959                                 if (i2 >= ss2.size()) {
960                                         break;
961                                 } else {
962                                         while (i2 < ss2.size()) {
963                                                 if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i2)));
964                                                 
965                                                 addAddition(ss2.get(i2));
966                                                 i2++;
967                                         }
968                                         break;
969                                 }
970                         } else if (i2 >= ss2.size()) {
971                                 while (i1 < ss1.size()) {
972                                         if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i1)));
973                                         addDeletion(ss1.get(i1));
974                                         i1++;
975                                 }
976                                 break;
977                         }
978                         int same1 = sameRel(ss1, i1);
979                         int same2 = sameRel(ss2, i2);
980                         int c = rcomp.compare(ss1.get(i1).getPredicate(),ss2.get(i2).getPredicate());
981                         if (c == 0) {
982                                 compareStatements(ss1, i1, same1, ss2, i2, same2,objectsLeft,objectsRight,unreliableLeft,unreliableRight);
983                                 i1+=same1;
984                                 i2+=same2;
985                         } else if (c < 0) {
986                                 for (int i = 0; i < same1; i++) {
987                                         if (DEBUG) System.out.println("Compare Statements deletion " + printStatement(g,ss1.get(i+i1)));
988                                         addDeletion(ss1.get(i+i1));
989                                 }
990                                 i1 += same1;
991                         } else {
992                                 for (int i = 0; i < same2; i++) {
993                                         if (DEBUG) System.out.println("Compare Statements addition " + printStatement(g,ss2.get(i+i2)));
994                                         addAddition(ss2.get(i+i2));
995                                 }
996                                 
997                                 i2 += same2;
998                         }
999                 }
1000         }
1001         
1002
1003         
1004         private int sameRel(List<Statement> statements, int off) {
1005                 if (statements.size() <= off)
1006                         return 0;
1007                 int same = 1;
1008                 long id = statements.get(off).getPredicate().getResourceId();
1009                 for (int i = off+1; i <statements.size(); i++) {
1010                         if (statements.get(i).getPredicate().getResourceId() == id)
1011                                 same++;
1012                         else 
1013                                 break;
1014                 }
1015                 return same;
1016                 
1017         }
1018
1019         private int compareObject(Resource o1, Resource o2) throws DatabaseException {
1020                 if (o1.equals(o2))
1021                         return -1;
1022                 if (comparableResources.contains(o1, o2))
1023                         return (-1);
1024                 if (comparableResources.containsLeft(o1))
1025                         return Integer.MAX_VALUE;
1026                 if (comparableResources.containsRight(o2))
1027                         return Integer.MAX_VALUE;
1028                 if (nonMatchedLeft.contains(o1))
1029                         return Integer.MAX_VALUE;
1030                 if (nonMatchedRight.contains(o2))
1031                         return Integer.MAX_VALUE;
1032                 return comparator.compare(g, o1, o2);
1033         }
1034         
1035         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 {
1036                 boolean[] used1 = new boolean[len1];
1037                 for (int i = 0; i < used1.length; i++) {
1038                         used1[i] = false;
1039                 }
1040                 
1041                 boolean[] used2 = new boolean[len2];
1042                 for (int i = 0; i < used2.length; i++) {
1043                         used2[i] = false;
1044                 }
1045                 
1046                 // left, right, difference
1047                 List<List<Integer>> differences = new ArrayList<List<Integer>>();
1048                 for (int i1 = off1; i1 < off1 + len1; i1++) {
1049                         Statement s1 = ss1.get(i1);
1050                         List<Integer> diff = new ArrayList<Integer>();
1051                         for (int i2 = off2; i2 < off2 + len2; i2++) {
1052                                 Statement s2 = ss2.get(i2);
1053                                 int d = compareObject(s1.getObject(), s2.getObject());
1054                                 if (d == 0) {
1055                                         for (Resource t : strong) {
1056                                                  if (s1.getPredicate().equals(t) || g.isSubrelationOf(s1.getPredicate(), t)) {
1057                                                          d = 1;
1058                                                          break;
1059                                                  }
1060                                         }
1061                                 }
1062                                 diff.add(d);
1063                         }
1064                         differences.add(diff);
1065                 }
1066                 // difference, left
1067                 MapList<Integer, Integer> priorities = new MapList<Integer, Integer>();
1068                 for (int i = 0; i < differences.size(); i++) {
1069                         List<Integer> list = differences.get(i);
1070                         for (int j = 0; j < list.size(); j++) {
1071                                 priorities.add(list.get(j), i);
1072                         }
1073                 }
1074                 
1075                 Integer[] pris = priorities.getKeys(new Integer[]{});
1076                 Arrays.sort(pris);
1077                 
1078                 for (Integer pri : pris) {
1079                         if (pri == Integer.MAX_VALUE) {
1080
1081                         } else if (pri == 0) {
1082                                 
1083                         } else {
1084                                 List<Integer> i1s = priorities.getValues(pri);
1085                                 for (Integer i1 : i1s) {
1086                                         if (used1[i1])
1087                                                 continue;
1088                                         List<Integer> i2diff = differences.get(i1);
1089                                         for (int i2 = 0; i2 < i2diff.size(); i2++) {
1090                                                 if (i2diff.get(i2) == pri) {
1091                                                         if (used2[i2])
1092                                                                 continue;
1093                                                         used1[i1] = true;
1094                                                         used2[i2] = true;
1095                                                         Statement s1  = ss1.get(i1+off1);
1096                                                         Statement s2  = ss2.get(i2+off2);
1097                                                         
1098                                                         if (objectsLeft != null) {
1099                                                                 objectsLeft.add(s1.getObject());
1100                                                                 objectsRight.add(s2.getObject());
1101                                                         } 
1102                                                         addComparable(s1, s2);
1103                                                         break;
1104                                                 }
1105                                         }
1106                                 }
1107                         }
1108                 }
1109                 
1110                 for (Integer pri : pris) {
1111                         if (pri != 0)
1112                                 continue;
1113                         Set<Statement> s1s = new HashSet<Statement>();
1114                         Set<Statement> s2s = new HashSet<Statement>();
1115                         Set<Integer> s1i = new HashSet<Integer>();
1116                         Set<Integer> s2i = new HashSet<Integer>();
1117                         List<Integer> i1s = priorities.getValues(pri);
1118                         for (Integer i1 : i1s) {
1119                                 if (used1[i1])
1120                                         continue;
1121                                 List<Integer> i2diff = differences.get(i1);
1122                                 for (int i2 = 0; i2 < i2diff.size(); i2++) {
1123                                         if (i2diff.get(i2) == pri) {
1124                                                 if (used2[i2])
1125                                                         continue;
1126                                                 Statement s1  = ss1.get(i1+off1);
1127                                                 Statement s2  = ss2.get(i2+off2);
1128                                                 s1s.add(s1);
1129                                                 s2s.add(s2);
1130                                                 s1i.add(i1);
1131                                                 s2i.add(i2);
1132                                         }
1133                                 }
1134                         }
1135                         if (unreliableLeft != null) {
1136                                 unreliableLeft.addAll(s1s);
1137                                 unreliableRight.addAll(s2s);
1138                         }
1139                         for (Integer i : s1i)
1140                                 used1[i] = true;
1141                         for (Integer i : s2i)
1142                                 used2[i] = true;
1143
1144                 }
1145                 for (int i1 = off1; i1 < off1 + len1; i1++) {
1146                         if (!used1[i1-off1]) {
1147                                 if (DEBUG) System.out.println("Compare Object deletion " + printStatement(g,ss1.get(i1)));
1148                                 addDeletion(ss1.get(i1));
1149                         }
1150                 }
1151                 for (int i2 = off2; i2 < off2 + len2; i2++) {
1152                         if (!used2[i2-off2]) {
1153                                 if (DEBUG) System.out.println("Compare Object addition " + printStatement(g,ss2.get(i2)));
1154                                 addAddition(ss2.get(i2));
1155                         }
1156                 }
1157         }
1158         
1159         
1160         
1161         /**
1162          * compares properties, assumes functional relations
1163          * @param r1
1164          * @param r2
1165          * @throws ServiceException
1166          * @throws DoesNotContainValueException
1167          * @throws ValidationException 
1168          */
1169         private void compareProps(Resource r1, Resource r2) throws DatabaseException {
1170                 if (DEBUG) System.out.println("compareProps " + r1 + " " + NameUtils.getSafeName(g, r1) + " " + r2 + " " + NameUtils.getSafeName(g, r2));
1171                 List<Statement> ss1 = new ArrayList<Statement>();
1172                 List<Statement> ss2 = new ArrayList<Statement>();
1173                 ss1.addAll(g.getStatements(r1, b.HasProperty));
1174                 ss2.addAll(g.getStatements(r2, b.HasProperty));
1175                 ss1 = filterNonTested(ss1);
1176                 ss2 = filterNonTested(ss2);
1177                 sortStatement(ss1, ss2);
1178                 
1179                 int i1 = 0; 
1180                 int i2 = 0;
1181                 
1182                 while (true) {
1183                         if (i1 >= ss1.size()) {
1184                                 if (i2 >= ss2.size())
1185                                         break;
1186                                 else {
1187                                         while (i2 < ss2.size()) {
1188                                                 if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,ss2.get(i2)));
1189                                                 addAddition(ss2.get(i2));
1190                                                 i2++;
1191                                         }
1192                                         break;
1193                                 }
1194                         } else if (i2 >= ss2.size()) {
1195                                 while (i1 < ss1.size()) {
1196                                         if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,ss1.get(i1)));
1197                                         addDeletion(ss1.get(i1));
1198                                         i1++;
1199                                 }
1200                                 break;
1201                         }
1202                         Statement s1 = ss1.get(i1);
1203                         Statement s2 = ss2.get(i2);
1204                         if (s1.isAsserted(r1) && s2.isAsserted(r2)) {
1205                                 i1++;
1206                                 i2++;
1207                                 continue;
1208                         }
1209                         int c = scomp.compare(s1, s2);
1210                         switch (c) {
1211                                 case 0:{
1212                                         boolean b1 = g.hasValue(s1.getObject());
1213                                         boolean b2 = g.hasValue(s2.getObject());
1214                                         if (b1 == b2) {
1215                                                 if (b1) {
1216 //                                                      Object v1 = g.getValue(s1.getObject());
1217 //                                                      Object v2 = g.getValue(s2.getObject());
1218 //                                                      boolean eq = compareValue(v1, v2);
1219                                                         boolean eq = compareValue(g,b,s1.getObject(), s2.getObject());
1220                                                         if (!eq) {
1221                                                                 addModification(r1,s1,r2,s2);
1222                                                                 if (!s1.isAsserted(r1) && !s2.isAsserted(r2))
1223                                                                         addComparable(s1, s2);
1224                                                         }
1225                                                 } else {
1226                                                         if (!s1.getObject().equals(s1.getSubject()) && !s2.getObject().equals(s2.getSubject()))
1227                                                                 compareProps(s1.getObject(), s2.getObject());
1228                                                 }
1229                                         } else {
1230                                                 addModification(r1,s1,r2,s2);
1231                                                 if (!s1.isAsserted(r1) && !s2.isAsserted(r2))
1232                                                         addComparable(s1, s2);
1233                                         }
1234                                         i1++;
1235                                         i2++;
1236                                         break;
1237                                 }
1238                                 case -1:{
1239                                         if (DEBUG) System.out.println("Compare Prop diff1s " + printStatement(g,s1));
1240                                         addDeletion(s1);
1241                                         i1++;
1242                                         break;
1243                                 }
1244                                         
1245                                 case 1:{
1246                                         if (DEBUG) System.out.println("Compare Prop diff2s " + printStatement(g,s2));
1247                                         addAddition(s2);
1248                                         i2++;
1249                                         break;
1250                                 }
1251                         }
1252
1253                 }
1254                 
1255                 ss1.clear();
1256                 ss2.clear();
1257                 
1258         }
1259         
1260         public static boolean compareValue(ReadGraph g, Layer0 b, Resource r1, Resource r2) throws DatabaseException {
1261                 Resource t1 = g.getSingleType(r1);
1262                 Resource t2 = g.getSingleType(r2);
1263                 if (!t1.equals(t2))
1264                         return false;
1265                 if (t1.equals(b.Integer)) {
1266                         int v1 = g.getValue(r1, Bindings.INTEGER);
1267                         int v2 = g.getValue(r2, Bindings.INTEGER);
1268                         return v1 == v2;
1269                 } else if (t1.equals(b.Float)) {
1270                         float v1 = g.getValue(r1, Bindings.FLOAT);
1271                         float v2 = g.getValue(r2, Bindings.FLOAT);
1272                         return v1 == v2;
1273                 } else if (t1.equals(b.Double)) {
1274                         double v1 = g.getValue(r1, Bindings.DOUBLE);
1275                         double v2 = g.getValue(r2, Bindings.DOUBLE);
1276                         return v1 == v2;
1277                 } else if (t1.equals(b.String)) {
1278                         String v1 = g.getValue(r1, Bindings.STRING);
1279                         String v2 = g.getValue(r2, Bindings.STRING);
1280                         return v1.equals(v2);
1281                 } else if (t1.equals(b.Boolean)) {
1282                         boolean v1 = g.getValue(r1, Bindings.BOOLEAN);
1283                         boolean v2 = g.getValue(r2, Bindings.BOOLEAN);
1284                         return v1 == v2;
1285                 } else if (t1.equals(b.Byte)) {
1286                         byte v1 = g.getValue(r1, Bindings.BYTE);
1287                         byte v2 = g.getValue(r2, Bindings.BYTE);
1288                         return v1 == v2;
1289                 } else if (t1.equals(b.Long)) {
1290                         long v1 = g.getValue(r1, Bindings.LONG);
1291                         long v2 = g.getValue(r2, Bindings.LONG);
1292                         return v1 == v2;
1293                 } else if (t1.equals(b.IntegerArray)) {
1294                         int[] v1 = g.getValue(r1, Bindings.INT_ARRAY);
1295                         int[] v2 = g.getValue(r2, Bindings.INT_ARRAY);
1296                         return Arrays.equals(v1,v2);
1297                 } else if (t1.equals(b.FloatArray)) {
1298                         float[] v1 = g.getValue(r1, Bindings.FLOAT_ARRAY);
1299                         float[] v2 = g.getValue(r2, Bindings.FLOAT_ARRAY);
1300                         return Arrays.equals(v1,v2);
1301                 } else if (t1.equals(b.DoubleArray)) {
1302                         double[] v1 = g.getValue(r1, Bindings.DOUBLE_ARRAY);
1303                         double[] v2 = g.getValue(r2, Bindings.DOUBLE_ARRAY);
1304                         return Arrays.equals(v1,v2);
1305                 } else if (t1.equals(b.StringArray)) {
1306                         String[] v1 = g.getValue(r1, Bindings.STRING_ARRAY);
1307                         String[] v2 = g.getValue(r2, Bindings.STRING_ARRAY);
1308                         return Arrays.equals(v1,v2);
1309                 } else if (t1.equals(b.BooleanArray)) {
1310                         boolean[] v1 = g.getValue(r1, Bindings.BOOLEAN_ARRAY);
1311                         boolean[] v2 = g.getValue(r2, Bindings.BOOLEAN_ARRAY);
1312                         return Arrays.equals(v1,v2);
1313                 } else if (t1.equals(b.ByteArray)) {
1314                         byte[] v1 = g.getValue(r1, Bindings.BYTE_ARRAY);
1315                         byte[] v2 = g.getValue(r2, Bindings.BYTE_ARRAY);
1316                         return Arrays.equals(v1,v2);
1317                 } else if (t1.equals(b.LongArray)) {
1318                         long[] v1 = g.getValue(r1, Bindings.LONG_ARRAY);
1319                         long[] v2 = g.getValue(r2, Bindings.LONG_ARRAY);
1320                         return Arrays.equals(v1,v2);
1321                 } else {
1322                         Object v1 = g.getValue(r1);
1323                         Object v2 = g.getValue(r2);
1324                         return compareValue(v1, v2);
1325                 }
1326                 
1327         }
1328         
1329         public static boolean compareValue(Object v1, Object v2) {
1330                 if (v1 instanceof Object[] && v2 instanceof Object[])
1331                         return Arrays.deepEquals((Object[])v1, (Object[])v2);
1332                 else if (v1 instanceof int[] && v2 instanceof int[]) 
1333                         return Arrays.equals((int[])v1, (int[])v2);
1334                 else if (v1 instanceof float[] && v2 instanceof float[]) 
1335                         return Arrays.equals((float[])v1, (float[])v2);
1336                 else if (v1 instanceof double[] && v2 instanceof double[]) 
1337                         return Arrays.equals((double[])v1, (double[])v2);
1338                 else if (v1 instanceof long[] && v2 instanceof long[]) 
1339                         return  Arrays.equals((long[])v1, (long[])v2);
1340                 else if (v1 instanceof byte[] && v2 instanceof byte[]) 
1341                         return Arrays.equals((byte[])v1, (byte[])v2);
1342                 else if (v1 instanceof boolean[] && v2 instanceof boolean[]) 
1343                         return Arrays.equals((boolean[])v1, (boolean[])v2);
1344                 else
1345                         return v1.equals(v2);
1346         }
1347
1348         
1349         public class PredicateComparator implements Comparator<Statement> {
1350                 @Override
1351                 public int compare(Statement o1, Statement o2) {
1352                         if (comparableResources.contains(o1.getPredicate(), o2.getPredicate()))
1353                                 return 0;
1354                         if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1355                                 return -1;
1356                         if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1357                                 return 1;
1358                         return 0;
1359                 }
1360         }
1361         
1362         public class SubjectComparator implements Comparator<Statement> {
1363                 @Override
1364                 public int compare(Statement o1, Statement o2) {
1365                         if (comparableResources.contains(o1.getSubject(), o2.getSubject()))
1366                                 return 0;
1367                         if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1368                                 return -1;
1369                         if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1370                                 return 1;
1371                         return 0;
1372                 }
1373         }
1374         
1375         public class ObjectComparator implements Comparator<Statement> {
1376                 @Override
1377                 public int compare(Statement o1, Statement o2) {
1378                         if (comparableResources.contains(o1.getObject(), o2.getObject()))
1379                                 return 0;
1380                         if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1381                                 return -1;
1382                         if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1383                                 return 1;
1384                         return 0;
1385                 }
1386         }
1387         
1388         public static class FullStatementComparator implements Comparator<Statement> {
1389                 @Override
1390                 public int compare(Statement o1, Statement o2) {
1391                         if (o1.getSubject().getResourceId() < o2.getSubject().getResourceId())
1392                                 return -1;
1393                         if (o1.getSubject().getResourceId() > o2.getSubject().getResourceId())
1394                                 return 1;
1395                         if (o1.getPredicate().getResourceId() < o2.getPredicate().getResourceId())
1396                                 return -1;
1397                         if (o1.getPredicate().getResourceId() > o2.getPredicate().getResourceId())
1398                                 return 1;
1399                         if (o1.getObject().getResourceId() < o2.getObject().getResourceId())
1400                                 return -1;
1401                         if (o1.getObject().getResourceId() > o2.getObject().getResourceId())
1402                                 return 1;
1403                         return 0;
1404                 }
1405         }
1406         
1407         public class ResComparator implements Comparator<Resource> {
1408                 @Override
1409                 public int compare(Resource o1, Resource o2) {
1410                         if (comparableResources.contains(o1, o2))
1411                                 return 0;
1412                         if (o1.getResourceId() < o2.getResourceId())
1413                                 return -1;
1414                         if (o1.getResourceId() > o2.getResourceId())
1415                                 return 1;
1416                         return 0;
1417                 }
1418         }
1419
1420 }