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

jadex.bdi.examples.booktrading.serviceimpl.buyer.PurchaseBookPlan Maven / Gradle / Ivy

Go to download

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

The newest version!
package jadex.bdi.examples.booktrading.serviceimpl.buyer;

import jadex.bdi.examples.booktrading.serviceimpl.IBuyBookService;
import jadex.bdi.examples.booktrading.common.NegotiationReport;
import jadex.bdi.examples.booktrading.common.Order;
import jadex.bdi.runtime.Plan;
import jadex.commons.Tuple2;
import jadex.commons.future.CollectionResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IResultListener;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;

/**
 * The plan tries to purchase a book.
 */
public class PurchaseBookPlan extends Plan
{
	//-------- methods --------

	/**
	 * The body method is called on the
	 * instatiated plan instance from the scheduler.
	 */
	public void body()
	{
//		System.out.println("PurchaseBookPlan");
		
		// Get order properties and calculate acceptable price.
		Order order = (Order)getParameter("order").getValue();
		double time_span = order.getDeadline().getTime() - order.getStartTime();
		double elapsed_time = getTime() - order.getStartTime();
		double price_span = order.getLimit() - order.getStartPrice();
		int acceptable_price = (int)(price_span * elapsed_time / time_span)
			+ order.getStartPrice();

		// Find available seller agents.
		IBuyBookService[]	services	= getServiceContainer().getRequiredServices("buyservice").get(this).toArray(new IBuyBookService[0]);
		if(services.length == 0)
		{
//			System.out.println("No seller found, purchase failed.");
			generateNegotiationReport(order, null, acceptable_price);
			fail();
		}

		// Initiate a call-for-proposal.
		Future>>	cfp	= new Future>>();
		final CollectionResultListener>	crl	= new CollectionResultListener>(services.length, true,
			new DelegationResultListener>>(cfp));
		for(int i=0; i()
			{
				public void resultAvailable(Integer result)
				{
					crl.resultAvailable(new Tuple2(seller, result));
				}
				
				public void exceptionOccurred(Exception exception)
				{
					crl.exceptionOccurred(exception);
				}
			});
		}
		// Sort results by price.
		Tuple2[]	proposals	= cfp.get(this).toArray(new Tuple2[0]);
		Arrays.sort(proposals, new Comparator>()
		{
			public int compare(Tuple2 o1, Tuple2 o2)
			{
				return o1.getSecondEntity().compareTo(o2.getSecondEntity());
			}
		});
		
		// Do we have a winner?
		if(proposals.length>0 && proposals[0].getSecondEntity().intValue()<=acceptable_price)
		{
			proposals[0].getFirstEntity().acceptProposal(order.getTitle(), proposals[0].getSecondEntity().intValue()).get(this);
			
			generateNegotiationReport(order, proposals, acceptable_price);
			
			// If contract-net succeeds, store result in order object.
			order.setState(Order.DONE);
			order.setExecutionPrice(proposals[0].getSecondEntity());
			order.setExecutionDate(new Date(getTime()));
		}
		else
		{
			generateNegotiationReport(order, proposals, acceptable_price);
			
			fail();
		}
		//System.out.println("result: "+cnp.getParameter("result").getValue());
	}
	
	/**
	 *  Generate and add a negotiation report.
	 */
	protected void generateNegotiationReport(Order order, Tuple2[] proposals, double acceptable_price)
	{
		String report = "Accepable price: "+acceptable_price+", proposals: ";
		if(proposals!=null)
		{
			for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy