]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/ConvertPreValues.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / procedures / ConvertPreValues.java
diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/ConvertPreValues.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/ConvertPreValues.java
new file mode 100644 (file)
index 0000000..ca1588f
--- /dev/null
@@ -0,0 +1,80 @@
+package org.simantics.graph.compiler.internal.procedures;\r
+\r
+import gnu.trove.list.array.TIntArrayList;\r
+import gnu.trove.map.hash.TIntObjectHashMap;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.graph.compiler.internal.store.PreValueStore;\r
+import org.simantics.graph.query.IDataTypeQuery;\r
+import org.simantics.graph.query.IGraph;\r
+import org.simantics.graph.query.Paths;\r
+import org.simantics.graph.store.GraphStore;\r
+import org.simantics.graph.store.StatementStore;\r
+import org.simantics.graph.store.ValueStore;\r
+import org.simantics.ltk.Problem;\r
+\r
+public class ConvertPreValues implements Runnable {\r
+\r
+       IGraph cg;\r
+       GraphStore store;\r
+       Collection<Problem> problems;\r
+       \r
+       public ConvertPreValues(IGraph cg, GraphStore store,\r
+                       Collection<Problem> problems) {\r
+               this.cg = cg;\r
+               this.store = store;\r
+               this.problems = problems;\r
+       }\r
+\r
+       @Override\r
+       public void run() {\r
+               PreValueStore preValues = store.getStore(PreValueStore.class);\r
+               preValues.convertPreValues(store.values, new IDataTypeQuery() { \r
+                       \r
+                   Paths paths = cg.getPaths();\r
+                       StatementStore statements = store.statements;\r
+                       ValueStore values = store.values;\r
+                       int HasDatatype = store.identities.createPathToId(paths.HasDatatype);\r
+                       int InstanceOf = store.identities.createPathToId(paths.InstanceOf);\r
+                       \r
+                       @Override\r
+                       public Binding getDataType(int id) {\r
+                               TIntArrayList objects = statements.getObjects(id, HasDatatype);\r
+                               if(objects.isEmpty()) {\r
+                                       objects = statements.getObjects(id, InstanceOf);\r
+                                       for(int i=0;i<objects.size();++i) {\r
+                                               int type = objects.getQuick(i);\r
+                                               Binding binding = getDatatypeForType(type);\r
+                                               if(binding != null)\r
+                                                       return binding;\r
+                                       }\r
+                                       return null;\r
+                               }\r
+                               else {\r
+                                       Datatype dt = values.getDatatypeValue(objects.getQuick(0));\r
+                                       if(dt == null)\r
+                                               return null;\r
+                                       else\r
+                                               return Bindings.getBinding(dt);\r
+                               }                               \r
+                       }\r
+\r
+                       TIntObjectHashMap<Binding> typeBindingCache = new TIntObjectHashMap<Binding>();\r
+                       private Binding getDatatypeForType(int type) {\r
+                               if(typeBindingCache.containsKey(type))\r
+                                       return typeBindingCache.get(type);\r
+                               else {\r
+                                       Datatype dt = cg.getAssertedDatatype(store.idToRes(type));\r
+                                       Binding binding = dt == null ? null : Bindings.getBinding(dt);\r
+                                       typeBindingCache.put(type, binding);\r
+                                       return binding;\r
+                               }                               \r
+                       }\r
+               }, problems);\r
+       }       \r
+\r
+}\r