]> gerrit.simantics Code Review - simantics/platform.git/blob
e7b80409af589f75d5a577d05f096151b61960a3
[simantics/platform.git] /
1 package org.simantics.graph.compiler.internal.procedures;
2
3 import gnu.trove.list.array.TIntArrayList;
4 import gnu.trove.map.hash.TIntObjectHashMap;
5
6 import java.util.Collection;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.type.Datatype;
11 import org.simantics.graph.compiler.internal.store.PreValueStore;
12 import org.simantics.graph.query.IDataTypeQuery;
13 import org.simantics.graph.query.IGraph;
14 import org.simantics.graph.query.Paths;
15 import org.simantics.graph.store.GraphStore;
16 import org.simantics.graph.store.StatementStore;
17 import org.simantics.graph.store.ValueStore;
18 import org.simantics.ltk.Problem;
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 }