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

jadex.bdi.examples.shop.BuyItemPlan 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.examples.shop;

import jadex.bdi.runtime.Plan;
import jadex.commons.future.IFuture;

/**
 *  Buy a specific item in a given shop.
 */
public class BuyItemPlan extends Plan
{
	/**
	 *  The plan body.
	 */
	public void body()
	{
		// Fetch shop and item data
		IShopService shop = (IShopService)getParameter("shop").getValue();
		String name	= (String)getParameter("name").getValue();
		double price = ((Double)getParameter("price").getValue()).doubleValue();
		double money = ((Double)getBeliefbase().getBelief("money").getFact()).doubleValue();

		// Check if enough money to buy the item
		if(money	future	= shop.buyItem(name, price);
//		System.out.println(getComponentName()+" getting item: "+future);
		ItemInfo item = (ItemInfo)future.get(this);
//		System.out.println(getComponentName()+" bought item: "+item);
		getParameter("result").setValue(item);
		
		// Update the customer inventory 
		ItemInfo ii = (ItemInfo)getBeliefbase().getBeliefSet("inventory").getFact(item);
		if(ii==null)
		{
			ii = new ItemInfo(name, price, 1);
			getBeliefbase().getBeliefSet("inventory").addFact(ii);
		}
		else
		{
			ii.setQuantity(ii.getQuantity()+1);
			getBeliefbase().getBeliefSet("inventory").modified(ii);
		}
		
		// Update the account
		// Re-read money, could have changed due to executed sell plan
		money = ((Double)getBeliefbase().getBelief("money").getFact()).doubleValue();
		getBeliefbase().getBelief("money").setFact(new Double(money-price));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy