1 package org.simantics.db.layer0.request;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import org.simantics.databoard.Bindings;
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.adapter.Instances;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.simulation.ontology.SimulationResource;
18 * Returns a Name->Resource map for instances of given type in a given model.
20 * Uses Dependencies-index for obtaining instances. Uses L0.HasName for
23 * @author Antti Villberg
25 public class PossibleModelInstances extends ResourceRead2<Map<String, Resource>> {
28 * @parameter modelPart is a Resource with URI under a model e.g. model can
29 * be found using L0.PartOf
30 * @parameter type is the type to search instances for. The query returns
31 * properly all instances of all subtypes.
33 public PossibleModelInstances(Resource modelPart, Resource type) {
34 super(modelPart, type);
37 private Resource getModel(ReadGraph graph) throws DatabaseException {
38 SimulationResource SIMU = SimulationResource.getInstance(graph);
39 if (graph.isInstanceOf(resource, SIMU.Model)) {
42 return graph.sync(new PossibleModel(resource));
47 public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
49 Layer0 L0 = Layer0.getInstance(graph);
51 Instances query = graph.getPossibleAdapter(resource2, Instances.class);
53 return Collections.emptyMap();
55 Resource model = getModel(graph);
57 return Collections.emptyMap();
59 Collection<Resource> queryResult = query.find(graph, model);
60 if (queryResult.isEmpty())
61 return Collections.emptyMap();
63 Map<String, Resource> result = new HashMap<String, Resource>(queryResult.size());
64 for(Resource instance : queryResult) {
65 String name = graph.getPossibleRelatedValue(instance, L0.HasName, Bindings.STRING);
67 result.put(name, instance);