]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/DependentInstances3.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / DependentInstances3.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common.request;\r
13 \r
14 import java.util.Set;\r
15 \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
24 \r
25 import gnu.trove.map.hash.THashMap;\r
26 \r
27 public class DependentInstances3 extends ResourceRead<ResourceSetGraph> {\r
28 \r
29     private static final boolean DEBUG = false;\r
30 \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
34         }\r
35     };\r
36 \r
37         public DependentInstances3(Resource resource) {\r
38                 super(resource);\r
39         }\r
40 \r
41         @Override\r
42         public ResourceSetGraph perform(ReadGraph graph) throws DatabaseException {\r
43                 \r
44                 if(resource.equals(graph.getRootLibrary())) return new ResourceSetGraph(graph, resource);\r
45         \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
49                 \r
50         // This is a loop\r
51         THashMap<DependentInstances3, ResourceSetGraph> workingSet = workArea.get();\r
52         ResourceSetGraph result = workingSet.get(this);\r
53         if(result != null) return result;\r
54 \r
55         result = new ResourceSetGraph(graph, resource);\r
56         \r
57         workingSet.put(this, result);\r
58         \r
59         CollectionSupport cs = graph.getService(CollectionSupport.class);\r
60         Set<Resource> objects = cs.createSet();\r
61         \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
68                 }\r
69                 \r
70                 for(Resource object : objects ) {\r
71             if(DEBUG)\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
77                 }\r
78                 \r
79                 workingSet.remove(this);\r
80                 return result;\r
81                 \r
82         }\r
83 \r
84 }\r