]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java
Prevent TypeComparator from matching different type resources.
[simantics/interop.git] / org.simantics.interop / src / org / simantics / interop / test / TypeComparator.java
index 61e8a0fbc193fa50cbc08e9a44f66207b241230d..a2a37226cbaf9dd538d7c4fef5873b25dfcdc5f2 100644 (file)
@@ -7,10 +7,6 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Statement;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.exception.DoesNotContainValueException;
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
-import org.simantics.db.exception.ServiceException;
-import org.simantics.db.exception.ValidationException;
 import org.simantics.layer0.Layer0;
 
 /**
@@ -30,20 +26,30 @@ public class TypeComparator extends ResourceComparator {
        
        @Override
        public int compare(ReadGraph g, Resource o1, Resource o2, boolean local) throws DatabaseException {
-               if (local)
-                       return new TypeComparator().compare(g, o1, o2);
-               else
-                       return compare(g, o1, o2);
+               if (o1.equals(o2)) {
+                       return EXACT_MATCH;
+               }
+               return compare(g, o1, o2);
        }
        
        @Override
        public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException{
                if (!compareType(g,o1, o2)) {
-                       return (Integer.MAX_VALUE);
+                       return NO_MATCH;
                }
                return propsDiffCount(g,o1, o2);
        }
        
+       /**
+        * Compares resources based on L0.InstaceOf relations.
+        * If there are no L0.InstanceOf relations, uses ResourceComparator for comparison.
+        *  
+        * @param g
+        * @param r1
+        * @param r2
+        * @return
+        * @throws DatabaseException
+        */
        protected boolean compareType(ReadGraph g,Resource r1, Resource r2) throws DatabaseException {
                Layer0 l0 = Layer0.getInstance(g);
                rs1.addAll(g.getObjects(r1, l0.InstanceOf));
@@ -54,6 +60,10 @@ public class TypeComparator extends ResourceComparator {
                        return false;
                }
                Comparator<Resource> rcomp = getComparator().getResourceComparator();
+               if (rs1.size() == 0) {
+                       return rcomp.compare(r1, r2) == 0;
+               }
+       
                getComparator().sortResource(rs1, rs2);
 
                for (int i = 0; i < rs1.size(); i++) {
@@ -64,6 +74,16 @@ public class TypeComparator extends ResourceComparator {
                                return false;
                        }
                }
+               if (rs1.size() == 1) {
+                       // Check for enumeration type. Without this enumeration instances could be mixed together.
+                       if (g.hasStatement(rs1.get(0), l0.Enumeration, rs1.get(0))) {
+                               if (!r1.equals(r2)) {
+                                       rs1.clear();
+                                       rs2.clear();
+                                       return false;
+                               }
+                       }
+               }
                
                rs1.clear();
                rs2.clear();
@@ -71,6 +91,7 @@ public class TypeComparator extends ResourceComparator {
                return true;
        }
        
+       
        protected int propsDiffCount(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
                Layer0 l0 = Layer0.getInstance(g);
                ArrayList<Statement> ss1 = new ArrayList<Statement>();
@@ -122,15 +143,10 @@ public class TypeComparator extends ResourceComparator {
                                        boolean b2 = g.hasValue(s2.getObject());
                                        if (b1 == b2) {
                                                if (b1) {
-//                                                     Object v1 = g.getValue(s1.getObject());
-//                                                     Object v2 = g.getValue(s2.getObject());
-//                                                     boolean eq = GraphComparator.compareValue(v1, v2);
                                                        boolean eq = GraphComparator.compareValue(g, l0 , s1.getObject(), s2.getObject());
-                                                       
                                                        if (!eq) {
                                                                count++;
                                                        }
-                                                       //System.out.println("Prop count values " + v1 + " " + v2);
                                                } else {
                                                        count += propsDiffCount(g,s1.getObject(), s2.getObject());
                                                }