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.HashMap;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.ProcedureBarrier;
21 import org.simantics.db.procedure.AsyncMultiProcedure;
22 import org.simantics.db.procedure.AsyncProcedure;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.operation.Layer0X;
26 public class MappedTypedPartsAsync extends ResourceAsyncRead2<Map<Resource, Resource>> {
28 public MappedTypedPartsAsync(Resource realization, Resource type) {
29 super(realization, type);
33 public void perform(AsyncReadGraph graph, final AsyncProcedure<Map<Resource, Resource>> procedure) {
35 final Layer0 L0 = graph.getService(Layer0.class);
36 final Layer0X L0X = graph.getService(Layer0X.class);
37 final HashMap<Resource, Resource> result = new HashMap<Resource, Resource>();
38 final ProcedureBarrier<Map<Resource, Resource>> ready = new ProcedureBarrier<Map<Resource, Resource>>(1);
40 graph.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedure<Resource>() {
43 public void execute(AsyncReadGraph graph, final Resource object) {
45 ready.incrementAndGet();
47 graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {
50 public void execute(AsyncReadGraph graph, Set<Resource> types) {
52 if(types.contains(resource2)) {
54 graph.forEachObject(object, L0X.Represents, new AsyncMultiProcedure<Resource>() {
57 public void execute(AsyncReadGraph graph, Resource mapped) {
59 synchronized(result) {
60 result.put(mapped, object);
66 public void finished(AsyncReadGraph graph) {
68 ready.dec(graph, procedure, result);
73 public void exception(AsyncReadGraph graph, Throwable throwable) {
75 ready.except(throwable);
76 ready.dec(graph, procedure, result);
84 ready.dec(graph, procedure, result);
91 public void exception(AsyncReadGraph graph, Throwable throwable) {
93 ready.except(throwable);
94 ready.dec(graph, procedure, result);
103 public void finished(AsyncReadGraph graph) {
105 ready.dec(graph, procedure, result);
110 public void exception(AsyncReadGraph graph, Throwable throwable) {
112 ready.except(throwable);
113 ready.dec(graph, procedure, result);
122 // public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
124 // Builtins b = graph.getBuiltins();
125 // Map<Resource, Resource> result = new HashMap<Resource, Resource>();
127 // for(Resource part : graph.syncRequest(new ObjectsWithType(resource, b.ConsistsOf, resource2))) {
128 // Resource represents = graph.getPossibleObject(part, b.Represents);
129 // if(represents != null) result.put(represents, part);