EnguerrandP
Messages postés295Date d'inscriptionvendredi 26 avril 2013StatutMembreDernière intervention15 juillet 2015
-
9 juil. 2015 à 15:18
EnguerrandP
Messages postés295Date d'inscriptionvendredi 26 avril 2013StatutMembreDernière intervention15 juillet 2015
-
15 juil. 2015 à 10:01
Bonjour, j'ai un probleme avec mon appli.
Je souhaite faire communicer un web service avec un agent jade.
voici le code que j'ai pour l'instant :
package lu.list.webservice;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.Result;
import jade.core.AID;
import jade.core.AgentContainer;
import jade.core.ContainerID;
import jade.core.Profile;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.domain.JADEAgentManagement.QueryAgentsOnLocation;
import jade.lang.acl.ACLMessage;
import jade.proto.AchieveREInitiator;
import jade.util.leap.List;
import jade.util.leap.Properties;
import jade.wrapper.gateway.JadeGateway;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
//gateway
Properties pp = new Properties();
pp.setProperty(Profile.MAIN_HOST, "localhost");
pp.setProperty(Profile.MAIN_PORT, "1099");
JadeGateway.init(null, pp);
try {
MainContainerAgentsRetriever retriever = new MainContainerAgentsRetriever();
JadeGateway.execute(retriever);
// At this point the retriever behaviour has been fully executed --> the list of
// agents running in the Main Container is available: get it and print it
List agents = retriever.getAgents();
if (agents != null) {
System.out.println("Agents living in the Main Container: ");
for (int i = 0; i < agents.size(); ++i) {
System.out.println("- "+ ((AID) agents.get(i)).getLocalName());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
/**
* Inner class MainContainerAgentsRetriever.
* This behaviour requests the AMS the list of agents running in the Main Container
*/
private static class MainContainerAgentsRetriever extends AchieveREInitiator {
private static final long serialVersionUID = 1L;
private List agents;
public MainContainerAgentsRetriever() {
super(null, null);
}
public List getAgents() {
return agents;
}
public void onStart() {
super.onStart();
// Be sure the JADEManagementOntology and the Codec for the SL language are
// registered in the Gateway Agent
myAgent.getContentManager().registerLanguage(new SLCodec());
myAgent.getContentManager().registerOntology(JADEManagementOntology.getInstance());
}
@Override
protected Vector<ACLMessage> prepareRequests(ACLMessage initialMsg) {
Vector<ACLMessage> v = null;
// Prepare the request to be sent to the AMS
// ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
// request.addReceiver(myAgent.getAMS());
// request.setOntology(JADEManagementOntology.getInstance().getName());
// request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
System.out.println("myAgent : "+myAgent);
System.out.println("myAgent local name : "+myAgent.getLocalName());
System.out.println("myAgent name : "+myAgent.getName());
System.out.println("myAgent toString : "+myAgent.toString());
System.out.println("myAgent AMS : "+myAgent.getAMS());
ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
// request.addReceiver(new AID("m" , AID.ISLOCALNAME));
request.addReceiver(myAgent.getAMS());
request.setOntology(JADEManagementOntology.getInstance().getName());
request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
request.setContent("You shall not pass");
// QueryAgentsOnLocation qaol = new QueryAgentsOnLocation();
// qaol.setLocation(new ContainerID(AgentContainer.MAIN_CONTAINER_NAME, null));
// Action actExpr = new Action(new AID("m" , AID.ISLOCALNAME), qaol);
try {
// myAgent.getContentManager().fillContent(request, actExpr);
v = new Vector<ACLMessage>(1);
v.add(request);
}
catch (Exception e) {
e.printStackTrace();
}
return v;
}
// @Override
// protected void handleInform(ACLMessage inform) {
// try {
// // Get the result from the AMS, parse it and store the list of agents
// Result result = (Result) myAgent.getContentManager().extractContent(inform);
// agents = (List) result.getValue();
// }
// catch (Exception e) {
// e.printStackTrace();
// }
// }
}
}
si on l'execute comme tel la page du web service va tourner dans le vide indéfiniment, mais le message est bien recu par l'agent.