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