def testWritesToSeparateGraph(self): g1 = ConjunctiveGraph() g2 = ConjunctiveGraph() c = CommandLog(Graph2(g1, initNs=INITNS), Graph2(g2, initNs=INITNS)) c.addCommand(CMD['c1'], dateTime('18:00:00'), USER['drewp']) self.assertEqual(len(g1), 0) self.assert_(len(g2) > 0)
def rdf(self): """ RDF """ if self._rdf: return self._rdf data = self.context if not hasattr(data, 'read'): if callable(data.data): data = StringInputSource(data.data()) else: data = StringInputSource(data.data) try: self._rdf = ConjunctiveGraph().parse(data) except Exception: s = data.getByteStream() # this is a StringIO instance s.seek(0) with tempfile.NamedTemporaryFile(prefix="rdflib_staff_log", delete=False) as f: f.write(s.read()) self.context.error_log.raising(sys.exc_info()) self.validDatas = False self._rdf = ConjunctiveGraph() return self._rdf
def getGraph(): """ get the main graph, including data from trains.n3 on disk """ graph = ConjunctiveGraph() graph.parse("trains.n3", format="n3", publicID=TT['disk#context']) return graph
def make_rdf_graph(movies): mg=ConjunctiveGraph() mg.bind('fb',FB) mg.bind('dc',DC) for movie in movies: # Make a movie node movie_node=IVA_MOVIE[movie['id']] mg.add((movie_node,DC['title'],Literal(movie['title']))) # Make the director node, give it a name and link it to the movie dir_node=IVA_PERSON[movie['director']['id']] mg.add((movie_node,FB['film.film.directed_by'],dir_node)) mg.add((dir_node,DC['title'],Literal(movie['director']['name']))) for actor in movie['actors']: # The performance node is a blank node -- it has no URI performance=BNode() # The performance is connected to the actor and the movie actor_node=IVA_PERSON[actor['id']] mg.add((actor_node,DC['title'],Literal(actor['name']))) mg.add((performance,FB['film.performance.actor'],actor_node)) # If you had the name of the role, you could also add it to the # performance node, e.g. # mg.add((performance,FB['film.performance.role'],Literal('Carrie Bradshaw'))) mg.add((movie_node,FB['film.film.performances'],performance)) return mg
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)
def readGraphs(): g = ConjunctiveGraph() # this file should only be reread when it changes g.parse("../config.n3", format="n3") dl = [] startTime = time.time() for uri in [ "http://bang:9055/graph", "http://bang:9069/graph", "http://bang:9070/graph", "http://bang:9072/bang-9002/processStatus", "http://bang:9072/bang/processStatus", "http://bang:9072/dash/processStatus", "http://bang:9072/slash-11000/processStatus", "http://bang:9072/slash/processStatus", "http://bang:9072/star/processStatus", "http://bang:9075/graph", ]: # this ought to not reparse the ones that are 304 not modified d = getPage(uri) def done(trig, uri): g.addN(parseTrig(trig)) print "%s done in %.02fms" % (uri, 1000 * (time.time() - startTime)) d.addCallback(done, uri) dl.append(d) return defer.DeferredList(dl).addCallback(lambda result: g)
def __init__(self, store, edb, derivedPredicates=None, idb=None, DEBUG=False, nsBindings={}, templateMap=None, identifyHybridPredicates=False, hybridPredicates=None, existentialsInHeads=False, toldBNode=False, addRIFFacts=False, embedOWL=False): self.toldBNode = toldBNode self.existentialInHead = existentialsInHeads self.dataset = store hybridPredicates = hybridPredicates if hybridPredicates else [] if hasattr(store, '_db'): self._db = store._db self.idb = idb if idb else set() self.edb = edb for rifUri in edb.query(RIF_REFERENCE_QUERY): try: from FuXi.Horn.RIFCore import RIFCoreParser print rifUri if rifUri in map(lambda i: i.identifier, ConjunctiveGraph(edb.store).contexts()): if DEBUG: print "RIF in RDF is in named graph %s" % rifUri.n3() rif_parser = RIFCoreParser(graph=Graph(edb.store, rifUri), debug=DEBUG, owlEmbeddings=embedOWL) else: if DEBUG: print "RIF / XML is remote" rif_parser = RIFCoreParser(location=rifUri, debug=DEBUG, owlEmbeddings=embedOWL) rules, facts = rif_parser.getRuleset() if addRIFFacts and facts: #Add any ground facts in the referenced RIF graph #to the edb if DEBUG: print "Added %s statements from RIF document" % len( facts) print map(BuildUnitermFromTuple, facts) if isinstance(self.edb, ConjunctiveGraph): for fact in facts: self.edb.add(fact) else: self.edb.addN(map(lambda i: (i + (self.edb, )), facts)) self.idb.update(rules) except ImportError, e: raise Exception( "Missing 3rd party libraries for RIF processing: %s" % e) if DEBUG: pprint(list(self.idb))
def toRDF(self): """ Print a message into RDF in XML format """ #rdf graph store = ConjunctiveGraph() #namespaces store.bind('sioc', SIOC) store.bind('foaf', FOAF) store.bind('rdfs', RDFS) store.bind('dc', DC) store.bind('dct', DCT) #message node message = URIRef(self.getUri()) store.add((message, RDF.type, SIOC["Post"])) #document node doc = URIRef(self.getUri() + '.rdf') store.add((doc, RDF.type, FOAF["Document"])) store.add((doc, FOAF["primaryTopic"], message)) try: store.add((message, SIOC['id'], Literal(self.getSwamlId()))) store.add((message, SIOC['link'], URIRef(self.getXhtmlUrl()))) store.add((message, SIOC['has_container'], URIRef(self.config.get('base') + 'forum'))) store.add((message, SIOC["has_creator"], URIRef(self.getSender().getUri()))) store.add((message, DC['title'], Literal(self.getSubject()))) store.add((message, DCT['created'], Literal(self.getDate(), datatype=XSD[u'dateTime']))) parent = self.getParent() if (parent != None): store.add((message, SIOC['reply_of'], URIRef(parent))) if (len(self.childs) > 0): for child in self.childs: store.add((message, SIOC['has_reply'], URIRef(child))) previous = self.getPreviousByDate() if (previous != None): store.add( (message, SIOC['previous_by_date'], URIRef(previous))) next = self.getNextByDate() if (next != None): store.add((message, SIOC['next_by_date'], URIRef(next))) store.add((message, SIOC['content'], Literal(self.getBody()))) except Exception, detail: print 'Error proccesing message ' + str( self.getId()) + ': ' + str(detail)
def setUp(self): NS = u"http://example.org/" self.graph = ConjunctiveGraph() self.graph.parse(StringInputSource(""" @prefix : <http://example.org/> . @prefix rdf: <%s> . @prefix rdfs: <%s> . [ :prop :val ]. [ a rdfs:Class ]."""%(RDF.RDFNS,RDFS.RDFSNS)), format="n3")
def parse(self, result): """ Parse query result @param result: text result @return: rdf graph """ graph = ConjunctiveGraph() graph.parse(StringInputSource(result)) return graph
def __init__(self, size, prefix, base): self.prefix = "%s/%s" % (base, prefix) self.pool = [ConjunctiveGraph() for i in range(int(size))] for graph in self.pool: graph.bind("deb", DEB) graph.bind("xhv", XHV) graph.bind("earl", EARL) graph.bind("dc", DC) graph.bind("content", CONTENT) graph.bind("r", R) graph.bind("rss", RSS)
def testModel(self): g = ConjunctiveGraph() g.parse(StringInputSource(input), format="n3") i = 0 for s, p, o in g: if isinstance(s, Graph): i += 1 self.assertEquals(i, 3) self.assertEquals(len(list(g.contexts())), 13) g.close()
def testModel(self): print 'Probando la función testModel\n_____________________________' g = ConjunctiveGraph() g.parse(StringInputSource(input), format="n3") i = 0 for s, p, o in g: if isinstance(s, Graph): i += 1 print i #self.assertEquals(i, 3) #self.assertEquals(len(list(g.contexts())), 13) #print g.serialize() g.close()
def parse(self, source, sink, **args): assert sink.store.context_aware g = ConjunctiveGraph(store=sink.store) self._parser = create_parser(g) content_handler = self._parser.getContentHandler() preserve_bnode_ids = args.get("preserve_bnode_ids", None) if preserve_bnode_ids is not None: content_handler.preserve_bnode_ids = preserve_bnode_ids # We're only using it once now #content_handler.reset() #self._parser.reset() self._parser.parse(source)
def OpenGraph(storeType, configStr, graphUri, storeName='rdfstore'): # Get the mysql plugin. You may have to install the python mysql libraries store = plugin.get(storeType, Store)(storeName, debug=False, perfLog=True) # Open previously created store, or create it if it doesn't exist yet rt = store.open(configStr, create=False) assert rt != NO_STORE, "'%s' store '%s' not found using config string '%s!'" % ( storeType, storeName, configStr) assert rt == VALID_STORE or rt is not None, "There underlying store is corrupted" #There is a store, use it; use ConjunctiveGraph to see everything! graph = ConjunctiveGraph(store, identifier=URIRef(graphUri)) return graph
def testDefaultGraph(): 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') G = ConjunctiveGraph(memStore) #test that CG includes triples from all 3 assert G.query(sparqlQ3), "CG as default graph should *all* triples" assert not graph2.query( sparqlQ3 ), "Graph as default graph should *not* include triples from other graphs"
def _discover_meta(self, homepage, candidate): self.triples.push_meta(homepage, candidate) self.stats.count_rdf() logging.debug("Analyzing '%s'" % candidate) # FIXME: Not a good idea, think about it. if re.match(r".*\.rdf$", candidate) is not None: graph = ConjunctiveGraph() try: graph.parse(candidate) except (SAXParseException, RdflibParserError), e: self.stats.count_invalidrdf() raise RDFDiscoveringMalformedError(str(e), candidate) except urllib2.URLError: self.stats.count_invalidrdf() raise RDFDiscoveringBrokenLinkError(candidate)
def closureGraph(self,sourceGraph,readOnly=True,store=None): if readOnly: if store is None and not sourceGraph: store = Graph().store store = store is None and sourceGraph.store or store roGraph=ReadOnlyGraphAggregate([sourceGraph,self.inferredFacts], store=store) roGraph.namespace_manager = NamespaceManager(roGraph) for srcGraph in [sourceGraph,self.inferredFacts]: for prefix,uri in srcGraph.namespaces(): roGraph.namespace_manager.bind(prefix,uri) return roGraph else: cg=ConjunctiveGraph() cg+=sourceGraph cg+=self.inferredFacts return cg
def testQueryingMore(self): for result in self.results: uri = result[0] g = ConjunctiveGraph() g.parse(uri) query = Parse(""" SELECT ?person WHERE { <%s> foaf:primaryTopic ?person . ?person rdf:type foaf:Person . } """ % uri) queryResults = g.query(query, initNs=NSbindings).serialize('python') if (len(queryResults) > 0): self.assertEquals(str(queryResults[0]), "http://www.wikier.org/foaf#wikier")
def FixViewsString(configStr, storeName='rdfstore'): # Get the mysql plugin. You may have to install the python mysql libraries store = plugin.get('MySQL', Store)(storeName,debug=False,perfLog=True) # Open previously created store, or create it if it doesn't exist yet rt = store.open(configStr,create=False) if rt == NO_STORE: #There is no underlying MySQL infrastructure, create it #store.open(configStr,create=True) #TODO: could create store & load appropriate data here assert False, "'%s' store '%s' not found using config string '%s!'" % (storeType, storeName, configStr) else: assert rt == VALID_STORE,"There underlying store is corrupted" #There is a store, use it; use ConjunctiveGraph to see everything! graph = ConjunctiveGraph(store, identifier = URIRef(graphUri)) FixViewsGraph(graph)
def testSPARQLNotEquals(): NS = u"http://example.org/" graph = ConjunctiveGraph() graph.parse(StringInputSource(""" @prefix : <http://example.org/> . @prefix rdf: <%s> . :foo rdf:value 1. :bar rdf:value 2.""" % RDF.RDFNS), format="n3") rt = graph.query("""SELECT ?node WHERE { ?node rdf:value ?val. FILTER (?val != 1) }""", initNs={'rdf': RDF.RDFNS}, DEBUG=False) for row in rt: item = row[0] assert item == URIRef("http://example.org/bar")
def message_board_to_sioc(dbfile): sg = ConjunctiveGraph() sg.bind('foaf', FOAF) sg.bind('sioc', SIOC) sg.bind('dc', DC) conn = sqlite3.connect(dbfile) cur = conn.cursor() # Get all the messages and add them to the graph cur.execute('SELECT id,title,content,user FROM messages') for id, title, content, user in cur.fetchall(): mnode = MB['messages/%d' % id] sg.add((mnode, RDF.type, SIOC['Post'])) sg.add((mnode, DC['title'], Literal(title))) sg.add((mnode, SIOC['content'], Literal(content))) sg.add((mnode, SIOC['has_creator'], MB['users/%s' % user])) # Get all the users and add them to the graph cur.execute('SELECT id,name,email FROM users') for id, name, email in cur.fetchall(): sg.add((mnode, RDF.type, SIOC['User'])) unode = MB['users/%d' % id] sg.add((unode, FOAF['name'], Literal(name))) sg.add((unode, FOAF['email'], Literal(email))) # Get subjects cur.execute('SELECT id,description FROM subjects') for id, description in cur.fetchall(): sg.add((mnode, RDF.type, DCTERMS['subject'])) sg.add((MB['subjects/%d' % id], RDFS['label'], Literal(description))) # Link subject to messages cur.execute('SELECT message_id,subject_id FROM message_subjects') for mid, sid in cur.fetchall(): sg.add( (MB['messages/%s' % mid], SIOC['topic'], MB['subjects/%s'] % sid)) conn.close() return sg
rdf:type foaf:PersonalProfileDocument . # Named graph: http://example.org/foaf/bobFoaf @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . _:z foaf:mbox <mailto:[email protected]> . _:z rdfs:seeAlso <http://example.org/foaf/bobFoaf> . _:z foaf:nick "Robert" . <http://example.org/foaf/bobFoaf> rdf:type foaf:PersonalProfileDocument . """ graph = ConjunctiveGraph(plugin.get('IOMemory', Store)()) graph.parse(StringIO(text), format="n3") print graph.serialize(format='xml') test_query = """ PREFIX data: <http://example.org/foaf/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?mbox ?nick ?ppd WHERE { GRAPH data:aliceFoaf { ?alice foaf:mbox <mailto:[email protected]> ; foaf:knows ?whom .
from rdflib.Graph import ConjunctiveGraph from rdflib import Namespace, BNode, Literal, RDF, URIRef import csv import pysesame JOBS = Namespace( "http://www.medev.ac.uk/interoperability/rss/1.0/modules/jobs/rss1.0jobsmodule#" ) DC = Namespace("http://purl.org/dc/elements/1.1/") JB = Namespace("http://semprog.com/schemas/jobboard#") COMPANY = Namespace("http://purl.org/rss/1.0/modules/company/") RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#') jg = ConjunctiveGraph() jg.bind('jobs', JOBS) jg.bind('dc', DC) jg.bind('jobboard', JB) jg.bind('company', COMPANY) jg.bind('rdfs', RDFS) # Incremental counter for vacancy IDs vid = 0 for title, salary, location, company, crunchbase, ticker in csv.reader( file('joblist.csv')): # Create the vacancy vid += 1 vacancy = JB[str(vid)] jg.add((vacancy, RDF.type, JOBS['Vacancy'])) jg.add((vacancy, DC['title'], Literal(title)))
fq = movie_graph.query("""SELECT ?film ?act ?perf ?an ?fn WHERE {?film fb:film.film.performances ?perf . ?perf fb:film.performance.actor ?act . ?act dc:title ?an. ?film dc:title ?fn . }""", initNs={ 'fb': FB, 'dc': DC }) graphs = {} for film, act, perf, an, fn in fq: filmid = fn.split(',')[0].replace(' ', '_') + '_' + str(film).split('=')[1] actid = an.replace(' ', '_') + '_' + str(act).split('=')[1] graphs.setdefault(filmid, ConjunctiveGraph()) graphs.setdefault(actid, ConjunctiveGraph()) graphs[filmid].add((film, FB['film.film.performance.actor'], act)) graphs[filmid].add((act, RDFS['seeAlso'], actid + '.xml')) graphs[filmid].add((film, DC['title'], fn)) graphs[actid].add((act, FB['film.actor.performance.film'], film)) graphs[actid].add((film, RDFS['seeAlso'], filmid + '.xml')) graphs[actid].add((act, DC['title'], an)) for id, graph in graphs.items(): graph.serialize('open_films/%s.xml' % id)
def resolveURI(self, uri): return _urljoin(self.baseuri or '', uri) def _popStacks(self, event, node): # check abouts if len(self.abouts) <> 0: about, aboutnode = self.abouts[-1] if aboutnode == node: self.abouts.pop() # keep track of nodes going out of scope self.elementStack.pop() # track xml:base and xml:lang going out of scope if self.xmlbases: self.xmlbases.pop() if self.xmlbases and self.xmlbases[-1]: self.baseuri = self.xmlbases[-1] if self.langs: self.langs.pop() if self.langs and self.langs[-1]: self.lang = self.langs[-1] if __name__ == "__main__": store = ConjunctiveGraph() store.load(sys.argv[1], format="rdfa") print store.serialize(format="pretty-xml")
def testN3Store(store="default", configString=None): g = ConjunctiveGraph(store=store) if configString: g.destroy(configString) g.open(configString) g.parse(StringInputSource(testN3), format="n3") print g.store try: for s, p, o in g.triples((None, implies, None)): formulaA = s formulaB = o assert type(formulaA) == QuotedGraph and type(formulaB) == QuotedGraph a = URIRef('http://test/a') b = URIRef('http://test/b') c = URIRef('http://test/c') d = URIRef('http://test/d') v = Variable('y') universe = ConjunctiveGraph(g.store) #test formula as terms assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1 #test variable as term and variable roundtrip assert len(list(formulaB.triples((None, None, v)))) == 1 for s, p, o in formulaB.triples((None, d, None)): if o != c: assert isinstance(o, Variable) assert o == v s = list(universe.subjects(RDF.type, RDFS.Class))[0] assert isinstance(s, BNode) assert len(list(universe.triples((None, implies, None)))) == 1 assert len(list(universe.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, RDF.type, None)))) == 1 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 assert len(list(universe.triples((None, None, None)))) == 3 assert len( list(formulaB.triples((None, URIRef('http://test/d'), None)))) == 2 assert len( list(universe.triples((None, URIRef('http://test/d'), None)))) == 1 #context tests #test contexts with triple argument assert len(list(universe.contexts((a, d, c)))) == 1 #Remove test cases universe.remove((None, implies, None)) assert len(list(universe.triples((None, implies, None)))) == 0 assert len(list(formulaA.triples((None, None, None)))) == 2 assert len(list(formulaB.triples((None, None, None)))) == 2 formulaA.remove((None, b, None)) assert len(list(formulaA.triples((None, None, None)))) == 1 formulaA.remove((None, RDF.type, None)) assert len(list(formulaA.triples((None, None, None)))) == 0 universe.remove((None, RDF.type, RDFS.Class)) #remove_context tests universe.remove_context(formulaB) assert len(list(universe.triples((None, RDF.type, None)))) == 0 assert len(universe) == 1 assert len(formulaB) == 0 universe.remove((None, None, None)) assert len(universe) == 0 g.store.destroy(configString) except: g.store.destroy(configString) raise
def localGraph(): g = ConjunctiveGraph() g.add((EXP['dp'], EXP['firstName'], Literal('Drew'))) g.add((EXP['labeled'], RDFS.label, Literal('Labeled'))) return g
def setUp(self): super(WithGraph, self).setUp() self.graph = ConjunctiveGraph() self.graph.add((CMD['c2'], RDF.type, CMD['Bright'])) self.cl = CommandLog(Graph2(self.graph, initNs=INITNS))
def testUsesSingleGraphForRW(self): g1 = ConjunctiveGraph() c = CommandLog(Graph2(g1, initNs=INITNS)) c.addCommand(CMD['c1'], dateTime('18:00:00'), USER['drewp']) self.assert_(len(g1) > 0)