]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ActiveModels.java
Merge "Removed extra modules OntologyModule imported to namespace" into release/1...
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / ActiveModels.java
1 package org.simantics.db.layer0.request;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.request.ObjectsWithType;
9 import org.simantics.db.common.request.ResourceRead;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.operation.Layer0X;
13 import org.simantics.simulation.ontology.SimulationResource;
14
15 /**
16  * Returns a set of active models.
17  * 
18  * @author Tuukka Lehtonen
19  */
20 public class ActiveModels extends ResourceRead<Collection<Resource>> {
21
22     public ActiveModels(Resource project) {
23         super(project);
24     }
25
26     @Override
27     public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
28         Layer0 L0 = Layer0.getInstance(graph);
29         Layer0X L0X = Layer0X.getInstance(graph);
30         SimulationResource SIMU = SimulationResource.getInstance(graph);
31         ArrayList<Resource> result = new ArrayList<Resource>();
32         for (Resource model : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, SIMU.Model))) {
33             if (graph.hasStatement(model, L0X.IsActivatedBy))
34                 result.add(model);
35         }
36         return result;
37     }
38     
39     public static Resource getPossibleActiveModel(ReadGraph graph, Resource project) throws DatabaseException {
40         Collection<Resource> models = graph.syncRequest(new ActiveModels(project));
41         if(models.isEmpty())
42             return null;
43         else
44             return models.iterator().next();
45     }
46
47 }