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 gnu.trove.procedure.TIntProcedure;
15 import gnu.trove.set.hash.TIntHashSet;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.procedure.AsyncProcedure;
22 import org.simantics.db.procedure.ListenerBase;
24 final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
26 private PossibleSuperRelation(final int resource) {
30 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<Resource> procedure) {
32 int result = provider.querySupport.getSingleSuperrelation(r);
34 procedure.execute(graph, provider.querySupport.getResource(result));
36 procedure.execute(graph, null);
42 final public void removeEntry(QueryProcessor provider) {
47 private TIntHashSet set = null;
48 public int single = 0;
50 public boolean add(int val) {
51 if(single == val) return false;
56 if(set == null) set = new TIntHashSet(4);
61 if(single == 0) return 0;
62 if(set == null) return 1;
63 return set.size() + 1;
66 public void forEach(TIntProcedure proc) {
67 if(single > 0) proc.execute(single);
68 if(set != null) set.forEach(proc);
74 public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
76 QueryProcessor processor = graph.processor;
78 processor.querySupport.ensureLoaded(graph, id);
79 int single = processor.querySupport.getSingleSuperrelation(id);
81 procedure.execute(graph, single);
82 procedure.finished(graph);
86 final int subrelationOf = processor.getSubrelationOf();
88 final IntSet result = new IntSet(processor.querySupport);
90 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
92 final public boolean execute(int r) {
97 final public void execute(ReadGraphImpl graph, int r) {
105 public void finished(ReadGraphImpl graph) {
108 public void exception(ReadGraphImpl graph, Throwable t) {
109 throw new Error("Errors are not supported.", t);
114 final DirectProcedure directProc = new DirectProcedure();
116 processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
118 int size = directProc.size();
122 procedure.finished(graph);
124 } else if (size == 1) {
126 procedure.execute(graph, directProc.single);
127 procedure.finished(graph);
131 directProc.forEach(new TIntProcedure() {
134 public boolean execute(int arg0) {
137 procedure.execute(graph, arg0);
138 } catch (DatabaseException e) {
139 Logger.defaultLogError(e);
147 procedure.finished(graph);
156 public String toString() {
157 return "PossibleSuperRelation[" + id + "]";
161 public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) {
162 throw new UnsupportedOperationException();
166 public void recompute(ReadGraphImpl graph) {