Sunteți pe pagina 1din 8

Abstract. Jade este o platform de dezvoltare a sistemelor multi-agent, n concordan cu specificaiile FIPA.

Aceast librrie este disponibil n seciunea de Download a site-ului oficial http://jade.tilab.com, fiind liceniat sub GNU LGPL (Lesser General Public License).

1. Introducere
Scopul acestui tutorial este acela de a ghida utilizatorii nceptori n a crea ageni JADE simpli i de a executa diverse operaii pe acetia. JADE este implementat integral n Java i ofer utilizatorilor posibilitatea de a folosi n Java n implementarea agenilor proprii n consecin pentru parcurgerea acestui tutorial este necesar un anumit nivel de familiaritate cu limbajul de programare Java. n afara facilitilor elementare descrise n acest tutorial, JADE ofer i cteva alte faciliti de exemplu, suport pentru protocoale avansate de interaciune.

2. Privire de ansamblu
JADE este o librrie middleware care faciliteaz dezvoltarea sistemelor multi-agent, care include: un mediu runtime, care servete ca platform pentru ageni i trebuie s ruleze pe orice sistem folosit n reeaua sistemului multi-agent; o colecie de componente pe care programatorii o pot folosi pentru a dezvolta ageni specializai; o colecie de intrumente grafice care permit monitorizarea i administrarea agenilor.

2.1 Contaninere i platforme


Fiecare instan activ a mediului runtime se numete container, deoarece poate menine numeroi ageni. Mulimea containerelor active se numete platform. Exist un singur container principal, care trebuie s fie activ pe toat ntreaga durat de via a platfomei toate celelalte containere trimit cereri de nregistrare ctre acesta, dup ce a fost creat.

Containere i Platforme

Avantajul folosirii JADE este acela c izoleaz complet detaliile implementrii mediului runtime. Un exemplu de lansare a unui container principal este:
javacp<classpath>jade.Bootgui

Pentru lansarea unui container periferic:


javacp<classpath>jade.Bootcontainerhostavalon.tilab.comagentspoke:myPackage.myclass

2.2. Servicii
n afara abilitii de a accepta cereri de nregistrare de la alte containere, un container principal difer de un container obinuit prin faptul c lucreaz cu doi ageni speciali, lansai n mod automat: Sistemul de management al agenilor care ofer serviciul de denumire (se asigur c fiecare agent are atribuit un nume unic) i servicii de administrare a agenilor. Mecanismul de localizare care ofer servicii gen Pagini Aurii - prin intermediul crora un agent poate localiza ali ageni.

3. Exemplu Comerul cu Cri


Acest capitol ilustreaz un exemplu practic de implementare a unei aplicaii multiagent folosind JADE. Scenariul descris n acest exemplu implic nite ageni care vnd cri i alii care cumpr, dirijai de utilizatori umani. Fiecare agent-cumprtor primete titlul crii pe care o cumpr sub forma unui argument n linia de comand. De asemeni, cere periodic tuturor agenilor-vnztori cunoscui s emit o ofert. n momentul cnd oferta este recepionat, cumprtorul o accept i emite un ordin de cumprare. n cazul n care mai muli vnztori emit aceai ofert , cumprtorul o alege pe cea mai rentabil. Dup ncheierea operaiei de cumprare, agentul-cumprtor ncheie execuia. Fiecare agent-vnztor are o interfa grafic simpl, prin intermediul creia utilizatorul poate aduga n catalogul asociat titluri noi i preul acestora. Agenii vnztori sunt n continu ateptare de cereri din partea cumprtorilor. Cnd sunt interogai pentru a emite o ofert, n prim instan verific dac aceasta exist n catalog i n cazul afirmativ, rspund cu valoarea preului sau n caz contrar refuz cererea. n momentul finalizrii cu succes a unei astfel de tranzacii, cartea vndut este eliminat din catalog.

4. Crearea unui agent (Clasa Agent)


