Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

Receive a contract net protocol (cnp) and answer it.
 
 public class CNPReceiverPlan extends AbstractReceiverPlan
 {
The body method is called on the instatiated plan instance from the scheduler.
 
 	public void body()
 	{
 		long timeout;
 		if(getBeliefbase().containsBelief("timeout") && getBeliefbase().getBelief("timeout").getFact()!=null)
 		{
 			timeout = ((Long)getBeliefbase().getBelief("timeout").getFact()).longValue();
 		}
 		else
 		{
 			timeout = -1;
 		}
 
 		Object cfp;
 		IMessageEvent reply;
 		List records = SCollection.createArrayList();
 		Object[] proposal = null;
 
 		for(int i=0; me.getType().equals(getShortProtocolName()+"_cfp"
 			&& (i==0 || isIterated()); i++)
 		{
 			getLogger().info("Negotiation round: "+i+" receiver got cfp: "+me);
 			records.add(me);
 
 			Exception ex = null;
 			try
 			{
 				proposal = makeProposal(cfpinitiator); // todo: include information about negotiation round
 			}
 			catch(Exception e)
 			{
 				ex = e;
 				getLogger().info("No proposal made due to exception: "+e);
 				//e.printStackTrace();
 			}
 			if(ex!=null || proposal == null || proposal[0]==null)
 			{
 				reply = getEventbase().createReply(megetShortProtocolName()+"_refuse");
 				getLogger().info("No proposal made. Finished.");
 				sendMessage(reply);
 				return// todo: ?
 				//fail();
 			}
 
 			reply = getEventbase().createReply(megetShortProtocolName()+"_propose");
 			reply.getParameter(.).setValue(proposal[0]);
 			getLogger().info("Receiver sent proposal: "+reply);
 			me = sendMessageAndWait(replytimeout);
 		}
 
 		if(me.getType().equals(getShortProtocolName()+"_accept"))
 		{
 			getLogger().info(getComponentName()+" excuting the cnp task.");
 			try
 			{
 				Object result = executeTask(proposal[0], proposal[1], initiator);
 				reply = getEventbase().createReply(megetShortProtocolName()+"_inform");
 				reply.getParameter(.).setValue(result); // todo: how to put in the Done()???
 				getLogger().info("Receiver sent done: "+reply);
 				sendMessage(reply);
 				getParameter("result").setValue(result);
 			}
 			catch(Exception e)
 			{
 				reply = getEventbase().createReply(me,getShortProtocolName()+"_failure");
 				getLogger().info("Receiver sent failure: "+reply);
 				sendMessage(reply);
 			}
 		}
 		else
 		{
 			getLogger().info("Proposal rejected. Finished.");
 		}
 	}

Test if it is the iterated contract-net version.

Returns:
True, if is is the iterated version.
	protected boolean isIterated()
	{
		return ((Boolean)getParameter("iterated").getValue()).booleanValue();	
	}

Get protocol abbrev name.

Returns:
The protocol abbrev name.
	{
		String ret = "cnp";
			ret = "icnp";
		return ret;
	}

Make a proposal based on the task description.

Parameters:
cfp The cfp including the task to execute.
Returns:
The proposal for executing the task.
	public Object[] makeProposal(Object cfpIComponentIdentifier initiator)
	{
		IGoal make_proposal = createGoal(getShortProtocolName()+"_make_proposal");
		make_proposal.getParameter("cfp").setValue(cfp);
		make_proposal.getParameter("initiator").setValue(initiator);
		dispatchSubgoalAndWait(make_proposal);
		return new Object[]{make_proposal.getParameter("proposal").getValue(),
			make_proposal.getParameter("proposal_info").getValue()};
	}

Execute the task.

Parameters:
proposal The proposal.
proposal_info The proposal info.
Returns:
The result of the task.
	public Object executeTask(Object proposalObject proposal_infoIComponentIdentifier initiator)
	{
		IGoal execute_task = createGoal(getShortProtocolName()+"_execute_task");
		execute_task.getParameter("proposal").setValue(proposal);
		execute_task.getParameter("proposal_info").setValue(proposal_info);
		execute_task.getParameter("initiator").setValue(initiator);
		return execute_task.getParameter("result").getValue();
	}
New to GrepCode? Check out our FAQ X