]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added -f SCL/Module/function argument for BaselineCreatorApplication 51/2451/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 12 Nov 2018 11:38:41 +0000 (13:38 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 12 Nov 2018 11:38:41 +0000 (13:38 +0200)
This allows the baseline creator to run a custom SCL initialization
function that is included in the currently running platform after the
platform has been started up and before the platform shut down.

gitlab #193

Change-Id: I68565cf638365aaf816845282e3c7a24eb45dd31

bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java

index a3b409027a0c314354a3e172eac62163094b8be0..f186467e4181802f2bab1a7bbf8d8133c24831e9 100644 (file)
@@ -19,11 +19,14 @@ import org.eclipse.equinox.app.IApplicationContext;
 import org.eclipse.osgi.service.datalocation.Location;
 import org.simantics.application.arguments.Arguments;
 import org.simantics.application.arguments.IArgumentFactory;
-import org.simantics.application.arguments.IArgumentFactory.StringArgumentFactory;
 import org.simantics.application.arguments.IArgumentFactory.NoValueArgumentFactory;
+import org.simantics.application.arguments.IArgumentFactory.StringArgumentFactory;
 import org.simantics.application.arguments.IArguments;
 import org.simantics.application.arguments.SimanticsArguments;
 import org.simantics.internal.Activator;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
+import org.simantics.scl.runtime.tuple.Tuple0;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -38,6 +41,24 @@ public class BaselineCreatorApplication implements IApplication {
        private static final IArgumentFactory<String> OUTPUT = new StringArgumentFactory("-o");
        private static final IArgumentFactory<Boolean> VERBOSE = new NoValueArgumentFactory("-v");
 
+       /**
+        * The -f argument specifies an SCL function that shall be executed after the
+        * platform has been started and before the platform is shutdown.
+        * 
+        * <p>
+        * The function must be:
+        * <ol>
+        * <li>a parameterless function</li>
+        * <li>a function with <code>&lt;Proc&gt;</code> effect only</li>. It must internally invoke
+        * <code>syncWrite</code> etc. to have other effects.
+        * </ol>
+        * 
+        * <p>
+        * This means the function must have the following signature:
+        * <code>func :: <Proc> ()</code>
+        */
+       private static final IArgumentFactory<String> FUNCTION = new StringArgumentFactory("-f");
+
        IArgumentFactory<?>[] accepted = {
                        SimanticsArguments.RECOVERY_POLICY_FIX_ERRORS,
                        SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL,
@@ -45,6 +66,7 @@ public class BaselineCreatorApplication implements IApplication {
                        SimanticsArguments.DATABASE_ID,
                        OUTPUT,
                        VERBOSE,
+                       FUNCTION,
        };
 
        private static String currentLocalDateTimeStamp() {
@@ -80,17 +102,35 @@ public class BaselineCreatorApplication implements IApplication {
                        IArguments parsedArgs = Arguments.parse(args, accepted);
 
                        Path output = constructOutputPath(workspace, parsedArgs);
+                       LOGGER.info("Selected output file: {} ", output);
 
                        // Create database and indexes
                        IProgressMonitor progress = parsedArgs.contains(VERBOSE)
                                        ? new TimingProgressMonitor()
                                        : new NullProgressMonitor();
                        Simantics.startUpHeadless(parsedArgs, progress);
+
+                       if (parsedArgs.contains(FUNCTION)) {
+                               String func = parsedArgs.get(FUNCTION);
+                               String[] moduleAndFunc = splitFunction(func);
+                               if (moduleAndFunc != null) {
+                                       try {
+                                               LOGGER.info("Invoking SCL function {}/{}", moduleAndFunc[0], moduleAndFunc[1]);
+                                               SCLContext.getCurrent().put(SCLReportingHandler.REPORTING_HANDLER, SCLReportingHandler.DEFAULT);
+                                               Simantics.applySCL(moduleAndFunc[0], moduleAndFunc[1], Tuple0.INSTANCE);
+                                       } catch (Throwable t) {
+                                               LOGGER.error("Invocation failed", t);
+                                       }
+                               } else {
+                                       LOGGER.error("SCL function '{}' not invocable for baselining the database", func);
+                               }
+                       }
+
                        Simantics.shutdown(progress);
 
                        // Create the baseline package file
                        Path actualOutput = DatabaseBaselines.packageBaseline(workspace, output);
-                       System.out.println("OK " + actualOutput.toAbsolutePath());
+                       System.out.println("OK " + actualOutput.normalize().toAbsolutePath());
 
                        return IApplication.EXIT_OK;
                } catch (Exception e) {
@@ -101,6 +141,13 @@ public class BaselineCreatorApplication implements IApplication {
                }
        }
 
+       private String[] splitFunction(String func) {
+               int l = func.lastIndexOf('/');
+               if (l < 0)
+                       return null;
+               return new String[] { func.substring(0, l), func.substring(l+1) };
+       }
+
        @Override
        public void stop() {
        }