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.RelationInfo;
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 final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
22 RelationInfoQuery(final int resource) {
27 final public void removeEntry(QueryProcessor provider) {
28 provider.cache.remove(this);
31 private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery entry, final InternalProcedure<RelationInfo> proc) throws DatabaseException {
33 QueryProcessor processor = graph.processor;
35 final int isUsedInAssertion = processor.getHasPredicateInverse();
36 assert(isUsedInAssertion != 0);
38 QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, entry, null, new IntProcedure() {
43 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
46 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true);
47 if(entry != null) entry.setResult(result);
48 proc.execute(graph, result);
52 public void finished(ReadGraphImpl graph) throws DatabaseException {
55 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
56 if(entry != null) entry.setResult(result);
57 proc.execute(graph, result);
61 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
64 DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
65 if(entry != null) entry.except(e);
66 proc.exception(graph, e);
73 public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
75 QueryProcessor provider = graph.processor;
77 final int superRelationOf = provider.getSuperrelationOf();
78 assert(superRelationOf != 0);
80 IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
81 IntSet types = QueryCache.resultTypes(graph, r, entry, null);
83 computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
88 public Object compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
89 computeForEach(graph, id, this, procedure);
94 public String toString() {
95 return "RelationInfoQuery[" + id + "]";
98 // public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
100 // assert(isPending());
102 // synchronized(this) {
104 // setResult(result);
112 public void setResult(Object result) {
113 super.setResult(result);
114 if(!(result instanceof RelationInfo) && !(result == NO_RESULT))
115 System.err.println("foo");
120 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
124 if(handleException(graph, procedure)) return EXCEPTED;
126 RelationInfo result = getResult();
128 procedure.execute(graph, result);
136 return RequestFlags.IMMEDIATE_UPDATE;
140 public void recompute(ReadGraphImpl graph) throws DatabaseException {
142 compute(graph, new InternalProcedure<RelationInfo>() {
145 public void execute(ReadGraphImpl graph, RelationInfo result) {
149 public void exception(ReadGraphImpl graph, Throwable t) {
150 throw new Error("Error in recompute.", t);