newspaperboyagent.java

来自「自己编写的一个Jade 的agent卖报纸小程序 可以自动添加报纸」· Java 代码 · 共 49 行

JAVA
49
字号
import jade.core.*;import jade.lang.acl.*;import jade.domain.FIPANames.InteractionProtocol;import jade.proto.SimpleAchieveREResponder;public class NewspaperBoyAgent extends Agent {     class RefillResponder extends SimpleAchieveREResponder{        public RefillResponder(Agent agent){            super(agent,createMessageTemplate(InteractionProtocol.FIPA_REQUEST));        }        /*create a prepareResponse method to respones the messages which the agent receive*/        protected ACLMessage prepareResponse(ACLMessage msg) {            ACLMessage response = msg.createReply();            if(msg.getContent()!=null && msg.getSender().getLocalName().equals("NC")){                System.out.println(myAgent.getLocalName() + ":refill the newspapers to  " +                       msg.getContent());                /*create a new message to response the message which receive from                 the NewspaperControlAgent and inform the ControlAgent he will refill                 the NewspaperMachine*/                ACLMessage msg1 = new ACLMessage(ACLMessage.REQUEST);                AID to1 = new AID();                to1.setLocalName("NC");                msg1.setSender(getAID());                msg1.addReceiver(to1);                msg1.setContent(msg.getContent());                msg1.setProtocol(InteractionProtocol.FIPA_REQUEST);                myAgent.send(msg1);                /*the newspaper boy refills a newspapermachine by sending a ACLMessage                 to the machine*/                ACLMessage msg0 = new ACLMessage(ACLMessage.REQUEST);                AID to0 = new AID();                to0.setLocalName(msg.getContent());                msg0.setSender(getAID());                msg0.addReceiver(to0);                msg0.setContent("boy");                msg0.setProtocol(InteractionProtocol.FIPA_REQUEST);                myAgent.send(msg0);            }            return response;        }    }    protected void setup() {         /*The task object is added to the list of Agent Missions*/        addBehaviour(new RefillResponder(this));     }//}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?