]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java
Added relations checks for graph path comparison
[simantics/interop.git] / org.simantics.interop / src / org / simantics / interop / test / GraphComparator.java
index 052a9a9ee19ad7bce23cba188e2bc923fbd76a2d..6f8c4d1b15e13d10a652dac5551abbb7d0a329e1 100644 (file)
@@ -52,7 +52,7 @@ import org.simantics.utils.datastructures.Pair;
  */
 public class GraphComparator {
        
-       private static final boolean DEBUG = true;
+       private static final boolean DEBUG = false;
 
        private Resource r1;
        private Resource r2;
@@ -308,8 +308,6 @@ public class GraphComparator {
                        
                        if (r1.equals(r2))
                                continue;
-                       if (r1.getResourceId() == 610446L)
-                               System.out.println();
                        if (processedResources.contains(r1))
                                continue;
                        processedResources.add(r1);
@@ -713,8 +711,19 @@ public class GraphComparator {
                                if (leftPath.getLength() != rightPath.getLength())
                                        continue;
                                if (comparableResources.contains(leftPath.getEnd(), rightPath.getEnd())) {
-                                       map.map(leftPath, rightPath);
-                                       break;
+                                       boolean preds = true;
+                                       for (int i = 0; i < leftPath.getLength(); i++) {
+                                               Statement sl = leftPath.getStatements().get(i);
+                                               Statement sr = rightPath.getStatements().get(i);
+                                               if (!sl.getPredicate().equals(sr.getPredicate()) && !comparableResources.contains(sl.getPredicate(), sr.getPredicate())) {
+                                                       preds = false;
+                                                       break;
+                                               }
+                                       }
+                                       if (preds) {
+                                               map.map(leftPath, rightPath);
+                                               break;
+                                       }
                                }
                        }
                }
@@ -789,8 +798,6 @@ public class GraphComparator {
                                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.");
                        } else {
                                if (DEBUG) System.out.println(left + " = " + right);
-                               if (left.getResourceId() == 610381L)
-                                       System.out.println();
                                comparableResources.map(left, right);
                        }
                }
@@ -807,6 +814,31 @@ public class GraphComparator {
                return out;
        }
        
+       public List<Statement> filterAssertedDuplicates(Resource r, List<Statement> in) throws DatabaseException {
+               List<Statement> out = new ArrayList<Statement>();
+               for (int i = 0; i < in.size(); i++) {
+                       Statement s = in.get(i);
+                       if (!s.isAsserted(r))
+                               out.add(s);
+                       else {
+                               boolean has = false;
+                               if (i > 1 && in.get(i-1).getPredicate().equals(s.getPredicate()))
+                                       has = true;
+                               else if (i < in.size()-1 && in.get(i+1).getPredicate().equals(s.getPredicate()))
+                                       has = true;
+                               if (!has)
+                                       out.add(s);
+                       }
+                       
+               }
+               for (Statement s : in) {
+                       if (!s.isAsserted(r))
+                               out.add(s);
+                       
+               }
+               return out;
+       }
+       
        
 
        private String printStatement(ReadGraph graph, Statement s) throws DatabaseException {
@@ -850,8 +882,6 @@ public class GraphComparator {
                if (!changes1Set.contains(s)) {
                        changes1Set.add(s);
                        changes1.add(s);
-                       if (s.getObject().getResourceId() == 532631L)
-                               System.out.println();
                }
        }
        
@@ -1175,6 +1205,9 @@ public class GraphComparator {
                ss1 = filterNonTested(ss1);
                ss2 = filterNonTested(ss2);
                sortStatement(ss1, ss2);
+               // getStatements(r1, b.HasProperty) returns both instance and asserted statements for the same property relation. 
+               ss1 = filterAssertedDuplicates(r1, ss1);
+               ss2 = filterAssertedDuplicates(r2, ss2);
                
                int i1 = 0; 
                int i2 = 0;
@@ -1185,16 +1218,20 @@ public class GraphComparator {
                                        break;
                                else {
                                        while (i2 < ss2.size()) {
-                                               if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,ss2.get(i2)));
-                                               addAddition(ss2.get(i2));
+                                               Statement s = ss2.get(i2);
+                                               if (DEBUG) System.out.println("Compare Prop diff2 " + printStatement(g,s));
+                                               if (!s.isAsserted(r2))
+                                                       addAddition(s);
                                                i2++;
                                        }
                                        break;
                                }
                        } else if (i2 >= ss2.size()) {
                                while (i1 < ss1.size()) {
-                                       if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,ss1.get(i1)));
-                                       addDeletion(ss1.get(i1));
+                                       Statement s = ss1.get(i1);
+                                       if (DEBUG) System.out.println("Compare Prop diff1 " + printStatement(g,s));
+                                       if (!s.isAsserted(r1))
+                                               addDeletion(s);
                                        i1++;
                                }
                                break;