package org.simantics.modeling; import java.util.Collections; import java.util.HashSet; import java.util.Set; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.QueryIndexUtils; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.db.request.Read; import org.simantics.project.exception.ProjectException; import org.simantics.project.features.AbstractProjectFeature; import org.simantics.scl.runtime.function.Function1; import org.simantics.scl.runtime.tuple.Tuple0; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LifeCycleProcesses extends AbstractProjectFeature { private static final Logger LOGGER = LoggerFactory.getLogger(LifeCycleProcesses.class); Set contexts = Collections.emptySet(); @Override public void configure() throws ProjectException { try { this.contexts = Simantics.getSession().syncRequest(new Read>() { @Override public Set perform(ReadGraph graph) throws DatabaseException { Set contexts = new HashSet<>(); ModelingResources MOD = ModelingResources.getInstance(graph); for(Resource indexRoot : Layer0Utils.listIndexRoots(graph)) { for(Resource lcp : QueryIndexUtils.searchByTypeShallow(graph, indexRoot, MOD.LifeCycleProcess)) { LOGGER.trace("Loading life cycle process " + graph.getURI(lcp)); Function1 load = null; Function1 unload = null; Variable loadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_load); if (loadProperty != null) { load = loadProperty.getPossibleValue(graph); } Variable unloadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_unload); if (unloadProperty != null) { unload = unloadProperty.getPossibleValue(graph); } LifeCycleContext lcc = new LifeCycleContext(lcp, load, unload); contexts.add(lcc); } } return contexts; } }); contexts.forEach(LifeCycleContext::load); } catch (DatabaseException e) { throw new ProjectException(e); } } @Override public void deconfigure() throws ProjectException { contexts.forEach(LifeCycleContext::unload); } }