newspapermachine.java
来自「自己编写的一个Jade 的agent卖报纸小程序 可以自动添加报纸」· Java 代码 · 共 64 行
JAVA
64 行
import jade.core.behaviours.*;import jade.core.*;import jade.lang.acl.*;import jade.domain.FIPANames.InteractionProtocol;import jade.proto.SimpleAchieveREResponder;public class NewspaperMachine extends Agent{ private class RequestPerformerBehaviour extends Behaviour { /*initialize the number of newspapers in each Newspaper Machine*/ private int numberofpaper = 100; public void action() { /*if one machine sells a newspaper then sends a message to the NewspaperControlMachine and it is uesed to update the information in the NewspaperControlMachine*/ while (numberofpaper > 0) { ACLMessage msg1 = new ACLMessage(ACLMessage.REQUEST); AID to1 = new AID(); to1.setLocalName("NC"); msg1.setSender(getAID()); msg1.addReceiver(to1); msg1.setContent("sell one paper"); msg1.setProtocol(InteractionProtocol.FIPA_REQUEST); myAgent.send(msg1); numberofpaper--; } } public boolean done() { /*ending the loop control */ return numberofpaper == 0; } } /*receive the message from the newspaper boy,if the newspaper boy refills this machine, then output the machine is full*/class ReceiveMessage extends SimpleAchieveREResponder{ public ReceiveMessage(Agent agent) { super(agent,createMessageTemplate(InteractionProtocol.FIPA_REQUEST)); } protected ACLMessage prepareResponse(ACLMessage msg) { ACLMessage response = msg.createReply(); if(msg.getContent()!=null && msg.getContent().equals("boy")) { System.out.println(myAgent.getLocalName()+": NewspaperMachine is full"); } return response; } }/** Creates a new instance of SimpleAgent */ protected void setup() { System.out.println("NewspaperMachine "+getLocalName()+" is ready."); // Add the generic behaviour addBehaviour(new RequestPerformerBehaviour()); addBehaviour(new ReceiveMessage(this)); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?