class owlObjectProperty(rdfsProperty): rdf_type = OWL.ObjectProperty range = rdfSingle(RDFS.range, range_type=RDFS.Class) inverseOf = rdfSingle(OWL.inverseOf, range_type=OWL.ObjectProperty) default_descriptor = rdfMultiple
class rdfsProperty(rdfsSubject): rdf_type = RDF.Property domain = rdfSingle(RDFS.domain, range_type=RDFS.Class) range = rdfSingle(RDFS.range) subPropertyOf = rdfMultiple(RDFS.subPropertyOf) default_descriptor = rdfMultiple
class owlDatatypeProperty(rdfsProperty): rdf_type = OWL.DatatypeProperty range = rdfSingle(RDFS.range, range_type=RDFS.Class) default_descriptor = rdfMultiple
class rdfsClass(rdfsSubject): """rdfSbject with some RDF Schema addons *Some* inferencing is implied Bleading edge: be careful""" rdf_type = RDFS.Class comment = rdfSingle(RDFS.comment) label = rdfSingle(RDFS.label) subClassOf = rdfMultiple(RDFS.subClassOf, range_type=RDFS.Class) @property def transitive_subClassOf(self): return [ rdfsClass(s) for s in self.db.transitive_objects(self.resUri, RDFS.subClassOf) ] @property def transitive_subClasses(self): return [ rdfsClass(s) for s in self.db.transitive_subjects(RDFS.subClassOf, self.resUri) ] @property def properties(self): # this doesn't get the rdfsProperty subclasses # return list(rdfsProperty.filter_by(domain=self.resUri)) # TODO: why iterate all rdfsProperty subclasses # try self.db.subjects(RDFS.domain,self.resUri) return [x for x in rdfsProperty.ClassInstances() if x.domain == self] def _emit_rdfSubject(self, visitedNS={}, visitedClass=set([])): """ Produce the text that might be used for a .py file TODO: This code should probably move into the commands module since that's the only place it's used """ ns, loc = self._splitname() try: prefix, qloc = self.db.qname(self.resUri).split(':') except: raise Exception("don't know how to handle a qname like %s" % (self.db.qname(self.resUri))) prefix = prefix.upper() if not visitedNS: src = """ from rdfalchemy import rdfSubject, Namespace, URIRef from rdfalchemy.rdfsSubject import rdfsSubject from rdfalchemy.orm import mapper """ for k, v in self.db.namespaces(): visitedNS[str(v)] = k.upper() src += '%s = Namespace("%s")\n' % (k.upper().replace('-', '_'), v) else: src = "" mySupers = [] for mySuper in self.subClassOf: sns, sloc = mySuper._splitname() if ns == sns: src += mySuper._emit_rdfSubject(visitedNS=visitedNS) mySupers.append(sloc.replace('-', '_')) mySupers = ",".join(mySupers) or "rdfsSubject" src += '\nclass %s(%s):\n' % (loc.replace('-', '_'), mySupers) src += '\t"""%s %s"""\n' % (self.label, self.comment) src += '\trdf_type = %s["%s"]\n' % (visitedNS[ns], loc) for p in self.properties: pns, ploc = p._splitname() ppy = '%s["%s"]' % (visitedNS[pns], ploc) try: assert str(p.range[RDF.type].resUri).endswith('Class') rns, rloc = rdfsSubject(p.range)._splitname() range_type = ', range_type = %s["%s"]' % (visitedNS[rns], rloc) except Exception: range_type = '' src += '\t%s = rdfMultiple(%s%s)\n' % (ploc.replace( '-', '_'), ppy, range_type) # Just want this once at the end src.replace("mapper()\n", "") src += "mapper()\n" return src