def resourcePropertyElt(self, e): n = e.children[0] self.nodeElement(n) self.triple(e.parent.subject, self.uri(e.URI), n.subject) if e.attrs.has_key(rdf.ID): i = self.uri(urijoin(e.base, ('#' + e.attrs[rdf.ID]))) self.reify(i, e.parent.subject, self.uri(e.URI), n.subject)
def __init__(self, uri, sink, baseURI=False): super(N3Processor, self).__init__(uri, branches, regexps) if baseURI is False: self.baseURI = uri else: self.baseURI = baseURI self.sink = sink self.bindings = {'': urijoin(self.baseURI, '#')} self.counter = 0 self.prefix = False self.userkeys = False self.anonsubj = False self.litinfo = False self.forAll = False self.forSome = False self.universals = {} self.existentials = {} self.formulae = [] self.labels = [] self.mode = [] self.triples = [] self.pathmode = 'path' self.paths = [] self.lists = [] self.bnodes = {}
def emptyPropertyElt(self, e): if e.attrs.keys() in ([], [rdf.ID]): r = self.literal(e.text, e.language) # was o self.triple(e.parent.subject, self.uri(e.URI), r) else: if e.attrs.has_key(rdf.resource): r = self.uri(urijoin(e.base, e[rdf.resource])) elif e.attrs.has_key(rdf.nodeID): r = self.bNode(e[rdf.nodeID]) else: r = self.bNode() for a in filter(lambda n: n not in self.disallowed, e.attrs.keys()): if a != rdf.type: o = self.literal(e.attrs[a], e.language) self.triple(r, self.uri(a), o) else: self.triple(r, self.uri(rdf.type), self.uri(e.attrs[a])) self.triple(e.parent.subject, self.uri(e.URI), r) if e.attrs.has_key(rdf.ID): i = self.uri(urijoin(e.base, ('#' + e.attrs[rdf.ID]))) self.reify(i, e.parent.subject, self.uri(e.URI), r)
def uri(self, tok): u = URIRef(urijoin(self.baseURI, tok)) if self.universals.has_key(u): formula, var = self.universals[u] if formula in self.formulae: return var if self.existentials.has_key(u): # @@ elif? formula, bnode = self.existentials[u] if formula in self.formulae: return bnode return u
def parseTypeResourcePropertyElt(self, e): n = self.bNode() self.triple(e.parent.subject, self.uri(e.URI), n) if e.attrs.has_key(rdf.ID): e.subject = i = self.uri(urijoin(e.base, ('#' + e.attrs[rdf.ID]))) self.reify(i, e.parent.subject, self.uri(e.URI), n) c = Element((rdf, 'Description'), e.qname, e.attrs, e.parent, e.base) c.subject = n for child in e.children: child.parent = c c.children += [child] self.nodeElement(c)
def nodeElement(self, e): assert e.URI not in self.disallowed, "Disallowed element used as node" if e.attrs.has_key(rdf.ID): e.subject = self.uri(urijoin(e.base, "#" + e[rdf.ID])) elif e.attrs.has_key(rdf.about): e.subject = self.uri(urijoin(e.base, e[rdf.about])) elif e.attrs.has_key(rdf.nodeID): e.subject = self.bNode(e[rdf.nodeID]) elif not hasattr(e, 'subject'): e.subject = self.bNode() if e.URI != rdf.Description: self.triple(e.subject, self.uri(rdf.type), self.uri(e.URI)) if e.attrs.has_key(rdf.type): self.triple(e.subject, self.uri(rdf.type), self.uri(e[rdf.type])) for attr in e.attrs.keys(): if attr not in self.disallowed + [rdf.type]: objt = self.literal(e[attr], e.language) self.triple(e.subject, self.uri(attr), objt) for element in e.children: self.propertyElt(element)
def parseTypeLiteralOrOtherPropertyElt(self, e): o = self.literal(e.xtext[1], e.language, rdf.XMLLiteral) self.triple(e.parent.subject, self.uri(e.URI), o) if e.attrs.has_key(rdf.ID): e.subject = i = self.uri(urijoin(e.base, ('#' + e.attrs[rdf.ID]))) self.reify(i, e.parent.subject, self.uri(e.URI), o)
def literalPropertyElt(self, e): o = self.literal(e.text, e.language, e.attrs.get(rdf.datatype)) self.triple(e.parent.subject, self.uri(e.URI), o) if e.attrs.has_key(rdf.ID): i = self.uri(urijoin(e.base, ('#' + e.attrs[rdf.ID]))) self.reify(i, e.parent.subject, self.uri(e.URI), o)