]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/QueryRuleInstruction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / rule / instructions / QueryRuleInstruction.java
diff --git a/bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/QueryRuleInstruction.java b/bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/QueryRuleInstruction.java
new file mode 100644 (file)
index 0000000..f2407bf
--- /dev/null
@@ -0,0 +1,147 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.mapping.rule.instructions;\r
+\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+import gnu.trove.procedure.TIntProcedure;\r
+import gnu.trove.set.hash.TIntHashSet;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.utils.triggers.IModification;\r
+\r
+public class QueryRuleInstruction implements IRuleInstruction {\r
+\r
+       IRuleInstruction rule;\r
+       int[] variables;\r
+       int workSpace;\r
+\r
+       public QueryRuleInstruction(IRuleInstruction rule) {\r
+               this.rule = rule;\r
+               TIntHashSet reads = new TIntHashSet();\r
+               TIntHashSet writes = new TIntHashSet();\r
+               rule.collectVariables(reads, writes);\r
+               reads.removeAll(writes.toArray());\r
+               variables = reads.toArray();\r
+               final TIntIntHashMap map = new TIntIntHashMap();\r
+               for(int i = 0;i<variables.length;++i)\r
+                       map.put(variables[i], i);\r
+               workSpace = variables.length;\r
+               writes.forEach(new TIntProcedure() {\r
+\r
+                       @Override\r
+                       public boolean execute(int arg0) {\r
+                               map.put(arg0, workSpace++);\r
+                               return true;\r
+                       }\r
+                       \r
+               });\r
+               rule.mapVariables(map);\r
+       }\r
+\r
+       @Override\r
+       public void collectVariables(TIntHashSet reads, TIntHashSet writes) {\r
+               reads.addAll(variables);\r
+       }\r
+\r
+       class Query implements Read<IModification> {\r
+\r
+               Object[] parameters;\r
+               \r
+               public Query(Object[] parameters) {\r
+                       this.parameters = parameters;\r
+               }\r
+\r
+               QueryRuleInstruction parent() {\r
+                       return QueryRuleInstruction.this;\r
+               }\r
+               \r
+               @Override\r
+               public boolean equals(Object other) {\r
+                       if(this == other)\r
+                               return true;\r
+                       if(other == null || other.getClass() != this.getClass())\r
+                               return false;\r
+                       Query q = (Query)other;\r
+                       if(!parent().equals(q.parent()))\r
+                               return false;\r
+                       if(parameters.length != q.parameters.length)\r
+                               return false;\r
+                       for(int i=0;i<parameters.length;++i)\r
+                               if(parameters[i] == null ? q.parameters[i] != null : !parameters[i].equals(q.parameters[i]))\r
+                                       return false;\r
+                       return true;\r
+               }\r
+\r
+               @Override\r
+               public int hashCode() {\r
+                       int result = QueryRuleInstruction.this.hashCode();\r
+                       for(Object parameter : parameters) {\r
+                               result *= 31;\r
+                               if(parameter != null)\r
+                                       result += parameter.hashCode();\r
+                               else\r
+                                       System.err.println("Parameter is null!!!");\r
+                       }\r
+                       return result;\r
+               }\r
+\r
+               @Override\r
+               public IModification perform(ReadGraph g) throws DatabaseException {                    \r
+                       final Object[] bindings = new Object[workSpace];\r
+                       System.arraycopy(parameters, 0, bindings, 0, parameters.length);\r
+                       return rule.execute(g, bindings);                       \r
+               }\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public IModification execute(ReadGraph g, Object[] bindings) throws DatabaseException {\r
+               Object[] parameters = new Object[variables.length];\r
+               for(int i=0;i<variables.length;++i)\r
+                       parameters[i] = bindings[variables[i]];\r
+               IModification result = g.syncRequest(new Query(parameters));\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {\r
+               Object[] parameters = new Object[variables.length];\r
+               for(int i=0;i<variables.length;++i)\r
+                       parameters[i] = bindings[variables[i]];\r
+               IModification modi = g.syncRequest(new Query(parameters));\r
+               if(modi != null)\r
+                       modi.perform(g);\r
+       }\r
+       \r
+       @Override\r
+       public void mapVariables(TIntIntHashMap map) {\r
+               for(int i=0;i<variables.length;++i)\r
+                       variables[i] = map.get(variables[i]);           \r
+       }\r
+       \r
+       @Override\r
+       public void toString(StringBuilder b, int indent) {\r
+               b.append("QUERY[");\r
+               b.append(workSpace);\r
+               b.append("]");\r
+               for(int i=0;i<variables.length;++i)\r
+                       b.append(" " + variables[i] + "->" + i);\r
+               b.append('\n');\r
+               for(int i=0;i<indent;++i)\r
+                       b.append(INDENTATION);\r
+               rule.toString(b, indent);\r
+       }\r
+       \r
+}\r