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.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.ListenerBase;
22 final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
24 private PossibleSuperRelation(final int resource) {
28 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<Resource> procedure) {
30 int result = provider.querySupport.getSingleSuperrelation(r);
32 procedure.execute(graph, provider.querySupport.getResource(result));
34 procedure.execute(graph, null);
40 public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
45 public void putEntry(QueryProcessor provider) {
49 final public void removeEntry(QueryProcessor provider) {
54 private TIntHashSet set = null;
55 public int single = 0;
57 public boolean add(int val) {
58 if(single == val) return false;
63 if(set == null) set = new TIntHashSet(4);
69 if(single == 0) return 0;
70 if(set == null) return 1;
71 return set.size() + 1;
75 // public int[] toArray() {
77 // int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
78 // result[set.size()] = single;
84 public void forEach(TIntProcedure proc) {
85 if(single > 0) proc.execute(single);
86 if(set != null) set.forEach(proc);
92 public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
94 provider.querySupport.ensureLoaded(graph, id);
95 int single = provider.querySupport.getSingleSuperrelation(id);
97 procedure.execute(graph, single);
98 procedure.finished(graph);
102 final int subrelationOf = provider.getSubrelationOf();
104 final IntSet result = new IntSet(provider.querySupport);
106 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
108 final public boolean execute(int r) {
113 final public void execute(ReadGraphImpl graph, int r) {
121 public void finished(ReadGraphImpl graph) {
124 public void exception(ReadGraphImpl graph, Throwable t) {
125 throw new Error("Errors are not supported.", t);
130 final DirectProcedure directProc = new DirectProcedure();
132 provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
134 int size = directProc.size();
138 procedure.finished(graph);
140 } else if (size == 1) {
142 procedure.execute(graph, directProc.single);
143 procedure.finished(graph);
147 directProc.forEach(new TIntProcedure() {
150 public boolean execute(int arg0) {
152 procedure.execute(graph, arg0);
159 procedure.finished(graph);
168 public String toString() {
169 return "SuperRelations2[" + id + "]";
173 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
174 throw new UnsupportedOperationException();
178 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {