tutorial 2 agent actions specification --- behavior dr. fuhua lin school of computing and...

6
Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct. 27, 2009

Upload: claud-joseph

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct

Tutorial 2Agent Actions Specification

--- BehaviorDr. Fuhua Lin

School of Computing and Information Systems Athabasca University, Alberta, Canada

Oct. 27, 2009

Page 2: Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct

Agent• Agent actions are normally

specified through Behavior internal classes.

• The actions are described in the “action” method of these Behaviors.

• The setup method only serves to create instances of these behaviors and linking them to the Agent object.

• SimpleBehavior --- predefined class or defined in a separate file;

• How to terminate the behavior? In JADE, as long as a behavior is not “done”, its action method will be called repeatedly after every event --- such as receipt of a message or expiry of a timer delay.

• A common technique used to terminate the behaviour: a FINISHED flag which is tested in the “done” method and can be set in “action”.

Import jade.core.Agent;Import jade.core.behaviors.*;Public class myAgent extends Agent{

protected void setup(){addBehaviour (new myBeahviour(this));}class myBeahviour extends SimpleBehavior{

public myBeahviour(Agent a) {super(a);

}public void action(){

// … this is where the real programming goes!!

}private boolean finished = false; public boolean done() {

return finished;} // end of myBehavior

} // end class myAgent

Page 3: Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct

Question 1

Page 4: Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct

Example 1

• Requirements:• Run 4 simple operations in order---- Use

CyclicBehaviour() • Second operation should use the one-shot

behavior OneShotBehaviour(myAgent) • Delete the agent when the task is done

Page 5: Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct

.

package examplesbehaviours;import jade.core.Agent;import jade.core.behaviours.Behaviour; import jade.core.behaviours.CyclicBehaviour; import jade.core.behaviours.OneShotBehaviour;public class SimpleAgent extends Agent {   

private class FourStepBehaviour extends Behaviour {        private int step = 1;         public void action() {            switch (step) {               

case 1:                // Perform operation 1: print out a message                    System.out.println("Operation 1");                   

break;                case 2:                    // Perform operation 2: Add a OneShotBehaviour                   

System.out.println("Operation 2. Adding one-shot behaviour");                    myAgent.addBehaviour(new OneShotBehaviour(myAgent) {                       

public void action() {                            System.out.println("One-shot");                        } 

                   });                    break;                case 3:                    // Perform operation 3: print out a message                   

System.out.println("Operation 3");    break;                case 4:                // Perform operation 3: print out a message                   

System.out.println("Operation 4");                    break;             }        

      step++;        }         public boolean done() {            return step == 5;        }        

public int onEnd() {            myAgent.doDelete();            System.out.println("Finished!");           

return super.onEnd();        }    }    // END of inner class FourStepBehaviour   

/** Creates a new instance of SimpleAgent */    public SimpleAgent() {    }   

protected void setup() {        System.out.println("Agent "+getLocalName()+" started.");        

// Add the CyclicBehaviour        addBehaviour(new CyclicBehaviour(this) {           

public void action() {                System.out.println("Cycling");            }        }      

  );         // Add the generic behaviour       

addBehaviour(new FourStepBehaviour());    }     }

Page 6: Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct

Running Results

jade.Boot –gui myagent:examplesbehaviours.SimpleAgent

Agent myagent started.CyclingOperation 1CyclingOperation 2. Adding one-shot behaviourCyclingOperation 3One-shotCyclingOperation 4Finished!