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;
19 final public class DirectPredicates extends CollectionUnaryQuery<InternalProcedure<IntSet>> {
21 DirectPredicates(final int resource) {
26 public void clearResult(QuerySupport support) {
27 // The cached result is never used
28 setResult(INVALID_RESULT);
32 final public void removeEntry(QueryProcessor provider) {
33 provider.cache.remove(this);
37 public Object compute(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
38 return computeForEach(graph, id, this, procedure);
41 public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
43 graph.processor.querySupport.ensureLoaded(graph, id);
45 final IntSet list = new IntSet(graph.processor.querySupport);
47 graph.processor.querySupport.getPredicates(graph, id, new IntProcedure() {
50 public void execute(ReadGraphImpl graph, int i) {
55 public void finished(ReadGraphImpl graph) {
59 public void exception(ReadGraphImpl graph, Throwable t) {
60 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
66 entry.setResult(list);
70 procedure.execute(graph, list);
77 public String toString() {
78 return "DirectPredicates[" + id + "]";
82 public void setReady() {
83 statusOrException = READY;
87 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
91 if(handleException(graph, procedure)) return EXCEPTED;
93 IntSet result = getResult();
95 procedure.execute(graph, result);
102 public void recompute(ReadGraphImpl graph) throws DatabaseException {
104 compute(graph, new InternalProcedure<IntSet>() {
107 public void execute(ReadGraphImpl graph, IntSet set) {
111 public void exception(ReadGraphImpl graph, Throwable t) {
112 new Error("Error in recompute.", t).printStackTrace();
121 boolean isImmutable(ReadGraphImpl graph) {
122 return graph.processor.isImmutable(id);