Jade V


Published on

Published in: Technology, Education

Jade V

  1. 1. JADE Java Agent Development Framework Jelena Jovanovic Email: [email_address]
  2. 2. JADE basics <ul><li>Software framework (middleware) for development of multi-agent systems </li></ul><ul><li>Based on FIPA standards for intelligent agents </li></ul><ul><ul><li>FIPA – Foundation for Intelligent Physical Agents (http://www.fipa.org) </li></ul></ul><ul><li>Fully coded in Java </li></ul>06/04/09
  3. 3. JADE basics <ul><li>JADE consists of: </li></ul><ul><ul><li>A runtime environment for agents </li></ul></ul><ul><ul><ul><li>Compliant with the FIPA standard for agent platforms </li></ul></ul></ul><ul><ul><li>A library of Java classes that provide: </li></ul></ul><ul><ul><ul><li>ready-made pieces of functionality and </li></ul></ul></ul><ul><ul><ul><li>abstract interfaces for custom, application dependent tasks. </li></ul></ul></ul><ul><ul><li>A suite of graphical tools </li></ul></ul><ul><ul><ul><li>Facilitate administration and monitoring of running agents </li></ul></ul></ul>06/04/09
  4. 4. JADE basics 06/04/09 CONTAINER a running instance of the JADE runtime environment PLATFORM a set of active containers The main container of the platform; all other containers register with it as soon as they start.
  5. 5. The FIPA compliant agent platform <ul><li>The agent platform can be split on several hosts. </li></ul><ul><li>Each host acts as a container of agents </li></ul><ul><ul><li>provides a complete run time environment for agent execution </li></ul></ul><ul><ul><li>allows several agents to execute concurrently </li></ul></ul><ul><ul><ul><li>each agent as a single Java Thread. </li></ul></ul></ul><ul><li>The first container started is the main container </li></ul><ul><ul><li>maintains a central registry of all the other containers </li></ul></ul><ul><ul><li>=> agents can discover and interact with each other across the whole platform. </li></ul></ul>06/04/09
  6. 6. The FIPA compliant agent platform <ul><li>Agent Management System (AMS) </li></ul><ul><ul><li>The principle authority in the platform </li></ul></ul><ul><ul><li>Keeps track of all other agents in the platform </li></ul></ul><ul><ul><li>Only one AMS will exist in a single platform </li></ul></ul><ul><ul><li>The AMS maintains a directory of agents’ identifiers (AIDs) and agents’ states. </li></ul></ul><ul><ul><ul><li>Each agent must register with the AMS in order to get a valid (i.e. unique) AID. </li></ul></ul></ul>06/04/09
  7. 7. The FIPA compliant agent platform <ul><li>Directory Facilitator (DF) </li></ul><ul><ul><li>the agent that provides the default ‘yellow page’ service in the platform. </li></ul></ul><ul><ul><ul><li>it publishes services of other registered agents from the platform, thus facilitating agents cooperation. </li></ul></ul></ul><ul><li>Message Transport System </li></ul><ul><ul><li>also called Agent Communication Channel (ACC), </li></ul></ul><ul><ul><li>software component controlling all exchange of messages within the platform, including messages to/from remote platforms. </li></ul></ul>06/04/09
  8. 8. Creating an agent in JADE <ul><li>… is as simple as extending the jade.core.Agent class - a common base class for user defined agents; </li></ul><ul><li>jade.core.Agent class encompasses: </li></ul><ul><ul><li>a basic set of methods that can be called to implement the custom behaviour of an agent (e.g. send/receive messages, use standard interaction protocols,…). </li></ul></ul><ul><li>Each functionality/service provided by an agent should be implemented as one or more behaviours that are executed concurrently. </li></ul>06/04/09
  9. 9. Agent identifier - AID <ul><li>Each agent has a unique ID – AID </li></ul><ul><li>AID is an instance of the jade.core.AID class </li></ul><ul><li>An AID object includes: </li></ul><ul><ul><li>a globally unique name of an agent in the form: <nickname>@<platform-name> </li></ul></ul><ul><ul><li>a number of addresses </li></ul></ul><ul><ul><ul><li>these are the addresses of the platform the agent lives in </li></ul></ul></ul><ul><ul><ul><li>only used when an agent needs to communicate with another agent living on a different platform. </li></ul></ul></ul>06/04/09
  10. 10. HelloWorldAgent 06/04/09 import jade.core.Agent; public class HelloWorldAgent extends Agent { protected void setup() { System.out.println(“Hello World!”); System.out.println(“My name is ” + getAID().getName() ); } }
  11. 11. Running an agent <ul><li>Agents can not be executed directly; they must execute within a larger program which provides the necessary services – JADE runtime environment. </li></ul><ul><li>To run your agent you have to: </li></ul><ul><ul><li>compile it: </li></ul></ul><ul><li>javac –classpath <JADE-jars> HelloWorldAgent.java </li></ul><ul><ul><li>start it from JADE runtime environment: </li></ul></ul><ul><li>java –classpath <JADE-jars> jade.Boot fred:HelloWorldAgent </li></ul>06/04/09
  12. 12. Running an agent <ul><li>Alternatively, you can set up CLASSPATH variable : </li></ul><ul><li>;.;c:jadelibjade.jar;c:jadelibjadeTools.jar;… </li></ul><ul><li>and then run the example with: </li></ul><ul><li>java jade.Boot fred:HelloWorldAgent </li></ul><ul><li>or create a batch file (e.g., helloworld.bat): </li></ul><ul><li>java -classpath <jade-jars> jade.Boot fred:HelloWorldAgent </li></ul><ul><li>inside jade directory and run it </li></ul>06/04/09
  13. 13. Running an agent <ul><li>The best alternative is to use EJIP – Eclipse JADE Integration Plugin </li></ul><ul><ul><li>http://www.mars-team.com/ejip/ </li></ul></ul><ul><ul><li>simply run your agents from your development environment </li></ul></ul><ul><ul><li>requirements: </li></ul></ul><ul><ul><ul><li>JDK 1.4.2 (at least) </li></ul></ul></ul><ul><ul><ul><li>Eclipse 3.2 </li></ul></ul></ul>06/04/09
  14. 14. HelloWorldAgent <ul><li>Note that our agent is still running! </li></ul><ul><li>If we want to stop it we have to tell it explicitly by executing its doDelete() method. </li></ul><ul><li>Let’s learn more about an agent’s lifecycle </li></ul>06/04/09
  15. 15. Agent life cycle <ul><li>a JADE agent can be in one of several states, according to Agent Platform Life Cycle specification provided by FIPA </li></ul>06/04/09
  16. 16. Agent life cycle <ul><li>Initiated - an Agent object is built, but hasn't registered itself yet with the AMS, has neither a name, nor an address and cannot communicate with other agents. </li></ul><ul><li>Active – the Agent object is registered with the AMS, has a regular name and an address and can access all the various JADE features. </li></ul><ul><ul><li>an agent is allowed to execute its behaviours (i.e. its tasks) only in this state. </li></ul></ul><ul><li>Suspended – the Agent object is currently stopped; no agent behaviour is being executed. </li></ul><ul><li>Waiting – the Agent object is blocked, waiting for something; it is ‘sleeping’ and will wake up when a condition is met (typically when a message arrives). </li></ul>06/04/09
  17. 17. Agent life cycle <ul><li>Unknown – the Agent is definitely dead; it is no more registered with the AMS. </li></ul><ul><li>Transit – a mobile agent enters this state while it is migrating to the new location. The system continues to buffer messages that will then be sent to its new location. </li></ul><ul><li>The Agent class contains : </li></ul><ul><ul><li>constants for representing agent’s possible states (e.g. AP_ACTIVE , AP_WAITING ) </li></ul></ul><ul><ul><li>public methods to perform transitions between the various states (e.g. doWait() , doSuspend() ). </li></ul></ul>06/04/09
  18. 18. Starting an agent <ul><li>Includes the following steps: </li></ul><ul><ul><li>The agent constructor is executed, </li></ul></ul><ul><ul><li>the agent is given an identifier (instance of jade.core.AID class) </li></ul></ul><ul><ul><li>it is registered with the AMS, </li></ul></ul><ul><ul><li>it is put in the AP_ACTIVE state, and finally </li></ul></ul><ul><ul><li>the setup() method is executed. </li></ul></ul><ul><li>The programmer has to implement the setup() method in order to initialize the agent. </li></ul>06/04/09
  19. 19. Starting an agent <ul><li>The most important (also obligatory) thing to do when initializing an agent (i.e. in its setup() method) is to add tasks (so called behaviours) to the queue of tasks to be executed </li></ul><ul><ul><li>The setup() method should add at least one task to the agent. </li></ul></ul><ul><li>After initialization, JADE automatically executes the first task from the agent’s queue of ready tasks </li></ul><ul><ul><li>agents tasks (forming a queue of ready tasks) are executed according to the round-robin non-preemptive strategy - this will be discussed later in more details. </li></ul></ul>06/04/09
  20. 20. The notion of behaviour <ul><li>Agents operate independently and execute in parallel with other agents . </li></ul><ul><li>The obvious way to implement this is to assign a java Thread to each agent - and this is how it is done in Jade . </li></ul><ul><li>However, there is a need for further parallelism within each agent because an agent may be involved in different tasks. </li></ul><ul><li>Possible solution: to use additional Threads to handle each concurrent agent activity; </li></ul>06/04/09
  21. 21. The notion of behaviour <ul><li>To support efficiently parallel activities within an agent, Jade has introduced a concept called Behaviour </li></ul><ul><li>A behaviour is basically an Event Handler – it describes how an agent reacts to an event . </li></ul><ul><ul><li>an event is a relevant change of state </li></ul></ul><ul><ul><ul><li>e.g. a reception of a message or a Timer interrupt (after the specified time has elapsed). </li></ul></ul></ul>06/04/09
  22. 22. Implementing a behaviour <ul><li>A behaviour is implemented as an object of a class that extends jade.core.behaviours.Behaviour . </li></ul><ul><li>Each class extending Behaviour must implement: </li></ul><ul><ul><li>the action() method – defines the operations to be performed when the behaviour is in execution </li></ul></ul><ul><ul><li>the done() method – specifies whether or not a behaviour has completed, through the boolean value it returns </li></ul></ul>06/04/09
  23. 23. Implementing a behaviour <ul><li>To implement an agent-specific task one should: </li></ul><ul><ul><li>define one or more Behaviour subclasses, </li></ul></ul><ul><ul><li>add the behaviour objects to the agent’s task list </li></ul></ul><ul><ul><ul><li>using addBehaviour(Behaviour) method </li></ul></ul></ul>06/04/09
  24. 24. The agent execution model 06/04/09
  25. 25. A more realistic HelloWorld Agent <ul><li>We have just learned that: </li></ul><ul><ul><li>Agent actions are normally specified through Behaviour classes, i.e. in the action() method of these behaviours. </li></ul></ul><ul><ul><li>The setup method only serves to create instances of these behaviours and link them to the Agent object. </li></ul></ul><ul><li>A more typical implementation of the HelloWorldAgent would be: </li></ul>06/04/09
  26. 26. A more realistic HelloWorld Agent 06/04/09 import jade.core.Agent; import jade.core.behaviours.*; public class HelloWorldAgent1 extends Agent { protected void setup() { addBehaviour ( new B1 ( this ) ); } } class B1 extends SimpleBehaviour { public B1(Agent a) { super (a); } public void action () { System.out.println( &quot;Hello World! My name is &quot; + myAgent .getAID().getName() ); finished = true; } private boolean finished = false; public boolean done () { return finished; } }
  27. 27. A more realistic HelloWorld Agent <ul><li>The example shows some new things: </li></ul><ul><ul><li>myAgent : a local variable of all Behaviour objects which stores the agent reference passed as a parameter when the behaviour was created. </li></ul></ul><ul><ul><li>SimpleBehaviour - the most flexible JADE's predefined behaviour. </li></ul></ul><ul><ul><li>Behaviour class can be specified as </li></ul></ul><ul><ul><ul><li>an internal class to the Agent; </li></ul></ul></ul><ul><ul><ul><li>an anonymous class </li></ul></ul></ul><ul><ul><ul><li>a separate class in the same or a separate file. </li></ul></ul></ul><ul><ul><li>In all cases, it is usual to pass a reference to the owner agent (this) when creating a Behaviour. </li></ul></ul>06/04/09
  28. 28. A more realistic HelloWorld Agent <ul><li>The example shows some new things: </li></ul><ul><ul><li>even if the behaviour finishes the program does NOT terminate because: </li></ul></ul><ul><ul><ul><li>the agent still exists, </li></ul></ul></ul><ul><ul><ul><li>the JADE environment which we started stays around to interact with other agents which could be started later in the network. </li></ul></ul></ul>06/04/09
  29. 29. Back to behaviours <ul><li>A Behaviour can be added whenever it is needed, and not only within Agent.setup() method. </li></ul><ul><li>JADE uses a scheduler to activate behaviours </li></ul><ul><ul><li>implemented by the Agent class and hidden to the programmer, </li></ul></ul><ul><ul><li>carries out a round-robin non-preemptive scheduling policy among all behaviours available in the ready queue </li></ul></ul><ul><ul><ul><li>executes each behaviour until it releases control </li></ul></ul></ul>06/04/09
  30. 30. Behaviours <ul><li>Because of the non-preemptive multitasking model, programmers must avoid using: </li></ul><ul><ul><li>endless loops and </li></ul></ul><ul><ul><li>long operations within the action() method of a behaviour. </li></ul></ul><ul><li>Remember! </li></ul><ul><ul><li>when the action() method of one behaviour is running, no other behaviour can execute until the running one is finished. </li></ul></ul>06/04/09
  31. 31. Behaviours <ul><li>Every single Behaviour is allowed to block its computation using the block method . </li></ul><ul><li>Warning: block(milisec) does not block execution of the behaviour; it just delays its next execution. </li></ul><ul><li>All blocked behaviours are rescheduled as soon as: </li></ul><ul><ul><li>a new message arrives </li></ul></ul><ul><ul><ul><li>the programmer must take care of blocking again a behaviour if it was not interested in the arrived message. </li></ul></ul></ul><ul><ul><li>the agent is restarted. </li></ul></ul>06/04/09
  32. 32. Behaviours <ul><li>The framework provides ready to use complex behaviours that contain sub-behaviours and execute them according to some policy. </li></ul><ul><ul><li>These are provided as subclasess of the Behaviour class </li></ul></ul><ul><ul><li>For example, the SequentialBehaviour class executes its sub-behaviours one after the other. </li></ul></ul><ul><li>UML Model of the Behaviour class hierarchy: </li></ul>06/04/09
  33. 33. 06/04/09
  34. 34. Primitive Behaviours <ul><li>CyclicBehaviour: </li></ul><ul><ul><li>stays active as long as its agent is alive and is called repeatedly after every event. </li></ul></ul><ul><ul><li>Quite useful to handle message reception. </li></ul></ul><ul><ul><li>jade.core.behaviours.CyclicBehaviour class </li></ul></ul><ul><li>OneShotBehaviour: </li></ul><ul><ul><li>executes ONCE and dies; </li></ul></ul><ul><ul><li>Not really that useful since the one shot may be triggered at the wrong time; </li></ul></ul><ul><ul><li>jade.core.behaviours.OneShotBehaviour class </li></ul></ul>06/04/09
  35. 35. Primitive Behaviours <ul><li>WakerBehaviour: </li></ul><ul><ul><li>executes the user code after a given timeout expires </li></ul></ul><ul><ul><ul><li>The user code is given in the handleElapsedTimeout() method </li></ul></ul></ul><ul><ul><ul><li>The timeout is set in the constructor </li></ul></ul></ul><ul><li>TickerBehaviour: </li></ul><ul><ul><li>cyclic behaviour which executes the user-defined piece of code repetitively waiting a given time period after each execution. </li></ul></ul><ul><ul><ul><li>The user code is given in the onTick() method </li></ul></ul></ul><ul><ul><ul><li>The time period is specified in the constructor </li></ul></ul></ul><ul><li>ReceiverBehaviour: </li></ul><ul><ul><li>triggers when a given type of message is received (or a timeout expires). </li></ul></ul>06/04/09
  36. 36. Composite Behaviours <ul><li>ParallelBehaviour: </li></ul><ul><ul><li>controls a set of child behaviours that execute in parallel. </li></ul></ul><ul><ul><li>the important thing is the termination condition: we can specify that the group terminates when: </li></ul></ul><ul><ul><ul><li>ALL children are done, </li></ul></ul></ul><ul><ul><ul><li>N children are done or </li></ul></ul></ul><ul><ul><ul><li>ANY child is done. </li></ul></ul></ul><ul><ul><li>behaviours added directly to an Agent operate in parallel by default. </li></ul></ul><ul><li>SequentialBehaviour: </li></ul><ul><ul><li>executes its child behaviours one after the other and terminates when the last child has ended. </li></ul></ul>06/04/09
  37. 37. Agent communication <ul><li>A fundamental characteristic of multi-agent systems is that individual agents communicate and interact. </li></ul><ul><li>According to the FIPA specification, agents communicate via asynchronous messages. </li></ul><ul><li>Each agent has a sort of mailbox (the agent message queue) where the JADE runtime posts messages sent by other agents. </li></ul><ul><ul><li>Whenever a message is added to the message queue the receiving agent is notified </li></ul></ul><ul><ul><ul><li>as gmail notifies you when a new email arrives. </li></ul></ul></ul><ul><ul><li>If and when the agent actually picks up the message from the message queue to process, it is completely up to the programmer </li></ul></ul><ul><ul><ul><li>as it is up to you whether you will read a received email </li></ul></ul></ul>06/04/09
  38. 38. Agent communication 06/04/09
  39. 39. Agent communication <ul><li>The Agent class provides a set of methods for inter-agent communication. </li></ul><ul><li>Method calls are completely transparent to where the agent resides </li></ul><ul><ul><li>the platform takes care of selecting the appropriate address and transport mechanism. </li></ul></ul>06/04/09
  40. 40. Agent communication <ul><li>To understand each other, it is crucial that agents agree on the format and the semantics of the messages they exchange. </li></ul><ul><li>In JADE, messages adhere strictly to the ACL (Agent Communication Language) FIPA standard. </li></ul><ul><li>A message is an instance of the jade.acl.ACLMessage class </li></ul><ul><li>Example: The simplest message </li></ul>06/04/09 ACLMessage msg = new ACLMessage( ACLMessage.INFORM ); msg.setContent(&quot;I sell seashells at $10/kg&quot; );
  41. 41. Agent communication <ul><li>An ACL message contains: </li></ul><ul><ul><li>The sender of the message (initialized automatically) </li></ul></ul><ul><ul><li>The list of receivers </li></ul></ul><ul><ul><li>The communicative intention (“ performative ”) indicating what the sender intends </li></ul></ul><ul><ul><ul><li>Uses the restricted vocabulary of the FIPA defined set of message types </li></ul></ul></ul><ul><ul><li>The content – the actual information included in the message </li></ul></ul><ul><ul><li>The content language – the syntax used to express the content </li></ul></ul><ul><ul><li>The ontology i.e. the vocabulary of the symbols used in the content and their meaning </li></ul></ul>06/04/09
  42. 42. Agent communication <ul><li>An ACL message contains – cont.: </li></ul><ul><ul><li>ConversationID - used to link messages in same conversation </li></ul></ul><ul><ul><li>InReplyTo - sender uses to help distinguish answers </li></ul></ul><ul><ul><li>ReplyWith - another field to help distinguish answers </li></ul></ul><ul><ul><li>ReplyBy - used to set a time limit on an answer </li></ul></ul><ul><li>All these properties can be accessed via the set/get<Property>() methods of the ACLMessage class. </li></ul>06/04/09
  43. 43. Agent communication <ul><li>FIPA defined performatives: </li></ul><ul><ul><li>INFORM whereby one agent gives another some useful information – the most common </li></ul></ul><ul><ul><li>QUERY to ask a question, </li></ul></ul><ul><ul><li>QUERY_IF if the sender wants to know whether or not a given condition holds </li></ul></ul><ul><ul><li>REQUEST to ask the other to do something </li></ul></ul><ul><ul><li>CFP to call for proposals </li></ul></ul><ul><ul><li>PROPOSE to start bargaining. </li></ul></ul><ul><ul><li>Performatives for answers include: </li></ul></ul><ul><ul><ul><li>AGREE </li></ul></ul></ul><ul><ul><ul><li>REFUSE. </li></ul></ul></ul>06/04/09
  44. 44. Agent communication <ul><li>An agent willing to send a message should : </li></ul><ul><ul><li>create a new ACLMessage object, </li></ul></ul><ul><ul><li>fill its attributes with appropriate values, </li></ul></ul><ul><ul><li>call the agent’s send() method </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>String receiver = … </li></ul></ul><ul><ul><li>ACLMessage msg = new ACLMessage(ACLMessage.INFORM); </li></ul></ul><ul><ul><li>msg.addReceiver(new AID(receiver)); </li></ul></ul><ul><ul><li>msg.setContent(&quot;FirstInform&quot;); </li></ul></ul><ul><ul><li>send(msg); </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>An alternative: adding an instance of the SenderBehaviour to the agent’s queue of tasks . </li></ul>06/04/09
  45. 45. Inter-agent communication <ul><li>The platform puts all the messages received by an agent into the agent’s private queue (mailbox). </li></ul><ul><ul><li>The message queue can be accessed in: </li></ul></ul><ul><ul><ul><li>a blocking way (using blockingReceive() method) - causes the suspension حرمان of all the agent’s behaviours; </li></ul></ul></ul><ul><ul><ul><li>a non-blocking way (using receive() method) - returns immediately null if the requested message is not present in the queue. </li></ul></ul></ul><ul><ul><li>The blocking access can have a timeout parameter representing the maximum number of milliseconds that the agent’s activity should remain blocked waiting for the requested message. </li></ul></ul>06/04/09
  46. 46. Agent communication <ul><li>An agent willing to receive a message should call: </li></ul><ul><ul><li>Agent.blockingReceive() </li></ul></ul><ul><ul><li>ACLMessage msg = blockingReceive(); </li></ul></ul><ul><li>or </li></ul><ul><ul><li>Agent.receive() </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>ACLMessage msg = receive(); </li></ul></ul><ul><ul><li>if (msg!=null) </li></ul></ul><ul><ul><li>System.out.println( &quot; - “ + myAgent.getLocalName() + &quot; <- &quot; + msg.getContent() ); </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>block(); </li></ul></ul><ul><ul><li> … </li></ul></ul><ul><li>An alternative: adding an instance of the ReceiverBehaviour to the agent queue of tasks. </li></ul>06/04/09
  47. 47. Agent communication 06/04/09 <ul><li>public class Receiver extends Agent { </li></ul><ul><ul><li>protected void setup() { </li></ul></ul><ul><ul><ul><li>addBehaviour(new CyclicBehaviour(this) { </li></ul></ul></ul><ul><ul><ul><ul><li>public void action() { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>ACLMessage msg = receive (); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if (msg!=null) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>System.out.println( &quot; - &quot; + myAgent.getLocalName() + &quot; <- &quot; + msg.getContent() ); </li></ul></ul></ul></ul></ul><ul><ul><ul><li>block() ; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>}); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>Example: a simple receiver agent
  48. 48. Agent communication <ul><li>Note the use of block() without a timeout </li></ul><ul><ul><li>This puts the behaviour on hold until the next message is received </li></ul></ul><ul><ul><li>When a message arrives all behaviours are activated and their action methods called one after the other. </li></ul></ul><ul><ul><ul><li>It doesn’t matter whether other behaviours were waiting for messages or the passage of time ( with block(dt) ) </li></ul></ul></ul><ul><ul><li>If you don't call block(), your behaviour will stay active and cause a LOOP. </li></ul></ul>06/04/09
  49. 49. Agent communication <ul><li>Answering a message: </li></ul><ul><ul><li>To simplify answering, Jade provides a method createReply() which creates a reply message with all necessary attributes set correctly . </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>ACLMessage msg = receive(); </li></ul></ul><ul><ul><li>if (msg!=null) { </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>ACLMessage reply = msg.createReply(); </li></ul></ul><ul><ul><li>reply.setPerformative( ACLMessage.INFORM ); </li></ul></ul><ul><ul><li>reply.setContent(“Hi!Nice hearing from you!&quot; ); </li></ul></ul><ul><ul><li>reply.send(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>block(); </li></ul></ul><ul><ul><li>… </li></ul></ul>06/04/09 Note: in this example, there isn't much advantage of using createReply, the real benefits become obvious in applications when other attributes like conversationID or ontology have to correspond to the original message.
  50. 50. Agent communication <ul><li>Often it is useful to filter messages and set up distinct behaviours to handle messages from various agents or various kinds of messages. </li></ul><ul><li>To enable this, Jade provides Message Templates and a receive method which takes a template as a parameter and only returns messages matching that template. </li></ul><ul><li>The MessageTemplate class provides static methods to create filters for each attribute of the ACLMessage : </li></ul><ul><ul><li>MatchPerformative(performative) </li></ul></ul><ul><ul><ul><li>ACLMessage.INFORM, ACLMessage.REQUEST, … </li></ul></ul></ul><ul><ul><li>MatchSender( AID ) </li></ul></ul><ul><ul><li>MatchConversationID( String ) … </li></ul></ul><ul><ul><li>An additional set of methods allow elementary patterns to be combined with AND, OR and NOT operators. </li></ul></ul>06/04/09
  51. 51. Agent communication <ul><li>Message Templates example: </li></ul><ul><ul><li>MessageTemplate m1 = </li></ul></ul><ul><ul><li>MessageTemplate.MatchPerformative (ACLMessage.INFORM); </li></ul></ul><ul><ul><li>MessageTemplate m2 = </li></ul></ul><ul><ul><li>MessageTemplate.MatchLanguage(&quot;PlainText&quot;); </li></ul></ul><ul><ul><li>MessageTemplate m3 = </li></ul></ul><ul><ul><li>MessageTemplate.MatchSender( new AID( &quot;a&quot;, AID.ISLOCALNAME)); </li></ul></ul><ul><ul><li>MessageTemplate m1andm2 = MessageTemplate.and(m1,m2); </li></ul></ul><ul><ul><li>MessageTemplate notm3 = MessageTemplate.not(m3); </li></ul></ul><ul><ul><li>MessageTemplate m1andm2_and_notm3 = </li></ul></ul><ul><ul><li>MessageTemplate.and(m1andm2, notm3); </li></ul></ul>06/04/09
  52. 52. Agent communication <ul><li>Tip: </li></ul><ul><ul><li>when your agent should have parallel negotiations with several other agents, you should: </li></ul></ul><ul><ul><ul><li>create a unique string (the ConversationID ) to uniquely identify messages that your agent exchanges with each one of his collaborative agents. </li></ul></ul></ul><ul><ul><ul><li>Then you set-up a behaviour for each negotiation which only responds to messages with that particular ConversationID . </li></ul></ul></ul><ul><ul><ul><ul><li>You use a Message Template to filter messages from the agent’s messages queue </li></ul></ul></ul></ul>06/04/09
  53. 53. Agent communication <ul><li>Finding agents to talk to </li></ul><ul><ul><li>Different ways to get an agent ID: </li></ul></ul><ul><ul><ul><li>using the agent's local name (e.g. the one specified on the command line when starting a container), </li></ul></ul></ul><ul><ul><ul><li>using JADE's GUI interface, </li></ul></ul></ul><ul><ul><ul><li>from a directory entry in the DF, </li></ul></ul></ul><ul><ul><ul><li>from the AMS </li></ul></ul></ul><ul><ul><ul><ul><li>keeps AIDs of all active agents </li></ul></ul></ul></ul>06/04/09
  54. 54. Agent communication <ul><li>Finding an agent using its local name </li></ul><ul><ul><li>Suppose that an agent container with two agents was started : </li></ul></ul><ul><li>jade.Boot fred:BuyerAgent store:SellerAgent </li></ul>06/04/09 Part of the BuyerAgent implementation: … ACLMessage msg = new ACLMessage(ACLMessage.INFORM); msg.setContent( &quot;bla...bla...bla&quot; ); msg.addReceiver( new AID( &quot;store&quot;, AID.ISLOCALNAME) ); send(msg); …
  55. 55. Agent communication <ul><li>Finding agents by searching the AMS: </li></ul><ul><ul><li>import jade.domain.AMSService; </li></ul></ul><ul><ul><li>import jade.domain.FIPAAgentManagement.*; </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>AMSAgentDescription [] agents = null; </li></ul></ul><ul><ul><li>try { </li></ul></ul><ul><ul><li>SearchConstraints c = new SearchConstraints(); </li></ul></ul><ul><ul><li>c.setMaxResults ( new Long(-1) ); //-1 means ALL </li></ul></ul><ul><ul><li>agents = AMSService.search( this, new AMSAgentDescription (), c ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>catch (Exception e) { ... } </li></ul></ul><ul><li>To extract the AID from a descriptor we have to use something like agents[i].getName() </li></ul>06/04/09 the searching agent A description of the agent(s) we are looking for
  56. 56. Agent communication <ul><li>Example: saying hello to all other agents </li></ul>06/04/09 <ul><li>… </li></ul><ul><li>AID myID = getAID(); </li></ul><ul><li>ACLMessage msg = new ACLMessage(ACLMessage.INFORM); </li></ul><ul><li>msg.setContent( “Hello!&quot; ); </li></ul><ul><li>for (int i=0; i<agents.length; i++) { </li></ul><ul><ul><li>AID agentID = agents[i].getName(); </li></ul></ul><ul><ul><li>If ( !agentID.equals( myID ) ) </li></ul></ul><ul><ul><ul><li>msg.addReceiver( agents[i].getName() ); </li></ul></ul></ul><ul><li>} </li></ul><ul><li>send(msg); </li></ul><ul><li>… </li></ul>All currently running agents found by using the code snippet from the previous slide
  57. 57. Directory Facilitator (DF) <ul><li>DF is often compared to the &quot;Yellow Pages&quot; phone book: </li></ul><ul><ul><li>Agents wishing to advertise their services register with the DF. </li></ul></ul><ul><ul><li>Visiting agents can then search the DF looking for other agents which provide the services they desire. </li></ul></ul><ul><li>More formally: DF is a centralized registry of entries which associate service descriptions to agent IDs. </li></ul>06/04/09
  58. 58. Directory Facilitator (DF) <ul><li>DFAgentDescription (DFD) class </li></ul><ul><ul><li>used both for adding an entry and searching for services . </li></ul></ul><ul><li>A DF entry i.e. an instance of DFD class should contain: </li></ul><ul><ul><li>The agent’s ID , </li></ul></ul><ul><ul><li>A set of ontologies, protocols and languages supported by the agent, </li></ul></ul><ul><ul><li>A set of services the agent provides, i.e. descriptions of those services </li></ul></ul><ul><li>A service description ( ServiceDescription ) includes: </li></ul><ul><ul><li>the service type , </li></ul></ul><ul><ul><li>the service name , </li></ul></ul><ul><ul><li>the languages and ontologies required to exploit that service, </li></ul></ul><ul><ul><li>a number of service specific properties. </li></ul></ul>06/04/09
  59. 59. DFAgentDescription Class <ul><li>DFAgentDescription </li></ul><ul><li>Name: AID // Required for registration </li></ul><ul><li>Protocols : set of Strings </li></ul><ul><li>Ontologies : set of Strings </li></ul><ul><li>Languages : set of Strings </li></ul><ul><li>Services : set of { </li></ul><ul><li>{ Name: String // Required for each service specified </li></ul><ul><li>Type: String // Required ... </li></ul><ul><li>Owner: String </li></ul><ul><li>Protocols : set of Strings </li></ul><ul><li>Ontologies : set of Strings </li></ul><ul><li>Languages : set of Strings </li></ul><ul><li>Properties : set of { </li></ul><ul><li>Name: String </li></ul><ul><li>Value: String </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>06/04/09
  60. 60. Registering with DF <ul><li>import jade.domain.DFService; </li></ul><ul><li>import jade.domain.FIPAAgentManagement.*; </li></ul><ul><li>import jade.domain.FIPAException; </li></ul><ul><li>.... </li></ul><ul><li>DFAgentDescription dfd = new DFAgentDescription(); </li></ul><ul><li>dfd.setName( getAID() ); </li></ul><ul><li>ServiceDescription sd = new ServiceDescription(); </li></ul><ul><li>sd.setType( &quot;buyer&quot; ); </li></ul><ul><li>sd.setName( “online trade” ); </li></ul><ul><li>dfd.addServices(sd); </li></ul><ul><li>try { </li></ul><ul><li>DFService.register(this, dfd ); </li></ul><ul><li>} </li></ul><ul><li>catch (FIPAException fe) { fe.printStackTrace(); } </li></ul>06/04/09
  61. 61. Deregistering <ul><li>When an agent terminates, it is good practice to delete its entry from the DF, since the system does not remove it automatically. </li></ul><ul><ul><li>They are automatically removed only from the AMS </li></ul></ul><ul><li>Each agent is allowed only ONE entry in the DF. </li></ul><ul><ul><li>An attempt to register an agent already present in the DF throws an Exception. </li></ul></ul><ul><li>Deregistration is usually done in takeDown() method which is called automatically when the agent dies. </li></ul><ul><li>protected void takeDown(){ </li></ul><ul><li>try { DFService.deregister(this); } </li></ul><ul><li>catch (FIPAException e) {} </li></ul><ul><li>} </li></ul>06/04/09
  62. 62. Searching the DF <ul><li>Purpose: Finding an agent with the specified properties. </li></ul><ul><li>To find an agent using the DF: </li></ul><ul><ul><li>create a DFD (with no AID) where the relevant fields are initialized to the properties you require. </li></ul></ul><ul><ul><li>extract the ID of the suitable agents from the results set </li></ul></ul><ul><ul><ul><li>The search returns an array of DFDs (with AIDs) whose attributes match your description. </li></ul></ul></ul>06/04/09
  63. 63. Searching the DF <ul><li>Example: searching for agents of the type ‘buyer’ </li></ul>06/04/09 DFAgentDescription dfd = new DFAgentDescription(); ServiceDescription sd = new ServiceDescription(); sd.setType( &quot;buyer&quot; ); dfd.addServices(sd); DFAgentDescription[] result = DFService.search(this,dfd); System.out.println(“Buyer agets:”); for (int i = 0; i < result.length; ++i) { System.out.println(&quot; &quot; + result[i].getName() ); }
  64. 64. Searching the DF <ul><li>By default, the search returns an array which is either empty or contains a single DFD. </li></ul><ul><li>To get entries for all agents with specified properties, a third parameter of the searchConstraints type must be added: </li></ul><ul><li>To get any available agent, just use a bare DFD with no service specification </li></ul>06/04/09 … SearchConstraints all = new SearchConstraints(); all.setMaxResults(new Long(-1)); DFAgentDescription[] result = DFService.search(this, dfd, all);
  65. 65. DF Subscription Services <ul><li>It is possible for an agent to ask the DF to send it a message whenever an agent of a given type registers. </li></ul><ul><ul><li>DFAgentDescription dfd = new DFAgentDescription(); </li></ul></ul><ul><ul><li>ServiceDescription sd = new ServiceDescription(); </li></ul></ul><ul><ul><li>sd.setType(....); </li></ul></ul><ul><ul><li>dfd.addServices(sd); </li></ul></ul><ul><ul><li>SearchConstraints sc = new SearchConstraints(); </li></ul></ul><ul><ul><li>sc.setMaxResults(new Long(1)); </li></ul></ul><ul><ul><li>send(DFService.createSubscriptionMessage(this, getDefaultDF(),dfd, sc)); </li></ul></ul><ul><li>The DF will then send an INFORM message to the subscribed agent whenever an agent matching the supplied description registers. </li></ul>06/04/09
  66. 66. Mobile Agents <ul><li>Agent mobility is the ability of an agent program to migrate or to make a copy (clone) of itself across one or multiple network hosts. </li></ul><ul><li>The current version of JADE supports only intra-platform mobility, i.e. an agent can move only within the same platform from one container to the others. </li></ul><ul><li>The Agent class contains a set of methods dedicated for managing agent mobility. </li></ul>06/04/09
  67. 67. Mobile Agents <ul><li>A typical behaviour pattern for a JADE mobile agent will be to ask the AMS for locations and then decide if, where and when to migrate. </li></ul><ul><li>To move an agent, one just need to call the method doMove(Location) whose parameter is the new destination of the agent. </li></ul><ul><li>To get a Location object, one must query this information with the AMS, by sending it a REQUEST with either </li></ul><ul><ul><li>WhereIsAgentAction or </li></ul></ul><ul><ul><li>QueryPlatformLocationsAction </li></ul></ul>06/04/09
  68. 68. Finding destination <ul><li>WhereIsAgentAction </li></ul><ul><ul><li>aimed for obtaining location of the agent whose identifier (AID) is passed as a parameter . </li></ul></ul><ul><li>QueryPlatformLocationsAction </li></ul><ul><ul><li>aimed for obtaining all available locations within the given platform (takes no parameters). </li></ul></ul>06/04/09 Action action = new Action(); action.setActor(a.getAMS()); action.setAction(new QueryPlatformLocationsAction()); sendRequest(action);
  69. 69. Finding destination <ul><li>Receiving response from the AMS </li></ul>06/04/09 Suspend all activities until the message arrives
  70. 70. Moving an agent <ul><li>To make an agent move to a certain location another agent has to: </li></ul><ul><ul><li>create a new MoveAction object </li></ul></ul><ul><ul><ul><li>and fill its argument with a suitable MobileAgentDescription object, </li></ul></ul></ul><ul><ul><li>create a request ACL message and set the MoveAction object as its content </li></ul></ul><ul><ul><ul><li>call the Agent.getContentManager().fillContent(…) method that turns the MoveAction object into a String and writes it into the content slot of the request. </li></ul></ul></ul><ul><ul><li>send the message to the agent that is supposed to move </li></ul></ul>06/04/09
  71. 71. Moving an agent 06/04/09 A utility method containing a call to the Agent.getContentManager().fillContent(…)
  72. 72. Moving an agent <ul><li>When the other agent receives this message it: </li></ul><ul><ul><li>extracts the information about the destination to move to </li></ul></ul><ul><ul><li>calls its method doMove(Location) to start the moving process. </li></ul></ul>06/04/09 <ul><li>ACLMessage msg = receive(…); </li></ul><ul><li>if (msg.getPerformative() == ACLMessage.REQUEST) { </li></ul><ul><ul><li>ContentElement content = getContentManager().extractContent(msg); Concept concept = ((Action)content).getAction(); </li></ul></ul><ul><ul><li>MoveAction ma = (MoveAction)concept; Location loc = ma.getMobileAgentDescription().getDestination(); if (loc != null) doMove(loc); </li></ul></ul><ul><li>} </li></ul>
  73. 73. Moving an agent <ul><li>To be able to use these objects, the SLCodec language and the MobilityOntology must be registered with the agents content manager. </li></ul><ul><ul><li>This is simple as inserting the following two lines of code in the agent’s setup() method </li></ul></ul>06/04/09 getContentManager().registerLanguage(new SLCodec()); getContentManager().registerOntology(MobilityOntology. getInstance());
  74. 74. Cloning an agent <ul><li>Cloning an agent is very similar to moving an agent: </li></ul><ul><ul><li>instead of using a MoveAction object as content of the REQUEST to the interested agent, a CloneAction object should be used. </li></ul></ul>06/04/09 Location dest = … AgentID aid = … MobileAgentDescription mad = new MobileAgentDescription(); mad.setName(aid); mad.setDestination(dest); String newName = &quot;Clone-&quot; + agentName; CloneAction ca = new CloneAction(); ca.setNewName(newName); ca.setMobileAgentDescription(mad); sendRequest(new Action(aid, ca));
  75. 75. Cloning an agent <ul><li>when the agent receives the message it just calls its method doClone(Location, String) which takes as parameters: </li></ul><ul><ul><li>the location where the cloned agent will start and </li></ul></ul><ul><ul><li>its new name which must be different from the original agent's name. </li></ul></ul>06/04/09 <ul><li>ACLMessage msg = receive(…); </li></ul><ul><li>if (msg.getPerformative() == ACLMessage.REQUEST) { </li></ul><ul><ul><li>ContentElement content = getContentManager().extractContent(msg); Concept concept = ((Action)content).getAction(); </li></ul></ul><ul><ul><li>CloneAction ca = (CloneAction)concept; String newName = ca.getNewName(); Location loc = ca.getMobileAgentDescription().getDestination(); if (loc != null) doClone(loc, newName); </li></ul></ul><ul><li>} </li></ul>
  76. 76. Agents with GUI <ul><li>To build an agent with integrated GUI, one should extend the JADE’s GuiAgent class. </li></ul><ul><li>This class has two specific methods: postGuiEvent() and onGuiEvent() , aimed for handling interactions between an agent and its GUI: </li></ul><ul><ul><li>GUI informs the agent about the user actions by posting events through the postGuiEvent() method. </li></ul></ul><ul><ul><li>onGuiEvent() method contains code for receiving and processing events posted by the GUI (via postGuiEvent() method ) . </li></ul></ul>06/04/09
  77. 77. Agents with GUI <ul><li>The onGuiEvent() method may be viewed as the equivalent of the actionPerformed() method in Java Swing. </li></ul><ul><li>To post an event to the agent, the GUI simply creates a jade.gui.GuiEvent object and passes it as an argument to the postGuiEvent() method . </li></ul><ul><li>A GuiEvent object has: </li></ul><ul><ul><li>two mandatory attributes : </li></ul></ul><ul><ul><ul><li>the source of the event and </li></ul></ul></ul><ul><ul><ul><li>an integer identifying the type of event </li></ul></ul></ul><ul><ul><li>an optional list of parameters that can be added to the event object. </li></ul></ul>06/04/09
  78. 78. Agents with GUI <ul><li>public class ControllerAgent extends GuiAgent { </li></ul><ul><li>… </li></ul><ul><li>protected ControllerAgentGui myGui; </li></ul><ul><li>… </li></ul><ul><li>protected void onGuiEvent(GuiEvent ev) { </li></ul><ul><li>command = ev.getType() ; </li></ul><ul><li>if (command == NEW_AGENT) { … } </li></ul><ul><li>if (command == MOVE_AGENT) { </li></ul><ul><li>String agentName = (String) ev.getParameter(0) ; </li></ul><ul><li>String destName = (String) ev.getParameter(1) ; </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>06/04/09
  79. 79. JADE’s suite of graphical tools <ul><li>IntrospectorAgent </li></ul><ul><ul><li>A tool useful for monitoring a running agent: </li></ul></ul><ul><ul><ul><li>the life cycle of the agent, </li></ul></ul></ul><ul><ul><ul><li>ACL messages it exchanges with other agents and </li></ul></ul></ul><ul><ul><ul><li>the agent’s behaviours in execution. </li></ul></ul></ul><ul><li>Dummy Agent </li></ul><ul><ul><li>a monitoring and debugging tool, </li></ul></ul><ul><ul><li>use it for: </li></ul></ul><ul><ul><ul><li>composing and sending ACL messages to other agents, </li></ul></ul></ul><ul><ul><ul><li>viewing the list of all the ACL messages sent or received, completed with timestamp information . </li></ul></ul></ul>06/04/09
  80. 80. JADE’s suite of graphical tools <ul><li>Sniffer </li></ul><ul><ul><li>An agent that can intercept ACL messages while they are in flight, and display them graphically </li></ul></ul><ul><ul><ul><li>notation similar to UML sequence diagrams </li></ul></ul></ul><ul><ul><li>Useful for observing how collaborating agents exchange ACL messages. </li></ul></ul>06/04/09
  81. 81. Jade Resources at WWW <ul><li>Jade Official Web Site: </li></ul><ul><li>http://jade.cselt.it </li></ul><ul><li>Jade Primer : </li></ul><ul><li>http://www.iro.umontreal.ca/~vaucher/Agents/Jade/JadePrimer.html </li></ul><ul><li>FIPA </li></ul><ul><li>http://www.fipa.org </li></ul>06/04/09
  82. 82. JADE Java Agent Development Framework Jelena Jovanovic Email: [email_address]