Restructuring code

This commit is contained in:
2013-10-14 12:54:57 +02:00
parent c8031db1fd
commit c1c8170f36
8 changed files with 106 additions and 35 deletions
@@ -43,6 +43,7 @@ import com.google.common.eventbus.EventBus;
import ch.psi.fda.core.ActionLoop;
import ch.psi.fda.core.Actor;
import ch.psi.fda.core.EngineConfiguration;
import ch.psi.fda.core.Manipulation;
import ch.psi.fda.core.Sensor;
import ch.psi.fda.core.actions.ChannelAccessCondition;
import ch.psi.fda.core.actions.ChannelAccessPut;
@@ -63,8 +64,6 @@ import ch.psi.fda.core.loops.cr.CrlogicLoop;
import ch.psi.fda.core.loops.cr.ParallelCrlogic;
import ch.psi.fda.core.loops.cr.ScrlogicLoop;
import ch.psi.fda.core.manipulator.JythonManipulation;
import ch.psi.fda.core.manipulator.Manipulation;
import ch.psi.fda.core.manipulator.Manipulator;
import ch.psi.fda.core.messages.DataMessageMetadata;
import ch.psi.fda.core.scripting.JythonGlobalVariable;
import ch.psi.fda.core.scripting.JythonGlobalVariableDictionary;
@@ -656,10 +655,11 @@ public class Acquisition {
}
}
Map<String,Object> gobjects = new HashMap<>();
gobjects.put("FILENAME", datafile.getName().replaceAll("\\.\\w*$", ""));
gobjects.put("DATAFILE", datafile.getAbsoluteFile());
ch.psi.fda.core.actions.JythonAction ja = new ch.psi.fda.core.actions.JythonAction(sa.getScript(), mapping, gobjects);
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);
}
}
@@ -17,13 +17,14 @@
*
*/
package ch.psi.fda.core.manipulator;
package ch.psi.fda.aq;
import java.util.List;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import ch.psi.fda.core.Manipulation;
import ch.psi.fda.core.messages.ComponentMetadata;
import ch.psi.fda.core.messages.DataMessage;
import ch.psi.fda.core.messages.DataMessageMetadata;
@@ -17,15 +17,11 @@
*
*/
package ch.psi.fda.core.manipulator;
package ch.psi.fda.core;
import ch.psi.fda.core.messages.DataMessage;
import ch.psi.fda.core.messages.DataMessageMetadata;
/**
* @author ebner
*
*/
public interface Manipulation {
/**
@@ -49,9 +49,15 @@ public class JythonAction implements Action {
private String jythonCall; // entry call to script - including all parameters, etc.
private Map<String,Object> gvariables = new HashMap<String,Object>();
private final Map<String,Object> globalObjects;
public JythonAction(String script, Map<String, ?> mapping){
this(script, mapping, new HashMap<String,Object>());
}
public JythonAction(String script, Map<String, ?> mapping, Map<String,Object> globalObjects){
this.globalObjects = globalObjects;
// Workaround for Jython memory leak
// http://blog.hillbrecht.de/2009/07/11/jython-memory-leakout-of-memory-problem/
@@ -115,12 +121,12 @@ public class JythonAction implements Action {
@Override
public void execute() {
// Set global variables - WORKAROUND gvariables
// Set global objects
// 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
// of this manipulation will get the same value (i.e. to prevent inconsistent behavior
// if variable was changed during an execution of the manipulation)
for(String k: gvariables.keySet()){
engine.put(k, gvariables.get(k));
for(String k: globalObjects.keySet()){
engine.put(k, globalObjects.get(k));
}
try {
@@ -133,14 +139,4 @@ public class JythonAction implements Action {
@Override
public void abort() {
}
/**
* Workaround to put variables into the jython engine.
* @param name
* @param value
*/
public void setVariable(String name, Object value){
gvariables.put(name, value);
}
}
@@ -31,6 +31,7 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import ch.psi.fda.core.Manipulation;
import ch.psi.fda.core.messages.DataMessage;
import ch.psi.fda.core.messages.DataMessageMetadata;
import ch.psi.fda.core.scripting.JythonParameterMapping;
@@ -0,0 +1,56 @@
package ch.psi.fda.core.sensors;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import ch.psi.fda.core.Action;
import ch.psi.fda.core.Sensor;
/**
* Complex sensor that complements an other sensor with pre and post actions.
* Before reading out the complemented sensor the pre actions are executed. After the
* readout the post actions.
*/
public class ComplexSensor implements Sensor {
private static Logger logger = Logger.getLogger(ComplexSensor.class.getName());
private String id;
private final Sensor sensor;
private final List<Action> preActions;
private final List<Action> postActions;
public ComplexSensor(String id, Sensor sensor){
this.id = id;
this.sensor = sensor;
this.preActions = new ArrayList<Action>();
this.postActions = new ArrayList<Action>();
}
@Override
public Object read() throws InterruptedException {
logger.finest("Execute pre actions");
for(Action action: preActions){
action.execute();
}
Object value = sensor.read();
logger.finest("Execute post actions");
for(Action action: postActions){
action.execute();
}
return value;
}
@Override
public String getId() {
return id;
}
public List<Action> getPreActions() {
return preActions;
}
public List<Action> getPostActions() {
return postActions;
}
}
@@ -86,5 +86,26 @@ public class JythonActionTest {
action.execute();
}
@Test
public void testExecuteGlobalObjects() throws ChannelException, InterruptedException, TimeoutException {
try{
Map<String,Object> mapping = new HashMap<>();
mapping.put("o", cservice.createChannel(new ChannelDescriptor<>(Double.class, TestChannels.ANALOG_OUT)));
Map<String,Object> gobjects = new HashMap<>();
gobjects.put("TESTVAR", "salli!");
String script = SCRIPT = "def process(o):\n print TESTVAR";
JythonAction action = new JythonAction(script, mapping, gobjects);
action.execute();
}
catch(Exception e){
e.printStackTrace();
throw e;
}
}
}
@@ -37,9 +37,9 @@ import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import ch.psi.fda.TestChannels;
import ch.psi.fda.aq.Manipulator;
import ch.psi.fda.core.Manipulation;
import ch.psi.fda.core.manipulator.JythonManipulation;
import ch.psi.fda.core.manipulator.Manipulation;
import ch.psi.fda.core.manipulator.Manipulator;
import ch.psi.fda.core.messages.ComponentMetadata;
import ch.psi.fda.core.messages.DataMessage;
import ch.psi.fda.core.messages.DataMessageMetadata;
@@ -73,7 +73,7 @@ public class ManipulatorTest {
}
/**
* Test method for {@link ch.psi.fda.core.manipulator.Manipulator#Manipulator()}.
* Test method for {@link ch.psi.fda.aq.Manipulator#Manipulator()}.
*/
@Test(expected=IllegalArgumentException.class)
public void testConstructor() {
@@ -128,7 +128,7 @@ public class ManipulatorTest {
}
/**
* Test method for {@link ch.psi.fda.core.manipulator.Manipulator#Manipulator()}.
* Test method for {@link ch.psi.fda.aq.Manipulator#Manipulator()}.
*/
@Test(expected=IllegalArgumentException.class)
public void testConstructorNoMapping() {
@@ -149,7 +149,7 @@ public class ManipulatorTest {
}
/**
* Test method for {@link ch.psi.fda.core.manipulator.Manipulator#run()}.
* Test method for {@link ch.psi.fda.aq.Manipulator#run()}.
* @throws InterruptedException
*/
@Test
@@ -272,7 +272,7 @@ public class ManipulatorTest {
}
/**
* Test method for {@link ch.psi.fda.core.manipulator.Manipulator#run()}.
* Test method for {@link ch.psi.fda.aq.Manipulator#run()}.
* @throws InterruptedException
*/
@Ignore
@@ -315,7 +315,7 @@ public class ManipulatorTest {
}
/**
* Test method for {@link ch.psi.fda.core.manipulator.Manipulator#run()}.
* Test method for {@link ch.psi.fda.aq.Manipulator#run()}.
* @throws InterruptedException
*/
@Test
@@ -390,7 +390,7 @@ public class ManipulatorTest {
/**
* Test method for {@link ch.psi.fda.core.manipulator.Manipulator#run()}.
* Test method for {@link ch.psi.fda.aq.Manipulator#run()}.
* @throws InterruptedException
* @throws CAException
* @throws TimeoutException