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

jadex.bdi.planlib.iasteps.DispatchGoalStep Maven / Gradle / Ivy

Go to download

The Jadex applib BDI package contains ready to use functionalities for BDI agents mostly in form of modules called capabilities.

The newest version!
package jadex.bdi.planlib.iasteps;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import jadex.bdiv3.runtime.IGoal;
import jadex.bdiv3x.features.IBDIXAgentFeature;
import jadex.bdiv3x.runtime.IParameter;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.commons.future.DefaultResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;

public class DispatchGoalStep implements IComponentStep>
{
	protected String goaltype;
	protected Map parameters;
	
	/**
	 *  Dispatches a goal.
	 *  @param goal The goal.
	 */
	public DispatchGoalStep(String goal)
	{
		this(goal, null);
	}
	
	/**
	 *  Dispatches a goal.
	 *  @param goal The goal.
	 *  @param parameterName Name of a goal parameter.
	 *  @param parameterValue Value of the goal parameter.
	 */
	public DispatchGoalStep(String goal, final String name, final Object value)
	{
		this(goal, new HashMap() 
		{
			{
				put(name, value);
			}
		});
	}
	
	/**
	 *  Dispatches a goal.
	 *  @param goal The goal.
	 *  @param parameters The goal parameters.
	 */
	public DispatchGoalStep(String goal, Map parameters)
	{
		this.goaltype = goal;
		this.parameters = parameters;
	}
	
	public IFuture> execute(IInternalAccess ia)
	{
		final IGoal goal = ia.getFeature(IBDIXAgentFeature.class).getGoalbase().createGoal(goaltype);
		if(parameters != null)
		{
			for(Iterator> it = parameters.entrySet().iterator(); it.hasNext(); )
			{
				Map.Entry paramEntry = (Map.Entry) it.next();
				goal.getParameter((String) paramEntry.getKey()).setValue(paramEntry.getValue());
			}
		}
		
		final Future goalFuture = new Future();
//		goal.addGoalListener(new IGoalListener()
//		{
//			public void goalFinished(AgentEvent ae)
//			{
//				goalFuture.setResult(goal.getParameters());
//			}
//			
//			public void goalAdded(AgentEvent ae)
//			{
//			}
//		});
		ia.getFeature(IBDIXAgentFeature.class).getGoalbase().dispatchTopLevelGoal(goal)
		.addResultListener(new IResultListener()
		{
			public void exceptionOccurred(Exception exception)
			{
				goalFuture.setException(goal.getException());
			}
			
			public void resultAvailable(Object result)
			{
				goalFuture.setResult(goal.getParameters());
			}
		});
		
		final Future> ret = new Future>();
		goalFuture.addResultListener(new DefaultResultListener()
		{
			public void resultAvailable(IParameter[] params)
			{
				Map results = new HashMap();
				for (int i = 0; i < params.length; ++i)
				{
//					String dir = ((IMParameter) params[i].getModelElement()).getDirection();
					//System.out.println(params[i].getName() + " " + params[i].getValue() + " " + dir);
					//if (OAVBDIMetaModel.PARAMETER_DIRECTION_INOUT.equals(dir) ||
						//OAVBDIMetaModel.PARAMETER_DIRECTION_OUT.equals(dir))
					results.put(params[i].getName(), params[i].getValue());
				}
				ret.setResult(results);
			}
		});
		
		return ret;
	}
}