Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package aop.behaviour; import aop.agents.Prisoner; import com.google.gson.Gson; import com.google.gson.internal.LinkedTreeMap; import jade.core.AID; import jade.core.behaviours.TickerBehaviour; import jade.lang.acl.ACLMessage; import jade.lang.acl.MessageTemplate; import java.awt.Point; import java.util.ArrayList; /** * * @author tony */ public class ListenBehaviour extends TickerBehaviour { final MessageTemplate tpl; public Prisoner prisoner; public ListenBehaviour(AID topic, Prisoner prisoner, long period) { super(prisoner, period); tpl = MessageTemplate.MatchTopic(topic); this.prisoner = prisoner; } @Override protected void onTick() { ACLMessage msg = myAgent.receive(tpl); if (msg != null && !prisoner.atExit()) { // System.out.println(this.myAgent.getLocalName() + " received: " + msg.getContent()); String content = msg.getContent(); Gson gson = new Gson(); String json = content.substring(5); ArrayList array = new ArrayList(); array = gson.fromJson(json, ArrayList.class); LinkedTreeMap map = (LinkedTreeMap) array.get(0); if ("exit".equals(content.substring(0, 4))) { Point exitPoint = new Point(); exitPoint.setLocation((double) map.get("exitX"), (double) map.get("exitY")); prisoner.exitLocation = exitPoint; Point exitRoomPoint = new Point(); exitRoomPoint.setLocation((double) map.get("passageX"), (double) map.get("passageY")); prisoner.exitRoomLocation = exitRoomPoint; int state = 1; if ("RoomMain".equals(prisoner.room.name)) { state = 2; } else if (map.get("room").equals(prisoner.room.name)) { state = 3; } prisoner.addBehaviour(new GotoExitBehaviour(prisoner, 10, state)); } else if ("room".equals(content.substring(0, 4))) { prisoner.roomsFound.put((String) map.get("room"), prisoner.roomsFound.get(map.get("room")) + 1); } } else { block(); } } }