/** * *************************************************************** * JADE - Java Agent DEvelopment Framework is a framework to develop * multi-agent systems in compliance with the FIPA specifications. * Copyright (C) 2000 CSELT S.p.A. * * GNU Lesser General Public License * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation, * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * ************************************************************** */ package examples.protocols; import jade.core.Agent; import jade.lang.acl.ACLMessage; import jade.lang.acl.MessageTemplate; import jade.proto.ContractNetResponder; import jade.domain.FIPANames; import jade.domain.FIPAAgentManagement.NotUnderstoodException; import jade.domain.FIPAAgentManagement.RefuseException; import jade.domain.FIPAAgentManagement.FailureException; /** This example shows how to implement the responder role in a FIPA-contract-net interaction protocol. In this case in particular we use a <code>ContractNetResponder</code> to participate into a negotiation where an initiator needs to assign a task to an agent among a set of candidates. @author Giovanni Caire - TILAB */ public class ContractNetResponderAgent extends Agent { protected void setup() { System.out.println("Agent "+getLocalName()+" waiting for CFP..."); MessageTemplate template = MessageTemplate.and( MessageTemplate.MatchProtocol(FIPANames.InteractionProtocol.FIPA_CONTRACT_NET), MessageTemplate.MatchPerformative(ACLMessage.CFP) ); addBehaviour(new ContractNetResponder(this, template) { @Override protected ACLMessage handleCfp(ACLMessage cfp) throws NotUnderstoodException, RefuseException { System.out.println("Agent "+getLocalName()+": CFP received from "+cfp.getSender().getName()+". Action is "+cfp.getContent()); int proposal = evaluateAction(); if (proposal > 2) { // We provide a proposal System.out.println("Agent "+getLocalName()+": Proposing "+proposal); ACLMessage propose = cfp.createReply(); propose.setPerformative(ACLMessage.PROPOSE); propose.setContent(String.valueOf(proposal)); return propose; } else { // We refuse to provide a proposal System.out.println("Agent "+getLocalName()+": Refuse"); throw new RefuseException("evaluation-failed"); } } @Override protected ACLMessage handleAcceptProposal(ACLMessage cfp, ACLMessage propose,ACLMessage accept) throws FailureException { System.out.println("Agent "+getLocalName()+": Proposal accepted"); if (performAction()) { System.out.println("Agent "+getLocalName()+": Action successfully performed"); ACLMessage inform = accept.createReply(); inform.setPerformative(ACLMessage.INFORM); return inform; } else { System.out.println("Agent "+getLocalName()+": Action execution failed"); throw new FailureException("unexpected-error"); } } protected void handleRejectProposal(ACLMessage cfp, ACLMessage propose, ACLMessage reject) { System.out.println("Agent "+getLocalName()+": Proposal rejected"); } } ); } private int evaluateAction() { // Simulate an evaluation by generating a random number return (int) (Math.random() * 10); } private boolean performAction() { // Simulate action execution by generating a random number return (Math.random() > 0.2); } }