Agent交互協議
來自專欄微網小分隊學習筆記
FIPA指明了一套標準的交互協議,可以作為構建Agent對話的模板。
JADE為遵循FIPA交互協議的角色之間的對話提供了一些有用的行為類,這些類寫在jade.proto包中,為Agent的通信提供了一個很好的方法。
Agent參與信息交互時,其在消息對談中又分為發起者和響應者兩種,對應的類又可分為發起者和響應者類。
常見的交互協議有FIPA-Query、FIPA-Propose、Contract-Net等,對應的交互類為AchieveREInitiator/AchieveREResponder、ProposeInitiator/ProposeResponder、ContractNetInitiator/ContractNetResponder,SSContractNetResponder。
下面是一個ContractNet協議的例子:
響應者:
import jade.core.Agent;import jade.domain.FIPAAgentManagement.FailureException;import jade.domain.FIPAAgentManagement.NotUnderstoodException;import jade.domain.FIPAAgentManagement.RefuseException;import jade.lang.acl.ACLMessage;import jade.lang.acl.MessageTemplate;import jade.proto.ContractNetResponder;import protocol.ContractNetIni.BookNegotiator;public class ContractNetRes extends Agent{ public void setup() { MessageTemplate mt=MessageTemplate.MatchPerformative(ACLMessage.CFP); addBehaviour(new Manage(this,mt)); } public class Manage extends ContractNetResponder { private String title="book1"; private int price=100; public Manage(Agent a, MessageTemplate mt) { super(a, mt); // TODO 自動生成的構造函數存根 } protected ACLMessage handleCfp(ACLMessage cfp) throws NotUnderstoodException, RefuseException { String name=cfp.getContent(); ACLMessage reply = cfp.createReply(); System.out.println(name); if(name.equals(title)) { reply.setPerformative(ACLMessage.PROPOSE); reply.setContent(String.valueOf(price)); } else { reply.setContent("Sorry,not-available"); reply.setPerformative(ACLMessage.REFUSE); } return reply; } protected ACLMessage handleAcceptProposal(ACLMessage cfp, ACLMessage propose,ACLMessage accept) throws FailureException{ System.out.println("Agent "+getLocalName()+": Action successfully performed"); ACLMessage inform = accept.createReply(); inform.setPerformative(ACLMessage.INFORM); inform.setPerformative(ACLMessage.FAILURE); return inform; } }}
發起者:
import java.util.Vector;import jade.core.AID;import jade.core.Agent;import jade.domain.FIPANames;import jade.lang.acl.ACLMessage;import jade.proto.ContractNetInitiator;public class ContractNetIni extends Agent{ public void setup() { ACLMessage msg = new ACLMessage(); msg.setProtocol(FIPANames.InteractionProtocol.FIPA_CONTRACT_NET); addBehaviour(new BookNegotiator(this,msg,"book1",100)); } public class BookNegotiator extends ContractNetInitiator{ private String title; private int price; public BookNegotiator(Agent a, ACLMessage cfp,String s,int price) { super(a, cfp); title=s; this.price=price; } protected Vector prepareCfps(ACLMessage cfp) { cfp=new ACLMessage(ACLMessage.CFP); cfp.setContent(title); cfp.addReceiver(new AID("sell1",AID.ISLOCALNAME)); cfp.addReceiver(new AID("sell2",AID.ISLOCALNAME)); Vector v=new Vector(); v.add(cfp); return v; } protected void handleAllResponses(Vector responses,Vector acceptances) { ACLMessage bestOffer = null; int bestPrice=-1; for(int i=0;i<responses.size();i++) { ACLMessage acl=(ACLMessage)responses.get(i); System.out.println("***"+acl.getContent()); if(acl.getPerformative()==ACLMessage.PROPOSE) { bestOffer=acl; bestPrice=Integer.parseInt(acl.getContent()); } } if(bestOffer!=null) { ACLMessage accept=bestOffer.createReply(); accept.setContent(title); accept.setPerformative(ACLMessage.ACCEPT_PROPOSAL); acceptances.add(accept); } } protected void handleInform(ACLMessage inform) { System.out.println("***:Agent "+inform.getSender().getName()+" successfully performed the requested action"); System.out.println("***"+inform.getContent()); } protected void handleFailure(ACLMessage inform) { System.out.println("***:Agent "+inform.getSender().getName()+" failedly performed the requested action"); System.out.println("***"+inform.getContent()); } }}
運行:響應者命名sell1、sell2,發起者buy.
結果如下:
book1book1***100***100Agent sell1: Action successfully performed***:Agent sell1@10.160.26.75:1099/JADE failedly performed the requested action***null
推薦閱讀:
※人機交互(HCI)和交互設計(InteractionDesign)的區別是什麼?
※交互設計師工作需要做些什麼?
※解讀設計師如何向上司介紹UI作品
※如何從交互維度量化用戶體驗?
※#7聊聊交互設計 —— 交互設計入門