]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntitySubgraphExtent.java
Sync git svn branch with SVN repository r33389.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / EntitySubgraphExtent.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.adapter.impl;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collections;\r
16 import java.util.Set;\r
17 import java.util.concurrent.atomic.AtomicInteger;\r
18 \r
19 import org.simantics.db.AsyncReadGraph;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.Statement;\r
23 import org.simantics.db.common.primitiverequest.Superrelations;\r
24 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncMultiListener;\r
25 import org.simantics.db.common.request.ReadRequest;\r
26 import org.simantics.db.common.request.ResourceAsyncMultiRead;\r
27 import org.simantics.db.common.utils.NameUtils;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.db.procedure.AsyncMultiProcedure;\r
30 import org.simantics.db.procedure.AsyncProcedure;\r
31 import org.simantics.layer0.Layer0;\r
32 \r
33 public class EntitySubgraphExtent extends TypeSubgraphExtent {\r
34 \r
35         public static boolean DEBUG = false;\r
36 \r
37         static class DefinedRelationSet extends ResourceAsyncMultiRead<Resource> {\r
38 \r
39                 public DefinedRelationSet(Resource type) {\r
40                         super(type);\r
41                 }\r
42 \r
43                 public void forType(AsyncReadGraph graph, Resource type, final AsyncMultiProcedure<Resource> procedure, final AtomicInteger ready) {\r
44 \r
45                         final Layer0 l0 = graph.getService(Layer0.class);\r
46 \r
47                         ready.incrementAndGet();\r
48 \r
49                         graph.forEachObject(type, l0.HasPropertyDefinition, new AsyncMultiProcedure<Resource>() {\r
50 \r
51                                 @Override\r
52                                 public void execute(AsyncReadGraph graph, Resource def) {\r
53 \r
54                                         ready.incrementAndGet();\r
55 \r
56                                         graph.forPossibleObject(def, l0.ConcernsRelation, new AsyncProcedure<Resource>() {\r
57 \r
58                                                 @Override\r
59                                                 public void execute(AsyncReadGraph graph, Resource relation) {\r
60                                                         if(relation != null) procedure.execute(graph, relation);\r
61                                                         if(ready.decrementAndGet() == 0) procedure.finished(graph);\r
62                                                 }\r
63 \r
64                                                 @Override\r
65                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
66                                                         throwable.printStackTrace();\r
67                                                 }\r
68 \r
69                                         });\r
70 \r
71                                 }\r
72 \r
73                                 @Override\r
74                                 public void finished(AsyncReadGraph graph) {\r
75 \r
76                                         if(ready.decrementAndGet() == 0) procedure.finished(graph);\r
77 \r
78                                 }\r
79 \r
80                                 @Override\r
81                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
82                                         throwable.printStackTrace();\r
83                                 }\r
84 \r
85                         });\r
86 \r
87                 }\r
88 \r
89                 @Override\r
90                 public void perform(AsyncReadGraph graph, final AsyncMultiProcedure<Resource> procedure) {\r
91 \r
92                         final AtomicInteger ready = new AtomicInteger(1);\r
93 \r
94                         forType(graph, resource, procedure, ready);\r
95 \r
96                         graph.forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {\r
97 \r
98                                 @Override\r
99                                 public void execute(AsyncReadGraph graph, Set<Resource> types) {\r
100 \r
101                                         for(Resource type : types) forType(graph, type, procedure, ready);\r
102 \r
103                                         if(ready.decrementAndGet() == 0) procedure.finished(graph);\r
104 \r
105                                 }\r
106 \r
107                                 @Override\r
108                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
109                                         throwable.printStackTrace();\r
110                                 }\r
111 \r
112                         });\r
113 \r
114                 }\r
115 \r
116         }\r
117 \r
118         @Override\r
119         public void accept(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<Classifier> procedure, final Callback callback) {\r
120 \r
121                 final AtomicInteger ready = new AtomicInteger(1);\r
122 \r
123                 class Result extends TransientCacheAsyncMultiListener<Resource> implements Classifier {\r
124 \r
125                         final ArrayList<Resource> predicates = new ArrayList<Resource>();\r
126 \r
127                         @Override\r
128                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
129                                 throwable.printStackTrace();\r
130                         }\r
131 \r
132                         @Override\r
133                         public void execute(AsyncReadGraph graph, Resource predicate) {\r
134                                 synchronized(predicates) {\r
135                                         predicates.add(predicate);\r
136                                 }\r
137                         }\r
138 \r
139                         @Override\r
140                         public void finished(AsyncReadGraph graph) {\r
141                                 if(ready.decrementAndGet() == 0) procedure.execute(graph, this);\r
142                         }\r
143 \r
144                         @Override\r
145                         public void classify(AsyncReadGraph graph, final Statement statement, ExtentStatus objectExtent, final Callback callback) {\r
146 \r
147                                 if(DEBUG) {\r
148                                         graph.asyncRequest(new ReadRequest() {\r
149 \r
150                                                 @Override\r
151                                                 public void run(ReadGraph graph) throws DatabaseException {\r
152                                                         System.out.println("classify " + NameUtils.toString(graph, statement));\r
153                                                 }\r
154 \r
155                                         });\r
156                                 }\r
157 \r
158                                 // TODO haxx\r
159                                 final Resource p = statement.getPredicate();\r
160                                 if(graph.getService(Layer0.class).PartOf.equals(p) && !ExtentStatus.INTERNAL.equals(objectExtent)) return; \r
161                                 // TODO haxx2\r
162                                 if(ExtentStatus.EXCLUDED.equals(objectExtent)) return;\r
163 \r
164                                 if(predicates.contains(p)) {\r
165                                         callback.statement(statement, true);\r
166 \r
167                                         if(DEBUG) {\r
168                                                 graph.asyncRequest(new ReadRequest() {\r
169 \r
170                                                         @Override\r
171                                                         public void run(ReadGraph graph) throws DatabaseException {\r
172                                                                 System.out.println("-accept " + NameUtils.toString(graph, statement));\r
173                                                         }\r
174 \r
175                                                 });\r
176                                         }\r
177 \r
178                                         return;\r
179                                 }\r
180 \r
181                                 graph.forDirectSuperrelations(p, new AsyncMultiProcedure<Resource>() {\r
182 \r
183                                         @Override\r
184                                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
185                                                 throwable.printStackTrace();\r
186                                         }\r
187 \r
188                                         @Override\r
189                                         public void execute(AsyncReadGraph graph, final Resource superRelation) {\r
190 \r
191 \r
192                                                 if(predicates.contains(superRelation)) {\r
193                                                         callback.statement(statement, true);\r
194 \r
195                                                         if(DEBUG) {\r
196                                                                 graph.asyncRequest(new ReadRequest() {\r
197 \r
198                                                                         @Override\r
199                                                                         public void run(ReadGraph graph) throws DatabaseException {\r
200                                                                                 System.out.println("-accept " + NameUtils.toString(graph, statement));\r
201                                                                         }\r
202 \r
203                                                                 });\r
204                                                         }\r
205 \r
206                                                         return;\r
207                                                 }\r
208 \r
209                                                 graph.asyncRequest(new Superrelations(superRelation), new AsyncProcedure<Set<Resource>>() {\r
210 \r
211                                                         @Override\r
212                                                         public void exception(AsyncReadGraph graph, Throwable t) {\r
213                                                                 t.printStackTrace();\r
214                                                         }\r
215 \r
216                                                         @Override\r
217                                                         public void execute(AsyncReadGraph graph, Set<Resource> supers) {\r
218 \r
219                                                                 if(!Collections.disjoint(supers, predicates)) {\r
220 \r
221                                                                         if(DEBUG) {\r
222                                                                                 graph.asyncRequest(new ReadRequest() {\r
223 \r
224                                                                                         @Override\r
225                                                                                         public void run(ReadGraph graph) throws DatabaseException {\r
226                                                                                                 System.out.println("-accept " + NameUtils.toString(graph, statement));\r
227                                                                                         }\r
228 \r
229                                                                                 });\r
230                                                                         }\r
231 \r
232                                                                         callback.statement(statement, true);\r
233 \r
234                                                                 }\r
235 \r
236                                                         }\r
237 \r
238                                                 });\r
239 \r
240                                         }\r
241 \r
242                                         @Override\r
243                                         public void finished(AsyncReadGraph graph) {\r
244                                         }\r
245 \r
246                                 });\r
247 \r
248                         }\r
249 \r
250                 };\r
251 \r
252                 final Result result = new Result();\r
253 \r
254                 graph.forEachPrincipalType(resource, new AsyncMultiProcedure<Resource>() {\r
255 \r
256                         @Override\r
257                         public void execute(AsyncReadGraph graph, Resource type) {\r
258                                 ready.incrementAndGet();\r
259                                 graph.asyncRequest(new DefinedRelationSet(type), result);\r
260                         }\r
261 \r
262                         @Override\r
263                         public void finished(AsyncReadGraph graph) {\r
264                                 if(ready.decrementAndGet() == 0) procedure.execute(graph, result);\r
265                         }\r
266 \r
267                         @Override\r
268                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
269                                 throwable.printStackTrace();\r
270                         }\r
271 \r
272                 });\r
273 \r
274         }\r
275 \r
276 }\r