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.layer0.adapter.impl;
\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
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
33 public class EntitySubgraphExtent extends TypeSubgraphExtent {
\r
35 public static boolean DEBUG = false;
\r
37 static class DefinedRelationSet extends ResourceAsyncMultiRead<Resource> {
\r
39 public DefinedRelationSet(Resource type) {
\r
43 public void forType(AsyncReadGraph graph, Resource type, final AsyncMultiProcedure<Resource> procedure, final AtomicInteger ready) {
\r
45 final Layer0 l0 = graph.getService(Layer0.class);
\r
47 ready.incrementAndGet();
\r
49 graph.forEachObject(type, l0.HasPropertyDefinition, new AsyncMultiProcedure<Resource>() {
\r
52 public void execute(AsyncReadGraph graph, Resource def) {
\r
54 ready.incrementAndGet();
\r
56 graph.forPossibleObject(def, l0.ConcernsRelation, new AsyncProcedure<Resource>() {
\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
65 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
66 throwable.printStackTrace();
\r
74 public void finished(AsyncReadGraph graph) {
\r
76 if(ready.decrementAndGet() == 0) procedure.finished(graph);
\r
81 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
82 throwable.printStackTrace();
\r
90 public void perform(AsyncReadGraph graph, final AsyncMultiProcedure<Resource> procedure) {
\r
92 final AtomicInteger ready = new AtomicInteger(1);
\r
94 forType(graph, resource, procedure, ready);
\r
96 graph.forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
\r
99 public void execute(AsyncReadGraph graph, Set<Resource> types) {
\r
101 for(Resource type : types) forType(graph, type, procedure, ready);
\r
103 if(ready.decrementAndGet() == 0) procedure.finished(graph);
\r
108 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
109 throwable.printStackTrace();
\r
119 public void accept(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<Classifier> procedure, final Callback callback) {
\r
121 final AtomicInteger ready = new AtomicInteger(1);
\r
123 class Result extends TransientCacheAsyncMultiListener<Resource> implements Classifier {
\r
125 final ArrayList<Resource> predicates = new ArrayList<Resource>();
\r
128 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
129 throwable.printStackTrace();
\r
133 public void execute(AsyncReadGraph graph, Resource predicate) {
\r
134 synchronized(predicates) {
\r
135 predicates.add(predicate);
\r
140 public void finished(AsyncReadGraph graph) {
\r
141 if(ready.decrementAndGet() == 0) procedure.execute(graph, this);
\r
145 public void classify(AsyncReadGraph graph, final Statement statement, ExtentStatus objectExtent, final Callback callback) {
\r
148 graph.asyncRequest(new ReadRequest() {
\r
151 public void run(ReadGraph graph) throws DatabaseException {
\r
152 System.out.println("classify " + NameUtils.toString(graph, statement));
\r
159 final Resource p = statement.getPredicate();
\r
160 if(graph.getService(Layer0.class).PartOf.equals(p) && !ExtentStatus.INTERNAL.equals(objectExtent)) return;
\r
162 if(ExtentStatus.EXCLUDED.equals(objectExtent)) return;
\r
164 if(predicates.contains(p)) {
\r
165 callback.statement(statement, true);
\r
168 graph.asyncRequest(new ReadRequest() {
\r
171 public void run(ReadGraph graph) throws DatabaseException {
\r
172 System.out.println("-accept " + NameUtils.toString(graph, statement));
\r
181 graph.forDirectSuperrelations(p, new AsyncMultiProcedure<Resource>() {
\r
184 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
185 throwable.printStackTrace();
\r
189 public void execute(AsyncReadGraph graph, final Resource superRelation) {
\r
192 if(predicates.contains(superRelation)) {
\r
193 callback.statement(statement, true);
\r
196 graph.asyncRequest(new ReadRequest() {
\r
199 public void run(ReadGraph graph) throws DatabaseException {
\r
200 System.out.println("-accept " + NameUtils.toString(graph, statement));
\r
209 graph.asyncRequest(new Superrelations(superRelation), new AsyncProcedure<Set<Resource>>() {
\r
212 public void exception(AsyncReadGraph graph, Throwable t) {
\r
213 t.printStackTrace();
\r
217 public void execute(AsyncReadGraph graph, Set<Resource> supers) {
\r
219 if(!Collections.disjoint(supers, predicates)) {
\r
222 graph.asyncRequest(new ReadRequest() {
\r
225 public void run(ReadGraph graph) throws DatabaseException {
\r
226 System.out.println("-accept " + NameUtils.toString(graph, statement));
\r
232 callback.statement(statement, true);
\r
243 public void finished(AsyncReadGraph graph) {
\r
252 final Result result = new Result();
\r
254 graph.forEachPrincipalType(resource, new AsyncMultiProcedure<Resource>() {
\r
257 public void execute(AsyncReadGraph graph, Resource type) {
\r
258 ready.incrementAndGet();
\r
259 graph.asyncRequest(new DefinedRelationSet(type), result);
\r
263 public void finished(AsyncReadGraph graph) {
\r
264 if(ready.decrementAndGet() == 0) procedure.execute(graph, result);
\r
268 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
269 throwable.printStackTrace();
\r