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

jadex.bdi.testcases.ResultPlan Maven / Gradle / Ivy

Go to download

The Jadex BDI applications package contain several example applications, benchmarks and testcases using BDI agents.

There is a newer version: 2.4
Show newest version
package jadex.bdi.testcases;

import jadex.bdi.runtime.Plan;

/**
 *  This plan waits for a specified time,
 *  sets the assigned result as goal state and finishes.
 */
public class ResultPlan extends Plan
{
	//-------- attributes --------

	/** The waiting time. */
	protected long	wait	= -1;

	/** The result state. */
	protected boolean	success	= true;

	/** The result value (if any). */
	protected Object	value;

	/** The belief name for storing the result. */
	protected String	belief;
	
	//-------- constructors --------

	/**
	 *  Create a new result plan.
	 */
	public ResultPlan()
	{
		if(hasParameter("wait"))
			wait	= ((Number)getParameter("wait").getValue()).longValue();
		if(hasParameter("success"))
			success	= ((Boolean)getParameter("success").getValue()).booleanValue();
		if(hasParameter("value"))
			value	= getParameter("value").getValue();
		if(hasParameter("belief"))
			belief	= (String)getParameter("belief").getValue();
	}

	//-------- methods --------

	/**
	 *  The plan body.
	 */
	public void body()
	{
		if(wait>-1)
			waitFor(wait);
		
		if(belief!=null)
		{
			if(getBeliefbase().containsBelief(belief))
			{
				getBeliefbase().getBelief(belief).setFact(value);
			}
			else if(getBeliefbase().containsBeliefSet(belief))
			{
				getBeliefbase().getBeliefSet(belief).addFact(value);
			}
			else
			{
				getLogger().info("Could not find belief(set): "+belief);
				fail();
			}
		}
		getLogger().info("Plan: "+this+" finished with state "+success
				+ (value==null?"":", result is "+value));

		if(hasParameter("result"))
			getParameter("result").setValue(value);

		if(!success)
			fail();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy