]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Using bindings when comparing literal values (perf optimization)
authorMarko Luukkainen <marko.luukkainen@vtt.fi>
Wed, 28 Feb 2018 11:14:29 +0000 (13:14 +0200)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Wed, 28 Feb 2018 11:14:29 +0000 (13:14 +0200)
org.simantics.interop/src/org/simantics/interop/test/GraphComparator.java
org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java

index 11b3d1a3849e0309ab59d6ad271aa2187ce409d2..f5c4dfc8f61e5f1128908e05eef225deeb0df3d4 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.Stack;
 
+import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
@@ -769,9 +770,6 @@ public class GraphComparator {
                        } else {
                                if (DEBUG) System.out.println(left + " = " + right);
                                comparableResources.map(left, right);   
-                               //if (left.getResourceId() == 313071 && right.getResourceId() == 325324)
-                               if (left.getResourceId() == 313231 && right.getResourceId() == 337775)
-                                       System.out.println();
                        }
                }
                
@@ -1191,9 +1189,10 @@ public class GraphComparator {
                                        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 = compareValue(v1, v2);
+//                                                     Object v1 = g.getValue(s1.getObject());
+//                                                     Object v2 = g.getValue(s2.getObject());
+//                                                     boolean eq = compareValue(v1, v2);
+                                                       boolean eq = compareValue(g,b,s1.getObject(), s2.getObject());
                                                        if (!eq) {
                                                                addModification(s1, s2);
                                                                addComparable(s1, s2);
@@ -1232,6 +1231,75 @@ public class GraphComparator {
                
        }
        
+       public static boolean compareValue(ReadGraph g, Layer0 b, Resource r1, Resource r2) throws DatabaseException {
+               Resource t1 = g.getSingleType(r1);
+               Resource t2 = g.getSingleType(r2);
+               if (!t1.equals(t2))
+                       return false;
+               if (t1.equals(b.Integer)) {
+                       int v1 = g.getValue(r1, Bindings.INTEGER);
+                       int v2 = g.getValue(r2, Bindings.INTEGER);
+                       return v1 == v2;
+               } else if (t1.equals(b.Float)) {
+                       float v1 = g.getValue(r1, Bindings.FLOAT);
+                       float v2 = g.getValue(r2, Bindings.FLOAT);
+                       return v1 == v2;
+               } else if (t1.equals(b.Double)) {
+                       double v1 = g.getValue(r1, Bindings.DOUBLE);
+                       double v2 = g.getValue(r2, Bindings.DOUBLE);
+                       return v1 == v2;
+               } else if (t1.equals(b.String)) {
+                       String v1 = g.getValue(r1, Bindings.STRING);
+                       String v2 = g.getValue(r2, Bindings.STRING);
+                       return v1.equals(v2);
+               } else if (t1.equals(b.Boolean)) {
+                       boolean v1 = g.getValue(r1, Bindings.BOOLEAN);
+                       boolean v2 = g.getValue(r2, Bindings.BOOLEAN);
+                       return v1 == v2;
+               } else if (t1.equals(b.Byte)) {
+                       byte v1 = g.getValue(r1, Bindings.BYTE);
+                       byte v2 = g.getValue(r2, Bindings.BYTE);
+                       return v1 == v2;
+               } else if (t1.equals(b.Long)) {
+                       long v1 = g.getValue(r1, Bindings.LONG);
+                       long v2 = g.getValue(r2, Bindings.LONG);
+                       return v1 == v2;
+               } else if (t1.equals(b.IntegerArray)) {
+                       int[] v1 = g.getValue(r1, Bindings.INT_ARRAY);
+                       int[] v2 = g.getValue(r2, Bindings.INT_ARRAY);
+                       return Arrays.equals(v1,v2);
+               } else if (t1.equals(b.FloatArray)) {
+                       float[] v1 = g.getValue(r1, Bindings.FLOAT_ARRAY);
+                       float[] v2 = g.getValue(r2, Bindings.FLOAT_ARRAY);
+                       return Arrays.equals(v1,v2);
+               } else if (t1.equals(b.DoubleArray)) {
+                       double[] v1 = g.getValue(r1, Bindings.DOUBLE_ARRAY);
+                       double[] v2 = g.getValue(r2, Bindings.DOUBLE_ARRAY);
+                       return Arrays.equals(v1,v2);
+               } else if (t1.equals(b.StringArray)) {
+                       String[] v1 = g.getValue(r1, Bindings.STRING_ARRAY);
+                       String[] v2 = g.getValue(r2, Bindings.STRING_ARRAY);
+                       return Arrays.equals(v1,v2);
+               } else if (t1.equals(b.BooleanArray)) {
+                       boolean[] v1 = g.getValue(r1, Bindings.BOOLEAN_ARRAY);
+                       boolean[] v2 = g.getValue(r2, Bindings.BOOLEAN_ARRAY);
+                       return Arrays.equals(v1,v2);
+               } else if (t1.equals(b.ByteArray)) {
+                       byte[] v1 = g.getValue(r1, Bindings.BYTE_ARRAY);
+                       byte[] v2 = g.getValue(r2, Bindings.BYTE_ARRAY);
+                       return Arrays.equals(v1,v2);
+               } else if (t1.equals(b.LongArray)) {
+                       long[] v1 = g.getValue(r1, Bindings.LONG_ARRAY);
+                       long[] v2 = g.getValue(r2, Bindings.LONG_ARRAY);
+                       return Arrays.equals(v1,v2);
+               } else {
+                       Object v1 = g.getValue(r1);
+                       Object v2 = g.getValue(r2);
+                       return compareValue(v1, v2);
+               }
+               
+       }
+       
        public static boolean compareValue(Object v1, Object v2) {
                if (v1 instanceof Object[] && v2 instanceof Object[])
                        return Arrays.deepEquals((Object[])v1, (Object[])v2);
index 3bd21b51b1cb3d0dd2f440d6467c87e954dd0af7..61e8a0fbc193fa50cbc08e9a44f66207b241230d 100644 (file)
-package org.simantics.interop.test;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Comparator;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.DoesNotContainValueException;\r
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
-import org.simantics.db.exception.ServiceException;\r
-import org.simantics.db.exception.ValidationException;\r
-import org.simantics.layer0.Layer0;\r
-\r
-/**\r
- * Object comparator that uses type of objects to check if objects are comparable. \r
- * \r
- * Difference value is amount of properties that have different values. If objects have no properties, the result is 0 (unreliable).\r
- * \r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- */\r
-public class TypeComparator extends ResourceComparator {\r
-\r
-       \r
-       ArrayList<Resource> rs1 = new ArrayList<Resource>();\r
-       ArrayList<Resource> rs2 = new ArrayList<Resource>();\r
-       \r
-       @Override\r
-       public int compare(ReadGraph g, Resource o1, Resource o2, boolean local) throws DatabaseException {\r
-               if (local)\r
-                       return new TypeComparator().compare(g, o1, o2);\r
-               else\r
-                       return compare(g, o1, o2);\r
-       }\r
-       \r
-       @Override\r
-       public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException{\r
-               if (!compareType(g,o1, o2)) {\r
-                       return (Integer.MAX_VALUE);\r
-               }\r
-               return propsDiffCount(g,o1, o2);\r
-       }\r
-       \r
-       protected boolean compareType(ReadGraph g,Resource r1, Resource r2) throws ServiceException, ManyObjectsForFunctionalRelationException {\r
-               Layer0 l0 = Layer0.getInstance(g);\r
-               rs1.addAll(g.getObjects(r1, l0.InstanceOf));\r
-               rs2.addAll(g.getObjects(r2, l0.InstanceOf));\r
-               if (rs1.size() != rs2.size()) {\r
-                       rs1.clear();\r
-                       rs2.clear();\r
-                       return false;\r
-               }\r
-               Comparator<Resource> rcomp = getComparator().getResourceComparator();\r
-               getComparator().sortResource(rs1, rs2);\r
-\r
-               for (int i = 0; i < rs1.size(); i++) {\r
-                       int c = rcomp.compare(rs1.get(i), rs2.get(i));\r
-                       if (c != 0) {\r
-                               rs1.clear();\r
-                               rs2.clear();\r
-                               return false;\r
-                       }\r
-               }\r
-               \r
-               rs1.clear();\r
-               rs2.clear();\r
-               \r
-               return true;\r
-       }\r
-       \r
-       protected int propsDiffCount(ReadGraph g, Resource r1, Resource r2) throws ServiceException, DoesNotContainValueException, ValidationException {\r
-               Layer0 l0 = Layer0.getInstance(g);\r
-               ArrayList<Statement> ss1 = new ArrayList<Statement>();\r
-               ArrayList<Statement> ss2 = new ArrayList<Statement>();\r
-               ss1.addAll(g.getStatements(r1, l0.HasProperty));\r
-               ss2.addAll(g.getStatements(r2, l0.HasProperty));\r
-               \r
-               if (ss1.size() == 0 && ss2.size() == 0)\r
-                       return 0;\r
-               \r
-               Comparator<Statement> scomp = getComparator().getStatementComparator();\r
-               \r
-               getComparator().sortStatement(ss1, ss2);\r
-               \r
-               int count = 1;\r
-               \r
-               int i1 = 0; \r
-               int i2 = 0;\r
-               \r
-               while (true) {\r
-                       if (i1 >= ss1.size()) {\r
-                               if (i2 >= ss2.size())\r
-                                       break;\r
-                               else {\r
-                                       while (i2 < ss2.size()) {\r
-                                               count++;\r
-                                               i2++;\r
-                                       }\r
-                                       break;\r
-                               }\r
-                       } else if (i2 >= ss2.size()) {\r
-                               while (i1 < ss1.size()) {\r
-                                       count++;\r
-                                       i1++;\r
-                               }\r
-                               break;\r
-                       }\r
-                       Statement s1 = ss1.get(i1);\r
-                       Statement s2 = ss2.get(i2);\r
-                       if (s1.isAsserted(r1) && s2.isAsserted(r2)) {\r
-                               i1++;\r
-                               i2++;\r
-                               continue;\r
-                       }\r
-                       int c = scomp.compare(s1, s2);\r
-                       switch (c) {\r
-                               case 0:{\r
-                                       boolean b1 = g.hasValue(s1.getObject());\r
-                                       boolean b2 = g.hasValue(s2.getObject());\r
-                                       if (b1 == b2) {\r
-                                               if (b1) {\r
-                                                       Object v1 = g.getValue(s1.getObject());\r
-                                                       Object v2 = g.getValue(s2.getObject());\r
-                                                       boolean eq = GraphComparator.compareValue(v1, v2);\r
-                                                       \r
-                                                       if (!eq) {\r
-                                                               count++;\r
-                                                       }\r
-                                                       //System.out.println("Prop count values " + v1 + " " + v2);\r
-                                               } else {\r
-                                                       count += propsDiffCount(g,s1.getObject(), s2.getObject());\r
-                                               }\r
-                                       } else {\r
-                                               //System.out.println("Props count structural vs literal");\r
-                                               count++;\r
-                                       }\r
-                                       i1++;\r
-                                       i2++;\r
-                                       break;\r
-                               }\r
-                               case -1:{\r
-                                       count++;\r
-                                       i1++;\r
-                                       break;\r
-                               }\r
-                                       \r
-                               case 1:{\r
-                                       count++;\r
-                                       i2++;\r
-                                       break;\r
-                               }\r
-                       }\r
-\r
-               }\r
-               \r
-               ss1.clear();\r
-               ss2.clear();\r
-               return count;\r
-       }\r
-\r
-}\r
+package org.simantics.interop.test;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+
+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;
+
+/**
+ * Object comparator that uses type of objects to check if objects are comparable. 
+ * 
+ * Difference value is amount of properties that have different values. If objects have no properties, the result is 0 (unreliable).
+ * 
+ * 
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class TypeComparator extends ResourceComparator {
+
+       
+       ArrayList<Resource> rs1 = new ArrayList<Resource>();
+       ArrayList<Resource> rs2 = new ArrayList<Resource>();
+       
+       @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);
+       }
+       
+       @Override
+       public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException{
+               if (!compareType(g,o1, o2)) {
+                       return (Integer.MAX_VALUE);
+               }
+               return propsDiffCount(g,o1, o2);
+       }
+       
+       protected boolean compareType(ReadGraph g,Resource r1, Resource r2) throws DatabaseException {
+               Layer0 l0 = Layer0.getInstance(g);
+               rs1.addAll(g.getObjects(r1, l0.InstanceOf));
+               rs2.addAll(g.getObjects(r2, l0.InstanceOf));
+               if (rs1.size() != rs2.size()) {
+                       rs1.clear();
+                       rs2.clear();
+                       return false;
+               }
+               Comparator<Resource> rcomp = getComparator().getResourceComparator();
+               getComparator().sortResource(rs1, rs2);
+
+               for (int i = 0; i < rs1.size(); i++) {
+                       int c = rcomp.compare(rs1.get(i), rs2.get(i));
+                       if (c != 0) {
+                               rs1.clear();
+                               rs2.clear();
+                               return false;
+                       }
+               }
+               
+               rs1.clear();
+               rs2.clear();
+               
+               return true;
+       }
+       
+       protected int propsDiffCount(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
+               Layer0 l0 = Layer0.getInstance(g);
+               ArrayList<Statement> ss1 = new ArrayList<Statement>();
+               ArrayList<Statement> ss2 = new ArrayList<Statement>();
+               ss1.addAll(g.getStatements(r1, l0.HasProperty));
+               ss2.addAll(g.getStatements(r2, l0.HasProperty));
+               
+               if (ss1.size() == 0 && ss2.size() == 0)
+                       return 0;
+               
+               Comparator<Statement> scomp = getComparator().getStatementComparator();
+               
+               getComparator().sortStatement(ss1, ss2);
+               
+               int count = 1;
+               
+               int i1 = 0; 
+               int i2 = 0;
+               
+               while (true) {
+                       if (i1 >= ss1.size()) {
+                               if (i2 >= ss2.size())
+                                       break;
+                               else {
+                                       while (i2 < ss2.size()) {
+                                               count++;
+                                               i2++;
+                                       }
+                                       break;
+                               }
+                       } else if (i2 >= ss2.size()) {
+                               while (i1 < ss1.size()) {
+                                       count++;
+                                       i1++;
+                               }
+                               break;
+                       }
+                       Statement s1 = ss1.get(i1);
+                       Statement s2 = ss2.get(i2);
+                       if (s1.isAsserted(r1) && s2.isAsserted(r2)) {
+                               i1++;
+                               i2++;
+                               continue;
+                       }
+                       int c = scomp.compare(s1, s2);
+                       switch (c) {
+                               case 0:{
+                                       boolean b1 = g.hasValue(s1.getObject());
+                                       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());
+                                               }
+                                       } else {
+                                               //System.out.println("Props count structural vs literal");
+                                               count++;
+                                       }
+                                       i1++;
+                                       i2++;
+                                       break;
+                               }
+                               case -1:{
+                                       count++;
+                                       i1++;
+                                       break;
+                               }
+                                       
+                               case 1:{
+                                       count++;
+                                       i2++;
+                                       break;
+                               }
+                       }
+
+               }
+               
+               ss1.clear();
+               ss2.clear();
+               return count;
+       }
+
+}