From: Tuukka Lehtonen Date: Mon, 30 Sep 2019 07:14:29 +0000 (+0300) Subject: Simple migration step implementation that runs a specified SCL script X-Git-Tag: v1.43.0~136^2~61 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=bd6a9e185eb6ba6b5f739f54b48c9607dd784ba9;p=simantics%2Fplatform.git Simple migration step implementation that runs a specified SCL script gitlab #343 Change-Id: I61a658e9888e9b03d595cee5fecee51c54d95719 --- diff --git a/bundles/org.simantics.modeling.ontology/graph/Migration.pgraph b/bundles/org.simantics.modeling.ontology/graph/Migration.pgraph index 0b974a008..1cd0a4933 100644 --- a/bundles/org.simantics.modeling.ontology/graph/Migration.pgraph +++ b/bundles/org.simantics.modeling.ontology/graph/Migration.pgraph @@ -10,4 +10,9 @@ MOD.Migration.attachCreationInformationStep : L0.MigrationStep // Fix old diagram layers MOD.Migration.layerCleanupMigrationStep : L0.MigrationStep -MOD.Migration.documentCleanupMigrationStep : L0.MigrationStep \ No newline at end of file +MOD.Migration.documentCleanupMigrationStep : L0.MigrationStep + +MOD.Migration.SCLScriptMigrationStep -- MOD.Migration.SCLScriptMigrationStep.script --> L0.String + + + + diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/LifeCycleProcesses.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/LifeCycleProcesses.java index e8e32796d..ae7cdfd2b 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/LifeCycleProcesses.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/LifeCycleProcesses.java @@ -1,40 +1,45 @@ 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 contexts = new HashSet(); + + private static final Logger LOGGER = LoggerFactory.getLogger(LifeCycleProcesses.class); + + Set contexts = Collections.emptySet(); @Override public void configure() throws ProjectException { - try { - - Simantics.getSession().syncRequest(new ReadRequest() { + this.contexts = Simantics.getSession().syncRequest(new Read>() { @Override - public void run(ReadGraph graph) throws DatabaseException { + 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 : 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 load = null; Function1 unload = null; Variable loadProperty = Variables.tryGetProperty(graph, lcp, MOD.LifeCycleProcess_load); @@ -48,33 +53,24 @@ public class LifeCycleProcesses extends AbstractProjectFeature { 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); } - + } diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/migration/SCLScriptMigrationStep.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/migration/SCLScriptMigrationStep.java new file mode 100644 index 000000000..0e6112d38 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/migration/SCLScriptMigrationStep.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * 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 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 roots, PrintWriter log) throws DatabaseException { + log.format("## Running SCL Script Migration Step `%s` ##%n", scriptId); + SCLReportingHandler rh = new ReportingHandler(log); + Map 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 mapNames(Collection roots) throws DatabaseException { + return Simantics.getSession().syncRequest((Read>) graph -> mapNames(graph, roots)); + } + + private Map mapNames(ReadGraph graph, Collection roots) throws DatabaseException { + Map map = new HashMap<>(); + for (Resource r : roots) + map.put(r, NameUtils.getSafeName(graph, r)); + return map; + } + +} \ No newline at end of file