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