]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntitySubgraphExtent.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / EntitySubgraphExtent.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntitySubgraphExtent.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntitySubgraphExtent.java
new file mode 100644 (file)
index 0000000..fb17efe
--- /dev/null
@@ -0,0 +1,276 @@
+/*******************************************************************************\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