Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1260481
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类:

2006-08-27 09:45:15

三.ACLagent communication language
Jadeagent之间进行通信使用的acl语言遵循fipa acl规范。一个acl消息通常包含这些参数:sende:消息的发送者,用agent标志AID表示; receivers,接受agent消息的agent可以是多个;Reply-to,应受到回应的接受者;Performative:标志该消息的目的,即发送者想要通过发送消息获取什么,通常有这样一些常值:REQUEST, INFORM, ACCEPT_PROPOSAL, REJECT_PROPOSAL, PROPOSEContent,消息的内容;语言,比如内容的编码格式;ontology,双方都能够理解的消息内容的概念说明和语义描述。
实例:
netbeans中创建常规项目:其代码文件有两个,分别为
package ips;
/**
 *
 * @author Administrator
 */
import java.io.InterruptedIOException;
import java.io.IOException;
import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import jade.domain.FIPANames.InteractionProtocol;
import jade.proto.SimpleAchieveREInitiator;
import java.util.Vector;
import java.util.Enumeration;
 
public class SimpleRequestInitiator extends Agent{
    static class MarriageProposer extends SimpleAchieveREInitiator{
        protected MarriageProposer(Agent agent, ACLMessage msg){
            super(agent, msg);
        }
        protected void handleAgree(ACLMessage msg) {
            System.out.println(myAgent.getLocalName() + ": 吼吼! " +
            msg.getSender().getLocalName() +" 已经同意嫁给我了, I'm so excited!");
        }
        protected void handleRefuse(ACLMessage msg) {
            System.out.println(myAgent.getLocalName() + ": Oh no! " +
            msg.getSender().getLocalName() +" 拒绝了我, i feel sad.");
        }
        protected void handleInform(ACLMessage msg) {
            System.out.println(myAgent.getLocalName() + ":" +
            msg.getSender().getLocalName() +" has informed me of the status of my request." +
            " They said : " + msg.getContent());
        }
        protected void handleNotUnderstood(ACLMessage msg){
            System.out.println(myAgent.getLocalName() + ":"
                + msg.getSender().getLocalName() +
                " has indicated that they didn't understand.");
        }
        protected void handleOutOfSequence(ACLMessage msg) {
            System.out.println(myAgent.getLocalName() + ":"
                + msg.getSender().getLocalName() +
                "has sent me a message which I wasn't" +
                " expecting in this conversation");
        }
    }
    protected void setup() {
        System.out.println(getLocalName() +": about to propose marriage to bob ");
        doWait(5000); //wait for bob to be started.
        ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
        AID to = new AID();
       
        to.setLocalName("bob");
        msg.setSender(getAID());
        msg.addReceiver(to);
        msg.setContent("Marry Me!");
        msg.setProtocol(InteractionProtocol.FIPA_REQUEST);
        addBehaviour(new MarriageProposer(this, msg));
    }
}
还有:
package ips;
 
import java.io.InterruptedIOException;
import java.io.IOException;
import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import jade.domain.FIPANames.InteractionProtocol;
import jade.proto.SimpleAchieveREResponder;
import java.util.Vector;
import java.util.Enumeration;
public class SimpleRequestResponder extends Agent {
    static class MarriageResponder extends SimpleAchieveREResponder{
        public MarriageResponder(Agent agent){
            super(agent,createMessageTemplate(InteractionProtocol.FIPA_REQUEST));
        }
        protected ACLMessage prepareResponse(ACLMessage msg) {
            ACLMessage response = msg.createReply();
            if(msg.getContent()!=null && msg.getContent().equals("Marry Me!")){
                System.out.println(myAgent.getLocalName() + ":" +
                        msg.getSender().getLocalName() +
                        " has asked me to marry him!");
            AID sender;
            sender = msg.getSender();
            if(sender.getLocalName().equals("baz")){
                response.setPerformative(ACLMessage.AGREE);
                System.out.println(myAgent.getLocalName() +
                    ":I'm going to agree.");
            }else{
                response.setPerformative(ACLMessage.REFUSE);
                System.out.println(myAgent.getLocalName() +
                    ":I'm going to turn him down.");
            }
            }else{
                response.setPerformative(ACLMessage.NOT_UNDERSTOOD);
                System.out.println(myAgent.getLocalName() +
                    ":I didn't understand what " +
                msg.getSender().getLocalName() +
                    " just said to me.");
            }
            return response;
        }
        protected ACLMessage prepareResultNotification(ACLMessage inmsg,
                                                            ACLMessage outmsg) {
 
        //what they have asked is now complete (or if it failed)
            ACLMessage msg = inmsg.createReply();
            msg.setPerformative(ACLMessage.INFORM);
            msg.setContent("I Do!");
            return msg;
        }
 
    }
    protected void setup() {
        System.out.println(getLocalName() +
                ": I wonder if anybody wants to marry me?");
        addBehaviour(new MarriageResponder(this));
     }//
}
按照以前blog中记载,在netbeans属性中加入库,然后再运行参数中,设置,主类依然为jade.Boot,参数为baz:ips.SimpleRequestInitiator bob:ips.SimpleRequestResponder,运行成功后,以下是输出结果:
baz: about to propose marriage to bob
bob: I wonder if anybody wants to marry me?
bob:baz has asked me to marry him!
bob:I'm going to agree.
baz: 吼吼! bob 已经同意嫁给我了, I'm so excited!
baz:bob has informed me of the status of my request. They said : I Do!
阅读(4037) | 评论(6) | 转发(1) |
给主人留下些什么吧!~~

我在茴薏天气晴2016-04-13 12:02:59

老师的这个博客和我看到的另外一个博客有些出入,不知道到底哪一个才是正确的

chinaunix网友2010-06-03 17:22:33

我是Agent初学者,希望与您取得联系。QQ190208183,向您请教

chinaunix网友2008-07-27 21:58:46

你好,我也是学习jade的,大家有兴趣的一起讨论吧。 QQ:731922303

chinaunix网友2008-04-23 10:16:32

您好,我是JADE初学者,想请教一个小问题 ACL通信时,可以对对方msg.setContent()的值进行条件判断吗?如何实现呢?是否可以进行多值操作呢?

hongweiwhu2008-04-03 09:11:25

一个Agent怎么与本地应用程序进行交互呢?