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;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.ProcedureBarrier;
20 import org.simantics.db.procedure.AsyncMultiProcedure;
21 import org.simantics.db.procedure.AsyncProcedure;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.operation.Layer0X;
25 public class MappedPartsAsync extends ResourceAsyncRead<Map<Resource, Resource>> {
27 public MappedPartsAsync(Resource realization) {
32 public void perform(AsyncReadGraph graph, final AsyncProcedure<Map<Resource, Resource>> procedure) {
34 final Layer0 L0 = graph.getService(Layer0.class);
35 final Layer0X L0X = graph.getService(Layer0X.class);
36 final HashMap<Resource, Resource> result = new HashMap<Resource, Resource>();
37 final ProcedureBarrier<Map<Resource, Resource>> ready = new ProcedureBarrier<Map<Resource, Resource>>(1);
39 graph.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedure<Resource>() {
42 public void execute(AsyncReadGraph graph, final Resource object) {
44 ready.incrementAndGet();
46 graph.forEachObject(object, L0X.Represents, new AsyncMultiProcedure<Resource>() {
49 public void execute(AsyncReadGraph graph, Resource mapped) {
51 synchronized(result) {
52 result.put(mapped, object);
58 public void finished(AsyncReadGraph graph) {
60 ready.dec(graph, procedure, result);
65 public void exception(AsyncReadGraph graph, Throwable throwable) {
67 ready.except(throwable);
68 ready.dec(graph, procedure, result);
77 public void finished(AsyncReadGraph graph) {
79 ready.dec(graph, procedure, result);
84 public void exception(AsyncReadGraph graph, Throwable throwable) {
86 ready.except(throwable);
87 ready.dec(graph, procedure, result);
96 // public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
98 // Builtins b = graph.getBuiltins();
99 // Map<Resource, Resource> result = new HashMap<Resource, Resource>();
101 // for(Resource part : graph.syncRequest(new ObjectsWithType(resource, b.ConsistsOf, resource2))) {
102 // Resource represents = graph.getPossibleObject(part, b.Represents);
103 // if(represents != null) result.put(represents, part);