X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Frequest%2FPossibleModelInstances.java;h=ca57f318a94d51ec45a3fce2793230e56ef447ca;hp=7c09dc9a8412fb705d9fc3cceacde6e4ac9b0c7b;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleModelInstances.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleModelInstances.java index 7c09dc9a8..ca57f318a 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleModelInstances.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PossibleModelInstances.java @@ -1,73 +1,73 @@ -package org.simantics.db.layer0.request; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.simantics.databoard.Bindings; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ResourceRead2; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.Instances; -import org.simantics.layer0.Layer0; -import org.simantics.simulation.ontology.SimulationResource; - -/** - * Returns a Name->Resource map for instances of given type in a given model. - * - * Uses Dependencies-index for obtaining instances. Uses L0.HasName for - * obtaining name. - * - * @author Antti Villberg - */ -public class PossibleModelInstances extends ResourceRead2> { - - /** - * @parameter modelPart is a Resource with URI under a model e.g. model can - * be found using L0.PartOf - * @parameter type is the type to search instances for. The query returns - * properly all instances of all subtypes. - */ - public PossibleModelInstances(Resource modelPart, Resource type) { - super(modelPart, type); - } - - private Resource getModel(ReadGraph graph) throws DatabaseException { - SimulationResource SIMU = SimulationResource.getInstance(graph); - if (graph.isInstanceOf(resource, SIMU.Model)) { - return resource; - } else { - return graph.sync(new PossibleModel(resource)); - } - } - - @Override - public Map perform(ReadGraph graph) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - - Instances query = graph.getPossibleAdapter(resource2, Instances.class); - if (query == null) - return Collections.emptyMap(); - - Resource model = getModel(graph); - if (model == null) - return Collections.emptyMap(); - - Collection queryResult = query.find(graph, model); - if (queryResult.isEmpty()) - return Collections.emptyMap(); - - Map result = new HashMap(queryResult.size()); - for(Resource instance : queryResult) { - String name = graph.getPossibleRelatedValue(instance, L0.HasName, Bindings.STRING); - if (name != null) - result.put(name, instance); - } - return result; - - } - -} +package org.simantics.db.layer0.request; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ResourceRead2; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.Instances; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; + +/** + * Returns a Name->Resource map for instances of given type in a given model. + * + * Uses Dependencies-index for obtaining instances. Uses L0.HasName for + * obtaining name. + * + * @author Antti Villberg + */ +public class PossibleModelInstances extends ResourceRead2> { + + /** + * @parameter modelPart is a Resource with URI under a model e.g. model can + * be found using L0.PartOf + * @parameter type is the type to search instances for. The query returns + * properly all instances of all subtypes. + */ + public PossibleModelInstances(Resource modelPart, Resource type) { + super(modelPart, type); + } + + private Resource getModel(ReadGraph graph) throws DatabaseException { + SimulationResource SIMU = SimulationResource.getInstance(graph); + if (graph.isInstanceOf(resource, SIMU.Model)) { + return resource; + } else { + return graph.sync(new PossibleModel(resource)); + } + } + + @Override + public Map perform(ReadGraph graph) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + + Instances query = graph.getPossibleAdapter(resource2, Instances.class); + if (query == null) + return Collections.emptyMap(); + + Resource model = getModel(graph); + if (model == null) + return Collections.emptyMap(); + + Collection queryResult = query.find(graph, model); + if (queryResult.isEmpty()) + return Collections.emptyMap(); + + Map result = new HashMap(queryResult.size()); + for(Resource instance : queryResult) { + String name = graph.getPossibleRelatedValue(instance, L0.HasName, Bindings.STRING); + if (name != null) + result.put(name, instance); + } + return result; + + } + +}