|JADE+WebService| Probleme de dialogue [Résolu]

Messages postés
295
Date d'inscription
vendredi 26 avril 2013
Statut
Membre
Dernière intervention
15 juillet 2015
- - Dernière réponse : EnguerrandP
Messages postés
295
Date d'inscription
vendredi 26 avril 2013
Statut
Membre
Dernière intervention
15 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.

Afficher la suite 

Votre réponse

1 réponse

Messages postés
295
Date d'inscription
vendredi 26 avril 2013
Statut
Membre
Dernière intervention
15 juillet 2015
0
Merci
Bonjour,
J'ai résolu mon problème voici la solution que j'ai trouver:
package lu.list.webservice;

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.core.AID;
import jade.core.Profile;
import jade.core.behaviours.SimpleBehaviour;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.lang.acl.ACLMessage;
import jade.util.leap.Properties;
import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;
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) {    	
    	String content = "";
    
		Properties pp = new Properties();
		pp.setProperty(Profile.MAIN_HOST, "localhost");
		pp.setProperty(Profile.MAIN_PORT, "1099");
		JadeGateway.init(null, pp);
			
		try {
			MyBehaviour beha = new MyBehaviour(name);
			JadeGateway.execute(beha);
			content = beha.getContent();
			
		} catch (StaleProxyException e) {e.printStackTrace();
		} catch (ControllerException e) {e.printStackTrace();
		} catch (InterruptedException e) {e.printStackTrace();}
		
        return new Greeting(counter.incrementAndGet(), String.format(template, content));
    }
    
    class MyBehaviour extends SimpleBehaviour{   
    	private static final long serialVersionUID = 1L;
		private boolean finished = false;
		private String content="";
		
		public MyBehaviour(String name) {
			this.content=name;
		}

		public String getContent() { return content; }

		public void setContent(String content) { this.content = content; }

		public void onStart() {
				ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
				request.addReceiver(new AID("m" , AID.ISLOCALNAME));
				request.setOntology(JADEManagementOntology.getInstance().getName());
				request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
				request.setContent(content); 
				myAgent.send(request);
		  }

		  public void action() {
		    ACLMessage response = myAgent.receive();
		    if (response != null) {
		      setContent(response.getContent());
		      finished= true;
		    }
		    else { block(); }
		  }

		  public boolean done() { return finished; }
    }
}

Commenter la réponse de EnguerrandP