1
2
3 """
4 rdfsSubject.py
5
6 rdfsSubject is similar to rdfsSubject but includes more
7 processing and *magic* based on an `RDF Schema`__
8
9 __ ::http://www.w3.org/TR/rdf-schema/
10
11 Created by Philip Cooper on 2008-05-14.
12 Copyright (c) 2008 Openvest. All rights reserved.
13 """
14
15 from rdfalchemy import rdfSubject, RDF, RDFS, Namespace
16 from descriptors import *
17 from orm import mapper, allsub
18
19 from weakref import WeakValueDictionary
20 import re
21
22 OWL = Namespace("http://www.w3.org/2002/07/owl#")
23
24 _all_ = ['rdfsSubject','rdfsClass','rdfsProperty',
25 'owlObjectProperty','owlDatatypeProperty',
26 'owlSymetricProperty', 'owlTransitiveProperty'
27 'owlFunctionalProperty','owlInverseFunctionalProperty']
28
29
30 re_ns_n = re.compile(r'(.*[/#])(.*)')
34 __weakrefs = WeakValueDictionary()
35
37 return re.match(r'(.*[/#])(.*)',self.resUri).groups()
38
39 @classmethod
41 """return a generator for instances of this rdf:type
42 you can look in MyClass.rdf_type to see the predicate being used"""
43
44 beenthere = set([])
45 for i in cls.db.subjects(RDF.type, cls.rdf_type):
46 if not i in beenthere:
47 yield cls(i)
48 beenthere.add(i)
49
50
51 pySubClasses = allsub(cls)
52 for sub in pySubClasses:
53 for i in sub.ClassInstances():
54 if not i in beenthere:
55 yield i
56 beenthere.add(i)
57
58
59 dbSubClasses = rdfsClass(cls.rdf_type).transitive_subClasses
60 moreSubClasses = [dbsub.resUri for dbsub in dbSubClasses
61 if dbsub.resUri not in [pysub.rdf_type for pysub in pySubClasses]]
62 for sub in moreSubClasses:
63 for i in cls.db.subjects(RDF.type, sub):
64 if '' and not i in beenthere:
65 yield i
66 beenthere.add(i)
67
71 """rdfSbject with some RDF Schema addons
72 *Some* inferencing is implied
73 Bleading edge: be careful"""
74 rdf_type = RDFS.Class
75 comment = rdfSingle(RDFS.comment)
76 label = rdfSingle(RDFS.label)
77 subClassOf = rdfMultiple(RDFS.subClassOf, range_type = RDFS.Class)
78
79 @property
82
83 @property
86
87 @property
94
95
96
98 """Procude the text that might be used for a .py file
99 TODO: This code should probably move into the commands module since that's the only place it's used"""
100 ns,loc = self._splitname()
101 try:
102 prefix, qloc = self.db.qname(self.resUri).split(':')
103 except:
104 raise Exception("don't know how do handle a qname like %s" % self.db.qname(self.resUri))
105 prefix = prefix.upper()
106
107 if not visitedNS:
108 src = """
109 from rdfalchemy import rdfSubject, Namespace, URIRef
110 from rdfalchemy.rdfsSubject import rdfsSubject
111 from rdfalchemy.orm import mapper
112
113 """
114 for k,v in self.db.namespaces():
115 visitedNS[str(v)] = k.upper()
116 src += '%s = Namespace("%s")\n' % (k.upper().replace('-','_'),v)
117 else:
118 src = ""
119
120 mySupers = []
121 for mySuper in self.subClassOf:
122 sns, sloc = mySuper._splitname()
123 if ns == sns:
124 src += mySuper._emit_rdfSubject(visitedNS=visitedNS)
125 mySupers.append( sloc.replace('-','_') )
126
127
128
129 mySupers = ",".join(mySupers) or "rdfsSubject"
130 src += '\nclass %s(%s):\n'%(loc.replace('-','_'), mySupers)
131 src += '\t"""%s %s"""\n'%(self.label, self.comment)
132 src += '\trdf_type = %s["%s"]\n' % (visitedNS[ns],loc)
133
134
135 for p in self.properties:
136 pns, ploc = p._splitname()
137 ppy = '%s["%s"]' % (visitedNS[pns],ploc)
138 try:
139 assert str(p.range[RDF.type].resUri).endswith('Class')
140 rns, rloc = rdfsSubject(p.range)._splitname()
141 range_type = ', range_type = %s["%s"]' % (visitedNS[rns],rloc)
142 except Exception, e:
143 range_type = ''
144 src += '\t%s = rdfMultiple(%s%s)\n' % (ploc.replace('-','_') ,ppy,range_type)
145
146
147 src.replace("mapper()\n","")
148 src += "mapper()\n"
149
150 return src
151
158
172
180
185
193
197
201
205
206
207 mapper()
208