DataForge REPL

This commit is contained in:
darksnake 2016-08-29 16:38:11 +03:00
parent a5c485a2c1
commit c0ad20fe6c
6 changed files with 48 additions and 18 deletions

View File

@ -39,5 +39,13 @@ task workspace(dependsOn: classes, type: JavaExec) {
args "-c \"D:\\Work\\Numass\\sterile2016\\workspace.groovy\" numass.fitsum sum_down".split() args "-c \"D:\\Work\\Numass\\sterile2016\\workspace.groovy\" numass.fitsum sum_down".split()
description "Run workspace task" description "Run workspace task"
group "numass" 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"
} }

View File

@ -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()

View File

@ -7,18 +7,21 @@ package inr.numass.utils;
import groovy.lang.Binding; import groovy.lang.Binding;
import groovy.lang.GroovyShell; 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.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer; import org.codehaus.groovy.control.customizers.ImportCustomizer;
import java.util.Map;
/** /**
*
* @author Alexander Nozik * @author Alexander Nozik
*/ */
public class ExpressionUtils { public class ExpressionUtils {
private static Map<String, Script> cache = CommonUtils.getLRUCache(100);
private static GroovyShell shell;
public static double evaluate(String expression, Map<String, Object> binding) { static {
Binding b = new Binding(binding);
// Add imports for script. // Add imports for script.
ImportCustomizer importCustomizer = new ImportCustomizer(); ImportCustomizer importCustomizer = new ImportCustomizer();
// import static com.mrhaki.blog.Type.* // import static com.mrhaki.blog.Type.*
@ -26,8 +29,20 @@ public class ExpressionUtils {
CompilerConfiguration configuration = new CompilerConfiguration(); CompilerConfiguration configuration = new CompilerConfiguration();
configuration.addCompilationCustomizers(importCustomizer); // Create shell and execute script. configuration.addCompilationCustomizers(importCustomizer); // Create shell and execute script.
shell = new GroovyShell(configuration);
}
GroovyShell shell = new GroovyShell(b,configuration); private static Script getScript(String expression) {
return ((Number) shell.evaluate(expression)).doubleValue(); return cache.computeIfAbsent(expression, expr -> shell.parse(expr));
}
public static double evaluate(String expression, Map<String, Object> binding) {
synchronized (cache) {
Binding b = new Binding(binding);
Script script = getScript(expression);
script.setBinding(b);
return ((Number) script.run()).doubleValue();
}
} }
} }

View File

@ -20,15 +20,12 @@ import hep.dataforge.tables.ListTable;
import hep.dataforge.tables.Table; import hep.dataforge.tables.Table;
import inr.numass.data.SpectrumDataAdapter; import inr.numass.data.SpectrumDataAdapter;
import inr.numass.storage.NMPoint; import inr.numass.storage.NMPoint;
import static java.lang.Math.abs; import org.apache.commons.math3.analysis.UnivariateFunction;
import static java.lang.Math.exp;
import static java.lang.Math.sqrt;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.commons.math3.analysis.UnivariateFunction;
import static java.lang.Math.abs; import static java.lang.Math.*;
import static java.lang.Math.abs;
import static java.lang.Math.abs;
/** /**
* *
@ -82,8 +79,7 @@ public class TritiumUtils {
* Integral beta spectrum background with given amplitude (total count rate * Integral beta spectrum background with given amplitude (total count rate
* from) * from)
* *
* @param energy * @param amplitude
* @param countRate
* @return * @return
*/ */
public static UnivariateFunction tritiumBackgroundFunction(double amplitude) { public static UnivariateFunction tritiumBackgroundFunction(double amplitude) {
@ -134,7 +130,6 @@ public class TritiumUtils {
* *
* @param point * @param point
* @param expression * @param expression
* @param countRate
* @return * @return
*/ */
public static double evaluateExpression(NMPoint point, String expression) { public static double evaluateExpression(NMPoint point, String expression) {

View File

@ -17,7 +17,7 @@ import java.util.stream.Collectors;
/** /**
* Created by darksnake on 12-Aug-16. * Created by darksnake on 12-Aug-16.
*/ */
public class NumassGrindLauncher { public class NumassJavaGrindLauncher {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Options options = new Options(); Options options = new Options();

View File

@ -137,7 +137,7 @@ public class MainViewerController implements Initializable {
NumassStorage root = NumassStorage.buildNumassRoot(path, true, false); NumassStorage root = NumassStorage.buildNumassRoot(path, true, false);
setRootStorage(root); setRootStorage(root);
Platform.runLater(() -> storagePathLabel.setText("Storage: " + path)); Platform.runLater(() -> storagePathLabel.setText("Storage: " + path));
} catch (StorageException ex) { } catch (Exception ex) {
callback.setProgress(0); callback.setProgress(0);
callback.updateMessage("Failed to load storage " + path); callback.updateMessage("Failed to load storage " + path);
Logger.getLogger(MainViewerController.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(MainViewerController.class.getName()).log(Level.SEVERE, null, ex);