PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sharking/src/examples/ontology/RequesterAgent.java

http://unlpbot.googlecode.com/
Java | 366 lines | 212 code | 43 blank | 111 comment | 8 complexity | 93c793373513385447ac64e6c1e881d8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*****************************************************************
  2. JADE - Java Agent DEvelopment Framework is a framework to develop
  3. multi-agent systems in compliance with the FIPA specifications.
  4. Copyright (C) 2000 CSELT S.p.A.
  5. GNU Lesser General Public License
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation,
  9. version 2.1 of the License.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.
  18. *****************************************************************/
  19. package examples.ontology;
  20. import jade.lang.acl.ACLMessage;
  21. import jade.core.*;
  22. import jade.core.behaviours.*;
  23. import jade.domain.FIPANames;
  24. import jade.proto.SimpleAchieveREInitiator;
  25. import jade.content.lang.Codec;
  26. import jade.content.lang.sl.*;
  27. import jade.content.abs.*;
  28. import jade.content.onto.*;
  29. import jade.content.onto.basic.*;
  30. import examples.ontology.employment.*;
  31. import java.io.*;
  32. /**
  33. This agent is able to handle the engagement of people by requesting
  34. an engager agent to do that.
  35. It first gets from the user
  36. <ul>
  37. <li>The name of the engager agent to send engagement requests to.</li>
  38. <li>The details of the company where to engage people</li>
  39. </ul>
  40. Then it cyclically gets from the user the details of a person
  41. to engage and handles the engagement of that person in collaboration
  42. with the initially indicated engager agent.
  43. <b>Note:</b>
  44. While entering input data, all fields composed of more than
  45. one word must be enclosed in "".
  46. E.g. the name Giovanni Caire must be entered as "Giovanni Caire".
  47. @author Giovanni Caire - CSELT S.p.A
  48. @version $Date: 2005-03-07 15:38:22 $ $Revision: 1.1 $
  49. @see examples.ontology.EngagerAgent
  50. */
  51. public class RequesterAgent extends Agent {
  52. /**
  53. * Comment for <code>serialVersionUID</code>
  54. */
  55. private static final long serialVersionUID = 3905244528565761592L;
  56. // AGENT BEHAVIOURS
  57. /**
  58. Main behaviour for the Requester Agent.
  59. First the details of a person to engage are requested
  60. to the user.
  61. Then a check is performed to verify that the indicated person is not
  62. already working for the indicated company
  63. Finally, according to the above check, the engagement is requested.
  64. This behaviour is executed cyclically.
  65. */
  66. class HandleEngagementBehaviour extends SequentialBehaviour {
  67. /**
  68. * Comment for <code>serialVersionUID</code>
  69. */
  70. private static final long serialVersionUID = 3257846575983177783L;
  71. // Local variables
  72. Behaviour queryBehaviour = null;
  73. Behaviour requestBehaviour = null;
  74. // Constructor
  75. public HandleEngagementBehaviour(Agent myAgent){
  76. super(myAgent);
  77. }
  78. // This is executed at the beginning of the behaviour
  79. public void onStart(){
  80. // Get detail of person to be engaged
  81. try{
  82. BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  83. Person p = new Person();
  84. Address a = new Address();
  85. System.out.println("ENTER details of person to engage");
  86. System.out.print(" Person name --> ");
  87. p.setName(buff.readLine());
  88. System.out.print(" Person age ---> ");
  89. p.setAge(new Long(buff.readLine()));
  90. System.out.println(" Person address");
  91. System.out.print(" Street -----> ");
  92. a.setStreet(buff.readLine());
  93. System.out.print(" Number -----> ");
  94. a.setNumber(new Long(buff.readLine()));
  95. System.out.print(" City -----> ");
  96. a.setCity(buff.readLine());
  97. p.setAddress(a);
  98. // Create an object representing the fact that person p works for company c
  99. WorksFor wf = new WorksFor();
  100. wf.setPerson(p);
  101. wf.setCompany(((RequesterAgent) myAgent).c);
  102. Ontology o = myAgent.getContentManager().lookupOntology(EmploymentOntology.NAME);
  103. // Create an ACL message to query the engager agent if the above fact is true or false
  104. ACLMessage queryMsg = new ACLMessage(ACLMessage.QUERY_IF);
  105. queryMsg.addReceiver(((RequesterAgent) myAgent).engager);
  106. queryMsg.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
  107. queryMsg.setOntology(EmploymentOntology.NAME);
  108. // Write the works for predicate in the :content slot of the message
  109. try {
  110. myAgent.getContentManager().fillContent(queryMsg, wf);
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. // Create and add a behaviour to query the engager agent whether
  115. // person p already works for company c following a FIPAQeury protocol
  116. queryBehaviour = new CheckAlreadyWorkingBehaviour(myAgent, queryMsg);
  117. addSubBehaviour(queryBehaviour);
  118. }
  119. catch (IOException ioe) {
  120. System.err.println("I/O error: " + ioe.getMessage());
  121. }
  122. }
  123. // This is executed at the end of the behaviour
  124. public int onEnd(){
  125. // Check whether the user wants to continue
  126. try{
  127. BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  128. System.out.println("Would you like to continue?[y/n] ");
  129. String stop = buff.readLine();
  130. if (stop.equalsIgnoreCase("y"))
  131. {
  132. reset(); // This makes this behaviour be cyclically executed
  133. myAgent.addBehaviour(this);
  134. }
  135. else
  136. myAgent.doDelete(); // Exit
  137. }
  138. catch (IOException ioe) {
  139. System.err.println("I/O error: " + ioe.getMessage());
  140. }
  141. return 0;
  142. }
  143. // Extends the reset method in order to remove the sub-behaviours that
  144. // are dynamically added
  145. public void reset(){
  146. if (queryBehaviour != null){
  147. removeSubBehaviour(queryBehaviour);
  148. queryBehaviour = null;
  149. }
  150. if (requestBehaviour != null){
  151. removeSubBehaviour(requestBehaviour);
  152. requestBehaviour = null;
  153. }
  154. super.reset();
  155. }
  156. }
  157. /**
  158. This behaviour embeds the check that the indicated person is not
  159. already working for the indicated company.
  160. This is done following a FIPA-Query interaction protocol
  161. */
  162. class CheckAlreadyWorkingBehaviour extends SimpleAchieveREInitiator {
  163. /**
  164. * Comment for <code>serialVersionUID</code>
  165. */
  166. private static final long serialVersionUID = 3977016245235103536L;
  167. // Constructor
  168. public CheckAlreadyWorkingBehaviour(Agent myAgent, ACLMessage queryMsg){
  169. super(myAgent, queryMsg);
  170. queryMsg.setProtocol(FIPANames.InteractionProtocol.FIPA_QUERY);
  171. }
  172. protected void handleInform(ACLMessage msg) {
  173. try{
  174. AbsPredicate cs = (AbsPredicate)myAgent.getContentManager().extractAbsContent(msg);
  175. Ontology o = myAgent.getContentManager().lookupOntology(EmploymentOntology.NAME);
  176. if (cs.getTypeName().equals(EmploymentOntology.WORKS_FOR)) {
  177. // The indicated person is already working for company c.
  178. // Inform the user
  179. WorksFor wf = (WorksFor)o.toObject((AbsObject)cs);
  180. Person p = (Person) wf.getPerson();
  181. Company c = (Company) wf.getCompany();
  182. System.out.println("Person " + p.getName() + " is already working for " + c.getName());
  183. }
  184. else if (cs.getTypeName().equals(SLVocabulary.NOT)){
  185. // The indicated person is NOT already working for company c.
  186. // Get person and company details and create an object representing the engagement action
  187. WorksFor wf = (WorksFor)o.toObject(cs.getAbsObject(SLVocabulary.NOT_WHAT));
  188. Person p = (Person) wf.getPerson();
  189. Company c = (Company) wf.getCompany();
  190. Engage e = new Engage();
  191. e.setPerson(p);
  192. e.setCompany(c);
  193. Action a = new Action();
  194. a.setActor(((RequesterAgent) myAgent).engager);
  195. a.setAction(e);
  196. // Create an ACL message to request the above action
  197. ACLMessage requestMsg = new ACLMessage(ACLMessage.REQUEST);
  198. requestMsg.addReceiver(((RequesterAgent) myAgent).engager);
  199. requestMsg.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
  200. requestMsg.setOntology(EmploymentOntology.NAME);
  201. // Write the action in the :content slot of the message
  202. try {
  203. myAgent.getContentManager().fillContent(requestMsg, a);
  204. } catch (Exception pe) {
  205. }
  206. // Create and add a behaviour to request the engager agent to engage
  207. // person p in company c following a FIPARequest protocol
  208. ((HandleEngagementBehaviour) parent).requestBehaviour = new RequestEngagementBehaviour(myAgent, requestMsg);
  209. ((SequentialBehaviour) parent).addSubBehaviour(((HandleEngagementBehaviour) parent).requestBehaviour);
  210. }
  211. else{
  212. // Unexpected response received from the engager agent.
  213. // Inform the user
  214. System.out.println("Unexpected response from engager agent");
  215. }
  216. } // End of try
  217. catch (Codec.CodecException fe) {
  218. System.err.println("FIPAException in fill/extract Msgcontent:" + fe.getMessage());
  219. }
  220. catch (OntologyException fe) {
  221. System.err.println("OntologyException in getRoleName:" + fe.getMessage());
  222. }
  223. }
  224. }
  225. /**
  226. This behaviour embeds the request to engage the indicated person
  227. in the indicated company.
  228. This is done following a FIPA-Request interaction protocol
  229. */
  230. class RequestEngagementBehaviour extends SimpleAchieveREInitiator {
  231. /**
  232. * Comment for <code>serialVersionUID</code>
  233. */
  234. private static final long serialVersionUID = 3689351013798918451L;
  235. // Constructor
  236. public RequestEngagementBehaviour(Agent myAgent, ACLMessage requestMsg){
  237. super(myAgent, requestMsg);
  238. requestMsg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
  239. }
  240. protected void handleAgree(ACLMessage msg) {
  241. System.out.println("Engagement agreed. Waiting for completion notification...");
  242. }
  243. protected void handleInform(ACLMessage msg) {
  244. System.out.println("Engagement successfully completed");
  245. }
  246. protected void handleNotUnderstood(ACLMessage msg) {
  247. System.out.println("Engagement request not understood by engager agent");
  248. }
  249. protected void handleFailure(ACLMessage msg) {
  250. System.out.println("Engagement failed");
  251. // Get the failure reason and communicate it to the user
  252. try{
  253. AbsPredicate absPred =(AbsPredicate)myAgent.getContentManager().extractContent(msg);
  254. System.out.println("The reason is: " + absPred.getTypeName());
  255. }
  256. catch (Codec.CodecException fe){
  257. System.err.println("FIPAException reading failure reason: " + fe.getMessage());
  258. }
  259. catch (OntologyException oe){
  260. System.err.println("OntologyException reading failure reason: " + oe.getMessage());
  261. }
  262. }
  263. protected void handleRefuse(ACLMessage msg) {
  264. System.out.println("Engagement refused");
  265. // Get the refusal reason and communicate it to the user
  266. try{
  267. AbsContentElementList list =(AbsContentElementList)myAgent.getContentManager().extractAbsContent(msg);
  268. AbsPredicate absPred = (AbsPredicate)list.get(1);
  269. System.out.println("The reason is: " + absPred.getTypeName());
  270. }
  271. catch (Codec.CodecException fe){
  272. System.err.println("FIPAException reading refusal reason: " + fe.getMessage());
  273. }
  274. catch (OntologyException oe){
  275. System.err.println("OntologyException reading refusal reason: " + oe.getMessage());
  276. }
  277. }
  278. }
  279. // AGENT LOCAL VARIABLES
  280. AID engager; // AID of the agent the engagement requests will have to be sent to
  281. Company c; // The company where people will be engaged
  282. // AGENT SETUP
  283. protected void setup() {
  284. // Register the codec for the SL0 language
  285. getContentManager().registerLanguage(new SLCodec(), FIPANames.ContentLanguage.FIPA_SL0);
  286. // Register the ontology used by this application
  287. getContentManager().registerOntology(EmploymentOntology.getInstance());
  288. // Get from the user the name of the agent the engagement requests
  289. // will have to be sent to
  290. try {
  291. BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  292. System.out.print("ENTER the local name of the Engager agent --> ");
  293. String name = buff.readLine();
  294. engager = new AID(name, AID.ISLOCALNAME);
  295. // Get from the user the details of the company where people will
  296. // be engaged
  297. c = new Company();
  298. Address a = new Address();
  299. System.out.println("ENTER details of the company where people will be engaged");
  300. System.out.print(" Company name --> ");
  301. c.setName(buff.readLine());
  302. System.out.println(" Company address");
  303. System.out.print(" Street ------> ");
  304. a.setStreet(buff.readLine());
  305. System.out.print(" Number ------> ");
  306. a.setNumber(new Long(buff.readLine()));
  307. System.out.print(" City ------> ");
  308. a.setCity(buff.readLine());
  309. c.setAddress(a);
  310. }
  311. catch (IOException ioe) {
  312. System.err.println("I/O error: " + ioe.getMessage());
  313. }
  314. // Create and add the main behaviour of this agent
  315. addBehaviour(new HandleEngagementBehaviour(this));
  316. }
  317. }