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 final static InternalProcedure<RelationInfo> ip = new InternalProcedure<RelationInfo>() {
166 public void execute(ReadGraphImpl graph, RelationInfo result) {
170 public void exception(ReadGraphImpl graph, Throwable throwable) {
175 final static InternalProcedure<IntSet> ip2 = new InternalProcedure<IntSet>() {
178 public void execute(ReadGraphImpl graph, IntSet result) {
182 public void exception(ReadGraphImpl graph, Throwable throwable) {
187 synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) throws DatabaseException {
191 IntArray value = (IntArray)getResult();
192 RelationInfo ri = QueryCacheBase.resultRelationInfoQuery(graph, add, parent, null, ip);
193 if(ri.isFunctional) {
194 // Replace existing functional predicate if found
196 IntSet supers = QueryCache.resultSuperRelations(graph, add, parent, null, ip2);
197 if(value.data == null) {
198 if(value.sizeOrData != IntArray.NO_DATA) {
199 if(supers.contains(value.sizeOrData)) {
200 value.sizeOrData = add;
205 for(int i = 0;i < value.sizeOrData ; i++) {
206 if(supers.contains(value.data[i])) {
212 } catch (Throwable e) {
218 // No replacement - append
224 public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
228 if(handleException(graph, procedure)) return EXCEPTED;
230 final IntArray value = (IntArray)getResult();
231 if(value.data == null) {
232 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
234 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
237 procedure.finished(graph);
244 public void recompute(ReadGraphImpl graph) throws DatabaseException {
246 compute(graph, new IntProcedureAdapter() {
249 public void finished(ReadGraphImpl graph) {
253 public void exception(ReadGraphImpl graph, Throwable t) {
254 new Error("Error in recompute.", t).printStackTrace();