Cleanup ChannelAccessCondition* classes
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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<Object> {
|
||||
|
||||
// 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();
|
||||
|
||||
@@ -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<E> implements Action {
|
||||
|
||||
private final Channel<E> channel;
|
||||
private final E expectedValue;
|
||||
private final Comparator<E> comparator;
|
||||
private final Long timeout;
|
||||
|
||||
private volatile boolean abort = false;
|
||||
@@ -60,6 +62,19 @@ public class ChannelAccessCondition<E> implements Action {
|
||||
|
||||
this.channel = channel;
|
||||
this.expectedValue = expectedValue;
|
||||
this.comparator = null;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public ChannelAccessCondition(Channel<E> channel, E expectedValue, Comparator<E> 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<E> 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){
|
||||
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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<Integer> 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<Integer> 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<Integer>() {
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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<Integer> 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<Integer> 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<Integer>() {
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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<String> 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<String> 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<String>() {
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<E> implements Action {
|
||||
|
||||
@@ -93,5 +91,4 @@ public class ChannelAccessPut<E> implements Action {
|
||||
@Override
|
||||
public void abort() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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<String> channel = cservice.createChannel(new ChannelDescriptor<>(String.class, TestChannels.STRING_OUT));
|
||||
channel.setValue("SomeValue");
|
||||
ChannelAccessConditionRegex c = new ChannelAccessConditionRegex(channel, "Some.*", 1000l);
|
||||
ChannelAccessCondition<String> c = new ChannelAccessCondition<>(channel, "Some.*",new ComparatorREGEX(), 1000l);
|
||||
c.execute();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user