]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/QueryRuleInstruction.java
Replace System.err and System.out with SLF4J Logging
[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 org.simantics.db.ReadGraph;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.request.Read;
18 import org.simantics.layer0.utils.triggers.IModification;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import gnu.trove.map.hash.TIntIntHashMap;
23 import gnu.trove.procedure.TIntProcedure;
24 import gnu.trove.set.hash.TIntHashSet;
25
26 public class QueryRuleInstruction implements IRuleInstruction {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(QueryRuleInstruction.class);
29         IRuleInstruction rule;
30         int[] variables;
31         int workSpace;
32
33         public QueryRuleInstruction(IRuleInstruction rule) {
34                 this.rule = rule;
35                 TIntHashSet reads = new TIntHashSet();
36                 TIntHashSet writes = new TIntHashSet();
37                 rule.collectVariables(reads, writes);
38                 reads.removeAll(writes.toArray());
39                 variables = reads.toArray();
40                 final TIntIntHashMap map = new TIntIntHashMap();
41                 for(int i = 0;i<variables.length;++i)
42                         map.put(variables[i], i);
43                 workSpace = variables.length;
44                 writes.forEach(new TIntProcedure() {
45
46                         @Override
47                         public boolean execute(int arg0) {
48                                 map.put(arg0, workSpace++);
49                                 return true;
50                         }
51                         
52                 });
53                 rule.mapVariables(map);
54         }
55
56         @Override
57         public void collectVariables(TIntHashSet reads, TIntHashSet writes) {
58                 reads.addAll(variables);
59         }
60
61         class Query implements Read<IModification> {
62
63                 Object[] parameters;
64                 
65                 public Query(Object[] parameters) {
66                         this.parameters = parameters;
67                 }
68
69                 QueryRuleInstruction parent() {
70                         return QueryRuleInstruction.this;
71                 }
72                 
73                 @Override
74                 public boolean equals(Object other) {
75                         if(this == other)
76                                 return true;
77                         if(other == null || other.getClass() != this.getClass())
78                                 return false;
79                         Query q = (Query)other;
80                         if(!parent().equals(q.parent()))
81                                 return false;
82                         if(parameters.length != q.parameters.length)
83                                 return false;
84                         for(int i=0;i<parameters.length;++i)
85                                 if(parameters[i] == null ? q.parameters[i] != null : !parameters[i].equals(q.parameters[i]))
86                                         return false;
87                         return true;
88                 }
89
90                 @Override
91                 public int hashCode() {
92                         int result = QueryRuleInstruction.this.hashCode();
93                         for(Object parameter : parameters) {
94                                 result *= 31;
95                                 if(parameter != null)
96                                         result += parameter.hashCode();
97                                 else
98                                         LOGGER.error("Parameter is null!!!");
99                         }
100                         return result;
101                 }
102
103                 @Override
104                 public IModification perform(ReadGraph g) throws DatabaseException {                    
105                         final Object[] bindings = new Object[workSpace];
106                         System.arraycopy(parameters, 0, bindings, 0, parameters.length);
107                         return rule.execute(g, bindings);                       
108                 }
109                 
110         }
111         
112         @Override
113         public IModification execute(ReadGraph g, Object[] bindings) throws DatabaseException {
114                 Object[] parameters = new Object[variables.length];
115                 for(int i=0;i<variables.length;++i)
116                         parameters[i] = bindings[variables[i]];
117                 IModification result = g.syncRequest(new Query(parameters));
118                 return result;
119         }
120
121         @Override
122         public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {
123                 Object[] parameters = new Object[variables.length];
124                 for(int i=0;i<variables.length;++i)
125                         parameters[i] = bindings[variables[i]];
126                 IModification modi = g.syncRequest(new Query(parameters));
127                 if(modi != null)
128                         modi.perform(g);
129         }
130         
131         @Override
132         public void mapVariables(TIntIntHashMap map) {
133                 for(int i=0;i<variables.length;++i)
134                         variables[i] = map.get(variables[i]);           
135         }
136         
137         @Override
138         public void toString(StringBuilder b, int indent) {
139                 b.append("QUERY[");
140                 b.append(workSpace);
141                 b.append("]");
142                 for(int i=0;i<variables.length;++i)
143                         b.append(" " + variables[i] + "->" + i);
144                 b.append('\n');
145                 for(int i=0;i<indent;++i)
146                         b.append(INDENTATION);
147                 rule.toString(b, indent);
148         }
149         
150 }