]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/QueryRuleInstruction.java
dbb0e7ca40459d7876bcc8d8f025d1ae9f7d7924
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / rule / instructions / QueryRuleInstruction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.mapping.rule.instructions;
13
14 import gnu.trove.map.hash.TIntIntHashMap;
15 import gnu.trove.procedure.TIntProcedure;
16 import gnu.trove.set.hash.TIntHashSet;
17
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;
23
24 public class QueryRuleInstruction implements IRuleInstruction {
25
26         IRuleInstruction rule;
27         int[] variables;
28         int workSpace;
29
30         public QueryRuleInstruction(IRuleInstruction rule) {
31                 this.rule = 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() {
42
43                         @Override
44                         public boolean execute(int arg0) {
45                                 map.put(arg0, workSpace++);
46                                 return true;
47                         }
48                         
49                 });
50                 rule.mapVariables(map);
51         }
52
53         @Override
54         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
55                 reads.addAll(variables);
56         }
57
58         class Query implements Read<IModification> {
59
60                 Object[] parameters;
61                 
62                 public Query(Object[] parameters) {
63                         this.parameters = parameters;
64                 }
65
66                 QueryRuleInstruction parent() {
67                         return QueryRuleInstruction.this;
68                 }
69                 
70                 @Override
71                 public boolean equals(Object other) {
72                         if(this == other)
73                                 return true;
74                         if(other == null || other.getClass() != this.getClass())
75                                 return false;
76                         Query q = (Query)other;
77                         if(!parent().equals(q.parent()))
78                                 return false;
79                         if(parameters.length != q.parameters.length)
80                                 return false;
81                         for(int i=0;i<parameters.length;++i)
82                                 if(parameters[i] == null ? q.parameters[i] != null : !parameters[i].equals(q.parameters[i]))
83                                         return false;
84                         return true;
85                 }
86
87                 @Override
88                 public int hashCode() {
89                         int result = QueryRuleInstruction.this.hashCode();
90                         for(Object parameter : parameters) {
91                                 result *= 31;
92                                 if(parameter != null)
93                                         result += parameter.hashCode();
94                                 else
95                                         System.err.println("Parameter is null!!!");
96                         }
97                         return result;
98                 }
99
100                 @Override
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);                       
105                 }
106                 
107         }
108         
109         @Override
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));
115                 return result;
116         }
117
118         @Override
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));
124                 if(modi != null)
125                         modi.perform(g);
126         }
127         
128         @Override
129         public void mapVariables(TIntIntHashMap map) {
130                 for(int i=0;i<variables.length;++i)
131                         variables[i] = map.get(variables[i]);           
132         }
133         
134         @Override
135         public void toString(StringBuilder b, int indent) {
136                 b.append("QUERY[");
137                 b.append(workSpace);
138                 b.append("]");
139                 for(int i=0;i<variables.length;++i)
140                         b.append(" " + variables[i] + "->" + i);
141                 b.append('\n');
142                 for(int i=0;i<indent;++i)
143                         b.append(INDENTATION);
144                 rule.toString(b, indent);
145         }
146         
147 }