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 java.util.ArrayList;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21 import org.simantics.db.procedure.ListenerBase;
23 final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
25 public ArrayList<InternalProcedure<IntSet>> procs = null;
27 private DirectSuperRelations(final int resource) {
31 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
33 new DirectSuperRelations(r).computeForEach(graph, provider, procedure, false);
38 public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
43 public void putEntry(QueryProcessor provider) {
47 final public void removeEntry(QueryProcessor provider) {
52 private TIntHashSet set = null;
53 public int single = 0;
55 public boolean add(int val) {
56 if(single == val) return false;
61 if(set == null) set = new TIntHashSet(4);
67 if(single == 0) return 0;
68 if(set == null) return 1;
69 return set.size() + 1;
73 // public int[] toArray() {
75 // int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
76 // result[set.size()] = single;
82 public void forEach(TIntProcedure proc) {
83 if(single > 0) proc.execute(single);
84 if(set != null) set.forEach(proc);
90 public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
92 provider.querySupport.ensureLoaded(graph, id);
94 int single = provider.querySupport.getSingleSuperrelation(id);
96 procedure.execute(graph, single);
97 procedure.finished(graph);
101 final int subrelationOf = provider.getSubrelationOf();
103 final IntSet result = new IntSet(provider.querySupport);
105 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
107 final public boolean execute(int r) {
112 final public void execute(ReadGraphImpl graph, int r) {
120 public void finished(ReadGraphImpl graph) {
123 public void exception(ReadGraphImpl graph, Throwable t) {
124 throw new Error("Errors are not supported.", t);
129 final DirectProcedure directProc = new DirectProcedure();
131 provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
133 int size = directProc.size();
137 procedure.finished(graph);
139 } else if (size == 1) {
141 procedure.execute(graph, directProc.single);
142 procedure.finished(graph);
146 directProc.forEach(new TIntProcedure() {
149 public boolean execute(int arg0) {
151 procedure.execute(graph, arg0);
158 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) {