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

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

/**
 *  Plan for selling an item.
 */
public class SellItemPlan extends Plan
{
	/**
	 *  The plan body.
	 */
	public void body()
	{
		// Fetch item data.
		String name = (String)getParameter("name").getValue();
		double price = ((Double)getParameter("price").getValue()).doubleValue();
		ItemInfo ii = (ItemInfo)getBeliefbase().getBeliefSet("catalog").getFact(new ItemInfo(name));
		
		// Check if enough money is given and it is in stock.
		if(ii.getQuantity()>0 && ii.getPrice()<=price)
		{
			// Sell item by updating catalog and account
//			System.out.println(getComponentName()+" sell item: "+name+" for: "+price);
			getParameter("result").setValue(new ItemInfo(name, ii.getPrice(), 1));
			ii.setQuantity(ii.getQuantity()-1);
			getBeliefbase().getBeliefSet("catalog").modified(ii);
			
			double money = ((Double)getBeliefbase().getBelief("money").getFact()).doubleValue();
			getBeliefbase().getBelief("money").setFact(new Double(money+price));
		}
		else if(ii.getQuantity()==0)
		{
			fail(new RuntimeException("Item not in store: "+name));
		}
		else
		{
			fail(new RuntimeException("Payment not sufficient: "+price));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy