Jade Basic

download Jade Basic

of 29

Transcript of Jade Basic

  • 8/2/2019 Jade Basic

    1/29

    TELECOM ITALIA GROUP

    JADE14 September 2010

    Giovanni CaireGiovanni CaireGiovanni CaireGiovanni Caire

    [email protected]

  • 8/2/2019 Jade Basic

    2/29

    Agenda

    Overview

    Creating Agents

    Agent tasks

    Agent communication

    The yellow pages service

  • 8/2/2019 Jade Basic

    3/29

    3

    TELECOM ITALIA

    JADE

    INFOR

    M

    REQUEST PROP

    OSE

    AGREE

    REFUSE

    A middleware for applications based on the agent paradigmA middleware for applications based on the agent paradigmA middleware for applications based on the agent paradigmA middleware for applications based on the agent paradigm

    ProvidesProvidesProvidesProvides

    The AgentAgentAgentAgent and BehaviourBehaviourBehaviourBehaviour (a task that an agent can execute) abstractions

    Transparent distributiondistributiondistributiondistribution of components (agents)

    PeerPeerPeerPeer----totototo----peerpeerpeerpeer communicationcommunicationcommunicationcommunication based on asynchronous message passing

    Publish-subscribe

    discoverydiscoverydiscoverydiscovery mechanisms Fully written inFully written inFully written inFully written in JavaJavaJavaJava

    Open SourceOpen SourceOpen SourceOpen Source

    http://jade.tilab.com

    ~230.000 downloads

    Current version: 4.014.014.014.01

  • 8/2/2019 Jade Basic

    4/29

    4

    TELECOM ITALIA

    Architecture

    JADE

    Container

    Container

    Main

    ContainerPLATFORM

    AMS DF

    PLATFORM 2JADE

    AMS DF

    Main

    Container

    JADE JADE

    host1

    host2 host3

    host4

  • 8/2/2019 Jade Basic

    5/29

    5

    TELECOM ITALIA

    The communication model

    A1 A2

    Prepare themessage to A2

    Get themessage fromthe messagequeue andprocess it

    JADE distributed runtime

    (PROPOSE(PROPOSE(PROPOSE(PROPOSE

    :sender A1:sender A1:sender A1:sender A1

    :receiver A2:receiver A2:receiver A2:receiver A2

    :content:content:content:content Cinema eveningCinema eveningCinema eveningCinema evening

    :language:language:language:language EnglishEnglishEnglishEnglish

    :ontology:ontology:ontology:ontology CinemaCinemaCinemaCinema----ontologyontologyontologyontology

    ))))

    Based onBased onBased onBased on asynchronousasynchronousasynchronousasynchronous message passingmessage passingmessage passingmessage passing

    Message format defined by theMessage format defined by theMessage format defined by theMessage format defined by the ACLACLACLACL language (FIPA)language (FIPA)language (FIPA)language (FIPA)

  • 8/2/2019 Jade Basic

    6/29

    Agenda

    Overview

    Creating Agents

    Agent tasks

    Agent communication

    The yellow pages service

  • 8/2/2019 Jade Basic

    7/29

    7

    TELECOM ITALIA

    The HelloWorld agent

    import jade.core.Agent;

    public class HelloWorldAgent extends Agent {

    protected void setup() {

    System.out.println(Hello World! my name is +getAID().getName());

    }

    }

    AAAA type of agenttype of agenttype of agenttype of agent is created by extending theis created by extending theis created by extending theis created by extending the jade.core.Agent classclassclassclass

    and redefining theand redefining theand redefining theand redefining the setup() method.method.method.method.

    EachEachEachEach Agent instanceAgent instanceAgent instanceAgent instance is identified by anis identified by anis identified by anis identified by an AIDAIDAIDAID ((((jade.core.AID).).).).

    An AID is composed of a unique name plus some addresses

    An agent can retrieve its AID through the getAID() method of the Agent

    class

  • 8/2/2019 Jade Basic

    8/29

    8

    TELECOM ITALIA

    Local names, GUID and addresses

    Agent names have the formAgent names have the formAgent names have the formAgent names have the form

    The complete name of an agent must beThe complete name of an agent must beThe complete name of an agent must beThe complete name of an agent must be globally uniqueglobally uniqueglobally uniqueglobally unique....

    TheTheTheThe defaultdefaultdefaultdefault platform name is /JADE

    The platform name can be set using theThe platform name can be set using theThe platform name can be set using theThe platform name can be set using the name optionoptionoptionoption

    Within a single JADE platform agents are referred through theirWithin a single JADE platform agents are referred through theirWithin a single JADE platform agents are referred through theirWithin a single JADE platform agents are referred through their namesnamesnamesnames

    only.only.only.only.

    Given the name of an agent its AID can be created asGiven the name of an agent its AID can be created asGiven the name of an agent its AID can be created asGiven the name of an agent its AID can be created as

    AID id = new AID(localname, AID.ISLOCALNAME);

    AID id = new AID(name, AID.ISGUID);

    The addresses included in an AID are those of the platform (The addresses included in an AID are those of the platform (The addresses included in an AID are those of the platform (The addresses included in an AID are those of the platform (MTPsMTPsMTPsMTPs) and) and) and) andareareareare ONLYONLYONLYONLY used in communication between agents living onused in communication between agents living onused in communication between agents living onused in communication between agents living on differentdifferentdifferentdifferent

    FIPA platformsFIPA platformsFIPA platformsFIPA platforms

  • 8/2/2019 Jade Basic

    9/29

    9

    TELECOM ITALIA

    Main startup options summary

    Switch optionsSwitch optionsSwitch optionsSwitch options

    -gui (activate the management GUI)

    -container (launch a peripheral container instead of a main container) KeyKeyKeyKey----value pair optionsvalue pair optionsvalue pair optionsvalue pair options

    -host (the host where the Main Container is running)

    -port (the port where the Main Container is running)

    -detect-main ((find the Main Container automatically)-local-port (the port to be used by the starting container)

    -agents (the agents to be started at bootstrap)

    -conf (get startup options from a property file)

    Command line examplesCommand line examplesCommand line examplesCommand line examplesjava cp ... jade.Boot gui agents john:hello.HelloWorldAgent

    java cp ... jade.Boot container host avalon.telecomitalia.it

  • 8/2/2019 Jade Basic

    10/29

    10

    TELECOM ITALIA

    Passing arguments to an agent

    protected void setup() {System.out.println(Hallo World! my name is +getAID().getName());

    Object[] args = getArguments();

    if (args != null) {

    System.out.println(My arguments are:);

    for (int i = 0; i < args.length; ++i) {

    System.out.println(- +args[i]);

    }

    }

    }

    It is possible to pass arguments to an agentIt is possible to pass arguments to an agentIt is possible to pass arguments to an agentIt is possible to pass arguments to an agent

    java jade.Boot .... a:myPackage.MyAgent(arg1,arg2)

    The agent can retrieve its arguments through the getArguments()

    method of the Agent class

  • 8/2/2019 Jade Basic

    11/29

    11

    TELECOM ITALIA

    Agent termination

    protected void setup() {

    System.out.println(Hallo World! my name is +getAID().getName());

    Object[] args = getArguments();

    if (args != null) {

    System.out.println(My arguments are:);for (int i = 0; i < args.length; ++i) {

    System.out.println(- +args[i]);

    }

    }

    doDelete();

    }

    protected void takeDown() {

    System.out.println(Bye...);

    }

    An agent terminates when itsAn agent terminates when itsAn agent terminates when itsAn agent terminates when its doDelete() method is called.() method is called.() method is called.() method is called.

    On termination the agentOn termination the agentOn termination the agentOn termination the agentssss takeDown() method is invoked (intended to() method is invoked (intended to() method is invoked (intended to() method is invoked (intended to

    include cleaninclude cleaninclude cleaninclude clean----up operations).up operations).up operations).up operations).

  • 8/2/2019 Jade Basic

    12/29

    Agenda

    Overview

    Creating Agents

    Agent tasks

    Agent communication

    The yellow pages service

  • 8/2/2019 Jade Basic

    13/29

    13

    TELECOM ITALIA

    The Behaviour class

    The actual job that an agent does is typically carried out withiThe actual job that an agent does is typically carried out withiThe actual job that an agent does is typically carried out withiThe actual job that an agent does is typically carried out withinnnn

    behavioursbehavioursbehavioursbehaviours

    BehavioursBehavioursBehavioursBehaviours are created by extending theare created by extending theare created by extending theare created by extending thejade.core.behaviours.Behaviour classclassclassclass

    To make an agent execute a task it is sufficient to create an inTo make an agent execute a task it is sufficient to create an inTo make an agent execute a task it is sufficient to create an inTo make an agent execute a task it is sufficient to create an instance ofstance ofstance ofstance of

    the correspondingthe correspondingthe correspondingthe corresponding Behaviour subclass and call thesubclass and call thesubclass and call thesubclass and call the

    addBehaviour() method of themethod of themethod of themethod of theAgent class.class.class.class.

    EachEachEachEach BehaviourBehaviourBehaviourBehaviour subclass must implementsubclass must implementsubclass must implementsubclass must implement

    public void action(): what the behaviour actually does

    public boolean done(): whether the behaviour is finished

  • 8/2/2019 Jade Basic

    14/29

    14

    TELECOM ITALIA

    Behaviour scheduling and execution

    Behaviour switch occurs only when

    the action() method of the currentlyscheduled behaviour returns.

    An agent can execute severalAn agent can execute severalAn agent can execute severalAn agent can execute several behavioursbehavioursbehavioursbehaviours in parallel, however,in parallel, however,in parallel, however,in parallel, however, behaviourbehaviourbehaviourbehaviour

    scheduling is not preemptive, butscheduling is not preemptive, butscheduling is not preemptive, butscheduling is not preemptive, but cooperativecooperativecooperativecooperative and everything occursand everything occursand everything occursand everything occurs

    within awithin awithin awithin a single Java Threadsingle Java Threadsingle Java Threadsingle Java Thread

  • 8/2/2019 Jade Basic

    15/29

    15

    TELECOM ITALIA

    setup()

    Agent has been killed

    (doDelete() method

    called)?

    Get the next behaviour from

    the pool of active behaviours

    b.action()

    b.done()?

    Remove currentBehaviour from

    the pool of active behaviours

    takeDown()

    - Initializations

    - Addition of initialbehaviours

    - Agent life (execution

    of behaviours)

    - Clean-up operations

    YES

    YES

    NO

    NO

    Highlighted in red

    the methods thatprogrammers have

    to/can implement

    The agent executionmodel

  • 8/2/2019 Jade Basic

    16/29

    16

    TELECOM ITALIA

    Behaviour types

    One shotOne shotOne shotOne shot behavioursbehavioursbehavioursbehaviours....

    Complete immediately and their action() method is executed only once.

    Their done() method simply returns true.

    jade.core.behaviours.OneShotBehaviour class

    CyclicCyclicCyclicCyclic behavioursbehavioursbehavioursbehaviours....

    Never complete and their action() method executes the same operation

    each time it is invoked

    Their done() method simply returns false.

    jade.core.behaviours.CyclicBehaviour class

    ComplexComplexComplexComplex behavioursbehavioursbehavioursbehaviours....

    Embed a state and execute in their action() method different operations

    depending on their state.

    Complete when a given condition is met.

  • 8/2/2019 Jade Basic

    17/29

    17

    TELECOM ITALIA

    Scheduling operations at given points in time

    JADE provides two readyJADE provides two readyJADE provides two readyJADE provides two ready----made classes by means of which it is possiblemade classes by means of which it is possiblemade classes by means of which it is possiblemade classes by means of which it is possible

    to easily implement behaviours that execute operations at givento easily implement behaviours that execute operations at givento easily implement behaviours that execute operations at givento easily implement behaviours that execute operations at given pointspointspointspoints

    in timein timein timein timeWakerBehaviour

    The action() and done() method are already implemented so that the

    onWake() method (to be implemented by subclasses) is executed after a

    given timeout

    After that execution the behaviour completes.

    TickerBehaviour

    The action() and done() method are already implemented so that the

    onTick() (to be implemented by subclasses) method is executed

    periodically with a given period

    The behaviour runs forever unless its stop() method is called.

  • 8/2/2019 Jade Basic

    18/29

  • 8/2/2019 Jade Basic

    19/29

    Agenda

    Overview

    Creating Agents

    Agent tasks

    Agent communication

    The yellow pages service

  • 8/2/2019 Jade Basic

    20/29

    20

    TELECOM ITALIA

    The communication model

    A1 A2

    Prepare themessage to A2

    Get themessage fromthe messagequeue andprocess it

    JADE distributed runtime

    (PROPOSE(PROPOSE(PROPOSE(PROPOSE

    :sender A1:sender A1:sender A1:sender A1

    :receiver A2:receiver A2:receiver A2:receiver A2

    :content:content:content:content Cinema eveningCinema eveningCinema eveningCinema evening

    :language:language:language:language EnglishEnglishEnglishEnglish

    :ontology:ontology:ontology:ontology CinemaCinemaCinemaCinema----ontologyontologyontologyontology

    ))))

    Based onBased onBased onBased on asynchronousasynchronousasynchronousasynchronous message passingmessage passingmessage passingmessage passing

    Message format defined by theMessage format defined by theMessage format defined by theMessage format defined by the ACLACLACLACL language (FIPA)language (FIPA)language (FIPA)language (FIPA)

  • 8/2/2019 Jade Basic

    21/29

    21

    TELECOM ITALIA

    The ACLMessage class

    Messages exchanged by agents are instances of theMessages exchanged by agents are instances of theMessages exchanged by agents are instances of theMessages exchanged by agents are instances of the

    jade.lang.acl.ACLMessage class.class.class.class.

    ProvideProvideProvideProvide accessoraccessoraccessoraccessor methods to get and set all the fields defined by themethods to get and set all the fields defined by themethods to get and set all the fields defined by themethods to get and set all the fields defined by theACL languageACL languageACL languageACL language

    get/setPerformative();

    get/setSender();

    add/getAllReceiver();

    get/setLanguage();

    get/setOntology();

    get/setContent();

    ....

  • 8/2/2019 Jade Basic

    22/29

    22

    TELECOM ITALIA

    Sending and receiving messages

    ACLMessage msg = new ACLMessage(ACLMessage.INFORM);

    msg.addReceiver(new AID(Peter, AID.ISLOCALNAME));

    msg.setLanguage(English);

    msg.setOntology(Weather-Forecast-Ontology);

    msg.setContent(Today its raining);

    send(msg);

    ACLMessage msg = receive();

    if (msg != null) {

    // Process the message

    }

    Sending a message is as simple as creating anSending a message is as simple as creating anSending a message is as simple as creating anSending a message is as simple as creating anACLMessage objectobjectobjectobject

    and calling theand calling theand calling theand calling the send() method of themethod of themethod of themethod of theAgent classclassclassclass

    Reading messages from the private message queue is accomplishedReading messages from the private message queue is accomplishedReading messages from the private message queue is accomplishedReading messages from the private message queue is accomplished

    through thethrough thethrough thethrough the receive() method of themethod of themethod of themethod of theAgent class.class.class.class.

  • 8/2/2019 Jade Basic

    23/29

    23

    TELECOM ITALIA

    Blocking a behaviour waiting for a message

    public void action() {

    ACLMessage msg = myAgent.receive();

    if (msg != null) {

    // Process the message

    }else {

    block();

    }}

    This is the strongly

    recommended

    pattern to receivemessages within abehaviour

    AAAA behaviourbehaviourbehaviourbehaviour that processes incoming messages does not know exactlythat processes incoming messages does not know exactlythat processes incoming messages does not know exactlythat processes incoming messages does not know exactly

    when a message will arrivewhen a message will arrivewhen a message will arrivewhen a message will arrive

    TheTheTheTheblock() method of themethod of themethod of themethod of the Behaviour class removes aclass removes aclass removes aclass removes a behaviourbehaviourbehaviourbehaviourfrom the agent pool and puts it in a blocked state (from the agent pool and puts it in a blocked state (from the agent pool and puts it in a blocked state (from the agent pool and puts it in a blocked state (not a blocking call!!not a blocking call!!not a blocking call!!not a blocking call!!).).).).

    Each time a message is received all blockedEach time a message is received all blockedEach time a message is received all blockedEach time a message is received all blocked behavioursbehavioursbehavioursbehaviours are insertedare insertedare insertedare inserted

    back in the pool and have a chance to read and process the messaback in the pool and have a chance to read and process the messaback in the pool and have a chance to read and process the messaback in the pool and have a chance to read and process the message.ge.ge.ge.

  • 8/2/2019 Jade Basic

    24/29

    24

    TELECOM ITALIA

    MessageTemplate tpl = MessageTemplate.MatchOntology(Test-Ontology);

    public void action() {

    ACLMessage msg = myAgent.receive(tpl);

    if (msg != null) {// Process the message

    }

    else {

    block();

    }

    }

    Selective reading from the message queue

    TheTheTheThe receive() method returns the first message in the messagemethod returns the first message in the messagemethod returns the first message in the messagemethod returns the first message in the message

    queue and removes it.queue and removes it.queue and removes it.queue and removes it.

    If there are two (or more)If there are two (or more)If there are two (or more)If there are two (or more) behavioursbehavioursbehavioursbehaviours receiving messages, one mayreceiving messages, one mayreceiving messages, one mayreceiving messages, one may

    stealstealstealsteal a message that the other one was interested in.a message that the other one was interested in.a message that the other one was interested in.a message that the other one was interested in.

    To avoid this it is possible to read only messages with certainTo avoid this it is possible to read only messages with certainTo avoid this it is possible to read only messages with certainTo avoid this it is possible to read only messages with certain

    characteristics (e.g. whose sender is agentcharacteristics (e.g. whose sender is agentcharacteristics (e.g. whose sender is agentcharacteristics (e.g. whose sender is agent PeterPeterPeterPeter) specifying a) specifying a) specifying a) specifying a

    jade.lang.acl.MessageTemplate parameter in theparameter in theparameter in theparameter in the receive()

    method.method.method.method.

  • 8/2/2019 Jade Basic

    25/29

    25

    TELECOM ITALIA

    Receiving messages in blocking mode

    - Use receive() + Behaviour.block() to

    receive messages within behaviours.- UseblockingReceive() to receive messageswithin the agent setup() and takeDown()

    methods.

    TheTheTheTheAgent class also provides theclass also provides theclass also provides theclass also provides theblockingReceive() method thatmethod thatmethod thatmethod that

    returns only when there is a message in the message queue.returns only when there is a message in the message queue.returns only when there is a message in the message queue.returns only when there is a message in the message queue.

    There areThere areThere areThere are overloaded versionsoverloaded versionsoverloaded versionsoverloaded versions that accept athat accept athat accept athat accept aMessageTemplate (the(the(the(themethod returns only when there is a message matching the templatmethod returns only when there is a message matching the templatmethod returns only when there is a message matching the templatmethod returns only when there is a message matching the template)e)e)e)

    and or a timeout (if it expires the method returns null).and or a timeout (if it expires the method returns null).and or a timeout (if it expires the method returns null).and or a timeout (if it expires the method returns null).

    Since it is a blocking call it isSince it is a blocking call it isSince it is a blocking call it isSince it is a blocking call it is dangerousdangerousdangerousdangerous to useto useto useto use

    blockingReceive() within awithin awithin awithin a behaviourbehaviourbehaviourbehaviour. In fact no other. In fact no other. In fact no other. In fact no other behaviourbehaviourbehaviourbehaviour

    can run untilcan run untilcan run untilcan run untilblockingReceive() returns.returns.returns.returns.

  • 8/2/2019 Jade Basic

    26/29

    Agenda

    Overview

    Creating Agents

    Agent tasks

    Agent communication

    The yellow pages service

  • 8/2/2019 Jade Basic

    27/29

    27

    TELECOM ITALIA

    The yellow pages service

    A1: - serviceX

    - serviceY

    A2: - serviceZ

    A3: - serviceW

    - serviceK

    - serviceH

    Yellow Pages service

    Publishprovidedservices

    A1

    A2

    A3

    A4

    A5

    A6

    Search foragents

    providing the

    requiredservices

    Exploit requiredservice

    DF

  • 8/2/2019 Jade Basic

    28/29

    28

    TELECOM ITALIA

    Interacting with the DF Agent

    The DF is an agent and as such it communicates using ACLThe DF is an agent and as such it communicates using ACLThe DF is an agent and as such it communicates using ACLThe DF is an agent and as such it communicates using ACL

    The ontology and language that the DFThe ontology and language that the DFThe ontology and language that the DFThe ontology and language that the DF understandsunderstandsunderstandsunderstands are specified byare specified byare specified byare specified by

    FIPAFIPAFIPAFIPA It is possible to search/register to a DF agent of a remoteIt is possible to search/register to a DF agent of a remoteIt is possible to search/register to a DF agent of a remoteIt is possible to search/register to a DF agent of a remoteplatform.platform.platform.platform.

    TheTheTheThe jade.domain.DFService class provides static utility methodsclass provides static utility methodsclass provides static utility methodsclass provides static utility methods

    that facilitate the interactions with the DFthat facilitate the interactions with the DFthat facilitate the interactions with the DFthat facilitate the interactions with the DF

    register();

    modify();

    deregister();

    search();

    The JADE DF also supports a subscription mechanismThe JADE DF also supports a subscription mechanismThe JADE DF also supports a subscription mechanismThe JADE DF also supports a subscription mechanism

  • 8/2/2019 Jade Basic

    29/29

    29

    TELECOM ITALIA

    DFDescription format

    When an agent registers with the DF it must provide a descriptioWhen an agent registers with the DF it must provide a descriptioWhen an agent registers with the DF it must provide a descriptioWhen an agent registers with the DF it must provide a descriptionnnn

    (implemented by the(implemented by the(implemented by the(implemented by thejade.domain.FIPAAgentManagement.DFAgentDescription

    class) basically composed ofclass) basically composed ofclass) basically composed ofclass) basically composed of The agent AID

    A collection of service descriptions (implemented by the classServiceDescription). This, on its turn, includes:

    The service type (e.g. Weather forecast);

    The service name (e.g. Meteo-1);

    The languages, ontologies and interaction protocols that must be known to

    exploit the service

    A collection of service-specific properties in the form key-value pair

    When an agent searches/subscribes to the DF it must specify anotWhen an agent searches/subscribes to the DF it must specify anotWhen an agent searches/subscribes to the DF it must specify anotWhen an agent searches/subscribes to the DF it must specify anotherherherherDFAgentDescription that is used as a templatethat is used as a templatethat is used as a templatethat is used as a template