--- /dev/null
+package org.simantics.graph.compiler.internal.store;\r
+\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+import gnu.trove.map.hash.TIntObjectHashMap;\r
+import gnu.trove.procedure.TIntObjectProcedure;\r
+import gnu.trove.set.hash.TIntHashSet;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.graph.compiler.internal.values.TreeValue;\r
+import org.simantics.graph.query.IDataTypeQuery;\r
+import org.simantics.graph.store.IStore;\r
+import org.simantics.graph.store.IndexMappingUtils;\r
+import org.simantics.graph.store.ValueStore;\r
+import org.simantics.ltk.Problem;\r
+\r
+public class PreValueStore implements IStore {\r
+\r
+ TIntObjectHashMap<IPreValue> preValues = new TIntObjectHashMap<IPreValue>();\r
+ TIntHashSet collisions = new TIntHashSet();\r
+ \r
+ @Override\r
+ public void map(TIntIntHashMap map) {\r
+ preValues = IndexMappingUtils.map(map, preValues, collisions);\r
+ }\r
+ \r
+ public void setValue(int id, IPreValue value) {\r
+ preValues.put(id, value);\r
+ }\r
+ \r
+ public void convertPreValues(final ValueStore values, final IDataTypeQuery dtq, final Collection<Problem> problems) {\r
+ preValues.forEachEntry(new TIntObjectProcedure<IPreValue>() { \r
+ @Override\r
+ public boolean execute(int resource, IPreValue preValue) {\r
+ try {\r
+ Binding binding = dtq.getDataType(resource);\r
+ if(binding == null) {\r
+ problems.add(new Problem(\r
+ preValue.getLocation(), \r
+ "Literal does not have a data type."\r
+ )); \r
+ return true;\r
+ }\r
+ Object value = preValue.toValue(binding, problems);\r
+ values.setValue(resource,\r
+ new Variant(binding, value)\r
+ ); \r
+ } catch(Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ return true;\r
+ }\r
+ });\r
+ preValues.clear();\r
+ }\r
+ \r
+ public void forEachPreValue(TIntObjectProcedure<IPreValue> procedure) {\r
+ preValues.forEachEntry(procedure);\r
+ }\r
+ \r
+ public String getStringValue(int i) {\r
+ IPreValue value = preValues.get(i);\r
+ if(value == null || !(value instanceof TreeValue))\r
+ return null;\r
+ ArrayList<Problem> problems = new ArrayList<Problem>();\r
+ String v = (String) ((TreeValue)value).toValue(Bindings.STRING, problems);\r
+ if(v == null || problems.size()>0)\r
+ return null;\r
+ return v;\r
+ }\r
+\r
+}\r