/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.db.impl.query; import org.simantics.db.common.exception.DebugException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; final public class DirectPredicates extends CollectionUnaryQuery> { DirectPredicates(final int resource) { super(resource); } @Override public void clearResult(QuerySupport support) { // The cached result is never used setResult(INVALID_RESULT); } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } @Override public Object compute(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { return computeForEach(graph, id, this, procedure); } public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure procedure) throws DatabaseException { graph.processor.querySupport.ensureLoaded(graph, id); final IntSet list = new IntSet(graph.processor.querySupport); graph.processor.querySupport.getPredicates(graph, id, new IntProcedure() { @Override public void execute(ReadGraphImpl graph, int i) { list.add(i); } @Override public void finished(ReadGraphImpl graph) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { if(DebugException.DEBUG) new DebugException(t).printStackTrace(); } }); if(entry != null) { entry.setResult(list); entry.setReady(); } procedure.execute(graph, list); return list; } @Override public String toString() { return "DirectPredicates[" + id + "]"; } @Override public void setReady() { statusOrException = READY; } @Override public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { assert(isReady()); if(handleException(graph, procedure)) return EXCEPTED; IntSet result = getResult(); procedure.execute(graph, result); return result; } @Override public void recompute(ReadGraphImpl graph) throws DatabaseException { compute(graph, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet set) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { new Error("Error in recompute.", t).printStackTrace(); } }); } @Override boolean isImmutable(ReadGraphImpl graph) { return graph.processor.isImmutable(id); } }