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