X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Fstore%2FPreValueStore.java;fp=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Fstore%2FPreValueStore.java;h=923fd9698d29003ea89d67a39e78995c4f37f1dd;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/store/PreValueStore.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/store/PreValueStore.java new file mode 100644 index 000000000..923fd9698 --- /dev/null +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/store/PreValueStore.java @@ -0,0 +1,76 @@ +package org.simantics.graph.compiler.internal.store; + +import gnu.trove.map.hash.TIntIntHashMap; +import gnu.trove.map.hash.TIntObjectHashMap; +import gnu.trove.procedure.TIntObjectProcedure; +import gnu.trove.set.hash.TIntHashSet; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.graph.compiler.internal.values.TreeValue; +import org.simantics.graph.query.IDataTypeQuery; +import org.simantics.graph.store.IStore; +import org.simantics.graph.store.IndexMappingUtils; +import org.simantics.graph.store.ValueStore; +import org.simantics.ltk.Problem; + +public class PreValueStore implements IStore { + + TIntObjectHashMap preValues = new TIntObjectHashMap(); + TIntHashSet collisions = new TIntHashSet(); + + @Override + public void map(TIntIntHashMap map) { + preValues = IndexMappingUtils.map(map, preValues, collisions); + } + + public void setValue(int id, IPreValue value) { + preValues.put(id, value); + } + + public void convertPreValues(final ValueStore values, final IDataTypeQuery dtq, final Collection problems) { + preValues.forEachEntry(new TIntObjectProcedure() { + @Override + public boolean execute(int resource, IPreValue preValue) { + try { + Binding binding = dtq.getDataType(resource); + if(binding == null) { + problems.add(new Problem( + preValue.getLocation(), + "Literal does not have a data type." + )); + return true; + } + Object value = preValue.toValue(binding, problems); + values.setValue(resource, + new Variant(binding, value) + ); + } catch(Exception e) { + e.printStackTrace(); + } + return true; + } + }); + preValues.clear(); + } + + public void forEachPreValue(TIntObjectProcedure procedure) { + preValues.forEachEntry(procedure); + } + + public String getStringValue(int i) { + IPreValue value = preValues.get(i); + if(value == null || !(value instanceof TreeValue)) + return null; + ArrayList problems = new ArrayList(); + String v = (String) ((TreeValue)value).toValue(Bindings.STRING, problems); + if(v == null || problems.size()>0) + return null; + return v; + } + +}