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.common.request;
\r
14 import java.util.Set;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.Statement;
\r
19 import org.simantics.db.common.utils.NameUtils;
\r
20 import org.simantics.db.exception.DatabaseException;
\r
21 import org.simantics.db.service.CollectionSupport;
\r
22 import org.simantics.layer0.Layer0;
\r
23 import org.simantics.utils.Development;
\r
25 import gnu.trove.map.hash.THashMap;
\r
27 public class DependentInstances3 extends ResourceRead<ResourceSetGraph> {
\r
29 private static final boolean DEBUG = false;
\r
31 private static ThreadLocal<THashMap<DependentInstances3, ResourceSetGraph>> workArea = new ThreadLocal<THashMap<DependentInstances3, ResourceSetGraph>>() {
\r
32 protected gnu.trove.map.hash.THashMap<DependentInstances3,ResourceSetGraph> initialValue() {
\r
33 return new THashMap<>();
\r
37 public DependentInstances3(Resource resource) {
\r
42 public ResourceSetGraph perform(ReadGraph graph) throws DatabaseException {
\r
44 if(resource.equals(graph.getRootLibrary())) return new ResourceSetGraph(graph, resource);
\r
46 if(Development.DEVELOPMENT)
\r
47 if(graph.isImmutable(resource))
\r
48 System.err.println("Immutable resource in DependentInstances3: " + NameUtils.getSafeName(graph, resource, true));
\r
51 THashMap<DependentInstances3, ResourceSetGraph> workingSet = workArea.get();
\r
52 ResourceSetGraph result = workingSet.get(this);
\r
53 if(result != null) return result;
\r
55 result = new ResourceSetGraph(graph, resource);
\r
57 workingSet.put(this, result);
\r
59 CollectionSupport cs = graph.getService(CollectionSupport.class);
\r
60 Set<Resource> objects = cs.createSet();
\r
62 Layer0 L0 = Layer0.getInstance(graph);
\r
63 for(Statement dep : graph.getStatements(resource, L0.IsDependencyOf)) {
\r
64 if(graph.isSubrelationOf(dep.getPredicate(), L0.HasPrevious)) continue;
\r
65 Resource object = dep.getObject();
\r
66 if(graph.isImmutable(object)) continue;
\r
67 objects.add(object);
\r
70 for(Resource object : objects ) {
\r
72 System.err.println(NameUtils.getSafeName(graph, object, true) + " depends on " + NameUtils.getSafeName(graph, resource, true));
\r
73 DependentInstances3 query = new DependentInstances3(object);
\r
74 ResourceSetGraph instances = workingSet.get(query);
\r
75 if(instances == null) instances = graph.syncRequest(query);
\r
76 result.references.add(instances);
\r
79 workingSet.remove(this);
\r