Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1279836
  • 博文数量: 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)

分类:

2007-04-12 07:41:07

原先有一个jspjade agent的接口,使用JadeGateway的方式,然后主要处理数据的agent改用jadex来写,但是jadexweb-bridge我乍一看好麻烦啊,单独接收用户端数据的agent改写为jadex的方式,又懒得写。这样可采用其他的Agent不动,只把处理数据的Agent改为jadex agent的方式,只要jade的原生agentjadex agent之间正常通信即可。按道理应该可以的。当然首先要写个测试程序。

JadexAgentADF文件的event主要部分如下:

<events>

       

        <messageevent name="request_addweight" direction="receive" type="fipa">

            <parameter name="performative" class="String" direction="fixed">

                <value>SFipa.REQUESTvalue>

            parameter>

            <parameter name="content-start" class="String" direction="fixed">

                <value>"add stock_weight"value>

            parameter>

//这部分消息相关内容 本例中由TestPong,也就是jadeAgent发送

        messageevent>

        //下面这两部分消息定义了jadex Agentplan中接收到消息后返回的消息相关内容

        <messageevent name="inform" direction="send" type="fipa">

            <parameter name="performative" class="String" direction="fixed">

                <value>SFipa.INFORMvalue>

            parameter>

        messageevent>

       

        <messageevent name="failure" direction="send" type="fipa">

            <parameter name="performative" class="String" direction="fixed">

                <value>SFipa.FAILUREvalue>

            parameter>

        messageevent>

    events>

对应plan文件中的部分为:

public class StockPropWeightAddPlan extends Plan

{

      

       public void body()

       {

              Map wordtable = (Map )getBeliefbase().getBelief("stockweight").getFact();

              String input=(String)((IMessageEvent)getInitialEvent()).getContent();    //关键:接收TestPong发送来的消息

//以下为处理过程,可以略过不看

              StringTokenizer stok = new StringTokenizer(input, " ");

              getLogger().info("Ready accept user web input:  "+input);

             

              IMessageEvent me = (IMessageEvent)getInitialEvent();

              String reply;

              String content;

             

              if(stok.countTokens()==4)

              {

                     stok.nextToken();

                     stok.nextToken();

                     String eword = stok.nextToken();

                     Double gword =Double.parseDouble( stok.nextToken() );

                     if(!wordtable.containsKey(eword))

                     {

                            wordtable.put(eword, gword);

                            addWeightData(eword, gword);//更新到数据库中,自定为方法

                           

                            getBeliefbase().getBelief("stockweight").setFact(wordtable);

                            getLogger().info("Added  new popety weight to database: "

                                   +eword+" - "+gword);

                           

                            gword = (Double)wordtable.get(eword);

                            getLogger().info("You have Input "+eword+":"+gword.toString());

                           

                            reply="inform";

                            content="
KPIStockWeightAgent  have finished it! ";

                     }

                     else

                     {

                            reply = "failure";

                            content = "Sorry database already contains popety "+eword;

                            getLogger().info("Sorry database already contains popety: "+eword);

                     }

                                         

              }

              else

              {

                     reply = "failure";

                     content="
Sorry format not correct.";

                     getLogger().warning("Sorry format not correct.");

              }

             

              sendMessage(me.createReply(reply, content));//关键:返回给TestPong

       }

}

 

       Jade的发送消息和接受消息的pongAgent

public class TestPong extends Agent

{

 

       protected void setup()

       {

              System.out.println("TestPongAent is already...");

              ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);

              msg.setProtocol("fipa.request");//对应SFipa.REQUEST

              msg.setContent("add stock_weight popu 4 ");

              AID kpi = new AID("KPIStockWeightAgent", AID.ISLOCALNAME );

              msg.addReceiver(kpi);

              System.out.println("+++ Sending: ");

              send(msg);

             

              addBehaviour(new CyclicBehaviour(this)

              {

                      public void action() {

                            ACLMessage msg= receive();

                            if (msg!=null)

                                   //接收消息后在控制台上打印出来

                                   System.out.println( "== Answer" + " <- "

                                    +  msg.getContent() + " from "

                                    +  msg.getSender().getName() );

                            block();

                      }

              });

             

       }

             

        protected void takeDown()

          {

                try { DFService.deregister(this); }

                catch (Exception e) {}

          }      

}

根据运行结果,两者通信是完全正常的。

阅读(5038) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~