Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jadex.bdi.planlib.protocols.dutchauction;
  
 
 import java.util.List;

This plan implements the receiver of the "FIPA Dutch Auction Interaction Protocol Specification" (XC00032 - Experimental). A dutch auction is one where the auctioneer starts with a high start price and continually lowers it until the first bidder accepts the price.
 
 public class DAReceiverPlan extends AbstractReceiverPlan
 {
The plan body.
 
 	public void body()
 	{
 		// Fetch the auction information.
 		
 		// HACK!!! Problem, there is no reply message sent in this protocol. 
 		// So that agent has no open active conversations.
 		IMessageEvent	dummycreateMessageEvent("da_propose");	// Hack??? Need some conversation message to wait for
 		
 		getLogger().info(getComponentName()+": Received inform_start_auction message with auction description " +
 			"start time: "+auctiondesc.getStarttime()+" Round time "+auctiondesc.getRoundTimeout()
 			+" topic: "+auctiondesc.getTopic());
 		if(auctiondesc.getRoundTimeout()<=0)
 		{
 			getLogger().warning(getComponentName()+"No round timeout specified");
 			fail();
 		}
 		
 		// Offer the possibility to decide not to participate in the auction
 		boolean participate = ((Boolean)tmp[0]).booleanValue();
 		Object auctioninfo = tmp[1];
 
 		long buftimeout = (long)(auctiondesc.getRoundTimeout()*1.1);
 		long firsttimeout = auctiondesc.getStarttime()==0 || (auctiondesc.getStarttime()-getTime()<=0)
 			? -1 : auctiondesc.getStarttime()-getTime()+buftimeout;
 	
 		List offers = SCollection.createArrayList();
 		boolean running = true;
 		Object winning_offer = null// my winning offer
 		Object auction_wo = null// the winning offer of the auction
 		int missing_cnt = 0;
 		
 		while(participate && running)
 		{
 			try
 			{
 				getLogger().info(getComponentName()+" waiting for: "+(firsttimeout==-1? buftimeoutfirsttimeout));
 				IMessageEvent msg = (IMessageEvent)waitForReply(dummyfirsttimeout==-1? buftimeoutfirsttimeout);
 				getLogger().info(getComponentName()+" received cfp: "+msg.getParameter(.).getValue());
 				missing_cnt = 0; // Reset missing_cnt as auction continues
 				firsttimeout=-1;
 				
 				if(msg.getType().equals("da_cfp"))
 				{
 					handleCFP(msgauctiondescauctioninfooffers);
 				}
 				else if(msg.getType().equals("da_accept_proposal"))
 				{
 					winning_offer = msg.getParameter(.).getValue();
 					running = false;
 				}
 				else if(msg.getType().equals("da_reject_proposal"))
 				{
 					winning_offer = null;
 				}
 				else if(msg.getType().equals("da_inform_end_auction"))
 				{
 					
 					if(!((Boolean)res[0]).booleanValue())
 						winning_offer = null;
 					
 					auction_wo = res[1];
 					running = false;
 				}
 				else
 				{
 					getLogger().warning("Could not understand: "+msg+" "+msg.getType());
 				}
			}
			{
				// Exit when no offers are received any more (for 3 times).
				//System.out.println(getAgentName()+" missed cfp: "+missing_cnt);
				if(++missing_cnt==3)
					running = false
			}
		}
		if(!running)
			getParameter("result").setValue(new Object[]{winning_offerauction_wo});
	}

Decide about participation. If the goal is not handled participation is true.

Parameters:
auctiondesc The auction description.
Returns:
The participation state (Boolean) and the local auction info (Object).
	protected Object[] decideParticipation(AuctionDescription auctiondescIComponentIdentifier initiator)
	{
		Object[] ret = new Object[2];
		ret[0] = .// participate
		ret[1] = null// auction info
		try
		{
			IGoal dp = getScope().getGoalbase().createGoal("da_decide_participation");
			dp.getParameter("auction_description").setValue(auctiondesc);
			dp.getParameter("initiator").setValue(initiator);
			ret[1] = dp.getParameter("auction_info").getValue();
			Boolean part = (Boolean)dp.getParameter("participate").getValue();
			ret[0] = part==null.part;
		}
		{
			// Participate if no explicit decision was made.
			getLogger().info("Optional goal ea_decide_request has not been handled.");
		}
		return ret;
	}

Handle a cfp message.

Parameters:
auctiondesc The auction description.
Returns:
The participation state (Boolean) and the local auction info (Object).
	protected Object[] handleCFP(IMessageEvent cfpAuctionDescription auctiondesc
			Object auctioninfoList offers)
	{
		//System.out.println("cfp: "+cfp.getContent());
		Object[] ret = new Object[2];
		ret[0] = .// participate
		ret[1] = auctioninfo;
		// Instantiate make_proposal-goal with the offer of the received CFP.
		IGoal mp = createGoal("da_make_proposal");
		offers.add(offer);
		mp.getParameter("cfp").setValue(offer);
		mp.getParameter("auction_description").setValue(auctiondesc);
		mp.getParameter("auction_info").setValue(auctioninfo);
		mp.getParameterSet("history").addValues(offers.toArray());
		try
		{
		}
		catch(Exception e)
		{
		}
		ret[1] = mp.getParameter("auction_info").getValue();
		Boolean leave = (Boolean)mp.getParameter("leave").getValue();
		Boolean accept = (Boolean)mp.getParameter("accept").getValue();
		if(leave!=null && leave.booleanValue())
		{
			getLogger().info(getComponentName() + " informs the initiator of the auction "
				+auctiondesc.getTopic()+" that it doesn't want to participate.");
			sendMessage(getEventbase().createReply(cfp"da_not_understood"));
			ret[0] = .;
		}
		else if(accept!=null && accept.booleanValue())
		{
			// System.out.println(getAgentName()+" sending proposal: "+offer);
			// Send propsal.
			sendMessage(getEventbase().createReply(cfp"da_propose"));
			getLogger().info(getComponentName()+" accepted proposal: "+cfp.getParameter(.).getValue());
		}
		else
		{
			getLogger().info(getComponentName()+" does not accept proposal and waits: "+cfp.getParameter(.).getValue());
		}
		return ret;
	}
New to GrepCode? Check out our FAQ X