Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tutorial 2 Agent Actions Specification --- Behavior Dr. Fuhua Lin School of Computing and Information Systems Athabasca University, Alberta, Canada Oct."— Presentation transcript:

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

2 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

3 Question 1

4 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

5 . 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()); } }

6 Running Results jade.Boot –gui myagent:examplesbehaviours.SimpleAgent Agent myagent started. Cycling Operation 1 Cycling Operation 2. Adding one-shot behaviour Cycling Operation 3 One-shot Cycling Operation 4 Finished!


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

Similar presentations


Ads by Google