--- /dev/null
+/*******************************************************************************\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.common.request;\r
+\r
+import java.util.Set;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.CollectionSupport;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.Development;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+public class DependentInstances3 extends ResourceRead<ResourceSetGraph> {\r
+\r
+ private static final boolean DEBUG = false;\r
+\r
+ private static ThreadLocal<THashMap<DependentInstances3, ResourceSetGraph>> workArea = new ThreadLocal<THashMap<DependentInstances3, ResourceSetGraph>>() {\r
+ protected gnu.trove.map.hash.THashMap<DependentInstances3,ResourceSetGraph> initialValue() {\r
+ return new THashMap<>();\r
+ }\r
+ };\r
+\r
+ public DependentInstances3(Resource resource) {\r
+ super(resource);\r
+ }\r
+\r
+ @Override\r
+ public ResourceSetGraph perform(ReadGraph graph) throws DatabaseException {\r
+ \r
+ if(resource.equals(graph.getRootLibrary())) return new ResourceSetGraph(graph, resource);\r
+ \r
+ if(Development.DEVELOPMENT)\r
+ if(graph.isImmutable(resource))\r
+ System.err.println("Immutable resource in DependentInstances3: " + NameUtils.getSafeName(graph, resource, true));\r
+ \r
+ // This is a loop\r
+ THashMap<DependentInstances3, ResourceSetGraph> workingSet = workArea.get();\r
+ ResourceSetGraph result = workingSet.get(this);\r
+ if(result != null) return result;\r
+\r
+ result = new ResourceSetGraph(graph, resource);\r
+ \r
+ workingSet.put(this, result);\r
+ \r
+ CollectionSupport cs = graph.getService(CollectionSupport.class);\r
+ Set<Resource> objects = cs.createSet();\r
+ \r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ for(Statement dep : graph.getStatements(resource, L0.IsDependencyOf)) {\r
+ if(graph.isSubrelationOf(dep.getPredicate(), L0.HasPrevious)) continue;\r
+ Resource object = dep.getObject();\r
+ if(graph.isImmutable(object)) continue;\r
+ objects.add(object);\r
+ }\r
+ \r
+ for(Resource object : objects ) {\r
+ if(DEBUG)\r
+ System.err.println(NameUtils.getSafeName(graph, object, true) + " depends on " + NameUtils.getSafeName(graph, resource, true));\r
+ DependentInstances3 query = new DependentInstances3(object);\r
+ ResourceSetGraph instances = workingSet.get(query);\r
+ if(instances == null) instances = graph.syncRequest(query);\r
+ result.references.add(instances);\r
+ }\r
+ \r
+ workingSet.remove(this);\r
+ return result;\r
+ \r
+ }\r
+\r
+}\r