在HelloAgent的例子中,我们把代码放在setup函数中,这是作弊。Agent actions 通常通过Behaviour classes定义。更确切的说,"action"是在这些 Behaviours中的action函数中描述的。setup函数只是用来创建这些Behaviours的实例,并把它们连接到Agent object。典型的agent class的结构如下:      

      import jade.core.Agent;
      import jade.core.behaviours.*;
   
    public class myAgent extends Agent
    {
        protected void setup()
        {
            addBehaviour( new myBehaviour( this ) );
        }
       
       
        class myBehaviour extends SimpleBehaviour
        {  
            public myBehaviour(Agent a) {
                super(a); //调用基类SimpleBehaviour中的构造函数,初始化Agent a  
            }
           
            public void action()
            {
               //...this is where the real programming goes !!
            }
           
            private boolean finished = false;
           
            public boolean done() {  //当return后面的内容为真,程序结束
                return finished; 
            }
           
        } // ----------- End myBehaviour
       
    }//end class myAgent
     这里,我们的Behaviour——myBehaviour被定义成SimpleBehaviour的子类,SimpleBehaviour是jade提前定义好的Behaviour中最灵活的一个。我们的Behaviour同样也是Agent的内部类。我们的Behaviour同样也可以作为一个无名类或者独立的文件来实现。通常,在创建Behaviour的时候,要指明它的owner agent,如在本例中是this。

     最后一点是,提供某种机制,来终止这个Behaviour.只要 behaviour 没有“done”,它的action函数在每个事件(比如说这样的事件:收到消息、时间计时器到时)结束时都会被调用。这里是在done函数中测试FINISHED,并可以在action中赋值。

 posted on 2009-05-13 21:50  凌枫天  阅读(276)  评论(0编辑  收藏  举报