1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.mapping.rule.instructions;
\r
14 import gnu.trove.map.hash.TIntIntHashMap;
\r
15 import gnu.trove.procedure.TIntProcedure;
\r
16 import gnu.trove.set.hash.TIntHashSet;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.exception.DatabaseException;
\r
21 import org.simantics.db.request.Read;
\r
22 import org.simantics.layer0.utils.triggers.IModification;
\r
24 public class QueryRuleInstruction implements IRuleInstruction {
\r
26 IRuleInstruction rule;
\r
30 public QueryRuleInstruction(IRuleInstruction rule) {
\r
32 TIntHashSet reads = new TIntHashSet();
\r
33 TIntHashSet writes = new TIntHashSet();
\r
34 rule.collectVariables(reads, writes);
\r
35 reads.removeAll(writes.toArray());
\r
36 variables = reads.toArray();
\r
37 final TIntIntHashMap map = new TIntIntHashMap();
\r
38 for(int i = 0;i<variables.length;++i)
\r
39 map.put(variables[i], i);
\r
40 workSpace = variables.length;
\r
41 writes.forEach(new TIntProcedure() {
\r
44 public boolean execute(int arg0) {
\r
45 map.put(arg0, workSpace++);
\r
50 rule.mapVariables(map);
\r
54 public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
\r
55 reads.addAll(variables);
\r
58 class Query implements Read<IModification> {
\r
60 Object[] parameters;
\r
62 public Query(Object[] parameters) {
\r
63 this.parameters = parameters;
\r
66 QueryRuleInstruction parent() {
\r
67 return QueryRuleInstruction.this;
\r
71 public boolean equals(Object other) {
\r
74 if(other == null || other.getClass() != this.getClass())
\r
76 Query q = (Query)other;
\r
77 if(!parent().equals(q.parent()))
\r
79 if(parameters.length != q.parameters.length)
\r
81 for(int i=0;i<parameters.length;++i)
\r
82 if(parameters[i] == null ? q.parameters[i] != null : !parameters[i].equals(q.parameters[i]))
\r
88 public int hashCode() {
\r
89 int result = QueryRuleInstruction.this.hashCode();
\r
90 for(Object parameter : parameters) {
\r
92 if(parameter != null)
\r
93 result += parameter.hashCode();
\r
95 System.err.println("Parameter is null!!!");
\r
101 public IModification perform(ReadGraph g) throws DatabaseException {
\r
102 final Object[] bindings = new Object[workSpace];
\r
103 System.arraycopy(parameters, 0, bindings, 0, parameters.length);
\r
104 return rule.execute(g, bindings);
\r
110 public IModification execute(ReadGraph g, Object[] bindings) throws DatabaseException {
\r
111 Object[] parameters = new Object[variables.length];
\r
112 for(int i=0;i<variables.length;++i)
\r
113 parameters[i] = bindings[variables[i]];
\r
114 IModification result = g.syncRequest(new Query(parameters));
\r
119 public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {
\r
120 Object[] parameters = new Object[variables.length];
\r
121 for(int i=0;i<variables.length;++i)
\r
122 parameters[i] = bindings[variables[i]];
\r
123 IModification modi = g.syncRequest(new Query(parameters));
\r
129 public void mapVariables(TIntIntHashMap map) {
\r
130 for(int i=0;i<variables.length;++i)
\r
131 variables[i] = map.get(variables[i]);
\r
135 public void toString(StringBuilder b, int indent) {
\r
136 b.append("QUERY[");
\r
137 b.append(workSpace);
\r
139 for(int i=0;i<variables.length;++i)
\r
140 b.append(" " + variables[i] + "->" + i);
\r
142 for(int i=0;i<indent;++i)
\r
143 b.append(INDENTATION);
\r
144 rule.toString(b, indent);
\r