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;
20 final public class AssertedPredicates extends UnaryQuery<IntProcedure> {
22 AssertedPredicates(final int r) {
26 public static AssertedPredicates newInstance(final int r) {
27 return new AssertedPredicates(r);
31 final public void clearResult(QuerySupport support) {
32 setResult(new IntArray());
36 final public void setReady() {
38 IntArray v = (IntArray)getResult();
40 if(size == 0) setResult(IntArray.EMPTY);
45 final public void removeEntry(QueryProcessor provider) {
46 provider.cache.remove(this);
49 void computeInheritedAssertions(ReadGraphImpl graph, int type, final IntProcedure proc) throws DatabaseException {
51 QueryProcessor processor = graph.processor;
53 QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), this, null, new SyncIntProcedure() {
56 public void run(ReadGraphImpl graph) {
60 public void execute(ReadGraphImpl graph,int inh) throws DatabaseException {
64 QueryCache.runnerAssertedPredicates(graph, inh, AssertedPredicates.this, null, new IntProcedure() {
67 public void execute(ReadGraphImpl graph, int ass) {
72 public void finished(ReadGraphImpl graph) throws DatabaseException {
77 public void exception(ReadGraphImpl graph, Throwable t) {
85 public void finished(ReadGraphImpl graph) throws DatabaseException {
94 public Object compute(ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
96 QueryProcessor processor = graph.processor;
98 computeInheritedAssertions(graph, id, proc);
100 QueryCache.runnerDirectObjects(graph, id, processor.getAsserts(), this, null, new IntProcedure() {
103 public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
105 QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), AssertedPredicates.this, null, new IntProcedure() {
108 public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
110 addOrSetHiding(graph, pred, AssertedPredicates.this);
116 public void finished(ReadGraphImpl graph) {
120 public void exception(ReadGraphImpl graph, Throwable t) {
121 // proc.exception(graph, t);
129 public void finished(ReadGraphImpl graph) {
133 public void exception(ReadGraphImpl graph, Throwable t) {
134 // proc.exception(graph, t);
139 finish(graph, processor);
141 performFromCache(graph, proc);
148 public String toString() {
149 return "AssertedPredicates[" + id + "]";
152 final public void finish(ReadGraphImpl graph, QueryProcessor provider) {
162 synchronized private void addOrSet(int add) {
166 IntArray value = (IntArray)getResult();
171 synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) throws DatabaseException {
175 IntArray value = (IntArray)getResult();
176 RelationInfo ri = QueryCacheBase.resultRelationInfoQuery(graph, add, parent, null);
177 if(ri.isFunctional) {
178 // Replace existing functional predicate if found
180 IntSet supers = QueryCache.resultSuperRelations(graph, add, parent, null);
181 if(value.data == null) {
182 if(value.sizeOrData != IntArray.NO_DATA) {
183 if(supers.contains(value.sizeOrData)) {
184 value.sizeOrData = add;
189 for(int i = 0;i < value.sizeOrData ; i++) {
190 if(supers.contains(value.data[i])) {
196 } catch (Throwable e) {
202 // No replacement - append
208 public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
212 if(handleException(graph, procedure)) return EXCEPTED;
214 final IntArray value = (IntArray)getResult();
215 if(value.data == null) {
216 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
218 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
221 procedure.finished(graph);
228 public void recompute(ReadGraphImpl graph) throws DatabaseException {
230 compute(graph, new IntProcedureAdapter() {
233 public void finished(ReadGraphImpl graph) {
237 public void exception(ReadGraphImpl graph, Throwable t) {
238 new Error("Error in recompute.", t).printStackTrace();