From c0ad20fe6c6717fc0ab9ad1cfde6cafcc5619764 Mon Sep 17 00:00:00 2001 From: darksnake Date: Mon, 29 Aug 2016 16:38:11 +0300 Subject: [PATCH] DataForge REPL --- numass-main/build.gradle | 8 ++++++ .../groovy/inr/numass/LaunchGrindShell.groovy | 12 +++++++++ .../inr/numass/utils/ExpressionUtils.java | 27 ++++++++++++++----- .../java/inr/numass/utils/TritiumUtils.java | 15 ++++------- ...cher.java => NumassJavaGrindLauncher.java} | 2 +- .../numass/viewer/MainViewerController.java | 2 +- 6 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy rename numass-main/src/main/java/inr/numass/workspace/{NumassGrindLauncher.java => NumassJavaGrindLauncher.java} (95%) diff --git a/numass-main/build.gradle b/numass-main/build.gradle index 67f23570..fe0c64f9 100644 --- a/numass-main/build.gradle +++ b/numass-main/build.gradle @@ -39,5 +39,13 @@ task workspace(dependsOn: classes, type: JavaExec) { args "-c \"D:\\Work\\Numass\\sterile2016\\workspace.groovy\" numass.fitsum sum_down".split() description "Run workspace task" group "numass" +} +task repl(dependsOn: classes, type: JavaExec) { + main 'inr.numass.LaunchGrindShell' + classpath = sourceSets.main.runtimeClasspath + description "Start Grind repl" + standardInput = System.in + standardOutput = System.out + group "numass" } \ No newline at end of file diff --git a/numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy b/numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy new file mode 100644 index 00000000..d82ae2c1 --- /dev/null +++ b/numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy @@ -0,0 +1,12 @@ +package inr.numass + +import hep.dataforge.grind.GrindShell + +/** + * Created by darksnake on 29-Aug-16. + */ + +GrindShell shell = new GrindShell() +shell.launcher.withSpec(NumassWorkspaceSpec) +shell.launcher.from(new File("D:\\Work\\Numass\\sterile2016\\workspace.groovy")) +shell.start() diff --git a/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java b/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java index 4502ac0d..50557a8a 100644 --- a/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java +++ b/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java @@ -7,18 +7,21 @@ package inr.numass.utils; import groovy.lang.Binding; import groovy.lang.GroovyShell; -import java.util.Map; +import groovy.lang.Script; +import hep.dataforge.utils.CommonUtils; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.customizers.ImportCustomizer; +import java.util.Map; + /** - * * @author Alexander Nozik */ public class ExpressionUtils { + private static Map cache = CommonUtils.getLRUCache(100); + private static GroovyShell shell; - public static double evaluate(String expression, Map binding) { - Binding b = new Binding(binding); + static { // Add imports for script. ImportCustomizer importCustomizer = new ImportCustomizer(); // import static com.mrhaki.blog.Type.* @@ -26,8 +29,20 @@ public class ExpressionUtils { CompilerConfiguration configuration = new CompilerConfiguration(); configuration.addCompilationCustomizers(importCustomizer); // Create shell and execute script. + shell = new GroovyShell(configuration); + } - GroovyShell shell = new GroovyShell(b,configuration); - return ((Number) shell.evaluate(expression)).doubleValue(); + private static Script getScript(String expression) { + return cache.computeIfAbsent(expression, expr -> shell.parse(expr)); + } + + + public static double evaluate(String expression, Map binding) { + synchronized (cache) { + Binding b = new Binding(binding); + Script script = getScript(expression); + script.setBinding(b); + return ((Number) script.run()).doubleValue(); + } } } diff --git a/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java b/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java index 4d2b348f..13f118e2 100644 --- a/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java +++ b/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java @@ -20,15 +20,12 @@ import hep.dataforge.tables.ListTable; import hep.dataforge.tables.Table; import inr.numass.data.SpectrumDataAdapter; import inr.numass.storage.NMPoint; -import static java.lang.Math.abs; -import static java.lang.Math.exp; -import static java.lang.Math.sqrt; +import org.apache.commons.math3.analysis.UnivariateFunction; + import java.util.HashMap; import java.util.Map; -import org.apache.commons.math3.analysis.UnivariateFunction; -import static java.lang.Math.abs; -import static java.lang.Math.abs; -import static java.lang.Math.abs; + +import static java.lang.Math.*; /** * @@ -82,8 +79,7 @@ public class TritiumUtils { * Integral beta spectrum background with given amplitude (total count rate * from) * - * @param energy - * @param countRate + * @param amplitude * @return */ public static UnivariateFunction tritiumBackgroundFunction(double amplitude) { @@ -134,7 +130,6 @@ public class TritiumUtils { * * @param point * @param expression - * @param countRate * @return */ public static double evaluateExpression(NMPoint point, String expression) { diff --git a/numass-main/src/main/java/inr/numass/workspace/NumassGrindLauncher.java b/numass-main/src/main/java/inr/numass/workspace/NumassJavaGrindLauncher.java similarity index 95% rename from numass-main/src/main/java/inr/numass/workspace/NumassGrindLauncher.java rename to numass-main/src/main/java/inr/numass/workspace/NumassJavaGrindLauncher.java index ad8bb9f5..90765547 100644 --- a/numass-main/src/main/java/inr/numass/workspace/NumassGrindLauncher.java +++ b/numass-main/src/main/java/inr/numass/workspace/NumassJavaGrindLauncher.java @@ -17,7 +17,7 @@ import java.util.stream.Collectors; /** * Created by darksnake on 12-Aug-16. */ -public class NumassGrindLauncher { +public class NumassJavaGrindLauncher { public static void main(String[] args) throws Exception { Options options = new Options(); diff --git a/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java b/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java index 6b612577..5b0d2c10 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java @@ -137,7 +137,7 @@ public class MainViewerController implements Initializable { NumassStorage root = NumassStorage.buildNumassRoot(path, true, false); setRootStorage(root); Platform.runLater(() -> storagePathLabel.setText("Storage: " + path)); - } catch (StorageException ex) { + } catch (Exception ex) { callback.setProgress(0); callback.updateMessage("Failed to load storage " + path); Logger.getLogger(MainViewerController.class.getName()).log(Level.SEVERE, null, ex);