]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/store/PreValueStore.java
923fd9698d29003ea89d67a39e78995c4f37f1dd
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / store / PreValueStore.java
1 package org.simantics.graph.compiler.internal.store;\r
2 \r
3 import gnu.trove.map.hash.TIntIntHashMap;\r
4 import gnu.trove.map.hash.TIntObjectHashMap;\r
5 import gnu.trove.procedure.TIntObjectProcedure;\r
6 import gnu.trove.set.hash.TIntHashSet;\r
7 \r
8 import java.util.ArrayList;\r
9 import java.util.Collection;\r
10 \r
11 import org.simantics.databoard.Bindings;\r
12 import org.simantics.databoard.binding.Binding;\r
13 import org.simantics.databoard.binding.mutable.Variant;\r
14 import org.simantics.graph.compiler.internal.values.TreeValue;\r
15 import org.simantics.graph.query.IDataTypeQuery;\r
16 import org.simantics.graph.store.IStore;\r
17 import org.simantics.graph.store.IndexMappingUtils;\r
18 import org.simantics.graph.store.ValueStore;\r
19 import org.simantics.ltk.Problem;\r
20 \r
21 public class PreValueStore implements IStore {\r
22 \r
23         TIntObjectHashMap<IPreValue> preValues = new TIntObjectHashMap<IPreValue>();\r
24         TIntHashSet collisions = new TIntHashSet();\r
25         \r
26         @Override\r
27         public void map(TIntIntHashMap map) {\r
28                 preValues = IndexMappingUtils.map(map, preValues, collisions);\r
29         }\r
30         \r
31         public void setValue(int id, IPreValue value) {\r
32                 preValues.put(id, value);\r
33         }\r
34         \r
35         public void convertPreValues(final ValueStore values, final IDataTypeQuery dtq, final Collection<Problem> problems) {\r
36                 preValues.forEachEntry(new TIntObjectProcedure<IPreValue>() {                   \r
37                         @Override\r
38                         public boolean execute(int resource, IPreValue preValue) {\r
39                                 try {\r
40                                         Binding binding = dtq.getDataType(resource);\r
41                                         if(binding == null) {\r
42                                                 problems.add(new Problem(\r
43                                                                 preValue.getLocation(), \r
44                                                                 "Literal does not have a data type."\r
45                                                         ));     \r
46                                                 return true;\r
47                                         }\r
48                                         Object value = preValue.toValue(binding, problems);\r
49                                         values.setValue(resource,\r
50                                                         new Variant(binding, value)\r
51                                                         );                                              \r
52                                 } catch(Exception e) {\r
53                                         e.printStackTrace();\r
54                                 }\r
55                                 return true;\r
56                         }\r
57                 });\r
58                 preValues.clear();\r
59         }\r
60         \r
61         public void forEachPreValue(TIntObjectProcedure<IPreValue> procedure) {\r
62                 preValues.forEachEntry(procedure);\r
63         }\r
64         \r
65         public String getStringValue(int i) {\r
66                 IPreValue value = preValues.get(i);\r
67                 if(value == null || !(value instanceof TreeValue))\r
68                         return null;\r
69                 ArrayList<Problem> problems = new ArrayList<Problem>();\r
70                 String v = (String) ((TreeValue)value).toValue(Bindings.STRING, problems);\r
71                 if(v == null || problems.size()>0)\r
72                         return null;\r
73                 return v;\r
74         }\r
75 \r
76 }\r