Changed SCLTypeUtils to not log warnings for non-property relations
[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.TVar;
17 import org.simantics.scl.compiler.types.Type;
18 import org.simantics.scl.compiler.types.Types;
19 import org.simantics.scl.compiler.types.kinds.Kinds;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class SCLTypeUtils {
24     private static final Logger LOGGER = LoggerFactory.getLogger(SCLTypeUtils.class);
25     private static final THashMap<String, Type> TYPE_MAP = new THashMap<String, Type>();
26     
27     private static final TVar STAR = Types.var(Kinds.STAR);
28     
29     private static void add(TCon type) {
30         TYPE_MAP.put(type.name, type);
31     }
32     
33     static {
34         add(Types.BOOLEAN);
35         add(Types.BYTE);
36         add(Types.CHARACTER);
37         add(Types.SHORT);
38         add(Types.INTEGER);
39         add(Types.LONG);
40         add(Types.FLOAT);
41         add(Types.DOUBLE);
42         add(Types.STRING);
43         
44         TYPE_MAP.put("[Boolean]", Types.list(Types.BOOLEAN));
45         TYPE_MAP.put("[Byte]", Types.list(Types.BYTE));
46         TYPE_MAP.put("[Character]", Types.list(Types.CHARACTER));
47         TYPE_MAP.put("[Short]", Types.list(Types.SHORT));
48         TYPE_MAP.put("[Integer]", Types.list(Types.INTEGER));
49         TYPE_MAP.put("[Long]", Types.list(Types.LONG));
50         TYPE_MAP.put("[Float]", Types.list(Types.FLOAT));
51         TYPE_MAP.put("[Double]", Types.list(Types.DOUBLE));
52         TYPE_MAP.put("[String]", Types.list(Types.STRING));
53         
54         TYPE_MAP.put("Vector Boolean", Types.vector(Types.BOOLEAN));
55         TYPE_MAP.put("Vector Byte", Types.vector(Types.BYTE));
56         TYPE_MAP.put("Vector Character", Types.vector(Types.CHARACTER));
57         TYPE_MAP.put("Vector Short", Types.vector(Types.SHORT));
58         TYPE_MAP.put("Vector Integer", Types.vector(Types.INTEGER));
59         TYPE_MAP.put("Vector Long", Types.vector(Types.LONG));
60         TYPE_MAP.put("Vector Float", Types.vector(Types.FLOAT));
61         TYPE_MAP.put("Vector Double", Types.vector(Types.DOUBLE));
62         TYPE_MAP.put("Vector String", Types.vector(Types.STRING));
63         TYPE_MAP.put("ByteArray", Types.BYTE_ARRAY);
64
65         // #83: L0.typeResource
66         TCon variable = Types.con("Simantics/Variables", "Variable");
67         add(variable);
68         TYPE_MAP.put("Variable -> Resource -> <ReadGraph> Resource",
69                 Types.functionE(new Type[] {variable, Types.RESOURCE}, Types.READ_GRAPH, Types.RESOURCE));
70
71         add((TCon)Types.RESOURCE);
72         add(Types.con("Simantics/ChangeInformation", "ChangeInformation")); // MOD.ChangeInformation
73         add(Types.con("Simantics/GUID", "GUID")); // L0.GUID
74         add(Types.con("Simantics/Variables", "StructuredProperty")); // L0.methods
75         add(Types.con("Simantics/Variables", "ValueAccessor")); // L0.ValueAccessor
76         add(Types.con("Simantics/Variables", "VariableMap"));
77     }
78     
79     /**
80      * This is very rude method for converting SCL type text to SCL type.
81      * All uses of this method should be replaced in the future by use of SCL compiler
82      * and actual lookups to dependent SCL modules 
83      */
84     public static Type getType(String typeText) {
85         Type type = TYPE_MAP.get(typeText);
86         if(type == null) {
87             LOGGER.warn("SCLTypeUtils.getType cannot transform '" + typeText + "' to type. Returns a as default.");
88             return STAR;
89         }
90         return type;
91     }
92     
93     public static Type getType(Datatype dataType) {
94         if(dataType instanceof DoubleType)
95             return Types.DOUBLE;
96         else if(dataType instanceof IntegerType)
97             return Types.INTEGER;
98         else if(dataType instanceof StringType)
99             return Types.STRING;
100         else if(dataType instanceof BooleanType)
101             return Types.BOOLEAN;
102         else if(dataType instanceof FloatType)
103             return Types.FLOAT;
104         else if(dataType instanceof LongType)
105             return Types.LONG;
106         else if(dataType instanceof ByteType)
107             return Types.BYTE;
108         else if(dataType instanceof ArrayType)
109             return Types.list(getType(((ArrayType)dataType).componentType));
110         else {
111             LOGGER.warn("SCLTypeUtils.getType cannot transform data type '" + dataType + "' to type. Returns a as default.");
112             return STAR;
113         }
114     }
115     
116     public static Type getType(PropertyInfo propertyInfo, boolean warnOfNoTypeInformation) {
117         if(propertyInfo.requiredValueType != null)
118             return getType(propertyInfo.requiredValueType);
119         else if(propertyInfo.requiredDatatype != null)
120             return getType(propertyInfo.requiredDatatype);
121         else {
122             if (warnOfNoTypeInformation)
123                 LOGGER.warn(propertyInfo.name + " doesn't have type information. Returns a as default.");
124             return STAR;
125         }
126     }
127     
128     public static Type getType(PropertyInfo propertyInfo) {
129         return getType(propertyInfo, propertyInfo.isHasProperty);
130     }
131 }