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.common.request;
14 import java.util.Collection;
16 import java.util.concurrent.ConcurrentLinkedQueue;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.ProcedureBarrier;
21 import org.simantics.db.common.utils.Logger;
22 import org.simantics.db.procedure.AsyncMultiProcedure;
23 import org.simantics.db.procedure.AsyncProcedure;
25 final public class ObjectsWithTypeAsync extends ResourceAsyncRead3<Collection<Resource>> {
27 public ObjectsWithTypeAsync(Resource subject, Resource subrelationOf, Resource instanceOf) {
28 super(subject, subrelationOf, instanceOf);
32 public void perform(AsyncReadGraph graph, final AsyncProcedure<Collection<Resource>> procedure) {
34 final Collection<Resource> result = new ConcurrentLinkedQueue<Resource>();
35 final ProcedureBarrier<Collection<Resource>> ready = new ProcedureBarrier<Collection<Resource>>(1);
37 graph.forEachObject(resource, resource2, new AsyncMultiProcedure<Resource>() {
40 public void execute(AsyncReadGraph graph, final Resource object) {
42 ready.incrementAndGet();
44 graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {
47 public void execute(AsyncReadGraph graph, Set<Resource> types) {
49 if(types.contains(resource3)) {
53 ready.dec(graph, procedure, result);
58 public void exception(AsyncReadGraph graph, Throwable throwable) {
60 throwable.printStackTrace();
61 Logger.defaultLogError(throwable);
63 ready.except(throwable);
64 ready.dec(graph, procedure, result);
73 public void finished(AsyncReadGraph graph) {
75 ready.dec(graph, procedure, result);
80 public void exception(AsyncReadGraph graph, Throwable throwable) {
82 throwable.printStackTrace();
83 Logger.defaultLogError(throwable);
85 ready.except(throwable);
86 ready.dec(graph, procedure, result);
95 // public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
97 // Set<Resource> result = new HashSet<Resource>();
98 // for(Resource object : graph.getObjects(resource, resource2)) {
99 // if(graph.isInstanceOf(object, resource3)) result.add(object);