From 879ec1d657a576f51560004e5c582f7b94b687ce Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Mon, 23 Sep 2013 15:34:31 +0200 Subject: [PATCH] FDA-27 added datafile and filename to script actions --- .../main/java/ch/psi/fda/aq/Acquisition.java | 14 +++++++-- .../ch/psi/fda/core/actions/JythonAction.java | 31 +++++++++++++++++-- .../home/scans/templates/scan1d-shell.xml | 17 ++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 ch.psi.fda/src/test/resources/home/scans/templates/scan1d-shell.xml diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java index 021cd93..9122c49 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/Acquisition.java @@ -513,6 +513,7 @@ public class Acquisition { if(configuration.getData()!=null){ // Safety manipulation.setVariable("FILENAME", configuration.getData().getFileName()); + manipulation.setVariable("DATAFILE", datafile.getAbsoluteFile()); } this.manipulations.add(manipulation); @@ -626,7 +627,9 @@ public class Acquisition { } else if(a instanceof ShellAction){ ShellAction sa = (ShellAction) a; - ch.psi.fda.core.actions.ShellAction action = new ch.psi.fda.core.actions.ShellAction(sa.getCommand()); + String com = sa.getCommand().replaceAll("\\$\\{DATAFILE\\}", datafile.getAbsolutePath()); + com = com.replaceAll("\\$\\{FILENAME\\}", datafile.getName().replaceAll("\\.\\w*$", "")); + ch.psi.fda.core.actions.ShellAction action = new ch.psi.fda.core.actions.ShellAction(com); action.setCheckExitValue(sa.isCheckExitValue()); action.setExitValue(sa.getExitValue()); alist.add(action); @@ -634,6 +637,9 @@ public class Acquisition { else if(a instanceof ScriptAction){ ScriptAction sa = (ScriptAction) a; + + // TODO set global variables DATAFILE and FILENAME + // TODO create Jython Action List mapping = new ArrayList(); for(ChannelParameterMapping ma: sa.getMapping()){ @@ -650,7 +656,11 @@ public class Acquisition { logger.warning("Channel type ["+ma.getType()+"] is not supported for mapping"); } } - alist.add(new ch.psi.fda.core.actions.JythonAction(sa.getScript(), mapping)); + + ch.psi.fda.core.actions.JythonAction ja = new ch.psi.fda.core.actions.JythonAction(sa.getScript(), mapping); + ja.setVariable("FILENAME", datafile.getName().replaceAll("\\.\\w*$", "")); + ja.setVariable("DATAFILE", datafile.getAbsoluteFile()); + alist.add(ja); } } return(alist); diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/JythonAction.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/JythonAction.java index 9eecdba..dea28d1 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/JythonAction.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/JythonAction.java @@ -21,7 +21,9 @@ package ch.psi.fda.core.actions; import gov.aps.jca.CAException; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -60,6 +62,8 @@ public class JythonAction implements Action { * Jython entry call */ private String jythonCall; + + private Map gvariables = new HashMap(); public JythonAction(String script, List mapping){ @@ -76,7 +80,12 @@ public class JythonAction implements Action { String[] functionParameters = null; if(matcher.find() && matcher.groupCount()==1){ logger.finest("Entry function '"+entryFunctionPattern+"' found - Identified parameters: "+matcher.group(1)); - functionParameters = matcher.group(1).split(" *, *"); + if(matcher.group(1).matches(" *")){ + functionParameters = new String[0]; + } + else{ + functionParameters = matcher.group(1).split(" *, *"); + } } else{ throw new IllegalArgumentException("Cannot determine entry function: "+entryFunctionPattern); @@ -112,7 +121,7 @@ public class JythonAction implements Action { StringBuffer buffer = new StringBuffer(); buffer.append(entryFunction); - buffer.append("("); + buffer.append("( "); // Need to have trailing space as otherwise there will be a problem if no paramters are specified for(JythonParameterMappingChannel b: mapping){ // Create channel @@ -142,6 +151,15 @@ public class JythonAction implements Action { */ @Override public void execute() { + + // Set global variables - WORKAROUND gvariables + // This block is not in initialization as we want to assure that all invocations + // of this manipulation will get the same value (i.e. to prevent inconsistent behaviour + // if variable was changed during an execution of the manipulation) + for(String k: gvariables.keySet()){ + engine.put(k, gvariables.get(k)); + } + try { engine.eval(jythonCall); } catch (ScriptException e) { @@ -166,4 +184,13 @@ public class JythonAction implements Action { // Nothing to be done } + /** + * Workaround to put variables into the jython engine. + * @param name + * @param value + */ + public void setVariable(String name, Object value){ + gvariables.put(name, value); + } + } diff --git a/ch.psi.fda/src/test/resources/home/scans/templates/scan1d-shell.xml b/ch.psi.fda/src/test/resources/home/scans/templates/scan1d-shell.xml new file mode 100644 index 0000000..bd5ace6 --- /dev/null +++ b/ch.psi.fda/src/test/resources/home/scans/templates/scan1d-shell.xml @@ -0,0 +1,17 @@ + + + + + + + + + + +