jadex.bdi.examples.shop.BuyItemPlan Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-bdi Show documentation
Show all versions of jadex-applications-bdi Show documentation
The Jadex BDI applications package contain
several example applications, benchmarks and
testcases using BDI agents.
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));
}
}