Crearea unui agent utiliznd JADE se rezum la simpla extindere a clasei jade.core.agent i implementarea metodei setup(), aa cum este demonstrat aici:
importjade.core.Agent; publicclassBookBuyerAgentextendsAgent { protectedvoidsetup() { System.out.println(Hello!Buyeragent+getAID().getName()+is ready.);
}

Metoda setup() este menit s serveasc la iniializarea agentului funcionalitatea efectiv a lui poate fi descris prin implementarea comportamentului.

4.1. Identificatori ai agenilor


Fiecare agent este identificat cu ajutorul unui identificator, obinut prin instanierea clasei jade.core.AID. Metoda getAID() a clasei Agent permite accesul la identificatorul asociat instanei agentului n cauz. Un obiect AID conine un nume unic i un numr de adrese. Sintaxa de formare a numelui unic n cadrul JADE este <nickname>@<platforma>. Cmpurile de adres n cadrul obiectelor AID se refer la adresele platformei unde agentul este rezident i sunt utilizate numai n eventualitatea n care un agent are necesitatea de a comunica cu un altul, localizat ntr-o platform diferit. Dac nickname-ul unui agent este cunoscut, obiectul AID asociat poate fi obinut astfel:
AIDid=newAID(nickname,AID.ISLOCALNAME);

Constanta ISLOCALNAME indic faptul c primul parametru (nickname-ul) se refer la un agent local platformei, nu la numele global unic al agentului.

4.2. Rularea agenilor


Agentul creat poate fi compilat astfel:
javacclasspath<JADE><CLASA>

Pentru a putea rula, agentul compilat trebuie lansat cu un nickname:


javaclasspath<JADE>;.jade.Boot<agent>

4.3. Suspendarea agenilor


Perntru a suspenda execuia unui agent este disponibil metoda doDelete(). Similar metodei setup(), invocat de mediul runtime pentru iniializarea agentului, exist metoda takeDown(), care are scopul de a disponibiliza resursele utilizate n timpul rulrii agentului.

4.4. Specificarea argumentelor pentru ageni


Agenii pot avea specificate argumente la lansare. Acestea pot fi accesate apelnd metoda getArguments() a clasei Agent. Urmtoarea secven prezint un exemplu complet de definire a unui agent:
importjade.core.Agent; importjade.core.AID; publicclassBookBuyerAgentextendsAgent {
//Titlulcrii

privateStringtargetBookTitle;
//listaagenilorvnztoricunoscui

privateAID[]sellerAgents={ newAID(seller1,AID.ISLOCALNAME), newAID(seller2,AID.ISLOCALNAME) };


//iniializareaagentului

protectedvoidsetup() { System.out.println(Hello!Buyeragent+getAID().getName()+isready.);
//citireatitluluicriidinargumentelelinieidecomand Object[]args=getArguments();

if(args!=null&&args.length>0) { targetBookTitle=(String)args[0]; System.out.println(Tryingtobuy+targetBookTitle);


}

else {

//Maketheagentterminateimmediately

System.out.println(Nobooktitlespecified);
doDelete(); } }

protectedvoidtakeDown() { System.out.println(Buyeragent+getAID().getName()+terminating.);
}

Argumentele liniei de comand pot fi specificate ntre paranteze, fiind permise spaii.

5. Definirea comportamentelor
Dup derivarea i iniializarea unui agent, comportamentul su reprezentnd funcia pe care acesta trebuie s o ndeplineasc poate fi definit prin derivarea unui obiect ce extinde jade.core.behaviours.Behaviour. Pentru a determina un agent s execute aciunea (numit comportament) definit de un obiect de definire a comportamentului, este suficient adugarea acestuia utiliznd addBehaviour() din obiectul Agent curent.

5.1. Funcii
Un agent poate executa multiple comportamente n paralel, ns spre deosebire de execuia tradiional, bazat pe fire de execuie comportamentele unui agent lucreaz n mod cooperativ, ceea ce nseamn c atunci cnd o funcie este programat pentru execuie, aceasta va rula fr intrerupere pn la terminarea execuiei. n consecin, este responsabilitatea programatorului s defineasc cnd un agent trece de la execuia unei funcii la alta.

Calea de execuie a unui agent

Dei necesit un efort suplimentar din partea programatorului, aceast abordare are multiple avantaje: face posibil ca fiecare agent s utilizeze un singur fir de execuie; elimin dificultile de sincronizare ntre fire de execuie paralele la accesarea resurselor comune; un ctig de performan n cazul delegrilor de comportament.

5.2. Comportamente singulare, comportamente ciclice i comportamente generice


Putem distinge trei tipuri distincte de comportamente, n funcie de modelul lor de execuie: 1) Comportamente singulare, care sunt finalizate imediat ce metoda action() termin execuia.
publicclassMyOneShotBehaviourextendsOneShotBehaviour { publicvoidaction() { //executoseriedeoperaii } }

Operaiile se execut o singur dat. 2) Comportamente ciclice care nu ajung la completare i a cror metod action() execut aceleai operaii la fiecare apel. Clasa care faciliteaz acest comportament este jade.core.behaviours.CyclicBehaviour.
publicclassMyCyclicBehaviourextendsCyclicBehaviour { publicvoidaction() { //executoseriedeoperaii } }

3) Comportamente generice, care nglobeaz variabile de status i execut diferite operatii n funcie de acest status, ajungnd la final cnd anumite condiii sunt ndeplinite.
publicclassMyThreeStepBehaviourextendsBehaviour { privateintstep=0; publicvoidaction() { switch(step) { case0: break;

//executoperaiaX step++;

case1: break;

//executoperaiaY step++;

case2: break;
} }

//executoperaiaZ step++;

publicbooleandone() { returnstep==3;
} }

Operaiile X, Y, i Z sunt efectuate n ordine, una cte una, dup care comportamentul se finalizeaz.

6. Comunicarea ntre ageni


O facilitate important a framework-ului JADE o reprezint abilitatea agenilor de a comunica. Paradigma de comunicare adoptat n acest scop este transmiterea asincron a mesajelor. Fiecare agent dispune de o coad de ateptare unde mediul runtime stocheaz mesajele ndat ce sunt recepionate,

agenii procesndu-le n ordinea inserrii cnd i dac programatorul dorete, dup urmtoarea schem:

Modelul de implementare a comunicaiei ntre ageni

6.1. Limbajul ACL


Mesajele tranzacionate de agenii JADE respect un format definit de FIPA (http://www.fipa.org), i anume limbajul ACL. Acest format integreaz o serie de cmpuri, n mod deosebit: emitorul mesajului; lista de receptori; intenia de comunicare, ce indic scopul n care mesajul a fost emis; acesta poate fi REQUEST, n cazul n care emitorul dorete ca receptorul s ndeplineasc o anumit sarcin, INFORM, n cazul n care emitorul dorete s ntiineze receptorul de un anume fapt, QUERY_IF - dac emitorul dorete s interogheze receptorul asupra unei aserii, CFP (call for proposal) i de asemeni PROPOSE, ACCEPT_PROPOSAL, REJECT_PROPOSAL dac emitorul i receptorul sunt angajai ntr-o negociere, i altele; coninutul, mai exact informaia specific coninut de mesaj (de exemplu tipul, felul i ali parametri ai unei aciuni ntr-un mesaj de tip REQUEST); limbajul coninutului mai exact sintaxa folosit pentru a descrie coninutul; vocabularul de simboluri folosite n coninut i nelesul lor; Un mesaj n JADE este implementat ca un obiect de tipul jade.lang.ACLMessage care conine metode de citire i scriere a diverselor cmpuri.

6.2. Trimiterea mesajelor


Trimiterea mesajelor ctre un agent presupune simpla scriere a cmpurilor ntr-o instan a unui obiect ACLMessage i apelarea metodei send() a clase Agent curente:
ACLMessagemsg=newACLMessage(ACLMessage.INFORM); msg.addReceiver(newAID(Peter,AID.ISLOCALNAME)); msg.setLanguage(English); msg.setOntology(Weatherforecastontology); msg.setContent(Todayitsraining); send(msg);

6.3. Recepionarea mesajelor


Aa cum am menionat mai devreme, n momentul recepionrii, mediul runtime JADE stocheaz mesajele n coada de ateptare a agentului-destinatar. Mesajele astfel primite sunt disponibile prin apelul metodei receive() a obiectului destinatar. Aceast metod ntoarce unul cte unul mesajele primite, n ordinea sosirii lor (FIFO) i apoi le elimin sau null n cazul n care nici un mesaj nu exist n coad.
ACLMessagemsg=receive();

if(msg!=null){ }

//Processeazmesajul

Bibliografie
1. Documentaia oficial JADE (http://jade.tilab.com/doc/) 2. Pagina wikipedia pentru sisteme distribuite (http://en.wikipedia.org/wiki/Distributed_computing) 3. Resurse variate n legtur cu programarea paralel, oferite de serviciul google code: (http://code.google.com/edu/parallel/)

Anex: Licena GPU LGPL GNU LESSER GENERAL PUBLIC LICENSE


Version 3, 29 June 2007 Copyright 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.

0. Additional Definitions.
As used herein, this License refers to version 3 of the GNU Lesser General Public License, and the GNU GPL refers to version 3 of the GNU General Public License.

The Library refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
An Application is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A Combined Work is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the Linked Version. The Minimal Corresponding Source for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The Corresponding Application Code for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.

1. Exception to Section 3 of the GNU GPL.


You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.

2. Conveying Modified Versions.


If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.

3. Object Code Incorporating Material from Library Header Files.


The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document.

4. Combined Works.
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interfacecompatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)

5. Combined Libraries.
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.

6. Revised Versions of the GNU Lesser General Public License.


The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License or any later version applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

S-ar putea să vă placă și