Attachment 'mcb_rdf.py'

Download

   1 from rdflib import Graph
   2 from rdflib import URIRef, Literal, BNode, Namespace
   3 from rdflib import RDF, RDFS
   4 
   5 import webcache
   6 from pprint import pprint
   7 
   8 
   9 class RDF_World:
  10 
  11     mcb2rdf_types = {"string" : 'string',
  12                      "number" : 'float',
  13                      "date" : 'dateTime',
  14                      "url" : 'anyURI',
  15                      'html' : 'string',
  16                      }
  17     
  18     def __init__(self, directory='.'):
  19         self.store = Graph()
  20         self.cache = webcache.WebCache(directory + "/pages.db",
  21                                        directory + "/times.db",
  22                                        24*60*60)  # Shared webpage cache
  23         
  24     def mcb2rdf_schema(self, schema):
  25         store = self.store
  26         id = schema.get('id')[0].strip()
  27         cls = URIRef("#" +id)
  28         store.add((cls, RDF.type, RDFS.Class))
  29         v = schema.get('label', [''])[0]
  30         if v:
  31             store.add((cls, RDFS.label, Literal(v)))
  32             
  33         for attribute in schema.get('attribute', []):
  34             key = attribute.get('key',[''])[0]
  35             if not key: continue
  36             print id + key
  37             attr = URIRef("#%s_%s" % (id, key))
  38             store.add((cls, RDFS.range, attr))
  39             store.add((attr,RDF.type, RDF.Property))
  40             label = attribute.get('label',[''])[0]
  41             if label:
  42                 store.add((attr, RDFS.label, Literal(label)))
  43 
  44             description = attribute.get('description',[''])[0]
  45             if description:
  46                 store.add((attr, RDFS.comment, Literal(description)))
  47 
  48             #required = attribute.get('label',[0])[0]
  49             #if required:
  50             #    store.add((attr, "owl:minCardinaly", Literal(1)))
  51 
  52             #multiple = attribute.get('label',[0])[1]
  53             #if not multiple:
  54             #    store.add((attr, "owl:maxCardinaly", Literal(1)))
  55 
  56             type = attribute.get('type',[''])[0].strip()
  57             if self.mcb2rdf_types.has_key(type):
  58                 store.add((attr, RDFS.Datatype,
  59                            Literal(self.mcb2rdf_types[type])))
  60             else:
  61                 pass #XXX
  62                 #    rdfs:range
  63 
  64             #"default" -> create entries
  65 
  66 
  67     def mcb2rdf(self, block):
  68         store = self.store
  69         b = BNode()
  70         
  71         for key, value in block.iteritems():
  72             if key=='type':
  73                 pass
  74             else:
  75                 pass
  76 
  77     def get_superclasses(self, cls, store):
  78         to_check = [cls]
  79         checked = []
  80         while to_check:
  81             check = to_check.pop()
  82             for cls in store.objects(check, RDFS.subClassOf):
  83                 if cls not in to_check and not cls in checked:
  84                     to_check.append(cls)
  85             checked.append(check)
  86         return checked
  87 
  88     def get_properties(self, cls_list, store):
  89         properties = {u'about' : RDF.about}
  90         for cls in cls_list:
  91             for property in store.subjects(RDFS.domain, cls):
  92                 local_name = property.split('#')
  93                 if len(local_name)>1:
  94                     local_name = local_name[-1].strip()
  95                 else:
  96                     local_name = property.split('/')[-1].lower()
  97                     if properties.has_key(local_name):
  98                         print "XXX", properties[local_name], "X", property
  99                 properties[local_name] = property
 100         return properties
 101 
 102     def mcb_rdf(self, mcb):
 103         store = Graph()
 104         store.parse("http://www.w3.org/1999/02/22-rdf-syntax-ns")
 105         store.parse("http://www.w3.org/2000/01/rdf-schema")
 106         
 107         for url in mcb.get('RDFFILE'):
 108             #file = self.cache.get_page(url)
 109             store.parse(url.strip())
 110 
 111         cls = mcb.get("CLASS", [''])[0].strip()
 112         if not cls: return
 113 
 114         for cls_node in store.subjects(RDF.type, RDFS.Class):
 115             if cls_node.endswith(cls):
 116                 cls = cls_node
 117                 break
 118             
 119         print cls
 120         inst = URIRef(mcb._url) #XXX id
 121         self.store.add((inst, RDF.type, cls))
 122         
 123         superclasses = self.get_superclasses(cls, store)
 124         superclasses.append(RDFS.Resource)
 125         properties = self.get_properties(superclasses, store)
 126         pprint(superclasses)
 127         print
 128         pprint(properties)
 129 
 130         for key, values in mcb.iteritems():
 131             key = key.lower()
 132             if properties.has_key(key):
 133                 property = properties[key]
 134                 #for po in store.predicate_objects(property):
 135                 #    print property, po
 136                 # XXX check property type!
 137                 for value in values:
 138                     self.store.add((inst, property, Literal(value)))
 139             else:
 140                  print key, "ignored X("
 141     
 142 
 143         print self.store.serialize(format="pretty-xml")

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2005-07-03 19:19:59, 11.1 KB) [[attachment:machineblock4.py]]
  • [get | view] (2005-07-03 19:10:49, 4.8 KB) [[attachment:mcb_rdf.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.