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 {
45 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true);
46 if(entry != null) entry.setResult(result);
47 proc.execute(graph, result);
51 public void finished(ReadGraphImpl graph) throws DatabaseException {
54 RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
55 if(entry != null) entry.setResult(result);
56 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);
74 public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
76 QueryProcessor provider = graph.processor;
78 final int superRelationOf = provider.getSuperrelationOf();
79 assert(superRelationOf != 0);
81 IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null, QueryCache.emptyProcedureDirectPredicates);
82 IntSet types = QueryCache.resultTypes(graph, r, entry, null, QueryCache.emptyProcedureTypes);
84 computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
89 public Object compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
90 computeForEach(graph, id, this, procedure);
95 public String toString() {
96 return "RelationInfoQuery[" + id + "]";
99 // public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
101 // assert(isPending());
103 // synchronized(this) {
105 // setResult(result);
113 public void setResult(Object result) {
114 super.setResult(result);
115 if(!(result instanceof RelationInfo) && !(result == NO_RESULT))
116 System.err.println("foo");
121 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
125 if(handleException(graph, procedure)) return EXCEPTED;
127 RelationInfo result = getResult();
129 procedure.execute(graph, result);
137 return RequestFlags.IMMEDIATE_UPDATE;
141 public void recompute(ReadGraphImpl graph) throws DatabaseException {
143 compute(graph, new InternalProcedure<RelationInfo>() {
146 public void execute(ReadGraphImpl graph, RelationInfo result) {
150 public void exception(ReadGraphImpl graph, Throwable t) {
151 throw new Error("Error in recompute.", t);