Simple migration step implementation that runs a specified SCL script 98/3298/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 30 Sep 2019 07:14:29 +0000 (10:14 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 30 Sep 2019 07:38:30 +0000 (10:38 +0300)
gitlab #343

Change-Id: I61a658e9888e9b03d595cee5fecee51c54d95719

bundles/org.simantics.modeling.ontology/graph/Migration.pgraph
bundles/org.simantics.modeling/adapters.xml
bundles/org.simantics.modeling/src/org/simantics/modeling/LifeCycleProcesses.java
bundles/org.simantics.modeling/src/org/simantics/modeling/migration/SCLScriptMigrationStep.java [new file with mode: 0644]

index 0b974a0084b27f05cc4ba75bc6d545fa79a1875a..1cd0a49339efa4f2897138aac0e5ad7bec11b695 100644 (file)
@@ -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 <T L0.MigrationStep
+    >-- MOD.Migration.SCLScriptMigrationStep.script --> L0.String <R L0.HasProperty : L0.TotalFunction
+        L0.HasLabel "SCL Script" 
+    @L0.assert MOD.Migration.SCLScriptMigrationStep.script ""
index 063a661c94bbd6bab2285b7df0826562438ae6b8..3d2a63df970ec7357f0f9cb84075204ce5329d4c 100644 (file)
                <resource uri="http://www.simantics.org/Modeling-1.2/Migration/documentCleanupMigrationStep"
                        class="org.simantics.modeling.migration.DocumentCleanupMigrationStep">
                </resource>
+               <type uri="http://www.simantics.org/Modeling-0.0/Migration/SCLScriptMigrationStep"
+                       class="org.simantics.modeling.migration.SCLScriptMigrationStep">
+                       <graph />
+                       <this />
+               </type>
        </target>
 
        <target interface="org.simantics.modeling.typicals.ITypicalSynchronizationRule">
index e8e32796d83421c3cc381050114fae226a06b626..ae7cdfd2bc4512411b51ea7cb96cba132c2c73d0 100644 (file)
@@ -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<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);
@@ -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 (file)
index 0000000..0e6112d
--- /dev/null
@@ -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<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