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