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