public void action() { // Receives information about people joining and leaving // the chat from the ChatManager agent ACLMessage msg = myAgent.receive(template); if (msg != null) { if (msg.getPerformative() == ACLMessage.INFORM) { try { AbsPredicate p = (AbsPredicate) myAgent.getContentManager().extractAbsContent(msg); if (p.getTypeName().equals(ChatOntology.JOINED)) { // Get new participants, add them to the list of participants // and notify the gui AbsAggregate agg = (AbsAggregate) p.getAbsTerm(ChatOntology.JOINED_WHO); if (agg != null) { Iterator it = agg.iterator(); while (it.hasNext()) { AbsConcept c = (AbsConcept) it.next(); participants.add(BasicOntology.getInstance().toObject(c)); } } myGui.notifyParticipantsChanged(getParticipantNames()); } if (p.getTypeName().equals(ChatOntology.LEFT)) { // Get old participants, remove them from the list of participants // and notify the gui AbsAggregate agg = (AbsAggregate) p.getAbsTerm(ChatOntology.JOINED_WHO); if (agg != null) { Iterator it = agg.iterator(); while (it.hasNext()) { AbsConcept c = (AbsConcept) it.next(); participants.remove(BasicOntology.getInstance().toObject(c)); } } myGui.notifyParticipantsChanged(getParticipantNames()); } } catch (Exception e) { Logger.println(e.toString()); e.printStackTrace(); } } else { handleUnexpected(msg); } } else { block(); } }
////////////////////////// // Private utility methods ////////////////////////// private String[] getParticipantNames() { String[] pp = new String[participants.size()]; Iterator it = participants.iterator(); int i = 0; while (it.hasNext()) { AID id = (AID) it.next(); pp[i++] = id.getLocalName(); } return pp; }
public void action() { spokenMsg.clearAllReceiver(); Iterator it = participants.iterator(); while (it.hasNext()) { spokenMsg.addReceiver((AID) it.next()); } spokenMsg.setContent(sentence); myGui.notifySpoken(myAgent.getLocalName(), sentence); send(spokenMsg); }