import jade.core.Agent; import jade.core.behaviours.Behaviour; import jade.core.behaviours.CyclicBehaviour; import jade.core.behaviours.OneShotBehaviour; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.ThreadContext; import PhdOnto2.PhdOnto2Ontology; import PhdOnto2.Application; import PhdOnto2.GetApplicationLIst; import PhdOnto2.impl.DefaultApplication; import PhdOnto2.impl.DefaultGetApplicationLIst; import jade.content.Concept; import jade.content.ContentElement; import jade.content.lang.Codec; import jade.content.lang.Codec.CodecException; import jade.content.lang.sl.SLCodec; import jade.content.onto.Ontology; import jade.content.onto.OntologyException; import jade.content.onto.basic.Action; import jade.content.onto.basic.Result; import jade.core.AID; import jade.domain.DFService; import jade.domain.FIPAException; import jade.domain.FIPANames; import jade.domain.FIPAAgentManagement.DFAgentDescription; import jade.domain.FIPAAgentManagement.ServiceDescription; import jade.lang.acl.ACLMessage; import jade.lang.acl.MessageTemplate; import jade.proto.SubscriptionInitiator; import jade.util.leap.ArrayList; import jade.util.leap.HashSet; import jade.util.leap.Iterator; import jade.util.leap.List; import jade.util.leap.Set; import jade.domain.FIPAAgentManagement.Property; import jade.domain.FIPAAgentManagement.SearchConstraints; public class AgentRequester extends Agent { private Codec codec = new SLCodec(); //FIPANames.ContentLanguage.FIPA_SL0; private Ontology ontology = PhdOnto2Ontology.getInstance(); public static String newline; private Logger logger;// = LogManager.getLogger(LEARNAMS.class); protected void setup() { try{ newline = System.getProperty("line.separator"); ThreadContext.put("ROUTINGKEY", this.getName()); logger = LogManager.getLogger(this.getName()); } catch (Exception e){//Catch exception if any System.err.println("ErrorE FILE: " + e.getMessage()); } getContentManager().registerLanguage(codec); // Register the ontology used by this application getContentManager().registerOntology(ontology); String serviceName = "GetApplicationLIst"; // Build the description used as template for the search DFAgentDescription template = new DFAgentDescription(); ServiceDescription templateSd = new ServiceDescription(); templateSd.setType("phd"); templateSd.setName(serviceName); template.addServices(templateSd); SearchConstraints sc = new SearchConstraints(); // We want to receive 10 results at most sc.setMaxResults(new Long(10)); // be careful, since this is a protocol... it simply somethings, but make complex other things... // in this case, you have to be sure that the inform from the DF is not catch by the other cyclic behaviour!! // NB: since this subscription returns the already existing registerd services, I removed the previous oneshot (comment later), and add this one addBehaviour(new SubscriptionInitiator(this, DFService.createSubscriptionMessage(this, getDefaultDF(), template, sc)) { protected void handleInform(ACLMessage inform) { System.out.println("Agent "+getLocalName()+": Notification received from DF"); try { DFAgentDescription[] results = DFService.decodeNotification(inform.getContent()); if (results.length > 0) { for (int i = 0; i < results.length; ++i) { DFAgentDescription dfd = results[i]; AID provider = dfd.getName(); Iterator it = dfd.getAllServices(); while (it.hasNext()) { Action a = new Action(); a.setActor(new AID(myAgent.getLocalName(), AID.ISLOCALNAME)); a.setAction(new DefaultGetApplicationLIst()); ServiceDescription sd = (ServiceDescription) it.next(); if (sd.getName().equals(serviceName)) { System.out.println("new service found, provided by "+provider.getName()); // previous code, I invoke the service if(sd.getName().equals(serviceName)){ ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); //AID r=new AID("r@Platform2",true);//AID.ISGUID); //r.addAddresses(p); msg.addReceiver(provider); msg.setLanguage(codec.getName()); msg.setOntology(ontology.getName()); try { getContentManager().fillContent(msg,a); send(msg); System.out.println( "send request for GetApplicationLIst to: " + provider.getName()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } } System.out.println(); } catch (FIPAException fe) { fe.printStackTrace(); } } } ); // old one /* addBehaviour(new OneShotBehaviour(this) { @Override public void action() { Action a = new Action(); a.setActor(new AID(myAgent.getLocalName(), AID.ISLOCALNAME)); a.setAction(new DefaultGetApplicationLIst()); //////////////// try { DFAgentDescription[] results = DFService.search(this.getAgent(), template, sc); if (results.length > 0) { //System.out.println("Agent "+getLocalName()+" found the following weather-forecast services:"); for (int i = 0; i < results.length; ++i) { DFAgentDescription dfd = results[i]; AID provider = dfd.getName(); Iterator it = dfd.getAllServices(); while (it.hasNext() ) { ServiceDescription sd = (ServiceDescription) it.next(); System.out.println("New Service (by local DF) \""+sd.getName()+"\" provided by agent "+provider.getName()); if(sd.getName().equals(serviceName)){ ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); //AID r=new AID("r@Platform2",true);//AID.ISGUID); //r.addAddresses(p); msg.addReceiver(provider); msg.setLanguage(codec.getName()); msg.setOntology(ontology.getName()); try { getContentManager().fillContent(msg,a); send(msg); System.out.println( "send request for GetApplicationLIst to: " + provider.getName()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } System.out.println("************"); } else { //System.out.println("Agent "+getLocalName()+" did not find any service"); } } catch (FIPAException fe) { //fe.printStackTrace(); System.out.println("errore (timeout"); } //////////////// } }); */ addBehaviour(new CyclicBehaviour(this) { public void action() { try{ // do not want to get here the messages from the DF!! MessageTemplate mt= MessageTemplate.not(MessageTemplate.MatchSender(getDefaultDF())); ACLMessage msg= receive(mt); if (msg!=null){ System.out.println( "I'm " + myAgent.getName() + ", received message: " + msg.getContent() + " from " + msg.getSender()); if(msg.getContent()!= null && msg.getPerformative()== ACLMessage.INFORM && !msg.getSender().getName().contains("df@")){ ContentElement ce = null; try { ce=myAgent.getContentManager().extractContent(msg); if (ce instanceof Result) { Result result = (Result) ce; // I should receive a Set of PlatformDescription (ontology) if (result.getValue() instanceof Set && !((HashSet)result.getValue()).isEmpty() && ((HashSet)result.getValue()).toArray()[0] instanceof DefaultApplication) { // it does not work properly since we did not implement the toString method in the OWL->Java classes System.out.println(((HashSet)result.getValue()).toString()); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else block(); } catch(Exception e){ logger.trace("would dead because " + e.getMessage()); } } }); } }