diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/otf/OTFLoop.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/otf/OTFLoop.java index f8819b1..24db595 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/otf/OTFLoop.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/otf/OTFLoop.java @@ -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 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; /** * @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(); this.postActions = new ArrayList(); this.sensors = new ArrayList(); - - this.dataQueue = new LinkedBlockingQueue(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; + } } diff --git a/ch.psi.fda/src/test/java/ch/psi/fda/aq/ManipulatorTest.java b/ch.psi.fda/src/test/java/ch/psi/fda/aq/ManipulatorTest.java index 106659d..3c1bb95 100644 --- a/ch.psi.fda/src/test/java/ch/psi/fda/aq/ManipulatorTest.java +++ b/ch.psi.fda/src/test/java/ch/psi/fda/aq/ManipulatorTest.java @@ -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 mapping = new ArrayList(); - 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 manipulations = new ArrayList(); - 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 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 manipulations = new ArrayList(); 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 dmm = new ArrayList<>(); + dmm.add(new Metadata("myid")); String id="cid"; @@ -226,33 +212,44 @@ public class ManipulatorTest { List manipulations = new ArrayList(); 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 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 manipulations = new ArrayList(); 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 dmm = new ArrayList<>(); + dmm.add(new Metadata("myid")); + dmm.add(new Metadata("myid2")); String id="cid"; @@ -330,37 +327,44 @@ public class ManipulatorTest { List manipulations = new ArrayList(); 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 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 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 manipulations = new ArrayList(); 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); diff --git a/ch.psi.fda/src/test/java/ch/psi/fda/core/loops/OTFLoopTest.java b/ch.psi.fda/src/test/java/ch/psi/fda/core/loops/OTFLoopTest.java index 2b36026..ae9afe4 100644 --- a/ch.psi.fda/src/test/java/ch/psi/fda/core/loops/OTFLoopTest.java +++ b/ch.psi.fda/src/test/java/ch/psi/fda/core/loops/OTFLoopTest.java @@ -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 queue; - public TestCollector(BlockingQueue 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); - } - } - } }