]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLTypeUtils.java
Layer0Utils.addL0Identifier to prevent possible differentiation of code
[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
20 public class SCLTypeUtils {
21     private static final THashMap<String, Type> TYPE_MAP = new THashMap<String, Type>();
22     
23     private static void add(TCon type) {
24         TYPE_MAP.put(type.name, type);
25     }
26     
27     static {
28         add(Types.BOOLEAN);
29         add(Types.BYTE);
30         add(Types.CHARACTER);
31         add(Types.SHORT);
32         add(Types.INTEGER);
33         add(Types.LONG);
34         add(Types.FLOAT);
35         add(Types.DOUBLE);
36         add(Types.STRING);
37         
38         TYPE_MAP.put("[Boolean]", Types.list(Types.BOOLEAN));
39         TYPE_MAP.put("[Byte]", Types.list(Types.BYTE));
40         TYPE_MAP.put("[Character]", Types.list(Types.CHARACTER));
41         TYPE_MAP.put("[Short]", Types.list(Types.SHORT));
42         TYPE_MAP.put("[Integer]", Types.list(Types.INTEGER));
43         TYPE_MAP.put("[Long]", Types.list(Types.LONG));
44         TYPE_MAP.put("[Float]", Types.list(Types.FLOAT));
45         TYPE_MAP.put("[Double]", Types.list(Types.DOUBLE));
46         TYPE_MAP.put("[String]", Types.list(Types.STRING));
47         
48         TYPE_MAP.put("Vector Boolean", Types.vector(Types.BOOLEAN));
49         TYPE_MAP.put("Vector Byte", Types.vector(Types.BYTE));
50         TYPE_MAP.put("Vector Character", Types.vector(Types.CHARACTER));
51         TYPE_MAP.put("Vector Short", Types.vector(Types.SHORT));
52         TYPE_MAP.put("Vector Integer", Types.vector(Types.INTEGER));
53         TYPE_MAP.put("Vector Long", Types.vector(Types.LONG));
54         TYPE_MAP.put("Vector Float", Types.vector(Types.FLOAT));
55         TYPE_MAP.put("Vector Double", Types.vector(Types.DOUBLE));
56         TYPE_MAP.put("Vector String", Types.vector(Types.STRING));
57         
58         add(Types.BYTE_ARRAY);
59         add((TCon)Types.RESOURCE);
60         add(Types.con("Simantics/Variables", "VariableMap"));
61     }
62     
63     /**
64      * This is very rude method for converting SCL type text to SCL type.
65      * All uses of this method should be replaced in the future by use of SCL compiler
66      * and actual lookups to dependent SCL modules 
67      */
68     public static Type getType(String typeText) {
69         Type type = TYPE_MAP.get(typeText);
70         if(type == null) {
71             System.err.println("SCLTypeUtils.getType cannot transform '" + typeText + "' to type. Returns a as default.");
72             return Types.var(Kinds.STAR);
73         }
74         return type;
75     }
76     
77     public static Type getType(Datatype dataType) {
78         if(dataType instanceof DoubleType)
79             return Types.DOUBLE;
80         else if(dataType instanceof IntegerType)
81             return Types.INTEGER;
82         else if(dataType instanceof StringType)
83             return Types.STRING;
84         else if(dataType instanceof BooleanType)
85             return Types.BOOLEAN;
86         else if(dataType instanceof FloatType)
87             return Types.FLOAT;
88         else if(dataType instanceof LongType)
89             return Types.LONG;
90         else if(dataType instanceof ByteType)
91             return Types.BYTE;
92         else if(dataType instanceof ArrayType)
93             return Types.list(getType(((ArrayType)dataType).componentType));
94         else {
95             System.err.println("SCLTypeUtils.getType cannot transform data type '" + dataType + "' to type. Returns a as default.");
96             return Types.var(Kinds.STAR);
97         }
98     }
99     
100     public static Type getType(PropertyInfo propertyInfo) {
101         if(propertyInfo.requiredValueType != null)
102             return getType(propertyInfo.requiredValueType);
103         else if(propertyInfo.requiredDatatype != null)
104             return getType(propertyInfo.requiredDatatype);
105         else {
106             System.err.println(propertyInfo.name + " doesn't have type information. Returns a as default.");
107             return Types.var(Kinds.STAR);
108         }
109     }
110 }