/******************************************************************************* * 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.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.ListenerBase; import gnu.trove.procedure.TIntProcedure; final public class TypeHierarchy extends UnaryQuery> { TypeHierarchy(final int resource) { super(resource); } final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache.runnerTypeHierarchy(graph, r, parent, listener, procedure); } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } @Override public IntSet compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { QueryProcessor processor = graph.processor; final IntSet result = new IntSet(processor.querySupport, id); final TIntProcedure addToResult = new TIntProcedure() { @Override public boolean execute(int r) { result.add(r); return true; } }; QueryCache.runnerSuperTypes(graph, id, TypeHierarchy.this, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { types.forEach(addToResult); addOrSet(graph, result, processor); procedure.execute(graph, result); } @Override public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); } }); return result; } @Override public String toString() { return "TypeHierarchy[" + id + "]"; } private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) { assert(!isReady()); synchronized(this) { value.trim(); setResult(value); setReady(); } } @Override public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { assert(isReady()); if(handleException(graph, procedure)) return null; 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 result) { } @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); } }