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;
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;
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 {
}
- THashMap<Class<?>, Resource> builtinValues = new THashMap<Class<?>, Resource>(32);
+ Map<Object, Resource> builtinValues = new IdentityHashMap<>(40);
private void initBuiltinValues(Layer0 b) {
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
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);
}
} 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);