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