]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java
3841e1d14640777706ce30421c00d2bcce2d43ea
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Predicates.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.db.impl.query;
13
14 import org.simantics.db.common.exception.DebugException;
15 import org.simantics.db.common.utils.Logger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.impl.procedure.InternalProcedure;
19 import org.simantics.db.procedure.ListenerBase;
20 import org.simantics.db.request.RequestFlags;
21
22 import gnu.trove.procedure.TIntProcedure;
23
24 final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
25
26         Predicates(final int r) {
27         super(r);
28     }
29
30         @Override
31         final public void removeEntry(QueryProcessor provider) {
32         provider.cache.remove(this);
33         }
34     
35     final static private void forAssertions(ReadGraphImpl graph, int r, Predicates entry, final IntSet set) throws DatabaseException {
36
37         QueryCache.runnerPrincipalTypes(graph, r, entry, null, new SyncIntProcedure() {
38             
39             @Override
40             public void run(ReadGraphImpl graph) throws DatabaseException {
41             }
42             
43             IntProcedure proc = new IntProcedure() {
44
45                 @Override
46                 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
47                         set.add(i);
48                 }
49
50                 @Override
51                 public void finished(ReadGraphImpl graph) throws DatabaseException {
52                     dec(graph);
53                 }
54                                 
55                                 @Override
56                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
57                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
58                     }
59
60             }; 
61
62             @Override
63             public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
64
65                 inc();
66                 QueryCache.runnerAssertedPredicates(graph, type, entry, null, proc);
67                 
68             }
69             
70             @Override
71             public void finished(ReadGraphImpl graph) throws DatabaseException {
72                 dec(graph);       
73             }
74             
75         });
76         
77
78     }
79
80         @Override
81         public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
82                 computeForEach(graph, id, this, procedure);
83                 return getResult();
84         }
85
86         public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
87         
88         
89         IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null, procedure);
90                 IntSet result = new IntSet();
91                 direct.forEach(new TIntProcedure() {
92                         @Override
93                         public boolean execute(int value) {
94                                 result.add(value);
95                                 return true;
96                         }
97                 });
98                 
99         forAssertions(graph, r, entry, result);
100         
101 //              DirectPredicates.queryEach(graph, r, processor, entry, null, new IntProcedure() {
102 //
103 //                      @Override
104 //                      public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
105 //                              result.add(pred);
106 //                      }
107 //
108 //                      @Override
109 //                      public void finished(ReadGraphImpl graph) throws DatabaseException {
110 //                              
111 //
112 //                      }
113 //
114 //                      @Override
115 //                      public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
116 //                              procedure.exception(graph, t);
117 //                      }
118 //
119 //              });
120                 
121                 if(entry != null) {
122                         entry.setResult(result);
123                         entry.setReady();
124                 }
125                 
126                 procedure.execute(graph, result);
127         
128     }
129     
130     @Override
131     public String toString() {
132         return "Predicates[" + id + "]";
133     }
134
135     final public void finish(final ReadGraphImpl graph, QueryProcessor provider) {
136         
137         synchronized(this) {
138                 setReady();
139         }
140
141     }
142
143     @Override
144     public void clearResult(QuerySupport support) {
145         setResult(new IntSet(support));
146     }
147     
148     @Override
149     public Object performFromCache(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
150         
151         assert(isReady());
152
153         if(handleException(graph, procedure)) return EXCEPTED;
154         
155         IntSet result = getResult();
156         
157         procedure.execute(graph, result);
158         
159         return result;
160         
161     }
162     
163     @Override
164     public void recompute(ReadGraphImpl graph) throws DatabaseException {
165         
166         compute(graph, new InternalProcedure<IntSet>() {
167
168                         @Override
169                         public void exception(ReadGraphImpl graph, Throwable t) {
170                                 throw new Error("Error in recompute.", t);
171             }
172
173                         @Override
174                         public void execute(ReadGraphImpl graph, IntSet i) {
175                         }
176
177         });
178         
179     }
180     
181     @Override
182     public int type() {
183         return RequestFlags.IMMEDIATE_UPDATE;
184     }
185
186
187     @Override
188     boolean isImmutable(ReadGraphImpl graph) {
189         return graph.processor.isImmutable(id);
190     }
191     
192 }