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.db.impl.query;
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;
22 import gnu.trove.procedure.TIntProcedure;
24 final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
26 Predicates(final int r) {
31 final public void removeEntry(QueryProcessor provider) {
32 provider.cache.remove(this);
35 final static private void forAssertions(ReadGraphImpl graph, int r, Predicates entry, final IntSet set) throws DatabaseException {
37 QueryCache.runnerPrincipalTypes(graph, r, entry, null, new SyncIntProcedure() {
40 public void run(ReadGraphImpl graph) throws DatabaseException {
43 IntProcedure proc = new IntProcedure() {
46 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
51 public void finished(ReadGraphImpl graph) throws DatabaseException {
56 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
57 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
63 public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
66 QueryCache.runnerAssertedPredicates(graph, type, entry, null, proc);
71 public void finished(ReadGraphImpl graph) throws DatabaseException {
81 public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
82 computeForEach(graph, id, this, procedure);
86 public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
88 IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
90 IntSet result = new IntSet(graph.processor.querySupport);
91 forAssertions(graph, r, entry, result);
93 if(result.isEmpty()) {
96 entry.setResult(direct);
100 procedure.execute(graph, direct);
104 direct.forEach(new TIntProcedure() {
106 public boolean execute(int value) {
113 entry.setResult(result);
117 procedure.execute(graph, result);
125 public String toString() {
126 return "Predicates[" + id + "]";
129 final public void finish(final ReadGraphImpl graph, QueryProcessor provider) {
138 public void clearResult(QuerySupport support) {
139 setResult(new IntSet(support));
143 public Object performFromCache(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
147 if(handleException(graph, procedure)) return EXCEPTED;
149 IntSet result = getResult();
151 procedure.execute(graph, result);
158 public void recompute(ReadGraphImpl graph) throws DatabaseException {
160 compute(graph, new InternalProcedure<IntSet>() {
163 public void exception(ReadGraphImpl graph, Throwable t) {
164 throw new Error("Error in recompute.", t);
168 public void execute(ReadGraphImpl graph, IntSet i) {
177 return RequestFlags.IMMEDIATE_UPDATE;
182 boolean isImmutable(ReadGraphImpl graph) {
183 return graph.processor.isImmutable(id);