/******************************************************************************* * 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.RelationInfo; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.request.RequestFlags; final public class RelationInfoQuery extends UnaryQuery> { RelationInfoQuery(final int resource) { super(resource); } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery entry, final InternalProcedure proc) throws DatabaseException { QueryProcessor processor = graph.processor; final int isUsedInAssertion = processor.getHasPredicateInverse(); assert(isUsedInAssertion != 0); QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, entry, null, new IntProcedure() { boolean done = false; @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(done) return; done = true; RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true); if(entry != null) entry.setResult(result); proc.execute(graph, result); } @Override public void finished(ReadGraphImpl graph) throws DatabaseException { if(done) return; done = true; RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false); if(entry != null) entry.setResult(result); proc.execute(graph, result); } @Override public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { if(done) return; done = true; DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery"); if(entry != null) entry.except(e); proc.exception(graph, e); } }); } public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure procedure) throws DatabaseException { QueryProcessor provider = graph.processor; final int superRelationOf = provider.getSuperrelationOf(); assert(superRelationOf != 0); IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null); IntSet types = QueryCache.resultTypes(graph, r, entry, null); computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure); } //@Override public Object compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { computeForEach(graph, id, this, procedure); return getResult(); } @Override public String toString() { return "RelationInfoQuery[" + id + "]"; } // public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) { // // assert(isPending()); // // synchronized(this) { // // setResult(result); // setReady(); // // } // // } @Override public void setResult(Object result) { super.setResult(result); if(!(result instanceof RelationInfo) && !(result == NO_RESULT)) System.err.println("foo"); setReady(); } @Override public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { assert(isReady()); if(handleException(graph, procedure)) return EXCEPTED; RelationInfo result = getResult(); procedure.execute(graph, result); return result; } @Override public int type() { return RequestFlags.IMMEDIATE_UPDATE; } @Override public void recompute(ReadGraphImpl graph) throws DatabaseException { compute(graph, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, RelationInfo result) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { throw new Error("Error in recompute.", t); } }); } }