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.common.utils.Logger;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.IntProcedureAdapter;
19 import gnu.trove.procedure.TIntProcedure;
20 import gnu.trove.set.hash.TIntHashSet;
22 final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
24 DirectSuperRelations(final int resource) {
29 final public void removeEntry(QueryProcessor provider) {
30 provider.cache.remove(this);
35 private TIntHashSet set = null;
36 public int single = 0;
38 public boolean add(int val) {
39 if(single == val) return false;
44 if(set == null) set = new TIntHashSet(4);
50 if(single == 0) return 0;
51 if(set == null) return 1;
52 return set.size() + 1;
56 public void forEach(TIntProcedure proc) {
57 if(single > 0) proc.execute(single);
58 if(set != null) set.forEach(proc);
64 public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
66 QueryProcessor processor = graph.processor;
68 processor.querySupport.ensureLoaded(graph, id);
70 int single = processor.querySupport.getSingleSuperrelation(id);
72 procedure.execute(graph, single);
73 procedure.finished(graph);
77 final int subrelationOf = processor.getSubrelationOf();
79 final IntSet result = new IntSet(processor.querySupport);
81 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
83 final public boolean execute(int r) {
88 final public void execute(ReadGraphImpl graph, int r) {
96 public void finished(ReadGraphImpl graph) {
99 public void exception(ReadGraphImpl graph, Throwable t) {
100 throw new Error("Errors are not supported.", t);
105 final DirectProcedure directProc = new DirectProcedure();
107 processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
109 int size = directProc.size();
113 procedure.finished(graph);
115 } else if (size == 1) {
117 procedure.execute(graph, directProc.single);
118 procedure.finished(graph);
122 directProc.forEach(new TIntProcedure() {
125 public boolean execute(int arg0) {
128 procedure.execute(graph, arg0);
129 } catch (DatabaseException e) {
130 Logger.defaultLogError(e);
138 procedure.finished(graph);
148 public String toString() {
149 return "DirectSuperRelations[" + id + "]";
153 public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
157 return compute(graph, procedure);
162 public void recompute(ReadGraphImpl graph) throws DatabaseException {
164 compute(graph, new IntProcedureAdapter() {
167 public void finished(ReadGraphImpl graph) {
171 public void exception(ReadGraphImpl graph, Throwable t) {
172 new Error("Error in recompute.", t).printStackTrace();