1 package org.simantics.modeling;
3 import java.util.Collections;
4 import java.util.HashSet;
7 import org.simantics.Simantics;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.QueryIndexUtils;
12 import org.simantics.db.layer0.util.Layer0Utils;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.db.layer0.variable.Variables;
15 import org.simantics.db.request.Read;
16 import org.simantics.project.exception.ProjectException;
17 import org.simantics.project.features.AbstractProjectFeature;
18 import org.simantics.scl.runtime.function.Function1;
19 import org.simantics.scl.runtime.tuple.Tuple0;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class LifeCycleProcesses extends AbstractProjectFeature {
25 private static final Logger LOGGER = LoggerFactory.getLogger(LifeCycleProcesses.class);
27 Set<LifeCycleContext> contexts = Collections.emptySet();
30 public void configure() throws ProjectException {
32 this.contexts = Simantics.getSession().syncRequest(new Read<Set<LifeCycleContext>>() {
35 public Set<LifeCycleContext> perform(ReadGraph graph) throws DatabaseException {
36 Set<LifeCycleContext> contexts = new HashSet<>();
38 ModelingResources MOD = ModelingResources.getInstance(graph);
39 for(Resource indexRoot : Layer0Utils.listIndexRoots(graph)) {
40 for(Resource lcp : QueryIndexUtils.searchByTypeShallow(graph, indexRoot, MOD.LifeCycleProcess)) {
42 LOGGER.trace("Loading life cycle process " + graph.getURI(lcp));
43 Function1<LifeCycleContext,Tuple0> load = null;
44 Function1<LifeCycleContext,Tuple0> unload = null;
45 Variable loadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_load);
46 if (loadProperty != null) {
47 load = loadProperty.getPossibleValue(graph);
49 Variable unloadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_unload);
50 if (unloadProperty != null) {
51 unload = unloadProperty.getPossibleValue(graph);
54 LifeCycleContext lcc = new LifeCycleContext(lcp, load, unload);
64 contexts.forEach(LifeCycleContext::load);
66 } catch (DatabaseException e) {
67 throw new ProjectException(e);
72 public void deconfigure() throws ProjectException {
73 contexts.forEach(LifeCycleContext::unload);