1
2
3
4 from Agent import PlatformAgent
5 import AID
6 import Behaviour
7 import BasicFipaDateTime
8 from SL0Parser import *
9 from content import ContentObject
10 import xmpp
11 import copy
12 import thread
13
14 -class DF(PlatformAgent):
15 """
16 Directory Facilitator Agent
17 """
18
20
24
26 error = False
27 msg = self._receive(True)
28 if msg != None:
29 if msg.getPerformative().lower() == 'request':
30 if msg.getOntology().lower() == "fipa-agent-management":
31 if msg.getLanguage().lower() == "fipa-sl0":
32 content = self.sl0parser.parse(msg.getContent())
33 ACLtemplate = Behaviour.ACLTemplate()
34 ACLtemplate.setConversationId(msg.getConversationId())
35
36 template = (Behaviour.MessageTemplate(ACLtemplate))
37
38 if "action" in content:
39 if "register" in content.action or "deregister" in content.action:
40 self.myAgent.addBehaviour(DF.RegisterBehaviour(msg,content), template)
41 self.myAgent.DEBUG("Received REGISTER action: "+str(content))
42 elif "search" in content.action:
43 self.myAgent.addBehaviour(DF.SearchBehaviour(msg,content), template)
44 self.myAgent.DEBUG("Received SEARCH action: "+str(content))
45 elif "modify" in content.action:
46 self.myAgent.addBehaviour(DF.ModifyBehaviour(msg,content), template)
47 self.myAgent.DEBUG("Received MODIFY action: "+str(content))
48 else:
49 reply = msg.createReply()
50 reply.setSender(self.myAgent.getAID())
51 reply.setPerformative("refuse")
52 reply.setContent("( "+msg.getContent() +"(unsuported-function "+ content.keys()[0] +"))")
53 self.myAgent.send(reply)
54 self.myAgent.DEBUG("Received message with no action. Refusing: "+str(content),'warn')
55
56 return -1
57
58
59 elif "rdf" in msg.getLanguage().lower():
60 co = msg.getContentObject()
61 ACLtemplate = Behaviour.ACLTemplate()
62 ACLtemplate.setConversationId(msg.getConversationId())
63
64 template = (Behaviour.MessageTemplate(ACLtemplate))
65
66 if co and co.has_key("fipa:action") and co["fipa:action"].has_key("fipa:act"):
67 if co["fipa:action"]["fipa:act"] in ["register","deregister"]:
68 self.myAgent.addBehaviour(DF.RegisterBehaviour(msg,co), template)
69 self.myAgent.DEBUG("Received REGISTER action: "+str(co))
70 elif co["fipa:action"]["fipa:act"] == "search":
71 self.myAgent.addBehaviour(DF.SearchBehaviour(msg,co), template)
72 self.myAgent.DEBUG("Received SEARCH action: "+str(co))
73 elif co["fipa:action"]["fipa:act"] == "modify":
74 self.myAgent.addBehaviour(DF.ModifyBehaviour(msg,co), template)
75 self.myAgent.DEBUG("Received MODIFY action: "+str(co))
76 else:
77 reply = msg.createReply()
78 reply.setSender(self.myAgent.getAID())
79 reply.setPerformative("refuse")
80 co2 = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
81 co2["unsuported-function"] = co["fipa:action"]["fipa:act"]
82 reply.setContentObject(co2)
83 self.myAgent.send(reply)
84 self.myAgent.DEBUG("Received message with no action. Refusing: "+str(co),'warn')
85 return -1
86
87
88 else: error = "(unsupported-language "+msg.getLanguage()+")"
89 else: error = "(unsupported-ontology "+msg.getOntology()+")"
90
91
92 elif msg.getPerformative().lower() not in ['failure','refuse']:
93 error = "(unsupported-act " + msg.getPerformative() + ")"
94 if error:
95 reply = msg.createReply()
96 reply.setSender(self.myAgent.getAID())
97 reply.setPerformative("not-understood")
98 reply.setContent("( "+msg.getContent() + error+")")
99 self.myAgent.send(reply)
100 self.myAgent.DEBUG("NOT-UNDERSTOOD: Could not process message. Error is:"+str(error),'error')
101 return -1
102
103
104
105
107
112
114
115 error = False
116
117
118 if "rdf" not in self.msg.getLanguage():
119 rdf = False
120 try:
121 if self.content.action == "register":
122 dad = DfAgentDescription(self.content.action.register['df-agent-description'])
123 else:
124 dad = DfAgentDescription(self.content.action.deregister['df-agent-description'])
125 except KeyError:
126 error = "(missing-argument df-agent-description)"
127 self.myAgent.DEBUG("REFUSE: Register Behaviour could not extract DfAgentDescription "+error,'error')
128
129
130 if error:
131 reply = self.msg.createReply()
132 reply.setSender(self.myAgent.getAID())
133 reply.setPerformative("refuse")
134 reply.setContent("( "+self.msg.getContent() + error + ")")
135 self.myAgent.send(reply)
136
137 return -1
138
139 else:
140 reply = self.msg.createReply()
141 reply.setSender(self.myAgent.getAID())
142 reply.setPerformative("agree")
143 reply.setContent("(" + str(self.msg.getContent()) + " true)")
144 self.myAgent.send(reply)
145
146
147 if self.content.action == "register":
148
149 if not self.myAgent.servicedb.has_key(dad.getAID().getName()):
150 self.myAgent.db_mutex.acquire()
151 self.myAgent.servicedb[dad.getAID().getName()] = dad
152 self.myAgent.db_mutex.release()
153 else:
154
155 for ss in dad.getServices():
156 found = False
157 for s in self.myAgent.servicedb[dad.getAID().getName()].getServices():
158 if s.match(ss):
159 found=True
160 if found:
161 reply.setPerformative("failure")
162 reply.setContent("("+self.msg.getContent() + "(already-registered))")
163 self.myAgent.send(reply)
164 self.myAgent.DEBUG("FAILURE: Service was already registered! Could not register "+str(dad),'warn')
165 return -1
166
167
168 try:
169 for s in dad.getServices():
170 self.myAgent.db_mutex.acquire()
171 self.myAgent.servicedb[dad.getAID().getName()].addService(s)
172 self.myAgent.db_mutex.release()
173 self.myAgent.DEBUG("Service successfully registered: "+str(s),'ok')
174 except Exception, err:
175 reply.setPerformative("failure")
176 reply.setContent("("+self.msg.getContent() + "(internal-error))")
177 self.myAgent.send(reply)
178 self.myAgent.DEBUG("FAILURE: Service could not be registered: "+str(err),'error')
179 return -1
180
181
182 self.DEBUG("Service succesfully deregistered: "+ str(dad),'ok')
183 reply.setPerformative("inform")
184 reply.setContent("(done "+self.msg.getContent() + ")")
185 self.myAgent.send(reply)
186
187
188 s = Service(dad=dad).asContentObject()
189 node = xmpp.Node(node=str(s))
190 self.myAgent.publishEvent("DF:Service:Register",node)
191
192 return 1
193
194
195 elif self.content.action == "deregister":
196
197 if not self.myAgent.servicedb.has_key(dad.getAID().getName()):
198 reply.setPerformative("failure")
199 reply.setContent("("+self.msg.getContent() + "(not-registered))")
200 self.myAgent.send(reply)
201 self.myAgent.DEBUG("FAILURE: Agent has no registered services! Could not deregister "+str(dad),'warn')
202 return -1
203
204
205 for ss in dad.getServices():
206 found=False
207 for s in self.myAgent.servicedb[dad.getAID().getName()].getServices():
208 if s.match(ss):
209 found = True
210 if not found:
211 reply.setPerformative("failure")
212 reply.setContent("("+self.msg.getContent() + "(not-registered))")
213 self.myAgent.send(reply)
214 self.myAgent.DEBUG("FAILURE: Service is not registered! Could not deregister "+str(dad),'warn')
215 return -1
216
217 try:
218 services = copy.copy(self.myAgent.servicedb[dad.getAID().getName()])
219 if dad.getServices() == []:
220 self.myAgent.db_mutex.acquire()
221 del self.myAgent.servicedb[dad.getAID().getName()]
222 self.myAgent.db_mutex.release()
223 else:
224 for ss in dad.getServices():
225 for s in services.getServices():
226 if ss.match(s):
227 self.myAgent.db_mutex.acquire()
228 self.myAgent.servicedb[dad.getAID().getName()].delService(s)
229 self.myAgent.db_mutex.release()
230 except Exception, err:
231 reply.setPerformative("failure")
232 reply.setContent("("+self.msg.getContent() + '(internal-error "could not deregister service"))')
233 self.myAgent.send(reply)
234 self.myAgent.DEBUG("FAILURE: Service could not be deregistered: "+str(err),'error')
235 return -1
236
237 self.DEBUG("Service succesfully deregistered: "+ str(dad),'ok')
238 reply.setPerformative("inform")
239 reply.setContent("(done "+self.msg.getContent() + ")")
240 self.myAgent.send(reply)
241
242
243 s = Service(dad=dad).asContentObject()
244 node = xmpp.Node(node=str(s))
245 self.myAgent.publishEvent("DF:Service:UnRegister",node)
246
247 return 1
248
249
250 elif "rdf" in self.msg.getLanguage():
251
252 rdf = True
253 co_error = None
254 try:
255 co = self.msg.getContentObject()
256 self.myAgent.DEBUG("Content processed "+str(co),'info')
257 dad = DfAgentDescription(co = co.action.argument)
258 self.myAgent.DEBUG("DfAgentDescription extracted "+str(dad.asRDFXML()),'info')
259 except KeyboardInterrupt,err:
260 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
261 co_error["fipa:error"] = "missing-argument df-agent-description"
262 self.myAgent.DEBUG("REFUSE: "+str(co_error) + ": "+str(err),'error')
263
264 if co_error:
265 reply = self.msg.createReply()
266 reply.setSender(self.myAgent.getAID())
267 reply.setPerformative("refuse")
268 reply.setContentObject(co_error)
269 self.myAgent.send(reply)
270 return -1
271
272 else:
273 reply = self.msg.createReply()
274 reply.setSender(self.myAgent.getAID())
275 reply.setPerformative("agree")
276 co["fipa:done"] = "true"
277 reply.setContentObject(co)
278 self.myAgent.send(reply)
279
280 if co["fipa:action"]["fipa:act"] == "register":
281 if not self.myAgent.servicedb.has_key(dad.getAID().getName()):
282 self.myAgent.db_mutex.acquire()
283 self.myAgent.servicedb[dad.getAID().getName()] = dad
284 self.myAgent.db_mutex.release()
285 else:
286
287 for ss in dad.getServices():
288 found = False
289 for s in self.myAgent.servicedb[dad.getAID().getName()].getServices():
290 if s.match(ss):
291 found=True
292 break
293 if found:
294 reply.setPerformative("failure")
295 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
296 co_error["fipa:error"] = "already-registered"
297 reply.setContentObject(co_error)
298 self.myAgent.send(reply)
299 self.myAgent.DEBUG("FAILURE: Service already registered! ",'warn')
300 return -1
301
302
303 try:
304 for s in dad.getServices():
305 self.myAgent.db_mutex.acquire()
306 self.myAgent.servicedb[dad.getAID().getName()].addService(s)
307 self.myAgent.db_mutex.release()
308 self.myAgent.DEBUG("Service successfully registered: "+str(s),'ok')
309 except Exception, err:
310 reply.setPerformative("failure")
311 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
312 co_error["fipa:error"] = "internal-error"
313 reply.setContentObject(co_error)
314 self.myAgent.send(reply)
315 self.myAgent.DEBUG("FAILURE: Service could not be registered: "+str(err),'error')
316 return -1
317
318
319 self.DEBUG("Service succesfully registered: "+ str(dad),'ok')
320 reply.setPerformative("inform")
321 co_rep = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
322 co_rep["fipa:done"] = "true"
323 reply.setContentObject(co_rep)
324 self.myAgent.send(reply)
325
326
327 self.myAgent.DEBUG("Publishing Event: "+str(dad))
328 s = Service(dad=dad).asContentObject()
329 node = xmpp.Node(node=str(s))
330 self.myAgent.publishEvent("DF:Service:Register",node)
331
332 return 1
333
334 elif co["fipa:action"]["fipa:act"] == "deregister":
335 if not self.myAgent.servicedb.has_key(dad.getAID().getName()):
336 reply.setPerformative("failure")
337 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
338 co_error["fipa:error"] = 'not-registered'
339 reply.setContentObject(co_error)
340 self.myAgent.send(reply)
341 self.myAgent.DEBUG("FAILURE: Agent has no registered services",'warn')
342 return -1
343
344
345 for ss in dad.getServices():
346 found=False
347 for s in self.myAgent.servicedb[dad.getAID().getName()].getServices():
348 if s.match(ss):
349 found = True
350 if not found:
351 reply.setPerformative("failure")
352 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
353 co_error["fipa:error"] = 'not-registered'
354 reply.setContentObject(co_error)
355 self.myAgent.send(reply)
356 self.myAgent.DEBUG("FAILURE: Service is not registered! Could not deregister "+str(dad),'warn')
357 return -1
358
359 try:
360 services = copy.copy(self.myAgent.servicedb[dad.getAID().getName()])
361 self.myAgent.DEBUG("Deregistering "+str(services) + " AND " + str(dad.getServices()))
362 if dad.getServices() == []:
363 self.myAgent.db_mutex.acquire()
364 del self.myAgent.servicedb[dad.getAID().getName()]
365 self.DEBUG("Deleting all agent entries: " + str(not self.myAgent.servicedb.has_key(dad.getAID().getName())))
366 self.myAgent.db_mutex.release()
367 else:
368 for ss in dad.getServices():
369 for s in services.getServices():
370 if ss.match(s):
371 self.myAgent.db_mutex.acquire()
372 self.myAgent.servicedb[dad.getAID().getName()].delService(s)
373 self.myAgent.db_mutex.release()
374 except Exception, err:
375 reply.setPerformative("failure")
376 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
377 co_error["fipa:error"] = 'internal-error "could not deregister service"'
378 reply.setContentObject(co_error)
379 self.myAgent.send(reply)
380 self.myAgent.DEBUG("FAILURE: internal-error 'could not deregister service': "+str(err),'error')
381 return -1
382
383 self.DEBUG("Service succesfully deregistered: "+ str(dad),'ok')
384 reply.setPerformative("inform")
385 co_rep = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
386 co_rep["fipa:done"] = "true"
387 reply.setContentObject(co_rep)
388 self.myAgent.send(reply)
389
390
391 s = Service(dad=dad).asContentObject()
392 node = xmpp.Node(node=str(s))
393 self.myAgent.publishEvent("DF:Service:UnRegister",node)
394
395 return 1
396
397
398
400
405
407
408 error = False
409
410 reply = self.msg.createReply()
411 reply.setSender(self.myAgent.getAID())
412 reply.setPerformative("agree")
413 if "rdf" in self.msg.getLanguage():
414 rdf = True
415 reply.setContent("(" + str(self.msg.getContent()) + " true)")
416 else:
417 rdf = False
418 self.myAgent.send(reply)
419
420 if not rdf:
421 max = 50
422 if "search-constraints" in self.content.action.search:
423 if "max-results" in self.content.action.search["search-constraints"]:
424 try:
425 max_str = str(self.content.action.search["search-constraints"]["max-results"]).strip("[']")
426 max = int(max_str)
427 except Exception, err:
428 error = '(internal-error "max-results is not an integer")'
429 self.myAgent.DEBUG("FAILURE: internal-error 'max-results is not an integer' "+str(err),'error')
430 if error:
431 reply = self.msg.createReply()
432 reply.setSender(self.myAgent.getAID())
433 reply.setPerformative("failure")
434 reply.setContent("( "+self.msg.getContent() + error+")")
435 self.myAgent.send(reply)
436 return -1
437
438
439 result = []
440
441 if "df-agent-description" in self.content.action.search:
442 try:
443 dad = DfAgentDescription(self.content.action.search["df-agent-description"])
444 except Exception, err:
445 self.myAgent.DEBUG("FAILURE: Could not extract DfAgentDescription from content: "+str(err),'error')
446
447 self.myAgent.db_mutex.acquire()
448 if max in [-1, 0]:
449
450 for agentid,dads in self.myAgent.servicedb.items():
451 if dads.match(dad):
452 d = copy.copy(dads)
453 if dad.services == []:
454 d.services = dads.getServices()
455 else:
456 d.services=[]
457 for ss in dad.getServices():
458 for s in dads.getServices():
459 if s.match(ss):
460 d.addService(s)
461 result.append(d)
462 else:
463 max = abs(max)
464 for agentid,dads in self.myAgent.servicedb.items():
465 if max >= 0:
466 if dads.match(dad):
467 d = copy.copy(dads)
468 if dad.services == []:
469 d.services = dads.getServices()
470 else:
471 d.services=[]
472 for ss in dad.getServices():
473 for s in dads.getServices():
474 if s.match(ss):
475 d.addService(s)
476 result.append(d)
477 max -= 1
478 else: break
479
480 self.myAgent.db_mutex.release()
481 content = "((result " + self.msg.getContent().strip("\n")[1:-1]
482 if len(result)>0:
483 content += " (sequence "
484 for i in result:
485 content += str(i) + " "
486 content += ")"
487 else:
488 pass
489 content += "))"
490 self.myAgent.DEBUG("Found " +str(len(result)) + " services",'ok')
491 for d in result:
492 self.myAgent.DEBUG(str(d),'ok')
493
494
495 reply.setPerformative("inform")
496 reply.setContent(content)
497 self.myAgent.send(reply)
498
499 recvs = ""
500 for r in reply.getReceivers():
501 recvs += str(r.getName())
502
503 return 1
504
505 else:
506
507 max = 50
508 if self.content.action.argument.max_results:
509 try:
510 max_str = str(self.content.action.argument.max_results)
511 max = int(max_str)
512 except Exception, err:
513
514 self.myAgent.DEBUG("FAILURE: (internal-error) max-results is not an integer! ",'error')
515
516
517
518 result = []
519
520 if self.content.action.argument.df_agent_description:
521 try:
522 dad = DfAgentDescription(co = self.content.action.argument.df_agent_description)
523 self.myAgent.DEBUG("Searching for: " +str(dad) + " in ServiceDB: " + str(map(lambda s: str(s), self.myAgent.servicedb.values())) )
524 except Exception, err:
525 self.myAgent.DEBUG("FAILURE: Could not extract DfAgentDescription from content: "+str(err),'error')
526
527 self.myAgent.db_mutex.acquire()
528 if max in [-1, 0]:
529
530 for agentid,dads in self.myAgent.servicedb.items():
531 self.myAgent.DEBUG("Comparing " + str(dad) + " WITH " + str(dads) + " ==> " + str(dads.match(dad)),'ok')
532 if dads.match(dad):
533 d = copy.copy(dads)
534 if dad.services == []:
535 d.services = dads.getServices()
536 else:
537 d.services=[]
538 for ss in dad.getServices():
539 for s in dads.getServices():
540 if s.match(ss):
541 d.addService(s)
542 result.append(d)
543 else:
544 max = abs(max)
545 for agentid,dads in self.myAgent.servicedb.items():
546 if max >= 0:
547 if dads.match(dad):
548 d = copy.copy(dads)
549 if dad.services == []:
550 d.services = dads.getServices()
551 else:
552 d.services=[]
553 for ss in dad.getServices():
554 for s in dads.getServices():
555 if s.match(ss):
556 d.addService(s)
557 result.append(d)
558 max -= 1
559 else: break
560 self.myAgent.db_mutex.release()
561
562 content = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
563 content["fipa:result"] = []
564 for i in result:
565 content["fipa:result"].append(i.asContentObject())
566 self.myAgent.DEBUG(str(i),'ok')
567 self.myAgent.DEBUG("Found "+str(len(result)) + " services.",'ok')
568 reply.setPerformative("inform")
569 reply.setContentObject(content)
570 self.myAgent.send(reply)
571
572
573
574
575
576 return 1
577
578
579
581
586
588
589
590 error = False
591 co_error = False
592 dad = None
593 if "rdf" in self.msg.getLanguage():
594
595 try:
596 co = self.msg.getContentObject()
597 dad = DfAgentDescription(co = co.action.argument)
598 except KeyboardInterrupt,err:
599 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
600 co_error["fipa:error"] = "missing-argument df-agent-description"
601
602 reply = self.msg.createReply()
603 reply.setSender(self.myAgent.getAID())
604 reply.setPerformative("refuse")
605 reply.setContentObject(co_error)
606 self.myAgent.send(reply)
607 self.myAgent.DEBUG("FAILURE: Could not extract DfAgentDescription from content: "+str(err),'error')
608 return -1
609
610 if dad and (dad.getAID().getName() != self.msg.getSender().getName()):
611 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
612 co_error["fipa:error"] = "unauthorised"
613
614 if co_error:
615 reply = self.msg.createReply()
616 reply.setSender(self.myAgent.getAID())
617 reply.setPerformative("refuse")
618 reply.setContentObject(co_error)
619 self.myAgent.send(reply)
620 self.myAgent.DEBUG("FAILURE: "+self.msg.getSender().getName()+" is UNAUTHORISED to modify service "+str(dad),'warn')
621
622 return -1
623
624
625 reply = self.msg.createReply()
626 reply.setSender(self.myAgent.getAID())
627 reply.setPerformative("agree")
628 co = self.msg.getContentObject()
629 co["fipa:done"] = "true"
630 reply.setContentObject(co)
631 self.myAgent.send(reply)
632
633
634 if self.myAgent.servicedb.has_key(dad.getAID().getName()):
635
636 try:
637 for ss in dad.getServices():
638 self.myAgent.db_mutex.acquire()
639 result=self.myAgent.servicedb[dad.getAID().getName()].updateService(ss)
640 self.myAgent.db_mutex.release()
641 if not result:
642 reply.setPerformative("failure")
643 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
644 co_error["fipa:error"] = "not-registered"
645 reply.setContentObject(co_error)
646 self.myAgent.send(reply)
647 self.myAgent.DEBUG("FAILURE: Could not modify service "+str(ss)+". Service is NOT registered.",'warn')
648 except Exception, err:
649 reply.setPerformative("failure")
650 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
651 co_error["fipa:error"] = "internal-error"
652 reply.setContentObject(co_error)
653 self.myAgent.send(reply)
654 self.myAgent.DEBUG("FAILURE: internal-error: "+str(err),'error')
655 return -1
656
657
658
659 reply.setPerformative("inform")
660 co = self.msg.getContentObject()
661 co["fipa:done"] = "true"
662 reply.setContentObject(co)
663 self.myAgent.send(reply)
664
665 return 1
666
667 else:
668 reply.setPerformative("failure")
669 co_error = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
670 co_error["fipa:error"] = "not-registered"
671 reply.setContentObject(co_error)
672 self.myAgent.send(reply)
673 self.myAgent.DEBUG("FAILURE: Could not modify service "+str(dad)+". Agent has no registered services.",'warn')
674 return -1
675
676 else:
677
678 try:
679 dad = DF.DfAgentDescription(self.content.action.modify[0][1])
680 except Exception,err:
681 error = "(missing-argument ams-agent-description)"
682 self.myAgent.DEBUG("FAILURE: Could not extract DfAgentDescription from content: "+str(err),'error')
683
684 if dad and (dad.getAID().getName() != self.msg.getSender().getName()):
685 error = "(unauthorised)"
686 self.myAgent.DEBUG("REFUSE: "+self.msg.getSender().getName()+" is not AUTHORISED to modify service "+str(dad),'warn')
687
688 if error:
689 reply = self.msg.createReply()
690 reply.setSender(self.myAgent.getAID())
691 reply.setPerformative("refuse")
692 reply.setContent("( "+self.msg.getContent() + error + ")")
693 self.myAgent.send(reply)
694
695 return -1
696
697 else:
698
699 reply = self.msg.createReply()
700 reply.setSender(self.myAgent.getAID())
701 reply.setPerformative("agree")
702 reply.setContent("(" + str(self.msg.getContent()) + " true)")
703 self.myAgent.send(reply)
704
705 if self.myAgent.servicedb.has_key(dad.getAID().getName()):
706
707 try:
708 for ss in dad.getServices():
709 self.myAgent.db_mutex.acquire()
710 result=self.myAgent.servicedb[dad.getAID().getName()].updateService(ss)
711 self.myAgent.db_mutex.release()
712 if not result:
713 reply.setPerformative("failure")
714 reply.setContent("("+self.msg.getContent() + "(not-registered))")
715 self.myAgent.send(reply)
716 self.myAgent.DEBUG("FAILURE: Could not modify service "+str(ss)+". Service is NOT registered.",'warn')
717 except Exception, err:
718 reply.setPerformative("failure")
719 reply.setContent("("+self.msg.getContent() + "(internal-error))")
720 self.myAgent.send(reply)
721 self.myAgent.DEBUG("FAILURE: (internal-error) Modifying service: "+str(err),'error')
722 return -1
723
724
725
726 reply.setPerformative("inform")
727 reply.setContent("(done "+self.msg.getContent() + ")")
728 self.myAgent.send(reply)
729
730 return 1
731
732 else:
733 reply.setPerformative("failure")
734 reply.setContent("("+self.msg.getContent() + "(not-registered))")
735 self.myAgent.send(reply)
736 self.myAgent.DEBUG("FAILURE: Could not modify service "+str(dad)+". Agent has no registered services.",'warn')
737 return -1
738
739
740 - def __init__(self,node,passw,server="localhost",port=5347, config={}):
742
743
761
762
764
765 - def __init__(self, content = None, co = None):
766
767 self.name = None
768 self.services = []
769 self.protocols = []
770 self.ontologies = []
771 self.languages = []
772 self.lease_time = None
773 self.scope = []
774
775 if content:
776 self.loadSL0(content)
777
778 if co:
779 if co.has_key("df-agent-description"):
780 co = co["df-agent-description"]
781 if co.name:
782 self.name = AID.aid(co = co.name)
783 if co.services:
784 self.services = []
785 if "ContentObject" in str(type(co.services)):
786 self.services.append(ServiceDescription(co = co.services))
787 else:
788
789 for s in co.services:
790 self.services.append(ServiceDescription(co = s))
791 if co.lease_time:
792 self.lease_time = co.lease_time
793 if co.has_key("lease-time"):
794 self.lease_time = co["lease-time"]
795 if co.protocols:
796 self.protocols = copy.copy(co.protocols)
797 if co.ontologies:
798 self.ontologies = copy.copy(co.ontologies)
799 if co.languages:
800 self.languages = copy.copy(co.languages)
801 if co.scope:
802 self.scope = copy.copy(co.scope)
803
804
805 - def asContentObject(self):
806 """
807 Returns a version of the DAD in ContentObject format
808 """
809 co = ContentObject(namespaces={"http://www.fipa.org/schemas/fipa-rdf0#":"fipa:"})
810 if self.name:
811 co["name"] = self.name.asContentObject()
812 else:
813 co["name"] = AID.aid().asContentObject()
814 if self.lease_time:
815 co["lease-time"] = str(self.lease_time)
816 if self.protocols:
817 co["protocols"] = copy.copy(self.protocols)
818 if self.services:
819 co["services"] = []
820 for s in self.services:
821 co["services"].append(s.asContentObject())
822 if self.ontologies:
823 co["ontologies"] = copy.copy(self.ontologies)
824 if self.languages:
825 co["languages"] = copy.copy(self.languages)
826 if self.scope:
827 co["scope"] = copy.copy(self.scope)
828
829 return co
830
831
833 """
834 returns a printable version of the DAD in RDF/XML format
835 """
836 return str(self.asContentObject())
837
840
845
848
851
863
880
903
905 return self.protocols
906
908 if p not in self.protocols:
909 self.protocols.append(p)
910
912 return self.ontologies
913
915 if o not in self.ontologies:
916 self.ontologies.append(o)
917
919 return self.languages
920
922 if l not in self.languages:
923 self.languages.append(l)
924
926 return self.lease_time
927
930
933
936
937
939
940 if y.name:
941 if not self.getAID().match(y.name):
942 return False
943 if y.protocols:
944 for p in y.protocols:
945 if not (p in self.protocols):
946 return False
947 if y.ontologies:
948 for o in y.ontologies:
949 if not (o in self.ontologies):
950 return False
951 if y.languages:
952 for l in y.languages:
953 if not (l in self.languages):
954 return False
955 if y.lease_time:
956 if self.lease_time != y.getLeaseTime():
957 return False
958 if y.scope:
959 if self.scope != y.getScope():
960 return False
961
962 if len(self.services)>0 and len(y.getServices())>0:
963 for i in y.services:
964 matched = False
965 for j in self.getServices():
966
967 if j.match(i):
968 matched=True
969 break
970 if not matched: return False
971 return True
972 else:
973 return True
974
977
1008
1011
1013
1014 sb = ''
1015 if self.name != None:
1016 sb = sb + ":name " + str(self.name) + "\n"
1017
1018 if len(self.protocols) > 0:
1019 sb = sb + ":protocols \n(set\n"
1020 for i in self.protocols:
1021 sb = sb + str(i) + '\n'
1022 sb = sb + ")\n"
1023
1024 if len(self.ontologies) > 0:
1025 sb = sb + ":ontologies \n(set\n"
1026 for i in self.ontologies:
1027 sb = sb + str(i) + '\n'
1028 sb = sb + ")\n"
1029
1030 if len(self.languages) > 0:
1031 sb = sb + ":languages \n(set\n"
1032 for i in self.languages:
1033 sb = sb + str(i) + '\n'
1034 sb = sb + ")\n"
1035
1036 if self.lease_time != None:
1037 sb = sb + ":lease-time " + str(self.lease_time) + '\n'
1038
1039 if self.scope != None:
1040 sb = sb + ":scope " + str(self.scope) + '\n'
1041
1042 if len(self.services) > 0:
1043 sb = sb + ":services \n(set\n"
1044 for i in self.services:
1045 sb = sb + str(i.asSL0()) +'\n'
1046 sb = sb + ")\n"
1047
1048 sb = "(df-agent-description \n" + sb + ")\n"
1049 return sb
1050
1052
1053 - def __init__(self, content = None, co = None):
1054
1055 self.name = None
1056 self.type = None
1057 self.protocols = []
1058 self.ontologies = []
1059 self.languages = []
1060 self.ownership = None
1061 self.properties = {}
1062
1063 if content != None:
1064 self.loadSL0(content)
1065
1066 if co:
1067
1068 if co.name:
1069 self.name = co.name
1070 if co.type:
1071 self.type = co.type
1072 if co.protocols:
1073 self.protocols = copy.copy(co.protocols)
1074 if co.ontologies:
1075 self.ontologies = copy.copy(co.ontologies)
1076 if co.languages:
1077 self.languages = copy.copy(co.languages)
1078 if co.ownership:
1079 self.ownership = co.ownership
1080 if co.properties:
1081
1082 for k,v in co.properties.items():
1083 if ":" in k: ns,key = k.split(":")
1084 else: key=k
1085 self.properties[key]=v
1086
1087
1090
1093
1096
1099
1101 return self.protocols
1102
1104 if p not in self.protocols:
1105 self.protocols.append(p)
1106
1108 return self.ontologies
1109
1111 if o not in self.ontologies:
1112 self.ontologies.append(o)
1113
1115 return self.languages
1116
1118 if l not in self.languages:
1119 self.languages.append(l)
1120
1122 return self.ownership
1123
1126
1128 return self.properties
1129
1131 if prop in self.properties.keys():
1132 return self.properties[prop]
1133 return None
1134
1136 if ":" in k: ns,key = k.split(":")
1137 else: key=k
1138 self.properties[key]=value
1139
1142
1144
1145 if y.name:
1146 if self.name != y.getName():
1147 return False
1148 if y.type:
1149 if self.type != y.getType():
1150 return False
1151 if y.protocols:
1152 for p in y.protocols:
1153 if not (p in self.protocols):
1154 return False
1155 if y.ontologies:
1156 for o in y.ontologies:
1157 if not (o in self.ontologies):
1158 return False
1159 if y.languages:
1160 for l in y.languages:
1161 if not (l in self.languages):
1162 return False
1163 if y.ownership:
1164 if self.ownership != y.getOwnership():
1165 return False
1166
1167 for k,v in y.properties.items():
1168 if self.getProperties().has_key(k):
1169 if y.getProperty(k) != v: return False
1170 else: return False
1171 return True
1172
1174 return not self == y
1175
1199
1202
1204
1205 sb = ""
1206 if self.name != None:
1207 sb += ":name " + str(self.name) + "\n"
1208 if self.type:
1209 sb += ":type " + str(self.type) + "\n"
1210
1211 if len(self.protocols) > 0:
1212 sb += ":protocols \n(set\n"
1213 for i in self.protocols:
1214 sb += str(i) + " "
1215 sb = sb + ")\n"
1216
1217 if len(self.ontologies) > 0:
1218 sb = sb + ":ontologies \n(set\n"
1219 for i in self.ontologies:
1220 sb += str(i) + " "
1221 sb = sb + ")\n"
1222
1223 if len(self.languages) > 0:
1224 sb = sb + ":languages \n(set\n"
1225 for i in self.languages:
1226 sb += str(i) + " "
1227 sb += ")\n"
1228
1229 if self.ownership:
1230 sb += ":ownership " + str(self.ownership) + "\n"
1231
1232 if len(self.properties) > 0:
1233 sb += ":properties \n (set\n"
1234 for k,v in self.properties.items():
1235 sb += " (property :name " + str(k) + " :value " + str(v) +")\n"
1236 sb += ")\n"
1237
1238
1239 if sb != "":
1240 sb = "(service-description\n" + sb + ")\n"
1241 return sb
1242
1243 - def asContentObject(self):
1244 """
1245 Returns a version of the SD in ContentObject format
1246 """
1247 co = ContentObject()
1248 if self.name:
1249 co["name"] = str(self.name)
1250 else:
1251 co["name"] = ""
1252 if self.type:
1253 co["type"] = str(self.type)
1254 else:
1255 co["type"] = ""
1256 if self.protocols:
1257 co["protocols"] = copy.copy(self.protocols)
1258 if self.ontologies:
1259 co["ontologies"] = copy.copy(self.ontologies)
1260 if self.languages:
1261 co["languages"] = copy.copy(self.languages)
1262 if self.ownership:
1263 co["ownership"] = self.ownership
1264 if self.properties != {}:
1265 co["properties"] = ContentObject()
1266 for k,v in self.properties.items():
1267 if ":" in k: ns,key = k.split(":")
1268 else: key=k
1269 co["properties"][str(key)] = v
1270 return co
1271
1273 """
1274 returns a printable version of the SD in RDF/XML format
1275 """
1276 return str(self.asContentObject())
1277
1279
1280 - def __init__(self, name=None, owner=None, P=[], Q=[], inputs=[], outputs=[], description= None, ontology=None, dad = None, co = None):
1319
1329
1332
1342
1345
1355
1357
1369
1375
1387
1393
1402
1404 if self.dad.getServices()==[]: return []
1405 return self.dad.getServices()[0].getProperty("inputs")
1406
1415
1419
1428
1432
1436
1440
1442 sd = dad.getServices()
1443 if len(sd)==1: sd = sd[0]
1444 else: return None
1445 self.name = sd.getName()
1446 self.owner = dad.getAID()
1447 self.ontology = dad.getOntologies()
1448
1449 self.dad = dad
1450
1453
1455 return y.dad.match(self.dad)
1456
1458 if y==None: return False
1459 return y.match(self)
1460
1463
1465 return str(self.dad)
1466
1469
1470 - def asContentObject(self):
1471
1472 co = ContentObject()
1473 co["service"]=ContentObject()
1474 if self.getName()!=None: co.service["name"] = self.getName()
1475 if self.getOwner()!=None: co.service["owner"] = self.getOwner().asContentObject()
1476 if self.getOntology()!=[]: co.service["ontology"] = self.getOntology()
1477 if self.getP()!=[]: co.service["P"] = self.getP()
1478 if self.getQ()!=[]: co.service["Q"] = self.getQ()
1479 if self.getDescription()!=None: co.service["description"] = self.getDescription()
1480
1481 return co
1482
1484 s = '<table class="servicesT" cellspacing="0">'
1485 s += '<tr><td class="servHd">Name</td><td class="servBodL">'+self.getName()+'</td></tr>'
1486 s += '<tr><td class="servHd">Owner</td><td class="servBodL">'+self.getOwner().getName()+'</td></tr>'
1487 if self.getType():
1488 s += '<tr><td class="servHd">Type</td><td class="servBodL">'+str(self.getType())+'</td></tr>'
1489 if self.getDescription():
1490 s += '<tr><td class="servHd">Description</td><td class="servBodL">'+str(self.getDescription())+'</td></tr>'
1491 if self.getOntology():
1492 s += '<tr><td class="servHd">Ontologies</td><td class="servBodL">'+str(self.getOntology())+'</td></tr>'
1493 if self.getP():
1494 s += '<tr><td class="servHd">Preconditions</td><td class="servBodL">'+str(self.getP())+'</td></tr>'
1495 if self.getQ():
1496 s += '<tr><td class="servHd">Postconditions</td><td class="servBodL">'+str(self.getQ())+'</td></tr>'
1497 if self.getInputs():
1498 s += '<tr><td class="servHd">Inputs</td><td class="servBodL">'+str(self.getInputs())+'</td></tr>'
1499 if self.getOutputs():
1500 s += '<tr><td class="servHd">Outputs</td><td class="servBodL">'+str(self.getOutputs())+'</td></tr>'
1501 s+='</table>'
1502
1503 return s
1504