jadex.rules.eca.MethodCondition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-rules-eca Show documentation
Show all versions of jadex-rules-eca Show documentation
Small Java annotation-based event-condition-action rule engine.
package jadex.rules.eca;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import jadex.commons.IMethodParameterGuesser;
import jadex.commons.SimpleMethodParameterGuesser;
import jadex.commons.SimpleParameterGuesser;
import jadex.commons.Tuple2;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
/**
* Condition implementation that invokes a predefined method.
*/
public class MethodCondition implements ICondition
{
//-------- attributes --------
/** The object. */
protected Object object;
/** The method. */
protected Method method;
/** The invert flag. Inverts method result. */
protected boolean invert;
/** The parameter guesser. */
protected IMethodParameterGuesser guesser;
//-------- constructors --------
/**
* Create a new method condition.
*/
public MethodCondition(Object object, Method method)
{
this(object, method, false);
}
/**
* Create a new method condition.
*/
public MethodCondition(Object object, Method method, IMethodParameterGuesser guesser)
{
this(object, method, false, guesser);
}
/**
* Create a new method condition.
*/
public MethodCondition(Object object, Method method, boolean invert)
{
this(object, method, invert, null);
}
/**
* Create a new method condition.
*/
public MethodCondition(Object object, Method method, boolean invert,
IMethodParameterGuesser guesser)
{
// if(object==null)
// System.out.println("hetre");
this.object = object;
this.method = method;
this.invert = invert;
this.guesser = guesser;
}
//-------- methods --------
/**
* Evaluate the condition.
*/
public IFuture> evaluate(IEvent event)
{
Future> fut = new Future>();
try
{
CommandCondition.evaluateResult(invokeMethod(event)).addResultListener(new DelegationResultListener>(fut)
{
public void customResultAvailable(Tuple2 result)
{
if(invert)
{
Boolean b = result.getFirstEntity().booleanValue()? Boolean.FALSE: Boolean.TRUE;
result = new Tuple2(b, result.getSecondEntity());
}
super.customResultAvailable(result);
}
});
}
catch(Exception e)
{
fut.setException(e);
//throw SUtil.throwUnchecked(e);
}
return fut;
}
/**
* Do the method invocation.
*/
protected Object invokeMethod(IEvent event) throws Exception
{
method.setAccessible(true);
Object[] params = null;
List