1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
14 import java.util.ArrayList;
15 import java.util.Collections;
17 import java.util.concurrent.atomic.AtomicInteger;
19 import org.simantics.db.AsyncReadGraph;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Statement;
23 import org.simantics.db.common.primitiverequest.Superrelations;
24 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncMultiListener;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.common.request.ResourceAsyncMultiRead;
27 import org.simantics.db.common.utils.NameUtils;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.procedure.AsyncMultiProcedure;
30 import org.simantics.db.procedure.AsyncProcedure;
31 import org.simantics.layer0.Layer0;
33 public class EntitySubgraphExtent extends TypeSubgraphExtent {
35 public static boolean DEBUG = false;
37 static class DefinedRelationSet extends ResourceAsyncMultiRead<Resource> {
39 public DefinedRelationSet(Resource type) {
43 public void forType(AsyncReadGraph graph, Resource type, final AsyncMultiProcedure<Resource> procedure, final AtomicInteger ready) {
45 final Layer0 l0 = graph.getService(Layer0.class);
47 ready.incrementAndGet();
49 graph.forEachObject(type, l0.HasPropertyDefinition, new AsyncMultiProcedure<Resource>() {
52 public void execute(AsyncReadGraph graph, Resource def) {
54 ready.incrementAndGet();
56 graph.forPossibleObject(def, l0.ConcernsRelation, new AsyncProcedure<Resource>() {
59 public void execute(AsyncReadGraph graph, Resource relation) {
60 if(relation != null) procedure.execute(graph, relation);
61 if(ready.decrementAndGet() == 0) procedure.finished(graph);
65 public void exception(AsyncReadGraph graph, Throwable throwable) {
66 throwable.printStackTrace();
74 public void finished(AsyncReadGraph graph) {
76 if(ready.decrementAndGet() == 0) procedure.finished(graph);
81 public void exception(AsyncReadGraph graph, Throwable throwable) {
82 throwable.printStackTrace();
90 public void perform(AsyncReadGraph graph, final AsyncMultiProcedure<Resource> procedure) {
92 final AtomicInteger ready = new AtomicInteger(1);
94 forType(graph, resource, procedure, ready);
96 graph.forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
99 public void execute(AsyncReadGraph graph, Set<Resource> types) {
101 for(Resource type : types) forType(graph, type, procedure, ready);
103 if(ready.decrementAndGet() == 0) procedure.finished(graph);
108 public void exception(AsyncReadGraph graph, Throwable throwable) {
109 throwable.printStackTrace();
119 public void accept(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<Classifier> procedure, final Callback callback) {
121 final AtomicInteger ready = new AtomicInteger(1);
123 class Result extends TransientCacheAsyncMultiListener<Resource> implements Classifier {
125 final ArrayList<Resource> predicates = new ArrayList<Resource>();
128 public void exception(AsyncReadGraph graph, Throwable throwable) {
129 throwable.printStackTrace();
133 public void execute(AsyncReadGraph graph, Resource predicate) {
134 synchronized(predicates) {
135 predicates.add(predicate);
140 public void finished(AsyncReadGraph graph) {
141 if(ready.decrementAndGet() == 0) procedure.execute(graph, this);
145 public void classify(AsyncReadGraph graph, final Statement statement, ExtentStatus objectExtent, final Callback callback) {
148 graph.asyncRequest(new ReadRequest() {
151 public void run(ReadGraph graph) throws DatabaseException {
152 System.out.println("classify " + NameUtils.toString(graph, statement));
159 final Resource p = statement.getPredicate();
160 if(graph.getService(Layer0.class).PartOf.equals(p) && !ExtentStatus.INTERNAL.equals(objectExtent)) return;
162 if(ExtentStatus.EXCLUDED.equals(objectExtent)) return;
164 if(predicates.contains(p)) {
165 callback.statement(statement, true);
168 graph.asyncRequest(new ReadRequest() {
171 public void run(ReadGraph graph) throws DatabaseException {
172 System.out.println("-accept " + NameUtils.toString(graph, statement));
181 graph.forDirectSuperrelations(p, new AsyncMultiProcedure<Resource>() {
184 public void exception(AsyncReadGraph graph, Throwable throwable) {
185 throwable.printStackTrace();
189 public void execute(AsyncReadGraph graph, final Resource superRelation) {
192 if(predicates.contains(superRelation)) {
193 callback.statement(statement, true);
196 graph.asyncRequest(new ReadRequest() {
199 public void run(ReadGraph graph) throws DatabaseException {
200 System.out.println("-accept " + NameUtils.toString(graph, statement));
209 graph.asyncRequest(new Superrelations(superRelation), new AsyncProcedure<Set<Resource>>() {
212 public void exception(AsyncReadGraph graph, Throwable t) {
217 public void execute(AsyncReadGraph graph, Set<Resource> supers) {
219 if(!Collections.disjoint(supers, predicates)) {
222 graph.asyncRequest(new ReadRequest() {
225 public void run(ReadGraph graph) throws DatabaseException {
226 System.out.println("-accept " + NameUtils.toString(graph, statement));
232 callback.statement(statement, true);
243 public void finished(AsyncReadGraph graph) {
252 final Result result = new Result();
254 graph.forEachPrincipalType(resource, new AsyncMultiProcedure<Resource>() {
257 public void execute(AsyncReadGraph graph, Resource type) {
258 ready.incrementAndGet();
259 graph.asyncRequest(new DefinedRelationSet(type), result);
263 public void finished(AsyncReadGraph graph) {
264 if(ready.decrementAndGet() == 0) procedure.execute(graph, result);
268 public void exception(AsyncReadGraph graph, Throwable throwable) {
269 throwable.printStackTrace();