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%2FModelInstances.java;h=ce74f6630f9c146362af8ef9066ce5191636696f;hp=6b25f549839612c1e7d7eb6c5f4b6125d644e8d4;hb=a52f0324c9435755d3da3f053794c077f7d58213;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ModelInstances.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ModelInstances.java index 6b25f5498..ce74f6630 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ModelInstances.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ModelInstances.java @@ -1,72 +1,72 @@ -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.PossibleIndexRoot; -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 ModelInstances 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 ModelInstances(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 PossibleIndexRoot(resource)); - } - } - - @Override - public Map perform(ReadGraph graph) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - - Instances query = graph.adapt(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.getRelatedValue(instance, L0.HasName, Bindings.STRING); - 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.PossibleIndexRoot; +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 ModelInstances 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 ModelInstances(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 PossibleIndexRoot(resource)); + } + } + + @Override + public Map perform(ReadGraph graph) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + + Instances query = graph.adapt(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.getRelatedValue(instance, L0.HasName, Bindings.STRING); + result.put(name, instance); + } + return result; + + } + +}