]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLTypeUtils.java
Contextual adapters must throw instead of returning null
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLTypeUtils.java
1 package org.simantics.modeling;
2
3 import gnu.trove.map.hash.THashMap;
4
5 import org.simantics.databoard.type.ArrayType;
6 import org.simantics.databoard.type.BooleanType;
7 import org.simantics.databoard.type.ByteType;
8 import org.simantics.databoard.type.Datatype;
9 import org.simantics.databoard.type.DoubleType;
10 import org.simantics.databoard.type.FloatType;
11 import org.simantics.databoard.type.IntegerType;
12 import org.simantics.databoard.type.LongType;
13 import org.simantics.databoard.type.StringType;
14 import org.simantics.db.layer0.request.PropertyInfo;
15 import org.simantics.scl.compiler.types.TCon;
16 import org.simantics.scl.compiler.types.Type;
17 import org.simantics.scl.compiler.types.Types;
18 import org.simantics.scl.compiler.types.kinds.Kinds;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class SCLTypeUtils {
23     private static final Logger LOGGER = LoggerFactory.getLogger(SCLTypeUtils.class);
24     private static final THashMap<String, Type> TYPE_MAP = new THashMap<String, Type>();
25     
26     private static void add(TCon type) {
27         TYPE_MAP.put(type.name, type);
28     }
29     
30     static {
31         add(Types.BOOLEAN);
32         add(Types.BYTE);
33         add(Types.CHARACTER);
34         add(Types.SHORT);
35         add(Types.INTEGER);
36         add(Types.LONG);
37         add(Types.FLOAT);
38         add(Types.DOUBLE);
39         add(Types.STRING);
40         
41         TYPE_MAP.put("[Boolean]", Types.list(Types.BOOLEAN));
42         TYPE_MAP.put("[Byte]", Types.list(Types.BYTE));
43         TYPE_MAP.put("[Character]", Types.list(Types.CHARACTER));
44         TYPE_MAP.put("[Short]", Types.list(Types.SHORT));
45         TYPE_MAP.put("[Integer]", Types.list(Types.INTEGER));
46         TYPE_MAP.put("[Long]", Types.list(Types.LONG));
47         TYPE_MAP.put("[Float]", Types.list(Types.FLOAT));
48         TYPE_MAP.put("[Double]", Types.list(Types.DOUBLE));
49         TYPE_MAP.put("[String]", Types.list(Types.STRING));
50         
51         TYPE_MAP.put("Vector Boolean", Types.vector(Types.BOOLEAN));
52         TYPE_MAP.put("Vector Byte", Types.vector(Types.BYTE));
53         TYPE_MAP.put("Vector Character", Types.vector(Types.CHARACTER));
54         TYPE_MAP.put("Vector Short", Types.vector(Types.SHORT));
55         TYPE_MAP.put("Vector Integer", Types.vector(Types.INTEGER));
56         TYPE_MAP.put("Vector Long", Types.vector(Types.LONG));
57         TYPE_MAP.put("Vector Float", Types.vector(Types.FLOAT));
58         TYPE_MAP.put("Vector Double", Types.vector(Types.DOUBLE));
59         TYPE_MAP.put("Vector String", Types.vector(Types.STRING));
60         TYPE_MAP.put("ByteArray", Types.BYTE_ARRAY);
61
62         // #83: L0.typeResource
63         TCon variable = Types.con("Simantics/Variables", "Variable");
64         add(variable);
65         TYPE_MAP.put("Variable -> Resource -> <ReadGraph> Resource",
66                 Types.functionE(new Type[] {variable, Types.RESOURCE}, Types.READ_GRAPH, Types.RESOURCE));
67
68         add((TCon)Types.RESOURCE);
69         add(Types.con("Simantics/GUID", "GUID")); // L0.GUID
70         add(Types.con("Simantics/Variables", "StructuredProperty")); // L0.methods
71         add(Types.con("Simantics/Variables", "ValueAccessor")); // L0.ValueAccessor
72         add(Types.con("Simantics/Variables", "VariableMap"));
73     }
74     
75     /**
76      * This is very rude method for converting SCL type text to SCL type.
77      * All uses of this method should be replaced in the future by use of SCL compiler
78      * and actual lookups to dependent SCL modules 
79      */
80     public static Type getType(String typeText) {
81         Type type = TYPE_MAP.get(typeText);
82         if(type == null) {
83             LOGGER.warn("SCLTypeUtils.getType cannot transform '" + typeText + "' to type. Returns a as default.");
84             return Types.var(Kinds.STAR);
85         }
86         return type;
87     }
88     
89     public static Type getType(Datatype dataType) {
90         if(dataType instanceof DoubleType)
91             return Types.DOUBLE;
92         else if(dataType instanceof IntegerType)
93             return Types.INTEGER;
94         else if(dataType instanceof StringType)
95             return Types.STRING;
96         else if(dataType instanceof BooleanType)
97             return Types.BOOLEAN;
98         else if(dataType instanceof FloatType)
99             return Types.FLOAT;
100         else if(dataType instanceof LongType)
101             return Types.LONG;
102         else if(dataType instanceof ByteType)
103             return Types.BYTE;
104         else if(dataType instanceof ArrayType)
105             return Types.list(getType(((ArrayType)dataType).componentType));
106         else {
107             LOGGER.warn("SCLTypeUtils.getType cannot transform data type '" + dataType + "' to type. Returns a as default.");
108             return Types.var(Kinds.STAR);
109         }
110     }
111     
112     public static Type getType(PropertyInfo propertyInfo) {
113         if(propertyInfo.requiredValueType != null)
114             return getType(propertyInfo.requiredValueType);
115         else if(propertyInfo.requiredDatatype != null)
116             return getType(propertyInfo.requiredDatatype);
117         else {
118             LOGGER.warn(propertyInfo.name + " doesn't have type information. Returns a as default.");
119             return Types.var(Kinds.STAR);
120         }
121     }
122 }