def intro_sparql(request): """ Introduction to using SPARQL to query an rdflib graph - http://code.google.com/p/rdflib/wiki/IntroSparql """ g = Graph() g.parse("http://bigasterisk.com/foaf.rdf") g.parse("http://www.w3.org/People/Berners-Lee/card.rdf") FOAF = Namespace("http://xmlns.com/foaf/0.1/") g.parse("http://danbri.livejournal.com/data/foaf") [ g.add((s, FOAF['name'], n)) for s, _, n in g.triples((None, FOAF['member_name'], None)) ] graph_as_list = [] """ The Graph.parse 'initNs' argument is a dictionary of namespaces to be expanded in the query string """ """ Example 'row': (rdflib.Literal('Dan Brickley', language=u'en', datatype=None), rdflib.Literal('Brad Fitzpatrick', language=u'en', datatype=None)) """ for row in g.query( 'SELECT ?aname ?bname \ WHERE {\ ?a foaf:knows ?b .\ ?a foaf:name ?aname .\ ?b foaf:name ?bname .\ }', initNs=dict(foaf=Namespace("http://xmlns.com/foaf/0.1/"))): exec "line = '%s knows %s'" % row row = row # explore... graph_as_list.append(line) context = { 'row': row, 'graph': graph_as_list, } return render_to_response('rdf/intro_sparql.html', context)
def len_graph(request): """ This Works...""" #store = Graph() #store.bind("contact", "http://www.example.com/contact#") #store.bind("person", "http://www.example.com/person#") #store.bind("xs", "http://www.w3.org/2001/XMLSchema#") #store.bind("rdfs", "http://www.w3.org/2000/01/rdf-schema#") #store.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#") #store.bind("owl", "http://www.w3.org/2002/07/owl#") # Declare namespaces to use. ns_sn = Namespace("http://www.snee.com/ns/misc#") ns_sd = Namespace("http://www.snee.com/docs/") ns_dc = Namespace("http://purl.org/dc/elements/1.1/") ns_pr = Namespace("http://prismstandard.org/1.0#") myfile = '/var/rdf/municipality.rdf' # Create storage object for triples. store = Graph() # Add triples to store. graph.add( (ns_sd["d1001"], ns_dc["title"], Literal("Sample Acrobat document"))) graph.add((ns_sd["d1001"], ns_dc["format"], Literal("PDF"))) graph.add((ns_sd["d1001"], ns_dc["creator"], Literal("Billy Shears"))) graph.add( (ns_sd["d1001"], ns_pr["publicationTime"], Literal("2002-12-19"))) graph.add((ns_sd["d1002"], ns_dc["title"], Literal("Sample RTF document"))) graph.add((ns_sd["d1002"], ns_dc["format"], Literal("RTF"))) graph.add((ns_sd["d1002"], ns_dc["creator"], Literal("Nanker Phelge"))) graph.add( (ns_sd["d1002"], ns_pr["publicationTime"], Literal("2002-12-15"))) graph.add( (ns_sd["d1003"], ns_dc["title"], Literal("Sample LaTeX document"))) graph.add((ns_sd["d1003"], ns_dc["format"], Literal("LaTeX"))) graph.add((ns_sd["d1003"], ns_dc["creator"], Literal("Richard Mutt"))) graph.add( (ns_sd["d1003"], ns_pr["publicationTime"], Literal("2002-12-16"))) graph.add((ns_sd["d1003"], ns_sn["quality"], Literal("pretty good"))) #store.parse (myfile) rdf_subjects = graph.subjects() rdf_predicates = graph.predicates() rdf_objects = graph.objects() select_predicate_by_subject = graph.predicates(subject=ns_sd["d1001"]) select_object_by_predicate = graph.objects(predicate=ns_dc["title"]) g = Graph() g.parse(myfile, format="xml") exec "html = 'the lenght of the graph is: %s'" % len(g) context = {'html': html, 'g': select_predicate_by_subject} return render_to_response('len_graph.html', context)
def __init__(self, graphs): """ Constructor takes a list of URLs that point to RDF/OWL files. :type graphs: list :param graphs: a list RDF/OWL files """ self.g = Graph() if type(graphs) == str: self.g.parse(graphs) else: for i in range(0, len(graphs)): self.g.parse(graphs[i]) # for testing - todo: load all namespaces from a merged graph in the code below self.owl = Namespace("http://www.w3.org/2002/07/owl#") self.rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") self.biblio = Namespace( "http://www.linguistics-ontology.org/bibliography/bibliography.owl#" ) self.goldbib = Namespace( "http://www.linguistics-ontology.org/bibliography/gold-bibliography.rdf#" ) self.gold = Namespace("http://purl.org/linguistics/gold/") self.rdfs = Namespace("http://www.w3.org/2000/01/rdf-schema#") self.bibtex = Namespace("http://purl.oclc.org/NET/nknouf/ns/bibtex#") self.person = Namespace( "http://www.linguistics-ontology.org/bibliography/person.rdf#") # get namespaces from graph self.namespaces = [] namespaces = self.g.namespaces()
def get_people(name_or_nick): model = get_model() sources = "" FOAF = Namespace("http://xmlns.com/foaf/0.1/") sources = model.subjects(FOAF['name'], Literal(name_or_nick)) if not sources: sources = model.subjects(FOAF['nick'], Literal(name_or_nick)) people = dict() for source in sources: people[source] = {} props = get_props() for prop in props.keys(): people[source][prop] = list() available = model.objects(source, URIRef(props[prop][1])) for i in available: people[source][prop].append(i) if len(people) < 1: sources = model.subjects(FOAF['nick'], Literal(name_or_nick)) for source in sources: people[source] = {} props = get_props() for prop in props.keys(): people[source][prop] = list() available = model.objects(source, URIRef(props[prop][1])) for i in available: people[source][prop].append(i) return people
def testAggregateSPARQL(): memStore = plugin.get('IOMemory', Store)() graph1 = Graph(memStore, URIRef("graph1")) graph2 = Graph(memStore, URIRef("graph2")) graph3 = Graph(memStore, URIRef("graph3")) for n3Str, graph in [(testGraph1N3, graph1), (testGraph2N3, graph2), (testGraph3N3, graph3)]: graph.parse(StringIO(n3Str), format='n3') print '-------------------testAggregateSPARQL()----------------------' print RDFS.RDFSNS print '---------------------------------------------------------------' graph4 = Graph(memStore, RDFS.RDFSNS) graph4.parse(RDFS.RDFSNS) #print graph4.serialize() G = ConjunctiveGraph(memStore) rt = G.query(sparqlQ) print '-------------------G.query(sparqlQ)----------------------' #print rt.serialize(format='xml') print '---------------------------------------------------------------' assert len(rt) > 1 #print rt.serialize(format='xml') LOG_NS = Namespace(u'http://www.w3.org/2000/10/swap/log#') rt = G.query(sparqlQ2, initBindings={u'?graph': URIRef("graph3")}) #print rt.serialize(format='json') assert rt.serialize('python')[0] == LOG_NS.N3Document, str(rt)
class RDF2Employee(object): """ RDF to employee """ zope.interface.implements(IEmployee) first_name = u'' last_name = u'' personnelNb = u'' email = u'' employment_start = u'' employment_end = u'' job_title = u'' organisation_code = u'' organisation_name = u'' manager = u'' NS = Namespace('http://intranet.eea.eu.int/inhousedir/staff-ns/1.0/') def __init__(self, empInfo): fieldNSmapping = {} for fieldName in getFieldNames(IEmployee): fieldNSmapping[self.NS[fieldName]] = fieldName # predicate, object for p, o in empInfo: fieldName = fieldNSmapping.get(p, None) if fieldName: setattr(self, fieldName, o.encode('utf8')) def __getitem__(self, key): return getattr(self, key, '') def __str__(self): result = '' for fieldName in getFieldNames(IEmployee): result += '%s: %s\n' % (fieldName, getattr(self, fieldName)) return result def items(self): """ Items """ return [(key, getattr(self, key, '')) for key in getFieldNames(IEmployee)]
from rdflib.Namespace import Namespace RDFSNS = Namespace("http://www.w3.org/2000/01/rdf-schema#") Resource = RDFSNS["Resource"] Class = RDFSNS["Class"] subClassOf = RDFSNS["subClassOf"] subPropertyOf = RDFSNS["subPropertyOf"] comment = RDFSNS["comment"] label = RDFSNS["label"] domain = RDFSNS["domain"] range = RDFSNS["range"] seeAlso = RDFSNS["seeAlso"] isDefinedBy = RDFSNS["isDefinedBy"] Literal = RDFSNS["Literal"] Container = RDFSNS["Container"] ContainerMembershipProperty = RDFSNS["ContainerMembershipProperty"] member = RDFSNS["member"] Datatype = RDFSNS["Datatype"]
sources = model.subjects(FOAF['nick'], Literal(name_or_nick)) for source in sources: people[source] = {} props = get_props() for prop in props.keys(): people[source][prop] = list() available = model.objects(source, URIRef(props[prop][1])) for i in available: people[source][prop].append(i) return people if __name__ == "__main__": mem_model = TripleStore() wordlist = get_words() FOAF = Namespace("http://xmlns.com/foaf/0.1/") GEO = Namespace("http://www.w3.org/2003/01/geo/wgs84_pos#") RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") DC = Namespace("http://purl.org/dc/elements/1.1/") CYC = Namespace("http://opencyc.sourceforge.net/daml/cyc.daml#") print "Image Annotation Tool" print "Enter the URI of a photo to annotate:" uri = raw_input("> ") mem_model.add((URIRef(uri), URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), URIRef("http://xmlns.com/foaf/0.1/Image"))) print "Should I add a thumbnail for this image, by adding '.sized' before the extension?" thumbnail = raw_input("> ") if thumbnail: thumb = "%ssized.%s" % (uri[:-3], uri[-3:]) mem_model.add((URIRef(uri), FOAF['thumbnail'], URIRef(thumb)))
from FuXi.Rete.TopDown import * from FuXi.Rete.Proof import ProofBuilder, PML, GMP_NS from FuXi.Rete.Magic import * from FuXi.Rete.SidewaysInformationPassing import * from rdflib.sparql.bison.Query import Prolog from rdflib.Namespace import Namespace from rdflib import plugin, RDF, RDFS, URIRef, URIRef, Literal, Variable from rdflib.store import Store from cStringIO import StringIO from rdflib.Graph import Graph, ReadOnlyGraphAggregate, ConjunctiveGraph from rdflib.syntax.NamespaceManager import NamespaceManager from rdflib.sparql.parser import parse as ParseSPARQL from rdflib.sparql.Algebra import ReduceGraphPattern import unittest, time, warnings, sys TEMPLATES = Namespace( 'http://code.google.com/p/fuxi/wiki/BuiltinSPARQLTemplates#') RDF_SERIALIZATION_FORMATS = [ 'xml', 'TriX', 'pretty-xml', 'turtle', 'n3', ] def main(): from optparse import OptionParser op = OptionParser( 'usage: %prog [options] factFile1 factFile2 ... factFileN') op.add_option('--why',
def testNamedGraph(self): from sets import Set OWL_NS = Namespace("http://www.w3.org/2002/07/owl#") rt = self.testGraph.query(sparqlQ4) self.assertEquals(Set(rt.serialize('python')),Set([OWL_NS.OntologyProperty,OWL_NS.Class,OWL_NS.Ontology,OWL_NS.AnnotationProperty,RDF.Property,RDFS.Class]))
from rdflib.Namespace import Namespace # The RDF Namespace # http://ilrt.org/discovery/2001/07/rdf-syntax-grammar/#section-Namespace RDFNS = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") # Syntax names RDF = RDFNS["RDF"] DESCRIPTION = RDFNS["Description"] ID = RDFNS["ID"] ABOUT = RDFNS["about"] PARSE_TYPE = RDFNS["parseType"] RESOURCE = RDFNS["resource"] LI = RDFNS["li"] NODE_ID = RDFNS["nodeID"] DATATYPE = RDFNS["datatype"] # RDF Classes SEQ = RDFNS["Seq"] BAG = RDFNS["Bag"] ALT = RDFNS["Alt"] STATEMENT = RDFNS["Statement"] PROPERTY = RDFNS["Property"] XMLLiteral = RDFNS["XMLLiteral"] LIST = RDFNS["List"] # RDF Properties SUBJECT = RDFNS["subject"] PREDICATE = RDFNS["predicate"] OBJECT = RDFNS["object"] TYPE = RDFNS["type"]
class TestStore(Graph): def __init__(self, expected): super(TestStore, self).__init__() self.expected = expected def add(self, (s, p, o)): if not isinstance(s, BNode) and not isinstance(o, BNode): if not (s, p, o) in self.expected: m = u"Triple not in expected result: %s, %s, %s" % (s.n3(), p.n3(), o.n3()) if verbose: write(m) #raise Exception(m) super(TestStore, self).add((s, p, o)) TEST = Namespace("http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#") import os def resolve(rel): return "http://www.w3.org/2000/10/rdf-tests/rdfcore/" + rel def _testPositive(uri, manifest): if verbose: write(u"TESTING: %s" % uri) result = 0 # 1=failed, 0=passed inDoc = first(manifest.objects(uri, TEST["inputDocument"])) outDoc = first(manifest.objects(uri, TEST["outputDocument"])) expected = Graph() if outDoc[-3:]==".nt": format = "nt" else: format = "xml"
def __init__(self, node, graph, inherited_state=None, base="", options=None): """ @param node: the current DOM Node @param graph: the RDFLib Graph @keyword inherited_state: the state as inherited from upper layers. This inherited_state is mixed with the state information retrieved from the current node. @type inherited_state: L{State.ExecutionContext} @keyword base: string denoting the base URI for the specific node. This is overridden by a possible C{@xml:base}, but it overrides the possible base inherited from the upper layers. Note: C{@xml:base} is not officially part of the XHTML+RDFa syntax, but this could/should handle by the DTD validation of the incoming document. The code itself is prepared for the C{@xml:base} usage, in accordnace with the reference (in the RDFa syntax document) to other XML dialects that might use it. @keyword options: invocation option @type options: L{Options<pyRdfa.Options>} """ #----------------------------------------------------------------- # settling the base # note that, strictly speaking, it is not necessary to add the base to the # context, because there is only one place to set it (<base> element of the <header>). # It is done because it is prepared for a possible future change in direction of # accepting xml:base on each element. # At the moment, it is invoked with a 'None' at the top level of parsing, that is # when the <base> element is looked for. if inherited_state: self.base = inherited_state.base self.warning_URI_ref = inherited_state.warning_URI_ref self.options = inherited_state.options else: # this is the branch called from the very top self.base = "" for bases in node.getElementsByTagName("base"): if bases.hasAttribute("href"): self.base = bases.getAttribute("href") continue if self.base == "": self.base = base if node.hasAttribute("xml:base"): self.base = node.getAttribute("xml:base") self.warning_URI_ref = URIRef(base) # this is just to play safe. I believe this branch should actually not happen... if options == None: from pyRdfa import Options self.options = Options() else: self.options = options # check the the presense of the @profile and or @version attribute for the RDFa profile... # (Not 100% sure that is necessary...) html = node.ownerDocument.documentElement if not (html.hasAttribute("version") and RDFa_VERSION == html.getAttribute("version")): # see if least the profile has been set # Find the <head> element head = None for index in range(0, html.childNodes.length - 1): if html.childNodes.item(index).nodeName == "head": head = html.childNodes.item(index) break if not (head != None and head.hasAttribute("profile") and RDFa_PROFILE in head.getAttribute("profile").strip().split()): self.add_warning( "Neither an RDFa profile nor an RFDa version is set") #----------------------------------------------------------------- # Settling the language tags # check first the lang or xml:lang attribute # RDFa does not allow the lang attribute. XHTML5 relies :-( on @lang; # I just want to be prepared here... if node.hasAttribute("lang"): self.lang = node.getAttribute("lang") if len(self.lang) == 0: self.lang = None elif node.hasAttribute("xml:lang"): self.lang = node.getAttribute("xml:lang") if len(self.lang) == 0: self.lang = None elif inherited_state: self.lang = inherited_state.lang else: self.lang = None #----------------------------------------------------------------- # Handling namespaces # First get the local xmlns declarations/namespaces stuff. dict = {} for i in range(0, node.attributes.length): attr = node.attributes.item(i) if attr.name.find('xmlns:') == 0: # yep, there is a namespace setting key = attr.localName if key != "": # exclude the top level xmlns setting... uri = attr.value # 1. create a new Namespace entry ns = Namespace(uri) # 2. 'bind' it in the current graph to # get a nicer output graph.bind(key, uri) # 3. Add an entry to the dictionary dict[key] = ns # See if anything has been collected at all. # If not, the namespaces of the incoming state is # taken over self.ns = {} if len(dict) == 0 and inherited_state: self.ns = inherited_state.ns else: if inherited_state: for k in inherited_state.ns: self.ns[k] = inherited_state.ns[k] # copying the newly found namespace, possibly overwriting # incoming values for k in dict: self.ns[k] = dict[k] else: self.ns = dict # see if the xhtml core vocabulary has been set self.xhtml_prefix = None for key in self.ns.keys(): if XHTML_URI == str(self.ns[key]): self.xhtml_prefix = key break if self.xhtml_prefix == None: if XHTML_PREFIX not in self.ns: self.ns[XHTML_PREFIX] = Namespace(XHTML_URI) self.xhtml_prefix = XHTML_PREFIX else: # the most disagreeable thing, the user has used # the prefix for something else... self.xhtml_prefix = XHTML_PREFIX + '_' + ( "%d" % random.randint(1, 1000)) self.ns[self.xhtml_prefix] = Namespace(XHTML_URI) graph.bind(self.xhtml_prefix, XHTML_URI) # extra tricks for unusual usages... # if the 'rdf' prefix is not used, it is artificially added... if "rdf" not in self.ns: self.ns["rdf"] = ns_rdf if "rdfs" not in self.ns: self.ns["rdfs"] = ns_rdfs # Final touch: setting the default namespace... if node.hasAttribute("xmlns"): self.defaultNS = node.getAttribute("xmlns") elif inherited_state and inherited_state.defaultNS != None: self.defaultNS = inherited_state.defaultNS else: self.defaultNS = None
class TextIndex(ConjunctiveGraph): """ An rdflib graph event handler than indexes text literals that are added to a another graph. This class lets you 'search' the text literals in an RDF graph. Typically in RDF to search for a substring in an RDF graph you would have to 'brute force' search every literal string looking for your substring. Instead, this index stores the words in literals into another graph whose structure makes searching for terms much less expensive. It does this by chopping up the literals into words, removing very common words (currently only in English) and then adding each of those words into an RDF graph that describes the statements in the original graph that the word came from. First, let's create a graph that will transmit events and a text index that will receive those events, and then subscribe the text index to the event graph: >>> e = ConjunctiveGraph() >>> t = TextIndex() >>> t.subscribe_to(e) When triples are added to the event graph (e) events will be fired that trigger event handlers in subscribers. In this case our only subscriber is a text index and its action is to index triples that contain literal RDF objects. Here are 3 such triples: >>> e.add((URIRef('a'), URIRef('title'), Literal('one two three'))) >>> e.add((URIRef('b'), URIRef('title'), Literal('two three four'))) >>> e.add((URIRef('c'), URIRef('title'), Literal('three four five'))) Of the three literal objects that were added, they all contain five unique terms. These terms can be queried directly from the text index: >>> t.term_strings() == set(['four', 'five', 'three', 'two', 'one']) True Now we can search for statement that contain certain terms. Let's search for 'one' which occurs in only one of the literals provided, 'a'. This can be queried for: >>> t.search('one') set([(rdflib.URIRef('a'), rdflib.URIRef('title'), None)]) 'one' and 'five' only occur in one statement each, 'two' and 'four' occur in two, and 'three' occurs in three statements: >>> len(list(t.search('one'))) 1 >>> len(list(t.search('two'))) 2 >>> len(list(t.search('three'))) 3 >>> len(list(t.search('four'))) 2 >>> len(list(t.search('five'))) 1 Lets add some more statements with different predicates. >>> e.add((URIRef('a'), URIRef('creator'), Literal('michel'))) >>> e.add((URIRef('b'), URIRef('creator'), Literal('Atilla the one Hun'))) >>> e.add((URIRef('c'), URIRef('creator'), Literal('michel'))) >>> e.add((URIRef('d'), URIRef('creator'), Literal('Hun Mung two'))) Now 'one' occurs in two statements: >>> assert len(list(t.search('one'))) == 2 And 'two' occurs in three statements, here they are: >>> t.search('two') set([(rdflib.URIRef('d'), rdflib.URIRef('creator'), None), (rdflib.URIRef('a'), rdflib.URIRef('title'), None), (rdflib.URIRef('b'), rdflib.URIRef('title'), None)]) The predicates that are searched can be restricted by provding an argument to 'search()': >>> t.search('two', URIRef('creator')) set([(rdflib.URIRef('d'), rdflib.URIRef('creator'), None)]) >>> t.search('two', URIRef(u'title')) set([(rdflib.URIRef('a'), rdflib.URIRef('title'), None), (rdflib.URIRef('b'), rdflib.URIRef('title'), None)]) You can search for more than one term by simply including it in the query: >>> t.search('two three', URIRef(u'title')) set([(rdflib.URIRef('c'), rdflib.URIRef('title'), None), (rdflib.URIRef('a'), rdflib.URIRef('title'), None), (rdflib.URIRef('b'), rdflib.URIRef('title'), None)]) The above query returns all the statements that contain 'two' OR 'three'. For the documents that contain 'two' AND 'three', do an intersection of two queries: >>> t.search('two', URIRef(u'title')).intersection(t.search(u'three', URIRef(u'title'))) set([(rdflib.URIRef('a'), rdflib.URIRef('title'), None), (rdflib.URIRef('b'), rdflib.URIRef('title'), None)]) Intersection two queries like this is probably not the most efficient way to do it, but for reasonable data sets this isn't a problem. Larger data sets will want to query the graph with sparql or something else more efficient. In all the above queries, the object of each statement was always 'None'. This is because the index graph does not store the object data, that would make it very large, and besides the data is available in the original data graph. For convenience, a method is provides to 'link' an index graph to a data graph. This allows the index to also provide object data in query results. >>> t.link_to(e) >>> set([str(i[2]) for i in t.search('two', URIRef(u'title')).intersection(t.search(u'three', URIRef(u'title')))]) == set(['two three four', 'one two three']) True You can remove the link by assigning None: >>> t.link_to(None) Unindexing means to remove statments from the index graph that corespond to a statement in the data graph. Note that while it is possible to remove the index information of the occurances of terms in statements, it is not possible to remove the terms themselves, terms are 'absolute' and are never removed from the index graph. This is not a problem since languages have finite terms: >>> e.remove((URIRef('a'), URIRef('creator'), Literal('michel'))) >>> e.remove((URIRef('b'), URIRef('creator'), Literal('Atilla the one Hun'))) >>> e.remove((URIRef('c'), URIRef('creator'), Literal('michel'))) >>> e.remove((URIRef('d'), URIRef('creator'), Literal('Hun Mung two'))) Now 'one' only occurs in one statement: >>> assert len(list(t.search('one'))) == 1 And 'two' only occurs in two statements, here they are: >>> t.search('two') set([(rdflib.URIRef('a'), rdflib.URIRef('title'), None), (rdflib.URIRef('b'), rdflib.URIRef('title'), None)]) The predicates that are searched can be restricted by provding an argument to 'search()': >>> t.search('two', URIRef(u'creator')) set([]) >>> t.search('two', URIRef(u'title')) set([(rdflib.URIRef('a'), rdflib.URIRef('title'), None), (rdflib.URIRef('b'), rdflib.URIRef('title'), None)]) """ linked_data = None text_index = Namespace('http://rdflib.net/text_index#') term = Namespace('http://rdflib.net/text_index#')["term"] termin = Namespace('http://rdflib.net/text_index#')["termin"] def __init__(self, store='default'): super(TextIndex, self).__init__(store) def add_handler(self, event): if type(event.triple[2]) is Literal: self.index(event.triple) def remove_handler(self, event): if type(event.triple[2]) is Literal: self.unindex(event.triple) def index(self, (s, p, o)): # this code is tricky so it's annotated. unindex is the reverse of this method. if type( o ) is Literal: # first, only index statements that have a literal object for word in stopper( splitter(o)): # split the literal and remove any stopwords word = Literal( word) # create a new literal for each word in the object # if that word already exists in the statement # loop over each context the term occurs in if self.value(predicate=self.term, object=word, any=True): for t in set(self.triples((None, self.term, word))): t = t[0] # if the graph does not contain an occurance of the term in the statement's subject # then add it if not (t, self.termin, s) in self: self.add((t, self.termin, s)) # ditto for the predicate if not (p, t, s) in self: self.add((p, t, s)) else: # if the term does not exist in the graph, add it, and the references to the statement. # t gets used as a predicate, create identifier accordingly (AKA can't be a BNode) h = md5(word) h.update(s) h.update(p) t = self.text_index["term_%s" % h.hexdigest()] self.add((t, self.term, word)) self.add((t, self.termin, s)) self.add((p, t, s))
from rdflib.Literal import Literal from rdflib.Namespace import Namespace __version__ = "$Id: RDFaParser.py 1185 2007-07-12 16:46:36Z whit $" rdfa_attribs = [ "about", "property", "rel", "rev", "href", "content", "role", "id" ] reserved_links = [ 'alternate', 'stylesheet', 'start', 'next', 'prev', 'contents', 'index', 'glossary', 'copyright', 'chapter', 'section', 'subsection', 'appendix', 'help', 'bookmark' ] xhtml = Namespace("http://www.w3.org/1999/xhtml") xml = Namespace("http://www.w3.org/XML/1998/namespace") rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") class RDFaSink(object): def __init__(self, graph): self.graph = graph def __str__(self): return self.graph.serialize(format="pretty-xml") def triple(self, s, p, o): self.graph.add((s, p, o)) def prefix(self, prefix, ns):
from FuXi.Rete.Proof import ProofBuilder, PML, GMP_NS from rdflib.Namespace import Namespace from rdflib import plugin, RDF, RDFS, URIRef, URIRef from rdflib.OWL import FunctionalProperty from rdflib.store import Store from cStringIO import StringIO from rdflib.Graph import Graph, ReadOnlyGraphAggregate, ConjunctiveGraph from rdflib.syntax.NamespaceManager import NamespaceManager from glob import glob from rdflib.sparql.parser import parse import unittest, os, time, itertools RDFLIB_CONNECTION = '' RDFLIB_STORE = 'IOMemory' CWM_NS = Namespace("http://cwmTest/") DC_NS = Namespace("http://purl.org/dc/elements/1.1/") STRING_NS = Namespace("http://www.w3.org/2000/10/swap/string#") MATH_NS = Namespace("http://www.w3.org/2000/10/swap/math#") FOAF_NS = Namespace("http://xmlns.com/foaf/0.1/") OWL_NS = Namespace("http://www.w3.org/2002/07/owl#") TEST_NS = Namespace("http://metacognition.info/FuXi/DL-SHIOF-test.n3#") LOG = Namespace("http://www.w3.org/2000/10/swap/log#") RDF_TEST = Namespace('http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#') OWL_TEST = Namespace('http://www.w3.org/2002/03owlt/testOntology#') LIST = Namespace('http://www.w3.org/2000/10/swap/list#') queryNsMapping = { 'test': 'http://metacognition.info/FuXi/test#', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'foaf': 'http://xmlns.com/foaf/0.1/',
#! /usr/bin/python # makeTriples.py: demonstrate the creation of an RDFLib TripleStore from rdflib.Graph import ConjunctiveGraph, Graph from rdflib.Literal import Literal from rdflib.Namespace import Namespace from rdflib.sparql import sparqlGraph ns_fich = Namespace('fich="http://siva.uco.es/onto/owl/fichas-01#') ns_rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") ns_xs = Namespace("http://www.w3.org/2001/XMLSchema#") ns_rdfs = Namespace("http://www.w3.org/2000/01/rdf-schema#") ns_owl = Namespace("http://siva.uco.es/onto/owl/personas-01#") ns_dam = Namespace("http://www.daml.org/2001/03/daml+oil#") ns_dc = Namespace("http://purl.org/dc/elements/1.1/") ns_per = Namespace("http://siva.uco.es/onto/owl/personas-01#") ns_idp = Namespace("http://siva.uco.es/onto/owl/identificacion-personajes#") store = Graph() #store.parse('identificacion-personajes.rdf') store.parse('identificacion-personajes.rdf') store.parse('personas.rdf') #print store.serialize() def objetos():
#note, fix rdflib imports when rdflib-2.5 is finalized #rdflib-2.5 #from rdflib.namespace import Namespace from rdflib.Namespace import Namespace """ Various widely used namespaces are listed in the namespaces module. """ OWLNS = Namespace("http://www.w3.org/2002/07/owl#") LINGNS = Namespace("http://purl.org/linguistics/") GOLDNS = Namespace("http://purl.org/linguistics/gold#") GOLDCOMMNS = Namespace("http://purl.org/linguistics/goldcomm/") TERMSETNS = Namespace("http://purl.org/linguistics/goldcomm/termset/") DATANS = Namespace("http://purl.org/linguistics/goldcomm/data/")
from rdflib.Literal import Literal from rdflib.BNode import BNode as bNode class Graph(TripleStore): def theObject(self, subj, pred): objects = tuple(self.objects(subj, pred)) if len(objects) == 1: return objects[0] elif len(objects) == 0: return None else: raise "Some kind of an error" SPARQL = Namespace('http://www.w3.org/2000/10/swap/grammar/sparql#') BNF = Namespace('http://www.w3.org/2000/10/swap/grammar/bnf#') RDF = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') def abbr(prodURI): return prodURI.split('#').pop() class N3Metaparser(object): def __init__(self, verbose=False): self.branches = {} self.regexps = {} self.verbose = verbose self.todo = []
from rdflib.Namespace import Namespace RDFNS = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") # Syntax names RDF = RDFNS["RDF"] Description = RDFNS["Description"] ID = RDFNS["ID"] about = RDFNS["about"] parseType = RDFNS["parseType"] resource = RDFNS["resource"] li = RDFNS["li"] nodeID = RDFNS["nodeID"] datatype = RDFNS["datatype"] # RDF Classes Seq = RDFNS["Seq"] Bag = RDFNS["Bag"] Alt = RDFNS["Alt"] Statement = RDFNS["Statement"] Property = RDFNS["Property"] XMLLiteral = RDFNS["XMLLiteral"] List = RDFNS["List"] # RDF Properties subject = RDFNS["subject"] predicate = RDFNS["predicate"] object = RDFNS["object"] type = RDFNS["type"] value = RDFNS["value"] first = RDFNS["first"]
""" read XMP xml files """ # see http://www.xml.com/lpt/a/1107 # see also raptor-utls # rapper -o ntriples http://planetrdf.com/guide/rss.rdf # rapper -o ntriples ../tests/fixture/xmp/sample_xmp.rdf from rdflib.TripleStore import TripleStore from rdflib.Namespace import Namespace ns_dc = Namespace("http://purl.org/dc/elements/1.1/") ns_pr = Namespace("http://prismstandard.org/1.0#") store = TripleStore() store.load("../tests/fixture/xmp/sample_xmp.rdf") # print dir(store) quit() # For all triples that have prism:publicationTime as their predicate, for s, o in store.subject_objects(ns_pr["publicationTime"]): # if the triples' object is greater than the cutoff date, if o > cutOffDate: # print the date, author name, and title. print o, for object in store.objects(s, ns_dc["creator"]): print object + ": ", for object in store.objects(s, ns_dc["title"]): print object
@var __empty_bnode: I{The} Bnode to be associated with the CURIE of the form "C{_:}". """ """ $Id: State.py,v 1.41 2008/05/06 11:38:42 ivan Exp $ $Date: 2008/05/06 11:38:42 $ """ from rdflib.RDF import RDFNS as ns_rdf from rdflib.RDFS import RDFSNS as ns_rdfs from rdflib.RDFS import comment as rdfs_comment from rdflib.Namespace import Namespace from rdflib.URIRef import URIRef from rdflib.Literal import Literal from rdflib.BNode import BNode _XSD_NS = Namespace(u'http://www.w3.org/2001/XMLSchema#') import re import random import urlparse RDFa_PROFILE = "http://www.w3.org/1999/xhtml/vocab" RDFa_VERSION = "XHTML+RDFa 1.0" usual_protocols = ["http", "https", "mailto", "ftp", "urn", "gopher", "tel"] ####Predefined @rel/@rev/@property values # predefined values for the @rel and @rev values. These are considered to be part of a specific # namespace, defined by the RDFa document. XHTML_PREFIX = "xhv" XHTML_URI = "http://www.w3.org/1999/xhtml/vocab#"
from rdflib.BNode import BNode from rdflib.URIRef import URIRef from rdflib.Literal import Literal import OwlAxiomReasoner import time import os import os.path import re #import random resultsPrefix = ",results-" pubUriPrefix = "" FOAF = Namespace("http://xmlns.com/foaf/0.1/") OTEST = Namespace("http://www.w3.org/2002/03owlt/testOntology#") TRES = Namespace("http://www.w3.org/2002/03owlt/resultsOntology#") RTEST = Namespace("http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#") DC = Namespace("http://purl.org/dc/elements/1.0/") RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") RDFS = Namespace("http://www.w3.org/2000/01/rdf-schema#") OWL = Namespace("http://www.w3.org/2002/07/owl#") def only(iter): result = None for x in iter: if result is None: result = x else:
else: tmpfile = mkstemp()[1] data = convert(form['description'].value, 'turtle', 'ntriples', infile=tmpfile) graph = TripleStore() graph.parse(StringIO(data), format='nt') thisURI = URIRef('http://%s%s' % (os.environ['HTTP_HOST'], os.environ['REQUEST_URI'])) graphNode = URIRef('file://%s' % tmpfile) FOAF = Namespace("http://xmlns.com/foaf/0.1/") try: bnode = graph.objects(graphNode, FOAF["primaryTopic"]).next() except StopIteration: print 'Content-Type: text/plain\r' print '\r' print 'no primary topic given' graph2 = TripleStore() for (s, p, o) in graph.triples((None, None, None)): if (s, p, o) == (graphNode, FOAF["primaryTopic"], bnode): continue if s == bnode: s = thisURI if o == bnode: o = thisURI
from FuXi.Rete.RuleStore import SetupRuleStore from FuXi.Horn.HornRules import HornFromN3 from FuXi.Rete.Proof import ImmutableDict from FuXi.SPARQL.BackwardChainingStore import * from FuXi.Rete.Util import setdict from rdflib.Namespace import Namespace from rdflib.Collection import Collection from rdflib import RDF, RDFS, URIRef, Variable, BNode, Literal from cStringIO import StringIO from rdflib.Graph import Graph, ReadOnlyGraphAggregate from rdflib.syntax.NamespaceManager import NamespaceManager from rdflib.sparql.parser import parse from rdflib.OWL import OWLNS from amara.lib import U DC = Namespace('http://purl.org/dc/elements/1.1/') MANIFEST = Namespace( 'http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#') QUERY = Namespace('http://www.w3.org/2001/sw/DataAccess/tests/test-query#') SD = Namespace('http://www.w3.org/ns/sparql-service-description#') TEST = Namespace( 'http://www.w3.org/2009/sparql/docs/tests/data-sparql11/entailment/manifest#' ) STRING = Namespace('http://www.w3.org/2000/10/swap/string#') ENT = Namespace('http://www.w3.org/ns/entailment/') EARL = Namespace('http://www.w3.org/ns/earl#') MY_FOAF = Namespace('http://metacognition.info/public_rdf/n3/foaf.ttl#') SUPPORTED_ENTAILMENT = [ENT.RDF, ENT.RIF, ENT.RDFS, ENT['OWL-RDF-Based']] COMPLETION_RULES = ["sparqldl-02"]
# # $Date: 2005/04/02 07:29:30 $, by $Author: ivan $, $Revision: 1.1 $ # """ """ import sys, os, time, datetime from rdflib.constants import RDFNS as ns_rdf from rdflib.constants import RDFSNS as ns_rdfs #from rdflib.sparql import ns_dc as ns_dc #from rdflib.sparql import ns_owl as ns_owl from rdflib.sparql.sparql import type_integer from rdflib.sparql.sparql import type_double from rdflib.sparql.sparql import type_float from rdflib.sparql.sparql import type_decimal from rdflib.sparql.sparql import type_dateTime from rdflib.Namespace import Namespace ns_foaf = Namespace("http://xmlns.com/foaf/0.1/") ns_ns = Namespace("http://example.org/ns#") ns_book = Namespace("http://example.org/book") ns_person = Namespace("http://example.org/person#") ns_dt = Namespace("http://example.org/datatype#") ns_dc0 = Namespace("http://purl.org/dc/elements/1.0/") ns_dc = Namespace("http://purl.org/dc/elements/1.1/") ns_vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")
from rdflib.constants import SUBJECT, PREDICATE, OBJECT from rdflib.constants import TYPE, VALUE, FIRST, REST from rdflib.constants import NIL from rdflib.syntax.xml_names import is_ncname NODE_ELEMENT_EXCEPTIONS = CORE_SYNTAX_TERMS + [LI,] + OLD_TERMS NODE_ELEMENT_ATTRIBUTES = [ID, NODE_ID, ABOUT] PROPERTY_ELEMENT_EXCEPTIONS = CORE_SYNTAX_TERMS + [DESCRIPTION,] + OLD_TERMS PROPERTY_ATTRIBUTE_EXCEPTIONS = CORE_SYNTAX_TERMS + [DESCRIPTION, LI] + OLD_TERMS PROPERTY_ELEMENT_ATTRIBUTES = [ID, RESOURCE, NODE_ID] XMLNS = Namespace("http://www.w3.org/XML/1998/namespace") BASE = (XMLNS, "base") LANG = (XMLNS, "lang") class BagID(URIRef): __slots__ = ['li'] def __init__(self, val): super(URIRef, self).__init__(val) self.li = 0 def next_li(self): self.li += 1 return URIRef(RDFNS + "_%s" % self.li)
import logging import sys from nose.plugins import Plugin from nose.suite import TestModule from rdflib import URIRef, BNode, Literal from rdflib import RDF, RDFS from rdflib.Graph import Graph from rdflib.Namespace import NamespaceDict as Namespace from rdflib.util import date_time log = logging.getLogger(__name__) EARL = Namespace("http://www.w3.org/ns/earl#") class EARLPlugin(Plugin): """ Activate the EARL plugin to generate a report of the test results using EARL. """ name = 'EARL' def begin(self): self.graph = Graph() self.graph.bind("earl", EARL.uri) def finalize(self, result): # TODO: add plugin options for specifying where to send