-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.layer0.adapter.impl;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.Set;\r
-import java.util.concurrent.atomic.AtomicInteger;\r
-\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.common.primitiverequest.Superrelations;\r
-import org.simantics.db.common.procedure.adapter.TransientCacheAsyncMultiListener;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.common.request.ResourceAsyncMultiRead;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.procedure.AsyncMultiProcedure;\r
-import org.simantics.db.procedure.AsyncProcedure;\r
-import org.simantics.layer0.Layer0;\r
-\r
-public class EntitySubgraphExtent extends TypeSubgraphExtent {\r
-\r
- public static boolean DEBUG = false;\r
-\r
- static class DefinedRelationSet extends ResourceAsyncMultiRead<Resource> {\r
-\r
- public DefinedRelationSet(Resource type) {\r
- super(type);\r
- }\r
-\r
- public void forType(AsyncReadGraph graph, Resource type, final AsyncMultiProcedure<Resource> procedure, final AtomicInteger ready) {\r
-\r
- final Layer0 l0 = graph.getService(Layer0.class);\r
-\r
- ready.incrementAndGet();\r
-\r
- graph.forEachObject(type, l0.HasPropertyDefinition, new AsyncMultiProcedure<Resource>() {\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, Resource def) {\r
-\r
- ready.incrementAndGet();\r
-\r
- graph.forPossibleObject(def, l0.ConcernsRelation, new AsyncProcedure<Resource>() {\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, Resource relation) {\r
- if(relation != null) procedure.execute(graph, relation);\r
- if(ready.decrementAndGet() == 0) procedure.finished(graph);\r
- }\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- throwable.printStackTrace();\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
- @Override\r
- public void finished(AsyncReadGraph graph) {\r
-\r
- if(ready.decrementAndGet() == 0) procedure.finished(graph);\r
-\r
- }\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- throwable.printStackTrace();\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
- @Override\r
- public void perform(AsyncReadGraph graph, final AsyncMultiProcedure<Resource> procedure) {\r
-\r
- final AtomicInteger ready = new AtomicInteger(1);\r
-\r
- forType(graph, resource, procedure, ready);\r
-\r
- graph.forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, Set<Resource> types) {\r
-\r
- for(Resource type : types) forType(graph, type, procedure, ready);\r
-\r
- if(ready.decrementAndGet() == 0) procedure.finished(graph);\r
-\r
- }\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- throwable.printStackTrace();\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
- }\r
-\r
- @Override\r
- public void accept(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<Classifier> procedure, final Callback callback) {\r
-\r
- final AtomicInteger ready = new AtomicInteger(1);\r
-\r
- class Result extends TransientCacheAsyncMultiListener<Resource> implements Classifier {\r
-\r
- final ArrayList<Resource> predicates = new ArrayList<Resource>();\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- throwable.printStackTrace();\r
- }\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, Resource predicate) {\r
- synchronized(predicates) {\r
- predicates.add(predicate);\r
- }\r
- }\r
-\r
- @Override\r
- public void finished(AsyncReadGraph graph) {\r
- if(ready.decrementAndGet() == 0) procedure.execute(graph, this);\r
- }\r
-\r
- @Override\r
- public void classify(AsyncReadGraph graph, final Statement statement, ExtentStatus objectExtent, final Callback callback) {\r
-\r
- if(DEBUG) {\r
- graph.asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- System.out.println("classify " + NameUtils.toString(graph, statement));\r
- }\r
-\r
- });\r
- }\r
-\r
- // TODO haxx\r
- final Resource p = statement.getPredicate();\r
- if(graph.getService(Layer0.class).PartOf.equals(p) && !ExtentStatus.INTERNAL.equals(objectExtent)) return; \r
- // TODO haxx2\r
- if(ExtentStatus.EXCLUDED.equals(objectExtent)) return;\r
-\r
- if(predicates.contains(p)) {\r
- callback.statement(statement, true);\r
-\r
- if(DEBUG) {\r
- graph.asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- System.out.println("-accept " + NameUtils.toString(graph, statement));\r
- }\r
-\r
- });\r
- }\r
-\r
- return;\r
- }\r
-\r
- graph.forDirectSuperrelations(p, new AsyncMultiProcedure<Resource>() {\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- throwable.printStackTrace();\r
- }\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, final Resource superRelation) {\r
-\r
-\r
- if(predicates.contains(superRelation)) {\r
- callback.statement(statement, true);\r
-\r
- if(DEBUG) {\r
- graph.asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- System.out.println("-accept " + NameUtils.toString(graph, statement));\r
- }\r
-\r
- });\r
- }\r
-\r
- return;\r
- }\r
-\r
- graph.asyncRequest(new Superrelations(superRelation), new AsyncProcedure<Set<Resource>>() {\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable t) {\r
- t.printStackTrace();\r
- }\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, Set<Resource> supers) {\r
-\r
- if(!Collections.disjoint(supers, predicates)) {\r
-\r
- if(DEBUG) {\r
- graph.asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- System.out.println("-accept " + NameUtils.toString(graph, statement));\r
- }\r
-\r
- });\r
- }\r
-\r
- callback.statement(statement, true);\r
-\r
- }\r
-\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
- @Override\r
- public void finished(AsyncReadGraph graph) {\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
- };\r
-\r
- final Result result = new Result();\r
-\r
- graph.forEachPrincipalType(resource, new AsyncMultiProcedure<Resource>() {\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, Resource type) {\r
- ready.incrementAndGet();\r
- graph.asyncRequest(new DefinedRelationSet(type), result);\r
- }\r
-\r
- @Override\r
- public void finished(AsyncReadGraph graph) {\r
- if(ready.decrementAndGet() == 0) procedure.execute(graph, result);\r
- }\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- throwable.printStackTrace();\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.layer0.adapter.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.common.primitiverequest.Superrelations;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncMultiListener;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.ResourceAsyncMultiRead;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.layer0.Layer0;
+
+public class EntitySubgraphExtent extends TypeSubgraphExtent {
+
+ public static boolean DEBUG = false;
+
+ static class DefinedRelationSet extends ResourceAsyncMultiRead<Resource> {
+
+ public DefinedRelationSet(Resource type) {
+ super(type);
+ }
+
+ public void forType(AsyncReadGraph graph, Resource type, final AsyncMultiProcedure<Resource> procedure, final AtomicInteger ready) {
+
+ final Layer0 l0 = graph.getService(Layer0.class);
+
+ ready.incrementAndGet();
+
+ graph.forEachObject(type, l0.HasPropertyDefinition, new AsyncMultiProcedure<Resource>() {
+
+ @Override
+ public void execute(AsyncReadGraph graph, Resource def) {
+
+ ready.incrementAndGet();
+
+ graph.forPossibleObject(def, l0.ConcernsRelation, new AsyncProcedure<Resource>() {
+
+ @Override
+ public void execute(AsyncReadGraph graph, Resource relation) {
+ if(relation != null) procedure.execute(graph, relation);
+ if(ready.decrementAndGet() == 0) procedure.finished(graph);
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ });
+
+ }
+
+ @Override
+ public void finished(AsyncReadGraph graph) {
+
+ if(ready.decrementAndGet() == 0) procedure.finished(graph);
+
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ });
+
+ }
+
+ @Override
+ public void perform(AsyncReadGraph graph, final AsyncMultiProcedure<Resource> procedure) {
+
+ final AtomicInteger ready = new AtomicInteger(1);
+
+ forType(graph, resource, procedure, ready);
+
+ graph.forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
+
+ @Override
+ public void execute(AsyncReadGraph graph, Set<Resource> types) {
+
+ for(Resource type : types) forType(graph, type, procedure, ready);
+
+ if(ready.decrementAndGet() == 0) procedure.finished(graph);
+
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ });
+
+ }
+
+ }
+
+ @Override
+ public void accept(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<Classifier> procedure, final Callback callback) {
+
+ final AtomicInteger ready = new AtomicInteger(1);
+
+ class Result extends TransientCacheAsyncMultiListener<Resource> implements Classifier {
+
+ final ArrayList<Resource> predicates = new ArrayList<Resource>();
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, Resource predicate) {
+ synchronized(predicates) {
+ predicates.add(predicate);
+ }
+ }
+
+ @Override
+ public void finished(AsyncReadGraph graph) {
+ if(ready.decrementAndGet() == 0) procedure.execute(graph, this);
+ }
+
+ @Override
+ public void classify(AsyncReadGraph graph, final Statement statement, ExtentStatus objectExtent, final Callback callback) {
+
+ if(DEBUG) {
+ graph.asyncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ System.out.println("classify " + NameUtils.toString(graph, statement));
+ }
+
+ });
+ }
+
+ // TODO haxx
+ final Resource p = statement.getPredicate();
+ if(graph.getService(Layer0.class).PartOf.equals(p) && !ExtentStatus.INTERNAL.equals(objectExtent)) return;
+ // TODO haxx2
+ if(ExtentStatus.EXCLUDED.equals(objectExtent)) return;
+
+ if(predicates.contains(p)) {
+ callback.statement(statement, true);
+
+ if(DEBUG) {
+ graph.asyncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ System.out.println("-accept " + NameUtils.toString(graph, statement));
+ }
+
+ });
+ }
+
+ return;
+ }
+
+ graph.forDirectSuperrelations(p, new AsyncMultiProcedure<Resource>() {
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, final Resource superRelation) {
+
+
+ if(predicates.contains(superRelation)) {
+ callback.statement(statement, true);
+
+ if(DEBUG) {
+ graph.asyncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ System.out.println("-accept " + NameUtils.toString(graph, statement));
+ }
+
+ });
+ }
+
+ return;
+ }
+
+ graph.asyncRequest(new Superrelations(superRelation), new AsyncProcedure<Set<Resource>>() {
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable t) {
+ t.printStackTrace();
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, Set<Resource> supers) {
+
+ if(!Collections.disjoint(supers, predicates)) {
+
+ if(DEBUG) {
+ graph.asyncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ System.out.println("-accept " + NameUtils.toString(graph, statement));
+ }
+
+ });
+ }
+
+ callback.statement(statement, true);
+
+ }
+
+ }
+
+ });
+
+ }
+
+ @Override
+ public void finished(AsyncReadGraph graph) {
+ }
+
+ });
+
+ }
+
+ };
+
+ final Result result = new Result();
+
+ graph.forEachPrincipalType(resource, new AsyncMultiProcedure<Resource>() {
+
+ @Override
+ public void execute(AsyncReadGraph graph, Resource type) {
+ ready.incrementAndGet();
+ graph.asyncRequest(new DefinedRelationSet(type), result);
+ }
+
+ @Override
+ public void finished(AsyncReadGraph graph) {
+ if(ready.decrementAndGet() == 0) procedure.execute(graph, result);
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ });
+
+ }
+
+}