fixed otf code

This commit is contained in:
2013-10-21 09:45:06 +02:00
parent 40cc7f6a31
commit 491812ddeb
3 changed files with 193 additions and 220 deletions

View File

@@ -25,19 +25,16 @@ import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Logger;
import com.google.common.eventbus.EventBus;
import ch.psi.fda.core.Action;
import ch.psi.fda.core.ActionLoop;
import ch.psi.fda.core.Sensor;
import ch.psi.fda.core.messages.ComponentMetadata;
import ch.psi.fda.core.messages.DataMessage;
import ch.psi.fda.core.messages.DataMessageMetadata;
import ch.psi.fda.core.messages.DataQueue;
import ch.psi.fda.core.messages.EndOfStreamMessage;
import ch.psi.fda.core.messages.Message;
import ch.psi.fda.core.messages.Metadata;
import ch.psi.fda.core.sensors.MillisecondTimestampSensor;
/**
@@ -114,12 +111,6 @@ public class OTFLoop implements ActionLoop {
*/
private int executionCount;
/**
* Data queue sensor data is posted to. A message consists of a list of data objects
* that are read out of the sensors of this loop.
*/
private BlockingQueue<Message> dataQueue;
/**
* Flag that indicates that the loop was requested to abort.
*/
@@ -135,6 +126,8 @@ public class OTFLoop implements ActionLoop {
private double integrationTime;
private double additionalBacklash;
private final EventBus eventbus;
private List<Metadata> metadata;
/**
* @param channelPrefix Prefix of the OTF related records, e.g. MTEST-HW3-OTF
@@ -145,6 +138,8 @@ public class OTFLoop implements ActionLoop {
*/
public OTFLoop(TemplateOTF obean, String server, String share, String smbShare, boolean zigZag){
this.eventbus = new EventBus();
this.obean = obean;
// Store loop configuration
@@ -157,8 +152,6 @@ public class OTFLoop implements ActionLoop {
this.preActions = new ArrayList<Action>();
this.postActions = new ArrayList<Action>();
this.sensors = new ArrayList<Sensor>();
this.dataQueue = new LinkedBlockingQueue<Message>(2000);
}
public void setActuator(String id, String name, String readback, double start, double end, double stepSize, double integrationTime, double additionalBacklash){
@@ -173,9 +166,6 @@ public class OTFLoop implements ActionLoop {
}
/* (non-Javadoc)
* @see ch.psi.fda.core.Action#execute()
*/
@Override
public void execute() throws InterruptedException {
@@ -200,15 +190,12 @@ public class OTFLoop implements ActionLoop {
// Issue end of loop control message
dataQueue.put(new EndOfStreamMessage(dataGroup));
eventbus.post(new EndOfStreamMessage(dataGroup));
// Increase execution count
executionCount++;
}
/* (non-Javadoc)
* @see ch.psi.fda.core.Action#abort()
*/
@Override
public void abort() {
// Abort otf scan logic
@@ -217,9 +204,6 @@ public class OTFLoop implements ActionLoop {
abort=true;
}
/* (non-Javadoc)
* @see ch.psi.fda.core.ActionLoop#prepare()
*/
@Override
public void prepare() {
executionCount = 0;
@@ -330,6 +314,13 @@ public class OTFLoop implements ActionLoop {
throw new RuntimeException("Unable to access share (temporary files)",e);
}
metadata = new ArrayList<>();
metadata.add(new Metadata(id)); // Id of the readback of the motor
// Build up data message metadata based on the sensors currently registered.
for(Sensor s: sensors){
metadata.add(new Metadata(s.getId()));
}
}
@Override
@@ -346,22 +337,6 @@ public class OTFLoop implements ActionLoop {
return postActions;
}
/**
* The structure of the data message depends on the sensors registered at this loop
* at the time this method is called.
* @return the data queue and the metadata of the data messages
*/
public DataQueue getDataQueue() {
DataMessageMetadata m = new DataMessageMetadata();
m.getComponents().add(new ComponentMetadata(id)); // Id of the readback of the motor
// Build up data message metadata based on the sensors currently registered.
for(Sensor s: sensors){
m.getComponents().add(new ComponentMetadata(s.getId()));
}
return new DataQueue(dataQueue, m);
}
/**
* Collect data written by the OTFScan logic
* @param dataSet
@@ -406,7 +381,7 @@ public class OTFLoop implements ActionLoop {
continue;
}
DataMessage message = new DataMessage();
DataMessage message = new DataMessage(metadata);
// Add data to dataset
String[] tokens = line.split("\t");
@@ -430,7 +405,7 @@ public class OTFLoop implements ActionLoop {
message.getData().add(new Double(0));
}
}
dataQueue.put(message);
eventbus.post(message);
}
}
}
@@ -453,4 +428,12 @@ public class OTFLoop implements ActionLoop {
public void setDataGroup(boolean dataGroup) {
this.dataGroup = dataGroup;
}
/* (non-Javadoc)
* @see ch.psi.fda.core.ActionLoop#getEventBus()
*/
@Override
public EventBus getEventBus() {
return eventbus;
}
}

View File

@@ -40,7 +40,6 @@ 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.messages.ComponentMetadata;
import ch.psi.fda.core.messages.DataMessage;
import ch.psi.fda.core.messages.EndOfStreamMessage;
import ch.psi.fda.core.messages.Message;
@@ -72,25 +71,6 @@ public class ManipulatorTest {
cservice.destroy();
}
/**
* Test method for {@link ch.psi.fda.aq.Manipulator#Manipulator()}.
*/
@Test(expected=IllegalArgumentException.class)
public void testConstructor() {
String id="computedId";
String script = "import math\ndef process(o):\n return math.cos(10.0) + math.sin(o)";
List<JythonParameterMapping> mapping = new ArrayList<JythonParameterMapping>();
mapping.add(new JythonParameterMappingID("o", "myid"));
JythonManipulation manipulation = new JythonManipulation(id, script, mapping);
// This constructor need to throw an IllegalArgumentException as there is no component
// id "myid" which is expected in the mapping
List<Manipulation> manipulations = new ArrayList<Manipulation>();
manipulations.add(manipulation);
new Manipulator(bus, manipulations);
}
@Test
public void testConstructorNoMappingNoParam() {
String id="cid";
@@ -150,8 +130,8 @@ public class ManipulatorTest {
*/
@Test
public void testRun() throws InterruptedException {
DataMessageMetadata dmm = new DataMessageMetadata();
dmm.getComponents().add(new ComponentMetadata("myid"));
List<Metadata> dmm = new ArrayList<>();
dmm.add(new Metadata("myid"));
String id="cid";
String script = "import math\ndef process(o):\n return math.cos(10.0) + math.sin(o)";
@@ -161,33 +141,39 @@ public class ManipulatorTest {
List<Manipulation> manipulations = new ArrayList<Manipulation>();
manipulations.add(manipulation);
Manipulator manipulator = new Manipulator(bus, dmm, manipulations);
Manipulator manipulator = new Manipulator(bus, manipulations);
// Check whether output queue message structur complies to expected one
DataMessageMetadata outMeta = manipulator.getMetadata();
// Test whether only the expected components are within the outgoing queue
if(outMeta.getComponents().size()!=2){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!outMeta.getComponents().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!outMeta.getComponents().get(1).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
bus.register(new Object(){
boolean first = true;
@Subscribe
public void onMessage(Message message){
logger.info(message.toString());
if(message instanceof DataMessage){
DataMessage dm = (DataMessage) message;
if(first){
first=false;
// Check whether output queue message structur complies to expected one
// Test whether only the expected components are within the outgoing queue
if(dm.getMetadata().size()!=2){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!dm.getMetadata().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!dm.getMetadata().get(1).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
}
dm.getData().get(0);
double res = ((Double)dm.getData().get(1)) - (Math.cos(10.0)+Math.sin(((Double)dm.getData().get(0))));
if( Math.abs(res) > 0.000000001){
@@ -200,7 +186,7 @@ public class ManipulatorTest {
EventBus b = new EventBus();
b.register(manipulator);
DataMessage m = new DataMessage();
DataMessage m = new DataMessage(dmm);
m.getData().add(10d);
b.post(m);
b.post(new EndOfStreamMessage());
@@ -214,8 +200,8 @@ public class ManipulatorTest {
*/
@Test
public void testRunIntegerReturn() throws InterruptedException {
DataMessageMetadata dmm = new DataMessageMetadata();
dmm.getComponents().add(new ComponentMetadata("myid"));
List<Metadata> dmm = new ArrayList<>();
dmm.add(new Metadata("myid"));
String id="cid";
@@ -226,33 +212,44 @@ public class ManipulatorTest {
List<Manipulation> manipulations = new ArrayList<Manipulation>();
manipulations.add(manipulation);
Manipulator manipulator = new Manipulator(bus, dmm, manipulations);
Manipulator manipulator = new Manipulator(bus, manipulations);
// Check whether output queue message structur complies to expected one
DataMessageMetadata outMeta = manipulator.getMetadata();
// Test whether only the expected components are within the outgoing queue
if(outMeta.getComponents().size()!=2){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!outMeta.getComponents().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!outMeta.getComponents().get(1).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
bus.register(new Object(){
boolean first = true;
@Subscribe
public void onMessage(Message message){
logger.info(message.toString());
if(message instanceof DataMessage){
DataMessage dm = (DataMessage) message;
if(first){
first=false;
// Check whether output queue message structur complies to expected one
// Test whether only the expected components are within the outgoing queue
if(dm.getMetadata().size()!=2){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!dm.getMetadata().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!dm.getMetadata().get(1).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
}
dm.getData().get(0);
}
}
@@ -261,7 +258,7 @@ public class ManipulatorTest {
EventBus b = new EventBus();
b.register(manipulator);
DataMessage m = new DataMessage();
DataMessage m = new DataMessage(dmm);
m.getData().add(10d);
b.post(m);
b.post(new EndOfStreamMessage());
@@ -274,8 +271,8 @@ public class ManipulatorTest {
@Ignore
@Test
public void testRunLongTimeTest() throws InterruptedException {
DataMessageMetadata dmm = new DataMessageMetadata();
dmm.getComponents().add(new ComponentMetadata("myid"));
List<Metadata> dmm = new ArrayList<>();
dmm.add(new Metadata("myid"));
String id="cid";
String script = "import math\ndef process(o):\n return math.cos(10.0) + math.sin(o)";
@@ -285,7 +282,7 @@ public class ManipulatorTest {
List<Manipulation> manipulations = new ArrayList<Manipulation>();
manipulations.add(manipulation);
Manipulator manipulator = new Manipulator(bus, dmm, manipulations);
Manipulator manipulator = new Manipulator(bus, manipulations);
bus.register(new Object(){
int count=0;
@@ -303,7 +300,7 @@ public class ManipulatorTest {
EventBus b = new EventBus();
b.register(manipulator);
for(Double i=0d;i<1000000;i++){
DataMessage m = new DataMessage();
DataMessage m = new DataMessage(dmm);
m.getData().add(i);
b.post(m);
}
@@ -316,9 +313,9 @@ public class ManipulatorTest {
*/
@Test
public void testRunMultipleParameter() throws InterruptedException {
DataMessageMetadata dmm = new DataMessageMetadata();
dmm.getComponents().add(new ComponentMetadata("myid"));
dmm.getComponents().add(new ComponentMetadata("myid2"));
List<Metadata> dmm = new ArrayList<>();
dmm.add(new Metadata("myid"));
dmm.add(new Metadata("myid2"));
String id="cid";
@@ -330,37 +327,44 @@ public class ManipulatorTest {
List<Manipulation> manipulations = new ArrayList<Manipulation>();
manipulations.add(manipulation);
Manipulator manipulator = new Manipulator(bus, dmm, manipulations);
// Check whether output queue message structur complies to expected one
DataMessageMetadata outMeta = manipulator.getMetadata();
// Test whether only the expected components are within the outgoing queue
if(outMeta.getComponents().size()!=3){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!outMeta.getComponents().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
if(!outMeta.getComponents().get(1).getId().equals("myid2")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!outMeta.getComponents().get(2).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
Manipulator manipulator = new Manipulator(bus, manipulations);
bus.register(new Object(){
boolean first = true;
@Subscribe
public void onMessage(Message message){
logger.info(message.toString());
if(message instanceof DataMessage){
DataMessage dm = (DataMessage) message;
if(first){
first=false;
// Check whether output queue message structur complies to expected one
// Test whether only the expected components are within the outgoing queue
if(dm.getMetadata().size()!=3){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!dm.getMetadata().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
if(!dm.getMetadata().get(1).getId().equals("myid2")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!dm.getMetadata().get(2).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
}
dm.getData().get(0);
double res = ((Double)dm.getData().get(2)) - (Math.cos(((Double)dm.getData().get(1)))+Math.sin(((Double)dm.getData().get(0))));
if( Math.abs(res) > 0.000000001){
@@ -374,7 +378,7 @@ public class ManipulatorTest {
EventBus b = new EventBus();
b.register(manipulator);
DataMessage m = new DataMessage();
DataMessage m = new DataMessage(dmm);
m.getData().add(10d);
m.getData().add(0.2d);
b.post(m);
@@ -401,9 +405,9 @@ public class ManipulatorTest {
Channel<Double> channel = cservice.createChannel(new ChannelDescriptor<>(Double.class, TestChannels.ANALOG_OUT));
DataMessageMetadata dmm = new DataMessageMetadata();
dmm.getComponents().add(new ComponentMetadata("myid"));
dmm.getComponents().add(new ComponentMetadata("myid2"));
List<Metadata> dmm = new ArrayList<>();
dmm.add(new Metadata("myid"));
dmm.add(new Metadata("myid2"));
String id="cid";
String script = "import math\ndef process(o ,c,d):\n d.setValue("+setValue+")\n return math.cos(c) + math.sin(o)";
@@ -415,40 +419,47 @@ public class ManipulatorTest {
List<Manipulation> manipulations = new ArrayList<Manipulation>();
manipulations.add(manipulation);
Manipulator manipulator = new Manipulator(bus, dmm, manipulations);
// Check whether output queue message structur complies to expected one
DataMessageMetadata outMeta = manipulator.getMetadata();
// Test whether only the expected components are within the outgoing queue
if(outMeta.getComponents().size()!=3){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!outMeta.getComponents().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
if(!outMeta.getComponents().get(1).getId().equals("myid2")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!outMeta.getComponents().get(2).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
Manipulator manipulator = new Manipulator(bus, manipulations);
// Change something different on the channel than the value that will be set in the manipulator script
channel.setValue(setValue+1);
bus.register(new Object(){
boolean first = true;
@Subscribe
public void onMessage(Message message){
logger.info(message.toString());
if(message instanceof DataMessage){
DataMessage dm = (DataMessage) message;
if(first){
first=false;
// Check whether output queue message structur complies to expected one
// Test whether only the expected components are within the outgoing queue
if(dm.getMetadata().size()!=3){
fail("There are more than the expected components in the outgoing message");
}
// Test whether the id of the first component matches the expected id
if(!dm.getMetadata().get(0).getId().equals("myid")){
fail("Id of the first component does not match the expected id 'myid'");
}
if(!dm.getMetadata().get(1).getId().equals("myid2")){
fail("Id of the first component does not match the expected id 'myid'");
}
// Test whether the id of the second component (which was computed) matches the expected id
if(!dm.getMetadata().get(2).getId().equals("cid")){
fail("Id of the second component does not match the expected id 'cid'");
}
}
dm.getData().get(0);
double res = ((Double)dm.getData().get(2)) - (Math.cos(((Double)dm.getData().get(1)))+Math.sin(((Double)dm.getData().get(0))));
if( Math.abs(res) > 0.000000001){
@@ -461,7 +472,7 @@ public class ManipulatorTest {
EventBus b = new EventBus();
b.register(manipulator);
DataMessage m = new DataMessage();
DataMessage m = new DataMessage(dmm);
m.getData().add(10d);
m.getData().add(0.2d);
b.post(m);

View File

@@ -23,7 +23,6 @@ import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
@@ -33,6 +32,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.common.eventbus.Subscribe;
import ch.psi.fda.TestConfiguration;
import ch.psi.fda.core.loops.otf.OTFLoop;
import ch.psi.fda.core.loops.otf.OTFNamedChannelSensor;
@@ -40,7 +41,6 @@ import ch.psi.fda.core.loops.otf.OTFScalerChannelSensor;
import ch.psi.fda.core.loops.otf.TemplateOTF;
import ch.psi.fda.core.messages.ControlMessage;
import ch.psi.fda.core.messages.DataMessage;
import ch.psi.fda.core.messages.DataMessageMetadata;
import ch.psi.fda.core.messages.Message;
import ch.psi.fda.core.sensors.MillisecondTimestampSensor;
import ch.psi.jcae.Channel;
@@ -114,13 +114,37 @@ public class OTFLoopTest {
@Test
public void testExecute() throws InterruptedException, TimeoutException, ChannelException, ExecutionException {
Thread t = new Thread(new TestCollector(loop.getDataQueue().getQueue()));
t.start();
Object l = new Object(){
boolean first = true;
@Subscribe
public void onMessage(DataMessage m) {
if(first){
first = false;
int numberOfSensors = 5;
if(m.getMetadata().size() != numberOfSensors){
fail("Loop returned wrong number of components inside the data message metadata");
}
boolean fail = false;
for(int x=0;x<numberOfSensors; x++){
if(! m.getMetadata().get(x).getId().equals("id"+x)){
fail = true;
}
}
if(fail){
fail("Ids of the component metadata elements inside the data message metadata is not correct");
}
}
logger.info(m.toString());
}
};
loop.getEventBus().register(l);
loop.prepare();
loop.execute();
loop.cleanup();
if(statusChannel.getValue()!=1){
@@ -139,8 +163,7 @@ public class OTFLoopTest {
@Test
public void testExecuteZigZag() throws InterruptedException, TimeoutException, ChannelException, ExecutionException {
Thread t = new Thread(new TestCollector(loopZigZag.getDataQueue().getQueue()));
t.start();
loopZigZag.getEventBus().register(new TestCollector());
loopZigZag.prepare();
@@ -165,9 +188,7 @@ public class OTFLoopTest {
@Test
public void testExecuteAbort() throws InterruptedException, TimeoutException, ChannelException, ExecutionException {
// Data collector thread
Thread t = new Thread(new TestCollector(loop.getDataQueue().getQueue()));
t.start();
loop.getEventBus().register(new TestCollector());
// Thread to simulate asynchronous abort operation
Thread t1 = new Thread(new Runnable() {
@@ -194,57 +215,15 @@ public class OTFLoopTest {
}
}
/**
* Test method for {@link ch.psi.fda.core.loops.ActorSensorLoop#getDataMessageMetadata()}.
*/
@Test
public void testGetDataMessageMetadata(){
DataMessageMetadata m = loop.getDataQueue().getDataMessageMetadata();
int numberOfSensors = 5;
if(m.getComponents().size() != numberOfSensors){
fail("Loop returned wrong number of components inside the data message metadata");
}
boolean fail = false;
for(int x=0;x<numberOfSensors; x++){
if(! m.getComponents().get(x).getId().equals("id"+x)){
fail = true;
class TestCollector {
@Subscribe
public void onMessage(Message m) {
if (m instanceof DataMessage) {
DataMessage x = (DataMessage) m;
logger.info(x.toString());
} else if (m instanceof ControlMessage) {
logger.info("---- " + m.toString() + " ----");
}
}
if(fail){
fail("Ids of the component metadata elements inside the data message metadata is not correct");
}
}
class TestCollector implements Runnable{
private final BlockingQueue<Message> queue;
public TestCollector(BlockingQueue<Message> queue){
this.queue = queue;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
while(true){
Message m = queue.take();
if(m instanceof DataMessage){
DataMessage x = (DataMessage) m;
logger.info( x.toString() );
}
else if(m instanceof ControlMessage){
logger.info("---- "+m.toString()+" ----");
}
}
} catch (InterruptedException e) {
logger.log(Level.SEVERE, "An Exception occured while reading data from the data queue", e);
}
}
}
}