All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.rules.eca.CommandCondition Maven / Gradle / Ivy

There is a newer version: 4.0.267
Show newest version
package jadex.rules.eca;

import jadex.commons.IResultCommand;
import jadex.commons.Tuple2;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;

/**
 *  Command version of the condition.
 */
public class CommandCondition implements ICondition
{
	/** The command. */
	protected IResultCommand command;
	
	/**
	 * 
	 */
	public CommandCondition(IResultCommand command)
	{
		this.command = command;
	}
	
	/**
	 * 
	 */
	public IFuture> evaluate(IEvent event)
	{
		Object res = command.execute(event);
		return evaluateResult(res);
	}
	
	/**
	 * 
	 */
	public static IFuture> evaluateResult(Object res)
	{
		final Future> ret = new Future>();
		if(res==null)
		{
			ret.setResult(new Tuple2(Boolean.FALSE, null));
		}
		else if(res instanceof Tuple2)
		{
			ret.setResult((Tuple2)res);
		}
		else if(res instanceof Boolean)
		{
			boolean bs = ((Boolean)res).booleanValue();
			ret.setResult(bs? ICondition.TRUE: ICondition.FALSE);
		}
		else if(res instanceof Future)
		{
			((Future)res).addResultListener(new ExceptionDelegationResultListener>(ret)
			{
				public void customResultAvailable(Object res)
				{
					evaluateResult(res).addResultListener(new DelegationResultListener>(ret));
				}
			});
		}
		else
		{
			ret.setResult(new Tuple2(Boolean.TRUE, res));
		}
		return ret;
	}
}