Car Agent Struktura chování: SequentialBehavior() SubBehavior(AgentRequestProtocol) send/receive parkingrequest SubBehavior(UseThisParkplace) reakce na příchozí zprávu usethisparkplace SequentialBehavior() SubBehavior(OneShotBehavior movingtotheparkplace) SubBehavior(OneShotBehavior parkingcar) SubBehavior(OneShotBehavior leavingparkplace) SubBehavior(AgentRequestProtocol) send leavingparkplace SubBehavior(finalization) Aktivity a procesy se mapují na metody Metody se obalují do OneShotBehavior. Odesílání zprávy a zpracování odpovědi se realizuje jako SubBehavior pomocí AgentRequestProtocol (AchieveREInitiator) Větvení v aktivitním diagramu se realizuje pomocí pomocné proměnné a stavů Každá větev reakce na zprávu má samostatné chování Každá sekvence aktivit a procesů je realizována pomocí SequentialBehavior
Zdrojový kód: import jade.core.aid; import jade.core.behaviours.behaviour; import jade.core.behaviours.oneshotbehaviour; import jade.core.behaviours.sequentialbehaviour; import jade.lang.acl.aclmessage; public class CarAgent extends LabisAgent { private AID dispatcheragent; //reference to Dispatcher private ACLMessage msg; //object of communication message private AgentMessage message; private Car car; private int messagestatus; public CarAgent() { dispatcheragent = new AID("MAS_Dispatcher", AID.ISLOCALNAME); message = new AgentMessage(); public void finalization() { super.finalization(); dodelete(); protected void setup() { car = (Car) this.getarguments()[0]; initialization(); msg = new ACLMessage(ACLMessage.REQUEST); msg.addreceiver(dispatcheragent); message.setcommand(public.msg_parking_request); message.setcontent(car); message.savetomsg(msg); //sending the messages messagestatus = 0; Behaviour b1 = new AgentRequestProtocol(this, msg) { protected void handleinform(aclmessage inform) { message.loadfrommsg(inform); System.out.println("Agent " + inform.getsender().getname() + " do: " + message.getcommand()); if (message.getcommand().equals(public.msg_use_this_parkplace)) messagestatus = 1; if (message.getcommand().equals(public.msg_parking_is_full)) messagestatus = 2; ; Behaviour b2 = new OneShotBehaviour(this) { finalization(); ; SequentialBehaviour mainprocess = new SequentialBehaviour(); mainprocess.addsubbehaviour(b1); mainprocess.addsubbehaviour(new UseThisParkplace()); mainprocess.addsubbehaviour(b2);
addbehaviour(mainprocess); private class UseThisParkplace extends SequentialBehaviour { public void onstart() { if (messagestatus == 2) { System.out.println("Parking is full"); return; if (messagestatus == 0) { System.out.println("Some problem with parking"); return; if (messagestatus == 1) { //actualbehaviour = this; this.addsubbehaviour(new OneShotBehaviour(this.myAgent) { movingtotheparkplace(); ); this.addsubbehaviour(new OneShotBehaviour(this.myAgent) { parkingcar(); ); this.addsubbehaviour(new OneShotBehaviour(this.myAgent) { leavingparkplace(); ); public void movingtotheparkplace() { + " moving to the parkplace: " + message.gettext()); public void parkingcar() { + " parking for " + car.gettime()); long start = System.currentTimeMillis(); while (start + (car.gettime() * 1000) > System.currentTimeMillis()) { public void leavingparkplace() { System.out.println("Agent " + getlocalname() + " leaving parkplace"); message.reset(); msg = new ACLMessage(ACLMessage.INFORM); msg.addreceiver(dispatcheragent); message.setcommand(public.msg_leaving_parkplace); message.setcontent(car); message.savetomsg(msg); addsubbehaviour(new AgentRequestProtocol(this.myAgent, msg) { );
DispatcherAgent Struktura chování: SubBehavior(AgentRespondProtocol) pro příjem REQEST zpráv SubBehavior(AgentRespondProtocol) pro příjem INFORM zpráv Aktivity a procesy se mapují na metody V průběhu života Agenta běží dvě paralelní chování, které pouze přijímají zprávy REQUEST a INFORM (to neodpovídá zcela aktivitnímu diagramu, ale je vyžadována systémem JADE a FIPA komunikací) Veškeré sekvence aktivit a procesů mohou být realizovány pouhým spouštěním, protože již spuštěné chování se vykoná celé Aktivity posílání zpráv jsou pouze odpovědí na zpracovanou přijatou zprávu
Zdrojový kód: import jade.domain.fipanames; import jade.domain.fipaagentmanagement.failureexception; import jade.lang.acl.aclmessage; import jade.lang.acl.messagetemplate; public class DispatcherAgent extends LabisAgent { private MyParkingDispatcherGui dgui; public Car[][] parking; private int carcount = 0; public String choosingandreservationoftheparkplace(car c) { String str = new String(); carcount++; for (int i = 0; i < parking.length; i++) for (int j = 0; j < parking[0].length; j++) { //System.out.println("parking: "+c+"/"+i+","+j+"-"+parking[i][j]); if (parking[i][j] == null) { parking[i][j] = c; str = "(" + i + "," + j + ")"; i = parking.length; j = parking[0].length; dgui.update(); return str; public void releaseoftheparkplace(car c) { carcount--; for (int i = 0; i < parking.length; i++) for (int j = 0; j < parking[0].length; j++) if (c!= null && parking[i][j]!= null) if (parking[i][j].getidnumber().equals(c.getidnumber())) { parking[i][j] = null; i = parking.length; j = parking[0].length; dgui.update(); protected void setup() { initialization(); + " waiting for requests..."); //receiving request's messages MessageTemplate template = MessageTemplate.and(MessageTemplate.MatchProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST), MessageTemplate.MatchPerformative(ACLMessage.REQUEST)); addbehaviour(new AgentRespondProtocol(this, template) { //request process protected ACLMessage prepareresultnotification(aclmessage request, ACLMessage response) throws FailureException { + ": Request action successfully performed"); if (receivemessage.getcommand().equals( Public.MSG_PARKING_REQUEST)) { AgentMessage message = new AgentMessage(); ACLMessage inform = request.createreply();
message.setcommand(public.msg_use_this_parkplace); message inform.setperformative(aclmessage.inform); if (carcount >= parking.length * parking[0].length) message.setcommand(public.msg_parking_is_full); else {.settext(choosingandreservationoftheparkplace((car) receivemessage.getcontent())); message.savetomsg(inform); return inform; else { + ": Action failed"); throw new FailureException("unexpected-error"); ); //receiving inform's messages template = MessageTemplate.and(MessageTemplate.MatchProtocol(FIPANames.InteractionProtocol.FIPA_SUBSCRIBE), MessageTemplate.MatchPerformative(ACLMessage.INFORM)); addbehaviour(new AgentRespondProtocol(this, template) { //vlastni zpracovani udalosti protected ACLMessage prepareresultnotification(aclmessage request, ACLMessage response) throws FailureException { + ": Inform action successfully performed"); if (receivemessage.getcommand().equals( Public.MSG_LEAVING_PARKPLACE)) { releaseoftheparkplace((car) receivemessage.getcontent()); AgentMessage message = new AgentMessage(); ACLMessage inform = request.createreply(); inform.setperformative(aclmessage.inform); message.setcommand(public.msg_ok); message.savetomsg(inform); return inform; else { + ": Action failed"); throw new FailureException("unexpected-error"); ); public DispatcherAgent() { this.availablemessages.add(public.msg_parking_request); this.availablemessages.add(public.msg_leaving_parkplace); parking = new Car[2][2];//initialization of parking dgui = new MyParkingDispatcherGui(this); dgui.setvisible(true); public int getcarcount() { return carcount;
Možný návrh mapování (bez FIPA) Aktivity Metody Procesy Metody Hlavní agentův proces SequentialBehavior Odeslání zprávy příkazy uvnitř chování Přijetí zprávy příkazy uvnitř chování (blokování chování dokud nebude nějaká zpráva přijata) Větvení větvení uvnitř chování založené na podmínce a zařazení potřebného chování každá větev realizována podle své struktury (nejčastěji SequentialBehavior) Sekvence SequentialBehavior Paralelismus CyclicBehavior, každá větev vnitřně jako SequentialBehavior Příkazy uvnitř chování metody obalené OneShotBehavior a spuštěné jako SubBehavior