]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/MappedTypedPartsAsync.java
Automatically import also SCLMain modules of dependent index roots
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / MappedTypedPartsAsync.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.request;
13
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17
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;
25
26 public class MappedTypedPartsAsync extends ResourceAsyncRead2<Map<Resource, Resource>> {
27
28     public MappedTypedPartsAsync(Resource realization, Resource type) {
29         super(realization, type);
30     }
31     
32     @Override
33     public void perform(AsyncReadGraph graph, final AsyncProcedure<Map<Resource, Resource>> procedure) {
34
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);
39         
40         graph.forEachObject(resource, L0.ConsistsOf, new AsyncMultiProcedure<Resource>() {
41
42                         @Override
43                         public void execute(AsyncReadGraph graph, final Resource object) {
44                                 
45                                 ready.incrementAndGet();
46                                 
47                                 graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {
48                                         
49                                         @Override
50                                         public void execute(AsyncReadGraph graph, Set<Resource> types) {
51
52                                                 if(types.contains(resource2)) {
53                                                         
54                                                         graph.forEachObject(object, L0X.Represents, new AsyncMultiProcedure<Resource>() {
55
56                                                                 @Override
57                                                                 public void execute(AsyncReadGraph graph, Resource mapped) {
58                                                                         
59                                                                         synchronized(result) {
60                                                                                 result.put(mapped, object);
61                                                                         }
62                                                                         
63                                                                 }
64
65                                                                 @Override
66                                                                 public void finished(AsyncReadGraph graph) {
67
68                                                                         ready.dec(graph, procedure, result);
69                                                                         
70                                                                 }
71
72                                                                 @Override
73                                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
74                                                                         
75                                                                         ready.except(throwable);
76                                                                         ready.dec(graph, procedure, result);
77                                                                         
78                                                                 }
79                                                                 
80                                                         });
81                                                         
82                                                 } else {
83                                                         
84                                                         ready.dec(graph, procedure, result);
85
86                                                 }
87                                                 
88                                         }
89                                         
90                                         @Override
91                                         public void exception(AsyncReadGraph graph, Throwable throwable) {
92                                                 
93                                                 ready.except(throwable);
94                                                 ready.dec(graph, procedure, result);
95                                                 
96                                         }
97                                         
98                                 });
99                                 
100                         }
101
102                         @Override
103                         public void finished(AsyncReadGraph graph) {
104                                 
105                                 ready.dec(graph, procedure, result);
106                                 
107                         }
108
109                         @Override
110                         public void exception(AsyncReadGraph graph, Throwable throwable) {
111                                 
112                                 ready.except(throwable);
113                                 ready.dec(graph, procedure, result);
114                                 
115                         }
116                 
117         });
118         
119     }
120     
121 //    @Override
122 //    public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
123 //        
124 //        Builtins b = graph.getBuiltins();
125 //        Map<Resource, Resource> result = new HashMap<Resource, Resource>();
126 //        
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);
130 //        }
131 //        
132 //        return result;
133 //        
134 //    }
135
136 }