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.IntProcedureAdapter;
18 import org.simantics.db.impl.procedure.InternalProcedure;
19 import org.simantics.db.procedure.ListenerBase;
22 final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
24 AssertedPredicates(final int r) {
28 public static AssertedPredicates newInstance(final int r) {
29 return new AssertedPredicates(r);
32 // final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
33 // QueryCache.runnerAssertedPredicates(graph, r, parent, listener, procedure);
37 final public void removeEntry(QueryProcessor provider) {
38 provider.cache.remove(this);
41 void computeInheritedAssertions(ReadGraphImpl graph, int type, final IntProcedure proc) throws DatabaseException {
43 QueryProcessor processor = graph.processor;
45 QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), this, null, new SyncIntProcedure() {
48 public void run(ReadGraphImpl graph) {
52 public void execute(ReadGraphImpl graph,int inh) throws DatabaseException {
56 QueryCache.runnerAssertedPredicates(graph, inh, AssertedPredicates.this, null, new IntProcedure() {
59 public void execute(ReadGraphImpl graph, int ass) {
64 public void finished(ReadGraphImpl graph) throws DatabaseException {
69 public void exception(ReadGraphImpl graph, Throwable t) {
77 public void finished(ReadGraphImpl graph) throws DatabaseException {
86 public Object compute(ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
88 QueryProcessor processor = graph.processor;
90 computeInheritedAssertions(graph, id, proc);
92 QueryCache.runnerDirectObjects(graph, id, processor.getAsserts(), this, null, new IntProcedure() {
95 public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
97 QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), AssertedPredicates.this, null, new IntProcedure() {
100 public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
102 addOrSetHiding(graph, pred, AssertedPredicates.this);
108 public void finished(ReadGraphImpl graph) {
112 public void exception(ReadGraphImpl graph, Throwable t) {
113 // proc.exception(graph, t);
121 public void finished(ReadGraphImpl graph) {
125 public void exception(ReadGraphImpl graph, Throwable t) {
126 // proc.exception(graph, t);
131 finish(graph, processor);
133 performFromCache(graph, proc);
140 public String toString() {
141 return "AssertedPredicates[" + id + "]";
144 final public void finish(ReadGraphImpl graph, QueryProcessor provider) {
154 synchronized private void addOrSet(int add) {
158 IntArray value = (IntArray)getResult();
163 synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) throws DatabaseException {
167 IntArray value = (IntArray)getResult();
168 RelationInfo ri = QueryCacheBase.resultRelationInfoQuery(graph, add, parent, null);
169 if(ri.isFunctional) {
170 // Replace existing functional predicate if found
172 IntSet supers = QueryCache.resultSuperRelations(graph, add, parent, null);
173 if(value.data == null) {
174 if(value.sizeOrData != IntArray.NO_DATA) {
175 if(supers.contains(value.sizeOrData)) {
176 value.sizeOrData = add;
181 for(int i = 0;i < value.sizeOrData ; i++) {
182 if(supers.contains(value.data[i])) {
188 } catch (Throwable e) {
194 // No replacement - append
200 public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
204 if(handleException(graph, procedure)) return EXCEPTED;
206 final IntArray value = (IntArray)getResult();
207 if(value.data == null) {
208 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
210 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
213 procedure.finished(graph);
220 public void recompute(ReadGraphImpl graph) throws DatabaseException {
222 compute(graph, new IntProcedureAdapter() {
225 public void finished(ReadGraphImpl graph) {
229 public void exception(ReadGraphImpl graph, Throwable t) {
230 new Error("Error in recompute.", t).printStackTrace();