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.common.request.ReadRequest;
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 {
-
- Set<LifeCycleContext> contexts = new HashSet<LifeCycleContext>();
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(LifeCycleProcesses.class);
+
+ Set<LifeCycleContext> contexts = Collections.emptySet();
@Override
public void configure() throws ProjectException {
-
try {
-
- Simantics.getSession().syncRequest(new ReadRequest() {
+ this.contexts = Simantics.getSession().syncRequest(new Read<Set<LifeCycleContext>>() {
@Override
- public void run(ReadGraph graph) throws DatabaseException {
+ public Set<LifeCycleContext> perform(ReadGraph graph) throws DatabaseException {
+ Set<LifeCycleContext> contexts = new HashSet<>();
ModelingResources MOD = ModelingResources.getInstance(graph);
for(Resource indexRoot : Layer0Utils.listIndexRoots(graph)) {
- for(Resource lcp : ModelingUtils.searchByTypeShallow(graph, indexRoot, MOD.LifeCycleProcess)) {
-
-// System.err.println("Loading life cycle process " + graph.getURI(lcp));
+ for(Resource lcp : QueryIndexUtils.searchByTypeShallow(graph, indexRoot, MOD.LifeCycleProcess)) {
+
+ LOGGER.trace("Loading life cycle process " + graph.getURI(lcp));
Function1<LifeCycleContext,Tuple0> load = null;
Function1<LifeCycleContext,Tuple0> unload = null;
Variable loadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_load);
LifeCycleContext lcc = new LifeCycleContext(lcp, load, unload);
contexts.add(lcc);
-
+
}
}
-
+
+ return contexts;
}
-
});
-
+
+ contexts.forEach(LifeCycleContext::load);
+
} catch (DatabaseException e) {
-
throw new ProjectException(e);
-
}
-
- for(LifeCycleContext context : contexts) {
- context.load();
- }
-
}
@Override
public void deconfigure() throws ProjectException {
-
- for(LifeCycleContext context : contexts) {
- context.unload();
- }
-
+ contexts.forEach(LifeCycleContext::unload);
}
-
+
}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2019 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.migration;
+
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.migration.MigrationState;
+import org.simantics.db.layer0.migration.MigrationStateKeys;
+import org.simantics.db.layer0.migration.MigrationStep;
+import org.simantics.db.layer0.migration.MigrationUtils;
+import org.simantics.db.layer0.migration.NullWriter;
+import org.simantics.db.request.Read;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.scl.compiler.commands.CommandSession;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
+
+/**
+ * Runs the SCL script associated with the migration step.
+ *
+ * @author Tuukka Lehtonen
+ * @since 1.41.0
+ */
+public class SCLScriptMigrationStep implements MigrationStep {
+
+ private String scriptId;
+ private String script;
+
+ public SCLScriptMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {
+ this.scriptId = NameUtils.getSafeName(graph, step);
+ this.script = graph.getRelatedValue(
+ step,
+ ModelingResources.getInstance(graph).Migration_SCLScriptMigrationStep_script,
+ Bindings.STRING);
+ }
+
+ @Override
+ public void applyTo(final IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
+ Collection<Resource> roots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
+ if (!roots.isEmpty()) {
+ PrintWriter log = MigrationUtils.getProperty(state, MigrationStateKeys.MESSAGE_LOG_WRITER, NullWriter.PRINT_INSTANCE);
+ runScript(monitor, roots, log);
+ }
+ }
+
+ private static class ReportingHandler extends AbstractSCLReportingHandler {
+ PrintWriter log;
+
+ public ReportingHandler(PrintWriter log) {
+ this.log = log;
+ }
+
+ @Override
+ public void print(String text) {
+ log.println(text);
+ }
+
+ @Override
+ public void printError(String error) {
+ log.println("ERROR: " + error);
+ }
+ }
+
+ private void runScript(IProgressMonitor monitor, Collection<Resource> roots, PrintWriter log) throws DatabaseException {
+ log.format("## Running SCL Script Migration Step `%s` ##%n", scriptId);
+ SCLReportingHandler rh = new ReportingHandler(log);
+ Map<Resource, String> rootNames = mapNames(roots);
+ for (Resource root : roots) {
+ log.format("### Running script for root `%s` ###%n", rootNames.get(root));
+ CommandSession session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, rh);
+ session.setVariable("root", Types.RESOURCE, root);
+ session.execute(new StringReader(script), rh);
+ }
+ }
+
+ private Map<Resource, String> mapNames(Collection<Resource> roots) throws DatabaseException {
+ return Simantics.getSession().syncRequest((Read<Map<Resource, String>>) graph -> mapNames(graph, roots));
+ }
+
+ private Map<Resource, String> mapNames(ReadGraph graph, Collection<Resource> roots) throws DatabaseException {
+ Map<Resource, String> map = new HashMap<>();
+ for (Resource r : roots)
+ map.put(r, NameUtils.getSafeName(graph, r));
+ return map;
+ }
+
+}
\ No newline at end of file