]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/ConvertPreValues.java
Removed org.simantics.ltk[.antlr] bundles, exact import for antlr
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / procedures / ConvertPreValues.java
1 package org.simantics.graph.compiler.internal.procedures;
2
3 import java.util.Collection;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.databoard.binding.Binding;
7 import org.simantics.databoard.type.Datatype;
8 import org.simantics.graph.compiler.internal.ltk.Problem;
9 import org.simantics.graph.compiler.internal.store.PreValueStore;
10 import org.simantics.graph.query.IDataTypeQuery;
11 import org.simantics.graph.query.IGraph;
12 import org.simantics.graph.query.Paths;
13 import org.simantics.graph.store.GraphStore;
14 import org.simantics.graph.store.StatementStore;
15 import org.simantics.graph.store.ValueStore;
16
17 import gnu.trove.list.array.TIntArrayList;
18 import gnu.trove.map.hash.TIntObjectHashMap;
19
20 public class ConvertPreValues implements Runnable {
21
22         IGraph cg;
23         GraphStore store;
24         Collection<Problem> problems;
25         
26         public ConvertPreValues(IGraph cg, GraphStore store,
27                         Collection<Problem> problems) {
28                 this.cg = cg;
29                 this.store = store;
30                 this.problems = problems;
31         }
32
33         @Override
34         public void run() {
35                 PreValueStore preValues = store.getStore(PreValueStore.class);
36                 preValues.convertPreValues(store.values, new IDataTypeQuery() { 
37                         
38                     Paths paths = cg.getPaths();
39                         StatementStore statements = store.statements;
40                         ValueStore values = store.values;
41                         int HasDatatype = store.identities.createPathToId(paths.HasDatatype);
42                         int InstanceOf = store.identities.createPathToId(paths.InstanceOf);
43                         
44                         @Override
45                         public Binding getDataType(int id) {
46                                 TIntArrayList objects = statements.getObjects(id, HasDatatype);
47                                 if(objects.isEmpty()) {
48                                         objects = statements.getObjects(id, InstanceOf);
49                                         for(int i=0;i<objects.size();++i) {
50                                                 int type = objects.getQuick(i);
51                                                 Binding binding = getDatatypeForType(type);
52                                                 if(binding != null)
53                                                         return binding;
54                                         }
55                                         return null;
56                                 }
57                                 else {
58                                         Datatype dt = values.getDatatypeValue(objects.getQuick(0));
59                                         if(dt == null)
60                                                 return null;
61                                         else
62                                                 return Bindings.getBinding(dt);
63                                 }                               
64                         }
65
66                         TIntObjectHashMap<Binding> typeBindingCache = new TIntObjectHashMap<Binding>();
67                         private Binding getDatatypeForType(int type) {
68                                 if(typeBindingCache.containsKey(type))
69                                         return typeBindingCache.get(type);
70                                 else {
71                                         Datatype dt = cg.getAssertedDatatype(store.idToRes(type));
72                                         Binding binding = dt == null ? null : Bindings.getBinding(dt);
73                                         typeBindingCache.put(type, binding);
74                                         return binding;
75                                 }                               
76                         }
77                 }, problems);
78         }       
79
80 }