1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.FSMBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ProvAg extends Agent {
boolean stop = false;
protected void setup(){
System.out.println("Agent "+getLocalName()+" started.");
FSMBehaviour ProvAg_beh= new FSMBehaviour();
ProvAg_beh.registerFirstState(new attendreService(), "attendreService");
ProvAg_beh.registerState(new afficher(), "afficher");
ProvAg_beh.registerState(new fin(), "fin");
ProvAg_beh.registerTransition("attendreService", "afficher",0);
ProvAg_beh.registerTransition("afficher", "fin",1);
ProvAg_beh.registerDefaultTransition("afficher","attendreService");
addBehaviour(ProvAg_beh);
}
/****************************************************/
private class attendreService extends OneShotBehaviour{
public void action() {
System.out.println(" La methode attendreService avait ete appele");
ACLMessage message = new ACLMessage(ACLMessage.INFORM);
message.addReceiver(new AID("InvAg", AID.ISLOCALNAME));
if(!stop){
message.setContent("pret");
send(message);
block();
}else{
message.setContent("arret");
send(message);
}
}
}
/***************************************************/
private class afficher extends OneShotBehaviour{
String service;
public void action() {
System.out.println(" La methode afficher avait ete commence ");
ACLMessage messageRecu = receive();
if (messageRecu != null) {
System.out.println("InvAg j'ai recu le service:"+messageRecu.getContent());
service=(messageRecu.getContent());
}
try {
FileInputStream file = new FileInputStream(new File("c:\\Code\\clouds.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("La liste des clouds offrant le service demande:");
String expression = "/Clouds/cloud[machine/Service/@id=" + service + "]/@idCloud";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
String L=nodeList.item(i).getFirstChild().getNodeValue();
ACLMessage message = new ACLMessage(ACLMessage.INFORM);
message.addReceiver(new AID("InvAg", AID.ISLOCALNAME));
message.setContent(L+"");
send(message);
System.out.println("InvAg je vous ai envoyé la liste");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
block();
}
}
/****************************************/
private class fin extends OneShotBehaviour{
public void action() {
System.out.println("fin de l'agent");
myAgent.doDelete();
}
}
} |
Partager