1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import gnu.trove.procedure.TIntProcedure;
\r
15 import gnu.trove.set.hash.TIntHashSet;
\r
17 import java.util.ArrayList;
\r
19 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
20 import org.simantics.db.impl.procedure.InternalProcedure;
\r
21 import org.simantics.db.procedure.ListenerBase;
\r
23 final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
\r
25 public ArrayList<InternalProcedure<IntSet>> procs = null;
\r
27 private DirectSuperRelations(final int resource) {
\r
31 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
\r
33 new DirectSuperRelations(r).computeForEach(graph, provider, procedure, false);
\r
38 public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
\r
43 public void putEntry(QueryProcessor provider) {
\r
47 final public void removeEntry(QueryProcessor provider) {
\r
52 private TIntHashSet set = null;
\r
53 public int single = 0;
\r
55 public boolean add(int val) {
\r
56 if(single == val) return false;
\r
61 if(set == null) set = new TIntHashSet(4);
\r
62 return set.add(val);
\r
67 if(single == 0) return 0;
\r
68 if(set == null) return 1;
\r
69 return set.size() + 1;
\r
73 // public int[] toArray() {
\r
75 // int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
\r
76 // result[set.size()] = single;
\r
82 public void forEach(TIntProcedure proc) {
\r
83 if(single > 0) proc.execute(single);
\r
84 if(set != null) set.forEach(proc);
\r
90 public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
\r
92 provider.querySupport.ensureLoaded(graph, id);
\r
94 int single = provider.querySupport.getSingleSuperrelation(id);
\r
96 procedure.execute(graph, single);
\r
97 procedure.finished(graph);
\r
101 final int subrelationOf = provider.getSubrelationOf();
\r
103 final IntSet result = new IntSet(provider.querySupport);
\r
105 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
\r
107 final public boolean execute(int r) {
\r
112 final public void execute(ReadGraphImpl graph, int r) {
\r
120 public void finished(ReadGraphImpl graph) {
\r
123 public void exception(ReadGraphImpl graph, Throwable t) {
\r
124 throw new Error("Errors are not supported.", t);
\r
129 final DirectProcedure directProc = new DirectProcedure();
\r
131 provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
\r
133 int size = directProc.size();
\r
137 procedure.finished(graph);
\r
139 } else if (size == 1) {
\r
141 procedure.execute(graph, directProc.single);
\r
142 procedure.finished(graph);
\r
146 directProc.forEach(new TIntProcedure() {
\r
149 public boolean execute(int arg0) {
\r
151 procedure.execute(graph, arg0);
\r
158 procedure.finished(graph);
\r
162 return getResult();
\r
168 public String toString() {
\r
169 return "SuperRelations2[" + id + "]";
\r
173 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
\r
174 throw new UnsupportedOperationException();
\r
178 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
\r