Package spade :: Module RDF0Parser
[hide private]
[frames] | no frames]

Source Code for Module spade.RDF0Parser

  1  # -*- coding: cp1252 -*-
 
  2  import xml.sax 
  3  from xml.sax import handler 
  4  import cStringIO 
  5  
 
6 -class Newdict(dict):
7 - def __getattr__(self, name): return self[name]
8 - def pprint(self, ind=0):
9 s = "" 10 for k,v in self.items(): 11 try: 12 s = s + ('\t'*ind)+str(k)+":\n"+v.pprint(ind+1) + '\n' 13 except: 14 s = s + ('\t'*ind)+str(k)+": " + str(v) + '\n' 15 return s
16 17 # build a shorthand tag
18 -def btag(n1,n2):
19 return str(n1)+":"+str(n2)
20
21 -class RDF0Parser(handler.ContentHandler):
22 """ 23 FIPA RDF Content Language Parser 24 """
25 - def __init__(self):
26 27 28 #Constants 29 self.OT = "<" 30 self.ET = "</" 31 self.CT = ">" 32 self.NULL = "" 33 self.TYPE_SEP = "^^" 34 35 36 #RDF syntax namespace constants 37 self.XML_VERSION = '<?xml version="1.0"?>' 38 self.RDF_PREF = "rdf" 39 self.RDF = "RDF" 40 self.RDF_NS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" 41 self.ID = "ID" 42 self.RESOURCE = "resource" 43 self.DESCRIPTION = "Description" 44 self.ABOUT = "about" 45 self.SUBJECT = "subject" 46 self.PREDICATE = "predicate" 47 self.OBJECT = "object" 48 self.BAG = "Bag" 49 self.SEQ = "Seq" 50 self.ALT = "Alt" 51 self.LI = "li" 52 self.DATATYPE = "datatype" 53 54 #Fipa namespace constants 55 self.FIPA_PREF = "fipa" 56 self.FIPA_NS = "http://www.fipa.org/schemas/fipa-rdf0#" 57 self.PROPOSITION = "Proposition" 58 self.BELIEF = "belief" 59 self.ACTION = "Action" 60 self.ACTOR = "actor" 61 self.ACT = "act" 62 self.ARGUMENT = "argument" 63 self.ACTOR = "actor" 64 self.DONE = "done" 65 self.RESULT = "result" 66 self.IMPLBY = "implementedBy" 67 self.CODE = "Code" 68 self.LANGUAGE = "language" 69 self.BINDING = "binding" 70 self.CODE_URI = "code-uri" 71 self.SCRIPT = "script" 72 73 74 75 self.content = Newdict()
76 77 78 79 80 # *************************************************** 81 # * Encoding methods * 82 # *************************************************** 83
84 - def encodeTag( self, tag, content ):
85 86 if isinstance(content,dict): 87 # encoding a RDF resource 88 sb = self.encodeOneLineTag( tag, btag(self.RDF_PREF,self.RESOURCE), content[self.RESOURCE]) 89 else: 90 if self.TYPE_SEP in content: 91 #encoding value with type 92 value, type = content.split(self.TYPE_SEP) 93 sb = self.encodeInitTagAttr( tag, btag(self.RDF_PREF,self.DATATYPE), type) 94 sb = sb + value.strip('"') 95 96 else: 97 sb = self.OT + tag + self.CT 98 sb = sb + content 99 100 sb = sb + self.ET + tag + self.CT + '\n' 101 102 return sb
103
104 - def encodeInitTag( self, tag):
105 sb = self.OT + tag + self.CT 106 return sb + '\n'
107
108 - def encodeEndTag( self, tag):
109 sb = self.ET + tag + self.CT 110 return sb + '\n'
111
112 - def encodeInitTagAttr( self, tag, attr, value):
113 sb = self.OT + tag + " " + attr + '="' + value + '"' + self.CT 114 return sb
115
116 - def encodeOneLineTag( self, tag1, tag2, value ):
117 sb = self.OT + tag1 + " " 118 sb = sb + tag2 + '="' + value + '"/>' 119 return sb + '\n'
120
121 - def encode(self, content):
122 """ 123 Content Encoding 124 """ 125 #version encoding 126 sb = self.XML_VERSION+'\n' 127 128 #namespaces encoding 129 sb = sb + self.OT + btag(self.RDF_PREF,self.RDF)+ ' ' + btag("xmlns",self.RDF_PREF) + '="' + self.RDF_NS + '"' 130 sb = sb +'\n ' + btag("xmlns",self.FIPA_PREF) + '="' + self.FIPA_NS + '"' + self.CT + '\n\n' 131 132 133 # Action encoding 134 if self.ACTION in content: 135 if self.ID in content[self.ACTION]: 136 sb_action = self.encodeInitTagAttr(btag(self.FIPA_PREF,self.ACTION),btag(self.RDF_PREF,self.ID),content[self.ACTION][self.ID]) 137 138 elif self.ABOUT in content[self.ACTION]: 139 sb_action = self.encodeInitTagAttr(btag(self.FIPA_PREF,self.ACTION),btag(self.RDF_PREF,self.ABOUT),content[self.ACTION][self.ABOUT]) 140 141 else: 142 sb_action = self.encodeInitTag(btag(self.FIPA_PREF,self.ACTION)) 143 144 145 146 if self.ACTOR in content[self.ACTION]: 147 sb_action = sb_action + self.encodeTag(btag(self.FIPA_PREF,self.ACTOR),content[self.ACTION][self.ACTOR]) 148 149 if self.ACT in content[self.ACTION]: 150 sb_action = sb_action + self.encodeTag(btag(self.FIPA_PREF,self.ACT),content[self.ACTION][self.ACT]) 151 152 if self.ARGUMENT in content[self.ACTION]: 153 container = None 154 if self.BAG in content[self.ACTION][self.ARGUMENT]: 155 container = self.BAG 156 elif self.SEQ in content[self.ACTION][self.ARGUMENT]: 157 container = self.SEQ 158 elif self.ALT in content[self.ACTION][self.ARGUMENT]: 159 container = self.ALT 160 161 accum = "" 162 if container == None: 163 accum = content[self.ACTION][self.ARGUMENT] 164 else: 165 for literal in content[self.ACTION][self.ARGUMENT][container]: 166 accum = accum + self.encodeTag(btag(self.RDF_PREF,self.LI),literal) 167 accum = self.encodeTag(btag(self.RDF_PREF,container),accum) 168 169 sb_action = sb_action + self.encodeTag(btag(self.FIPA_PREF,self.ARGUMENT),accum) 170 171 172 if self.IMPLBY in content[self.ACTION]: 173 container = None 174 # An action can be performed following alternative implementations 175 if self.ALT in content[self.ACTION][self.IMPLBY]: 176 container = self.ALT 177 178 keys = [self.LANGUAGE, self.BINDING, self.CODE_URI, self.SCRIPT] 179 180 if container == None: 181 if self.CODE in content[self.ACTION][self.IMPLBY]: 182 accum = "" 183 184 for key in keys: 185 if key in content[self.ACTION][self.IMPLBY][self.CODE]: 186 accum = accum + self.encodeTag(btag(self.FIPA_PREF,key),content[self.ACTION][self.IMPLBY][self.CODE][key]) 187 accum = self.encodeTag(btag(self.FIPA_PREF,self.CODE),accum) 188 189 else: 190 accum = "" 191 for code in content[self.ACTION][self.IMPLBY][self.ALT]: 192 accum2 = "" 193 for key in keys: 194 if key in code[self.CODE].keys(): 195 accum2 = accum2 + self.encodeTag(btag(self.FIPA_PREF,key),code[self.CODE][key]) 196 accum = accum + self.encodeTag(btag(self.FIPA_PREF,self.CODE),accum2) 197 198 accum = self.encodeTag(btag(self.RDF_PREF,container),accum) 199 200 201 sb_action = sb_action + self.encodeTag(btag(self.FIPA_PREF,self.IMPLBY),accum) 202 203 204 205 if self.DONE in content[self.ACTION]: 206 sb_action = sb_action + self.encodeTag(btag(self.FIPA_PREF,self.DONE),content[self.ACTION][self.DONE]) 207 208 209 if self.RESULT in content[self.ACTION]: 210 sb_action = sb_action + self.encodeTag(btag(self.FIPA_PREF,self.RESULT),content[self.ACTION][self.RESULT]) 211 212 213 214 sb = sb + sb_action + self.encodeEndTag(btag(self.FIPA_PREF,self.ACTION)) 215 216 217 218 # Proposition Encoding 219 if self.PROPOSITION in content: 220 accum = "" 221 if self.SUBJECT in content[self.PROPOSITION]: 222 accum = accum + self.encodeTag(btag(self.RDF_PREF,self.SUBJECT),content[self.PROPOSITION][self.SUBJECT]) 223 if self.PREDICATE in content[self.PROPOSITION]: 224 accum = accum + self.encodeTag(btag(self.RDF_PREF,self.PREDICATE),content[self.PROPOSITION][self.PREDICATE]) 225 if self.OBJECT in content[self.PROPOSITION]: 226 accum = accum + self.encodeTag(btag(self.RDF_PREF,self.OBJECT),content[self.PROPOSITION][self.OBJECT]) 227 if self.BELIEF in content[self.PROPOSITION]: 228 accum = accum + self.encodeTag(btag(self.FIPA_PREF,self.BELIEF),content[self.PROPOSITION][self.BELIEF]) 229 230 sb = sb + self.encodeTag(btag(self.FIPA_PREF,self.PROPOSITION),accum) 231 232 233 sb = sb + self.encodeEndTag(btag(self.RDF_PREF,self.RDF)) 234 235 return sb 236 237 # Description Encoding 238 if self.DESCRIPTION in content: 239 sb = sb + self.encodeInitTagAttr(btag(self.RDF_PREF,self.DESCRIPTION),btag(self.RDF_PREF,self.ABOUT),content[self.DESCRIPTION][self.ABOUT]) 240 if self.DONE in content[self.DESCRIPTION]: 241 sb = sb + self.encodeTag(btag(self.FIPA_PREF,self.DONE),content[self.DESCRIPTION][self.DONE]) 242 243 244 if self.RESULT in content[self.DESCRIPTION]: 245 sb = sb_action + self.encodeTag(btag(self.FIPA_PREF,self.RESULT),content[self.DESCRIPTION][self.RESULT]) 246 247 248 249 sb = sb + self.encodeEndTag(btag(self.FIPA_PREF,self.ACTION))
250 251 252 253 254 255 256 """ 257 *************************************************** 258 * Decoding methods * 259 *************************************************** 260 """ 261 262 #This method is called when start the document XML
263 - def startDocument(self):
264 self.prefixs = Newdict() 265 self.content = Newdict() 266 self.resource = {} 267 self.datatype = {} 268 self.container = None 269 self.accumulator = "" 270 self.s = "" 271 self.p = ""
272 273
274 - def startPrefixMapping(self,prefix,uri):
275 if uri == self.RDF_NS: 276 self.prefixs['rdf']=prefix 277 elif uri == self.FIPA_NS: 278 self.prefixs['fipa']=prefix 279 else: 280 print "RDFParser: Not recognized URI"
281 #excepcio sino peta despres la clau 282 283
284 - def startElementNS(self, name, qname, attributes):
285 286 print "RDF0: seNS"+str(name)+" "+str(qname)+" "+str(attributes) 287 288 self.accumulator = "" 289 290 # Fix "lower" bug 291 if qname == None: 292 qname = "" 293 294 #fipa Actions 295 if str(self.FIPA_NS+self.ACTION).lower() == str(name[0]+name[1]).lower(): 296 #if btag(self.prefixs.fipa,self.ACTION).lower() == qname.lower(): 297 print "MATCH ACTION" 298 self.content[self.ACTION] = Newdict() 299 self.s = self.ACTION 300 if btag(self.prefixs.rdf,self.ID) in attributes.getQNames(): 301 self.content[self.ACTION][self.ID] = attributes.getValueByQName(btag(self.prefixs.rdf,self.ID)) 302 elif btag(self.prefixs.rdf,self.ABOUT) in attributes.getQNames(): 303 self.content[self.ACTION][self.ABOUT] = attributes.getValueByQName(btag(self.prefixs.rdf,self.ABOUT)) 304 305 306 elif str(self.FIPA_NS+self.ARGUMENT).lower() == str(name[0]+name[1]).lower(): 307 #elif btag(self.prefixs.fipa,self.ARGUMENT).lower() == qname.lower(): 308 self.content[self.s][self.ARGUMENT] = Newdict() 309 self.p = self.ARGUMENT 310 311 elif str(self.FIPA_NS+self.IMPLBY).lower() == str(name[0]+name[1]).lower(): 312 #elif btag(self.prefixs.fipa,self.IMPLBY).lower() == qname.lower(): 313 self.content[self.s][self.IMPLBY] = Newdict() 314 self.p = self.IMPLBY 315 316 elif str(self.FIPA_NS+self.CODE).lower() == str(name[0]+name[1]).lower(): 317 #elif btag(self.prefixs.fipa,self.CODE).lower() == qname.lower(): 318 self.code = Newdict() 319 320 elif str(self.RDF_NS+self.BAG).lower() == str(name[0]+name[1]).lower(): 321 #elif btag(self.prefixs.rdf,self.BAG).lower() == qname.lower(): 322 self.content[self.s][self.p][self.BAG] = [] 323 self.container = self.BAG 324 325 elif str(self.RDF_NS+self.SEQ).lower() == str(name[0]+name[1]).lower(): 326 #elif btag(self.prefixs.rdf,self.SEQ).lower() == qname.lower(): 327 self.content[self.s][self.p][self.SEQ] = [] 328 self.container = self.SEQ 329 330 elif str(self.RDF_NS+self.ALT).lower() == str(name[0]+name[1]).lower(): 331 #elif btag(self.prefixs.rdf,self.ALT).lower() == qname.lower(): 332 self.content[self.s][self.p][self.ALT] = [] 333 self.container = self.ALT 334 335 336 #fipa Propositions 337 elif str(self.FIPA_NS+self.PROPOSITION).lower() == str(name[0]+name[1]).lower(): 338 #elif btag(self.prefixs.fipa,self.PROPOSITION).lower() == qname.lower(): 339 self.content[self.PROPOSITION]=Newdict() 340 self.s = self.PROPOSITION 341 342 #rdf Descriptions 343 elif str(self.RDF_NS+self.DESCRIPTION).lower() == str(name[0]+name[1]).lower(): 344 #elif btag(self.prefixs.rdf,self.DESCRIPTION).lower() == qname.lower(): 345 self.content[self.DESCRIPTION]=Newdict() 346 self.s = self.DESCRIPTION 347 if btag(self.prefixs.rdf,self.ABOUT) in attributes.getQNames(): 348 self.content[self.DESCRIPTION][self.ABOUT] = attributes.getValueByQName(btag(self.prefixs.rdf,self.ABOUT)) 349 350 else: 351 print "NO FIPA MATCH" 352 #self.content[name[1]] = None 353 354 print "SELF.S "+str(self.s) 355 356 # if tag refers to a resource 357 if str(self.RDF_NS+self.RESOURCE).lower() == str(name[0]+name[1]).lower(): 358 #if btag(self.prefixs.rdf,self.RESOURCE) in attributes.getQNames(): 359 self.resource[name] = attributes.getValueByQName(btag(self.prefixs.rdf,self.RESOURCE)) 360 361 # catching datatype 362 if str(self.RDF_NS+self.DATATYPE).lower() == str(name[0]+name[1]).lower(): 363 #if btag(self.prefixs.rdf,self.DATATYPE) in attributes.getQNames(): 364 self.datatype[name] = attributes.getValueByQName(btag(self.prefixs.rdf,self.DATATYPE)) 365 366 print "RDF0: seNS: self.s "+str(self.s)
367 368 369 370 371
372 - def endElementNS(self, name, qname):
373 374 print "RDF0: eeNS"+str(name)+" "+str(qname) 375 print "RDF0: eeNS: self.s "+str(self.s) 376 377 # tags with resource don't need any value 378 if self.resource.has_key(name): 379 self.accumulator = {self.RESOURCE:self.resource[name]} 380 self.resource.clear() 381 382 # Adding datatype to tag with value 383 if self.datatype.has_key(name): 384 self.accumulator = '"'+ self.accumulator + '"' + self.TYPE_SEP + self.datatype[name] 385 self.datatype.clear() 386 387 388 # start name check 389 390 if self.ACTOR.lower() == name[1].lower(): 391 self.content[self.s][self.ACTOR] = self.accumulator 392 393 elif self.ACT.lower() == name[1].lower(): 394 self.content[self.s][self.ACT] = self.accumulator 395 396 elif self.ARGUMENT.lower() == name[1].lower(): 397 if self.content[self.s][self.ARGUMENT].keys() == []: 398 self.content[self.s][self.ARGUMENT] = self.accumulator 399 400 elif self.BAG.lower() == name[1].lower() or self.SEQ.lower() == name[1].lower() or self.ALT.lower() == name[1].lower(): 401 self.container = None 402 403 404 elif self.LI.lower() == name[1].lower(): 405 if self.container != None: 406 self.content[self.s][self.p][self.container].append(self.accumulator) 407 else: pass 408 409 elif self.SUBJECT.lower() == name[1].lower(): 410 self.content[self.s][self.SUBJECT] = self.accumulator 411 412 elif self.PREDICATE.lower() == name[1].lower(): 413 self.content[self.s][self.PREDICATE] = self.accumulator 414 415 elif self.OBJECT.lower() == name[1].lower(): 416 self.content[self.s][self.OBJECT] = self.accumulator 417 418 elif self.BELIEF.lower() == name[1].lower(): 419 self.content[self.s][self.BELIEF] = self.accumulator 420 421 elif self.DONE.lower() == name[1].lower(): 422 self.content[self.s][self.DONE] = self.accumulator 423 424 elif self.RESULT.lower() == name[1].lower(): 425 self.content[self.s][self.RESULT] = self.accumulator 426 427 elif self.CODE.lower() == name[1].lower(): 428 if self.container != None: 429 self.content[self.s][self.p][self.container].append({self.CODE:self.code}) 430 else: self.content[self.s][self.p][self.CODE] = self.code 431 432 elif self.LANGUAGE.lower() == name[1].lower(): 433 self.code[self.LANGUAGE] = self.accumulator 434 435 elif self.BINDING.lower() == name[1].lower(): 436 self.code[self.BINDING] = self.accumulator 437 438 elif self.CODE_URI.lower() == name[1].lower(): 439 self.code[self.CODE_URI] = self.accumulator 440 441 elif self.SCRIPT.lower() == name[1].lower(): 442 self.code[self.SCRIPT] = self.accumulator 443 444 445 self.accumulator=""
446 447 448
449 - def characters(self, buff):
450 self.accumulator = self.accumulator + buff
451 452 453 # Main decoding method #
454 - def parse(self, s):
455 parser = xml.sax.make_parser() 456 parser.start_namespace_decl("xml", "http://www.w3.org/XML/1998/namespace") 457 #enabling namespaces 458 try: 459 parser.setFeature(xml.sax.handler.feature_namespaces, 1) 460 #parser.setFeature(xml.sax.handler.feature_namespace_prefixes, 1) 461 #parser.setFeature(xml.sax.handler.feature_string_interning, 1) 462 except xml.sax._exceptions.SAXNotSupportedException: pass 463 except xml.sax._exceptions.SAXNotRecognizedException:pass 464 465 #setting handler and start parsing 466 parser.setContentHandler(self) 467 parser.parse(cStringIO.StringIO(str(s))) 468 469 #return generated content 470 return self.content
471 472 473 if __name__ == "__main__": 474 f=open("example7.rdf","r") 475 s=f.read() 476 rdfparser=RDF0Parser() 477 content=rdfparser.parse(s) 478 print "#################################" 479 #print content 480 print content.pprint() 481 #result=rdfparser.encode(content) 482 #print result 483 #print "#################################" 484 #print rdfparser.parse(result) 485 486 487 """ 488 TODO 489 - RDF Collections 490 - Description namespace 491 """ 492