]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/WriteGraphImpl.java
Added more literal type resolution logic to claimLiteral
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / WriteGraphImpl.java
index 0c4e681385da7ab25a4c5afae9217a649c1a7966..8cb1688fb73f584f1900f6589fd0a45c448c96c6 100644 (file)
@@ -14,10 +14,13 @@ package org.simantics.db.impl.graph;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.util.IdentityHashMap;
+import java.util.Map;
 import java.util.TreeMap;
 import java.util.function.Consumer;
 
 import org.simantics.databoard.Bindings;
+import org.simantics.databoard.Datatypes;
 import org.simantics.databoard.accessor.Accessor;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.binding.error.BindingConstructionException;
@@ -31,7 +34,16 @@ import org.simantics.databoard.primitives.MutableLong;
 import org.simantics.databoard.primitives.MutableString;
 import org.simantics.databoard.serialization.SerializationException;
 import org.simantics.databoard.serialization.Serializer;
+import org.simantics.databoard.type.ArrayType;
+import org.simantics.databoard.type.BooleanType;
+import org.simantics.databoard.type.ByteType;
 import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.DoubleType;
+import org.simantics.databoard.type.FloatType;
+import org.simantics.databoard.type.IntegerType;
+import org.simantics.databoard.type.LongType;
+import org.simantics.databoard.type.StringType;
+import org.simantics.databoard.type.VariantType;
 import org.simantics.databoard.util.binary.RandomAccessBinary;
 import org.simantics.db.DevelopmentKeys;
 import org.simantics.db.ExternalValueSupport;
@@ -69,8 +81,6 @@ import org.simantics.layer0.Layer0;
 import org.simantics.utils.Development;
 import org.simantics.utils.datastructures.Pair;
 
