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 da0f13f..15d3365 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 @@ -45,9 +45,6 @@ import ch.psi.fda.core.Actor; import ch.psi.fda.core.EngineConfiguration; import ch.psi.fda.core.Sensor; import ch.psi.fda.core.actions.ChannelAccessCondition; -import ch.psi.fda.core.actions.ChannelAccessConditionAnd; -import ch.psi.fda.core.actions.ChannelAccessConditionOr; -import ch.psi.fda.core.actions.ChannelAccessConditionRegex; import ch.psi.fda.core.actions.ChannelAccessPut; import ch.psi.fda.core.actions.Delay; import ch.psi.fda.core.actors.ChannelAccessFunctionActuator; @@ -122,6 +119,9 @@ import ch.psi.jcae.ChannelDescriptor; import ch.psi.jcae.ChannelException; import ch.psi.jcae.ChannelService; import ch.psi.jcae.impl.type.DoubleTimestamp; +import ch.psi.jcae.util.ComparatorAND; +import ch.psi.jcae.util.ComparatorOR; +import ch.psi.jcae.util.ComparatorREGEX; /** * Data acquisition engine for performing scans @@ -581,7 +581,7 @@ public class Acquisition { timeout = Math.round(ca.getTimeout()*1000); } if(type.equals("String")){ - alist.add(new ChannelAccessConditionRegex(createChannel(String.class, ca.getChannel()), ca.getValue(), timeout)); + alist.add(new ChannelAccessCondition<>(createChannel(String.class, ca.getChannel()), ca.getValue(), new ComparatorREGEX(), timeout)); } else{ logger.warning("Operation "+operation+" wity type "+type+" for action is not supported"); @@ -594,7 +594,7 @@ public class Acquisition { } if(type.equals("Integer")){ - alist.add(new ChannelAccessConditionOr(createChannel(Integer.class,ca.getChannel()), new Integer(ca.getValue()), timeout)); + alist.add(new ChannelAccessCondition<>(createChannel(Integer.class,ca.getChannel()), new Integer(ca.getValue()), new ComparatorOR(), timeout)); } else{ logger.warning("Operation "+operation+" wity type "+type+" for action is not supported"); @@ -606,7 +606,7 @@ public class Acquisition { timeout = Math.round(ca.getTimeout()*1000); } if(type.equals("Integer")){ - alist.add(new ChannelAccessConditionAnd(createChannel(Integer.class,ca.getChannel()), new Integer(ca.getValue()), timeout)); + alist.add(new ChannelAccessCondition<>(createChannel(Integer.class,ca.getChannel()), new Integer(ca.getValue()), new ComparatorAND(), timeout)); } else { logger.warning("Operation "+operation+" wity type "+type+" for action is not supported"); diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/ActorSetCallable.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/ActorSetCallable.java index 8357123..7c68591 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/ActorSetCallable.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/core/ActorSetCallable.java @@ -23,21 +23,15 @@ import java.util.concurrent.Callable; /** * Callable used for parallel execution of the set method of an actor - * @author ebner - * */ public class ActorSetCallable implements Callable { - // Callable actor private Actor actor; public ActorSetCallable(Actor actor){ this.actor = actor; } - /* (non-Javadoc) - * @see java.util.concurrent.Callable#call() - */ @Override public Object call() throws Exception { actor.set(); diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessCondition.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessCondition.java index ee64461..2756d91 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessCondition.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessCondition.java @@ -19,6 +19,7 @@ package ch.psi.fda.core.actions; +import java.util.Comparator; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; import java.util.logging.Logger; @@ -39,6 +40,7 @@ public class ChannelAccessCondition implements Action { private final Channel channel; private final E expectedValue; + private final Comparator comparator; private final Long timeout; private volatile boolean abort = false; @@ -60,6 +62,19 @@ public class ChannelAccessCondition implements Action { this.channel = channel; this.expectedValue = expectedValue; + this.comparator = null; + this.timeout = timeout; + } + + public ChannelAccessCondition(Channel channel, E expectedValue, Comparator comparator, Long timeout){ + + if(timeout != null && timeout<=0){ + throw new IllegalArgumentException("Timeout must be > 0"); + } + + this.channel = channel; + this.expectedValue = expectedValue; + this.comparator = comparator; this.timeout = timeout; } @@ -74,11 +89,21 @@ public class ChannelAccessCondition implements Action { try{ waitT = Thread.currentThread(); try { - if(timeout == null){ - channel.waitForValue(expectedValue); + if(comparator==null){ + if(timeout == null){ + channel.waitForValue(expectedValue); + } + else{ + channel.waitForValue(expectedValue, timeout); + } } else{ - channel.waitForValue(expectedValue, timeout); + if(timeout == null){ + channel.waitForValue(expectedValue, comparator); + } + else{ + channel.waitForValue(expectedValue, comparator, timeout); + } } } catch (ExecutionException | ChannelException | TimeoutException | InterruptedException e) { if(abort && e instanceof InterruptedException){ diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionAnd.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionAnd.java deleted file mode 100644 index 6c7cee6..0000000 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionAnd.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * - * Copyright 2010 Paul Scherrer Institute. All rights reserved. - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This code is distributed in the hope that it will be useful, - * but without any warranty; without even the implied warranty of - * merchantability or fitness for a particular purpose. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - * - */ - -package ch.psi.fda.core.actions; - -import java.util.Comparator; -import java.util.concurrent.ExecutionException; -import java.util.logging.Logger; - -import ch.psi.fda.core.Action; -import ch.psi.jcae.Channel; -import ch.psi.jcae.ChannelException; - -/** - * Condition a channnel needs to match - * Only accepts channels of type Integer - * - * @author ebner - * - */ -public class ChannelAccessConditionAnd implements Action { - - private static Logger logger = Logger.getLogger(ChannelAccessConditionAnd.class.getName()); - - private final Channel channel; - private final Integer expectedValue; - private final Long timeout; - - private volatile boolean abort = false; - private volatile Thread waitT = null; - - /** - * Constructor - * @param channel Channel to wait for - * @param expectedValue Value to wait for - * @param timeout Timeout of the condition in milliseconds (null accepted - will take default wait timeout for channels ch.psi.jcae.ChannelBeanFactory.waitTimeout) - * - * @throws IllegalArgumentException Timeout specified is not >=0 - */ - public ChannelAccessConditionAnd(Channel channel, Integer expectedValue, Long timeout){ - if(timeout !=null && timeout<=0){ - throw new IllegalArgumentException("Timeout must be > 0"); - } - this.channel=channel; - this.expectedValue = expectedValue; - this.timeout = timeout; - } - - - /** - * @throws RuntimeException Channel value did not reach expected value (within the specified timeout period) - */ - @Override - public void execute() throws InterruptedException { - abort=false; - logger.finest("Checking channel "+channel.getName()+" for value "+expectedValue+" [timeout: "+timeout+"]" ); - try{ - waitT = Thread.currentThread(); - try { - channel.waitForValue(expectedValue, new Comparator() { - - @Override - public int compare(Integer o1, Integer o2) { - int one = o1; - int two = o2; - if((one & two) != 0){ - return 0; - } - return 1; - } - } - , timeout); - } catch (ExecutionException | ChannelException e) { - if(abort && e instanceof ExecutionException){ - return; - } - throw new RuntimeException("Channel [name:"+channel.getName()+"] did not reach expected value "+expectedValue+" ", e); - } - } - finally{ - waitT=null; - } - } - - @Override - public void abort() { - abort=true; - if(waitT!=null){ - waitT.interrupt(); - } - } -} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionOr.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionOr.java deleted file mode 100644 index a44447a..0000000 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionOr.java +++ /dev/null @@ -1,110 +0,0 @@ -/** - * - * Copyright 2010 Paul Scherrer Institute. All rights reserved. - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This code is distributed in the hope that it will be useful, - * but without any warranty; without even the implied warranty of - * merchantability or fitness for a particular purpose. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - * - */ - -package ch.psi.fda.core.actions; - -import java.util.Comparator; -import java.util.concurrent.ExecutionException; -import java.util.logging.Logger; - -import ch.psi.fda.core.Action; -import ch.psi.jcae.Channel; -import ch.psi.jcae.ChannelException; - -/** - * Or condition of a channel - * Only supports Integer values and channel - * - * @author ebner - * - */ -public class ChannelAccessConditionOr implements Action { - - private static Logger logger = Logger.getLogger(ChannelAccessConditionOr.class.getName()); - - private final Channel channel; - private final Integer expectedValue; - private final Long timeout; - - private volatile boolean abort = false; - private volatile Thread waitT = null; - - /** - * Constructor - * @param channel - * @param expectedValue Value to wait for - * @param timeout Timeout of the condition in milliseconds (null accepted - will take default wait timeout for channels ch.psi.jcae.ChannelBeanFactory.waitTimeout) - * @throws IllegalArgumentException Timeout specified is not >=0 - */ - public ChannelAccessConditionOr(Channel channel, Integer expectedValue, Long timeout){ - - if(timeout !=null && timeout<=0){ - throw new IllegalArgumentException("Timeout must be > 0"); - } - - this.channel = channel; - this.expectedValue = expectedValue; - this.timeout = timeout; - } - - - /** - * @throws InterruptedException - * @throws RuntimeException Channel value did not reach expected value (within the specified timeout period) - */ - @Override - public void execute() throws InterruptedException { - abort=false; - logger.finest("Checking channel "+channel.getName()+" for value "+expectedValue+" [timeout: "+timeout+"]" ); - try{ - waitT = Thread.currentThread(); - try { - channel.waitForValue(expectedValue, new Comparator() { - - @Override - public int compare(Integer o1, Integer o2) { - int one = o1; - int two = o2; - if((one | two) != 0){ - return 0; - } - return 1; - } - } - , timeout); - } catch (ExecutionException | ChannelException e) { - if(abort && e instanceof ExecutionException){ - return; - } - throw new RuntimeException("Channel [name:"+channel.getName()+"] did not reach expected value "+expectedValue+" ", e); - } - } - finally{ - waitT=null; - } - } - - @Override - public void abort() { - abort=true; - if(waitT!=null){ - waitT.interrupt(); - } - } -} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionRegex.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionRegex.java deleted file mode 100644 index b6374bf..0000000 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessConditionRegex.java +++ /dev/null @@ -1,104 +0,0 @@ -/** - * - * Copyright 2010 Paul Scherrer Institute. All rights reserved. - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This code is distributed in the hope that it will be useful, - * but without any warranty; without even the implied warranty of - * merchantability or fitness for a particular purpose. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - * - */ - -package ch.psi.fda.core.actions; - -import java.util.Comparator; -import java.util.concurrent.ExecutionException; -import java.util.logging.Logger; - -import ch.psi.fda.core.Action; -import ch.psi.jcae.Channel; -import ch.psi.jcae.ChannelException; - -/** - * Regex condition - * Only supports String value/channel. - * @author ebner - * - */ -public class ChannelAccessConditionRegex implements Action { - - private static Logger logger = Logger.getLogger(ChannelAccessConditionRegex.class.getName()); - - private final Channel channel; - private final String expectedValue; - private final Long timeout; - - private volatile boolean abort = false; - private volatile Thread waitT = null; - - /** - * Constructor - * @param channel - * @param expectedValue Value to wait for - * @param timeout Timeout of the condition in milliseconds (null accepted - will take default wait timeout for channels ch.psi.jcae.ChannelBeanFactory.waitTimeout) - * - * @throws IllegalArgumentException Timeout specified is not >=0 - */ - public ChannelAccessConditionRegex(Channel channel, String expectedValue, Long timeout){ - - if(timeout !=null && timeout<=0){ - throw new IllegalArgumentException("Timeout must be > 0"); - } - - this.channel=channel; - this.expectedValue = expectedValue; - this.timeout = timeout; - } - - - /** - * @throws InterruptedException - * @throws RuntimeException Channel value did not reach expected value (within the specified timeout period) - */ - @Override - public void execute() throws InterruptedException { - abort=false; - logger.finest("Checking channel "+channel.getName()+" for value "+expectedValue+" [timeout: "+timeout+"]" ); - try{ - waitT = Thread.currentThread(); - try { - channel.waitForValue(expectedValue, new Comparator() { - - @Override - public int compare(String o1, String o2) { - return o1.matches(o2) ? 0:1; - } - }, timeout); - } catch (ExecutionException | ChannelException e) { - if(abort && e instanceof ExecutionException){ - return; - } - throw new RuntimeException("Channel [name:"+channel.getName()+"] did not reach expected value "+expectedValue+" ", e); - } // Workaround use 10seconds default set timeout to check several times whether the channel has reached the value - } - finally{ - waitT=null; - } - } - - @Override - public void abort() { - abort=true; - if(waitT!=null){ - waitT.interrupt(); - } - } -} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessPut.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessPut.java index 0675cc7..8c71ca3 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessPut.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/ChannelAccessPut.java @@ -31,8 +31,6 @@ import ch.psi.jcae.ChannelException; /** * Perform a put on the specified Channel Access channel. The put can be done synchronous or * asynchronously. - * @author ebner - * */ public class ChannelAccessPut implements Action { @@ -93,5 +91,4 @@ public class ChannelAccessPut implements Action { @Override public void abort() { } - } diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/Delay.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/Delay.java index 00da829..b799823 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/Delay.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/core/actions/Delay.java @@ -23,8 +23,6 @@ import ch.psi.fda.core.Action; /** * Wait a specific time until executing the next action ... - * @author ebner - * */ public class Delay implements Action { diff --git a/ch.psi.fda/src/test/java/ch/psi/fda/core/actions/ChannelAccessConditionTest.java b/ch.psi.fda/src/test/java/ch/psi/fda/core/actions/ChannelAccessConditionTest.java index 560617f..91e86f7 100644 --- a/ch.psi.fda/src/test/java/ch/psi/fda/core/actions/ChannelAccessConditionTest.java +++ b/ch.psi.fda/src/test/java/ch/psi/fda/core/actions/ChannelAccessConditionTest.java @@ -38,6 +38,7 @@ import ch.psi.jcae.ChannelDescriptor; import ch.psi.jcae.ChannelException; import ch.psi.jcae.ChannelService; import ch.psi.jcae.impl.DefaultChannelService; +import ch.psi.jcae.util.ComparatorREGEX; /** * @author ebner @@ -79,7 +80,7 @@ public class ChannelAccessConditionTest { public void testChannelAccessStringConditionRegex() throws InterruptedException, CAException, ExecutionException, ChannelException, TimeoutException { final Channel channel = cservice.createChannel(new ChannelDescriptor<>(String.class, TestChannels.STRING_OUT)); channel.setValue("SomeValue"); - ChannelAccessConditionRegex c = new ChannelAccessConditionRegex(channel, "Some.*", 1000l); + ChannelAccessCondition c = new ChannelAccessCondition<>(channel, "Some.*",new ComparatorREGEX(), 1000l); c.execute(); }