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.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.InternalProcedure;
18 import org.simantics.db.request.RequestFlags;
20 import gnu.trove.procedure.TIntProcedure;
22 final public class Predicates extends UnaryQueryPIntSet {
24 Predicates(final int r) {
29 final public void removeEntry(QueryProcessor provider) {
30 provider.cache.remove(this);
33 final static private void forAssertions(ReadGraphImpl graph, int r, Predicates parent, final IntSet set) throws DatabaseException {
35 QueryCache.runnerPrincipalTypes(graph, r, parent, null, new SyncIntProcedure() {
38 public void run(ReadGraphImpl graph) throws DatabaseException {
41 IntProcedure proc = new IntProcedure() {
44 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
49 public void finished(ReadGraphImpl graph) throws DatabaseException {
54 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
55 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
61 public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
64 QueryCache.runnerAssertedPredicates(graph, type, parent, null, proc);
69 public void finished(ReadGraphImpl graph) throws DatabaseException {
79 public void compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
80 computeForEach(graph, id, this, procedure);
83 public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
85 InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
87 IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
89 IntSet result = new IntSet(graph.processor.querySupport);
90 forAssertions(graph, r, entry, result);
92 if(result.isEmpty()) {
93 procedure.execute(graph, direct);
95 direct.forEach(new TIntProcedure() {
97 public boolean execute(int value) {
102 procedure.execute(graph, result);
105 if(entry != null) entry.performFromCache(graph, procedure_);
110 public String toString() {
111 return "Predicates[" + id + "]";
115 public void clearResult(QuerySupport support) {
116 setResult(new IntSet(support));
121 return RequestFlags.IMMEDIATE_UPDATE;