-import gnu.trove.map.hash.THashMap;
-
 
 final public class WriteGraphImpl extends ReadGraphImpl implements WriteGraph {
 
@@ -683,7 +693,7 @@ final public class WriteGraphImpl extends ReadGraphImpl implements WriteGraph {
         
     }
 
-    THashMap<Class<?>, Resource> builtinValues = new THashMap<Class<?>, Resource>(32);
+    Map<Object, Resource> builtinValues = new IdentityHashMap<>(40);
 
     private void initBuiltinValues(Layer0 b) {
 
@@ -714,6 +724,76 @@ final public class WriteGraphImpl extends ReadGraphImpl implements WriteGraph {
         builtinValues.put(MutableByte.class, b.Byte);
         builtinValues.put(MutableBoolean.class, b.Boolean);
 
+        builtinValues.put(Datatypes.DOUBLE, b.Double);
+        builtinValues.put(Datatypes.STRING, b.String);
+        builtinValues.put(Datatypes.INTEGER, b.Integer);
+        builtinValues.put(Datatypes.LONG, b.Long);
+        builtinValues.put(Datatypes.FLOAT, b.Float);
+        builtinValues.put(Datatypes.BYTE, b.Byte);
+        builtinValues.put(Datatypes.BOOLEAN, b.Boolean);
+        builtinValues.put(Datatypes.VARIANT, b.Variant);
+
+        builtinValues.put(Datatypes.DOUBLE_ARRAY, b.DoubleArray);
+        builtinValues.put(Datatypes.STRING_ARRAY, b.StringArray);
+        builtinValues.put(Datatypes.INTEGER_ARRAY, b.IntegerArray);
+        builtinValues.put(Datatypes.LONG_ARRAY, b.LongArray);
+        builtinValues.put(Datatypes.FLOAT_ARRAY, b.FloatArray);
+        builtinValues.put(Datatypes.BYTE_ARRAY, b.ByteArray);
+        builtinValues.put(Datatypes.BOOLEAN_ARRAY, b.BooleanArray);
+        builtinValues.put(Datatypes.VARIANT_ARRAY, b.VariantArray);
+    }
+
+    private static Datatype canonicalizeToBuiltinDatatype(Datatype datatype) {
+        if (datatype instanceof ArrayType) {
+            ArrayType at = (ArrayType) datatype;
+            datatype = at.componentType();
+            if (datatype instanceof ByteType) {
+                return Datatypes.BYTE_ARRAY;
+            } else if (datatype instanceof DoubleType) {
+                return Datatypes.DOUBLE_ARRAY;
+            } else if (datatype instanceof FloatType) {
+                return Datatypes.FLOAT_ARRAY;
+            } else if (datatype instanceof IntegerType) {
+                return Datatypes.INTEGER_ARRAY;
+            } else if (datatype instanceof LongType) {
+                return Datatypes.LONG_ARRAY;
+            } else if (datatype instanceof BooleanType) {
+                return Datatypes.BOOLEAN_ARRAY;
+            } else if (datatype instanceof StringType) {
+                return Datatypes.STRING_ARRAY;
+            } else if (datatype instanceof VariantType) {
+                return Datatypes.VARIANT_ARRAY;
+            }
+            return null;
+        }
+        if (datatype instanceof ByteType) {
+            return Datatypes.BYTE;
+        } else if (datatype instanceof DoubleType) {
+            return Datatypes.DOUBLE;
+        } else if (datatype instanceof FloatType) {
+            return Datatypes.FLOAT;
+        } else if (datatype instanceof IntegerType) {
+            return Datatypes.INTEGER;
+        } else if (datatype instanceof LongType) {
+            return Datatypes.LONG;
+        } else if (datatype instanceof BooleanType) {
+            return Datatypes.BOOLEAN;
+        } else if (datatype instanceof StringType) {
+            return Datatypes.STRING;
+        } else if (datatype instanceof VariantType) {
+            return Datatypes.VARIANT;
+        }
+        return null;
+    }
+
+    private Resource resolveBuiltinResourceType(Class<?> valueClass, Datatype datatype) {
+        Resource type = builtinValues.get(valueClass);
+        return type != null ? type : builtinValues.get(datatype);
+    }
+
+    private Resource resolveBuiltinResourceTypeByCanonicalizedDatatype(Datatype datatype, Resource defaultResult) {
+        Datatype cdt = canonicalizeToBuiltinDatatype(datatype);
+        return cdt != null ? builtinValues.get(cdt) : defaultResult;
     }
 
     @Override
@@ -734,13 +814,13 @@ final public class WriteGraphImpl extends ReadGraphImpl implements WriteGraph {
         initBuiltinValues(b);
         Resource literal = newResource();
         
-        Class<?> clazz = value.getClass();
-        Resource type = builtinValues.get(clazz);
+        Datatype dt = binding.type();
+        Resource type = resolveBuiltinResourceType(value.getClass(), dt);
         if (type == null) {
-            type = b.Literal;
+            type = resolveBuiltinResourceTypeByCanonicalizedDatatype(dt, b.Literal);
             Resource dataType = newResource();
             claim(dataType, b.InstanceOf, null, b.DataType);
-            claimValue(dataType, binding.type(), DATA_TYPE_BINDING);
+            claimValue(dataType, dt, DATA_TYPE_BINDING);
             claim(literal, b.HasDataType, b.HasDataType_Inverse, dataType);
         }
         
@@ -805,14 +885,14 @@ final public class WriteGraphImpl extends ReadGraphImpl implements WriteGraph {
 
         } else {
 
-            Class<?> clazz = value.getClass();
-            Resource type = builtinValues.get(clazz);
             Resource literal = newResource();
+            Datatype dt = binding.type();
+            Resource type = resolveBuiltinResourceType(value.getClass(), dt);
             if (type == null) {
-                type = b.Literal;
+                type = resolveBuiltinResourceTypeByCanonicalizedDatatype(dt, b.Literal);
                 Resource dataType = newResource();
                 claim(dataType, b.InstanceOf, null, b.DataType);
-                claimValue(dataType, binding.type(), DATA_TYPE_BINDING);
+                claimValue(dataType, dt, DATA_TYPE_BINDING);
                 claim(literal, b.HasDataType, null, dataType);
             }
             claim(literal, b.InstanceOf, null, type);