]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/IndexedInstances.java
d37805403f77f59255db1442753d1751a44bbee2
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / IndexedInstances.java
1 package org.simantics.db.layer0.request;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Set;
8
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.ResourceRead2;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.operation.Layer0X;
15 import org.simantics.scl.runtime.function.Function;
16
17 public class IndexedInstances extends ResourceRead2<Set<Resource>> {
18
19         public IndexedInstances(Resource type, Resource indexRoot) {
20                 super(type, indexRoot);
21         }
22
23         @Override
24         public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
25                 
26         Layer0 L0 = Layer0.getInstance(graph);
27         Layer0X L0X = Layer0X.getInstance(graph);
28
29         String typeName = graph.getRelatedValue(resource, L0.HasName);
30
31         Function dependencies = graph.adapt(L0X.Dependencies, Function.class);
32         
33         Collection<Map<String, Object>> results = (Collection<Map<String, Object>>)dependencies.apply(graph, resource2, "Types:*" + typeName);
34         if (results == null)
35             return Collections.emptySet();
36
37         HashSet<Resource> result = new HashSet<Resource>(results.size());
38         for(Map<String, Object> entry : results) {
39             Resource res = (Resource)entry.get("Resource");
40             if(res != null) {
41                 if (graph.isInstanceOf(res, resource))
42                         result.add(res);
43             }
44         }
45         return result;
46                 
47         }
48
49 }