class RecursionTests(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_simple_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(BASIC_KNOWS_DATA), format='n3') results = graph.query(KNOWS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) person1 = URIRef('ex:person.1') person2 = URIRef('ex:person.2') nose.tools.assert_equal( results, set([(person1, None), (person1, Literal('person 3')), (person2, Literal('person 3'))])) def test_secondary_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(SUBCLASS_DATA), format='n3') results = graph.query(SUBCLASS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) ob = URIRef('ex:ob') class1 = URIRef('ex:class.1') class2 = URIRef('ex:class.2') class3 = URIRef('ex:class.3') nose.tools.assert_equal( results, set([(ob, class1), (ob, class2), (ob, class3)]))
class TestSparqlASK(unittest.TestCase): def setUp(self): self.graph = Graph() io = StringIO(""" @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix : <http://goonmill.org/2007/skill.n3#> . :Foo a rdfs:Class . :bar a :Foo . """) self.graph.load(io, format='n3') self.compliance_setting, algebra.DAWG_DATASET_COMPLIANCE = algebra.DAWG_DATASET_COMPLIANCE, False def tearDown(self): algebra.DAWG_DATASET_COMPLIANCE = self.compliance_setting def test_ask_true(self): """ Ask for a triple that exists, assert that the response is True. """ res = self.graph.query('ASK { <http://goonmill.org/2007/skill.n3#bar> a <http://goonmill.org/2007/skill.n3#Foo> } ') self.assertEquals(res.askAnswer, True, "The answer should have been that the triple was found") def test_ask_false(self): """ Ask for a triple that does not exist, assert that the response is False. """ res = self.graph.query('ASK { <http://goonmill.org/2007/skill.n3#baz> a <http://goonmill.org/2007/skill.n3#Foo> } ') self.assertEquals(res.askAnswer, False, "The answer should have been that the triple was not found")
class TestSparqlEquals(unittest.TestCase): PREFIXES = {'rdfs': "http://www.w3.org/2000/01/rdf-schema#"} def setUp(self): testContent = """ @prefix rdfs: <%(rdfs)s> . <http://example.org/doc/1> rdfs:label "Document 1"@en . <http://example.org/doc/2> rdfs:label "Document 2"@en . <http://example.org/doc/3> rdfs:label "Document 3"@en . """ % self.PREFIXES self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_uri_equals(self): uri = URIRef("http://example.org/doc/1") query = (""" PREFIX rdfs: <%(rdfs)s> SELECT ?uri WHERE { ?uri rdfs:label ?label . FILTER( ?uri = <""" + uri + """> ) } """) % self.PREFIXES res = self.graph.query(query) expected = [(uri, )] self.assertEqual(list(res), expected)
class TestSparqlEquals(unittest.TestCase): PREFIXES = { 'rdfs': "http://www.w3.org/2000/01/rdf-schema#" } def setUp(self): testContent = """ @prefix rdfs: <%(rdfs)s> . <http://example.org/doc/1> rdfs:label "Document 1"@en . <http://example.org/doc/2> rdfs:label "Document 2"@en . <http://example.org/doc/3> rdfs:label "Document 3"@en . """ % self.PREFIXES self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_uri_equals(self): uri = URIRef("http://example.org/doc/1") query = (""" PREFIX rdfs: <%(rdfs)s> SELECT ?uri WHERE { ?uri rdfs:label ?label . FILTER( ?uri = <"""+uri+"""> ) } """) % self.PREFIXES res = self.graph.query(query) expected = [(uri,)] self.assertEqual(list(res),expected)
def fromuri(self, uri): self.uri = uri if not uri.startswith('http://rdf.freebase.com'): self.checkuri() try: g = ConjunctiveGraph() g.load(self.uri) if g: logger.info("INFO process_rdf.py - returning graph for " + self.uri) return g else: raise Exception('Nothing was returned, probably caused URL serving no RDF or bad RDF (eg. Freebase): ' '"No handlers could be found for logger "process_rdf.py" -- uri was ' + self.uri) except URLError as e: logger.error("URLError process_rdf.py - " + e.message) raise Exception('URLError, cause either bad URL or no internet connection - ' + e.message + '(uri was ' + self.uri + ')') except SAXParseException as e: logger.error("SAXParseException process_rdf.py - " + e.message + '(uri was' + self.uri + ')') raise Exception('SAXParseException') except AttributeError as e: logger.error("AttributeError process_rdf.py - " + e.message + '(uri was' + self.uri + ')') raise Exception('AttributeError') else: self.fromfreebaseuri()
class RecursionTests(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_simple_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(BASIC_KNOWS_DATA), format='n3') results = graph.query(KNOWS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) person1 = URIRef('ex:person.1') person2 = URIRef('ex:person.2') nose.tools.assert_equal( results, set([(person1, None), (person1, Literal('person 3')), (person2, Literal('person 3'))])) def test_secondary_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(SUBCLASS_DATA), format='n3') results = graph.query(SUBCLASS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) ob = URIRef('ex:ob') class1 = URIRef('ex:class.1') class2 = URIRef('ex:class.2') class3 = URIRef('ex:class.3') nose.tools.assert_equal( results, set([(ob, class1), (ob, class2), (ob, class3)]))
def build_network(rules): if isinstance(rules, basestring): rules = StringIO(rules) graph = ConjunctiveGraph() graph.load(rules, publicID="test", format="n3") network = NetworkFromN3(graph, additionalBuiltins={STRING_NS.startsWith: StringStartsWith}) network.feedFactsToAdd(generateTokenSet(extractBaseFacts(graph))) return network
class TestSparqlOPT_FILTER2(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format="n3") def test_OPT_FILTER(self): results = self.graph.query(QUERY, DEBUG=False).serialize(format="python") results = list(results) self.failUnless(results == [doc1], "expecting : %s . Got: %s" % ([doc1], repr(results)))
def build_network(rules): if isinstance(rules, basestring): rules = StringIO(rules) graph = ConjunctiveGraph() graph.load(rules, publicID='test', format='n3') network = NetworkFromN3( graph, additionalBuiltins={STRING_NS.startsWith: StringStartsWith}) network.feedFactsToAdd(generateTokenSet(extractBaseFacts(graph))) return network
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format="n3") def test_OPT_FILTER(self): results = self.graph.query(QUERY, DEBUG=False, initBindings={"?label": RDFS.label}) print results.vars self.failUnless(list(results) == [(doc2,)], "expecting : %s, got %s" % (repr([(doc2,)]), repr(list(results))))
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): xd3Objs = [o for o in self.graph.objects(subject=exNS.xd3, predicate=exNS.p)] self.failUnless(xd3Objs[0].datatype == XSD.double, "Expecting %r, got instead : %r"%(double1, xd3Objs[0]))
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): results = self.graph.query(QUERY, DEBUG=False, initBindings={'?label':RDFS.label}).serialize(format='python') self.failUnless(list(results) == [doc2], "expecting : %s"%repr([doc2]))
def build_network2(rules): graph = ConjunctiveGraph() graph.load(StringIO(rules), publicID='test', format='n3') rule_store, rule_graph = SetupRuleStore( StringIO(rules), additionalBuiltins={STRING_NS.startsWith: StringStartsWith}) from FuXi.Rete.Network import ReteNetwork network = ReteNetwork(rule_store) network.feedFactsToAdd(generateTokenSet(extractBaseFacts(graph))) return network
def build_network(rules): import sys if isinstance(rules, basestring if sys.version < '3' else str): rules = StringIO(rules) graph = ConjunctiveGraph() graph.load(rules, publicID='test', format='n3') network = NetworkFromN3( graph, additionalBuiltins={STRING_NS.startsWith: StringStartsWith}) network.feedFactsToAdd(generateTokenSet(extractBaseFacts(graph))) return network
def build_network2(rules): graph = ConjunctiveGraph() graph.load(StringIO(rules), publicID='test', format='n3') rule_store, rule_graph = SetupRuleStore( StringIO(rules), additionalBuiltins={STRING_NS.startsWith: StringStartsWith}) from FuXi.Rete.Network import ReteNetwork network = ReteNetwork(rule_store) network.feedFactsToAdd(generateTokenSet(extractBaseFacts(graph))) return network
def _convertJSONLD(self): """ Convert a RDF JSON-LDresult into an RDFLib triple store. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: RDFLib Graph """ from rdflib import ConjunctiveGraph retval = ConjunctiveGraph() retval.load(self.response, format='json-ld', publicID=' ') return retval
def _convertJSONLD(self): """ Convert a RDF JSON-LDresult into an RDFLib triple store. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: RDFLib Graph """ from rdflib import ConjunctiveGraph retval = ConjunctiveGraph() retval.load(self.response, format='json-ld', publicID=' ') return retval
class TestSparqlOPT_FILTER2(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): results = self.graph.query(QUERY, DEBUG=False) results = list(results) self.failUnless( results == [(doc1, )], "expecting : %s . Got: %s" % ([(doc1, )], repr(results)))
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): results = self.graph.query(QUERY, DEBUG=False, initBindings={'?label':RDFS.label}) print results.vars self.failUnless(list(results) == [(doc2,)], "expecting : %s, got %s"%(repr([(doc2,)]), repr(list(results))))
def test_simple_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(BASIC_KNOWS_DATA), format='n3') results = graph.query(KNOWS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) person1 = URIRef('ex:person.1') person2 = URIRef('ex:person.2') nose.tools.assert_equal( results, set([(person1, None), (person1, Literal('person 3')), (person2, Literal('person 3'))]))
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): xd3Objs = [ o for o in self.graph.objects(subject=exNS.xd3, predicate=exNS.p) ] self.failUnless( xd3Objs[0].datatype == XSD.double, "Expecting %r, got instead : %r" % (double1, xd3Objs[0]))
def test_secondary_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(SUBCLASS_DATA), format='n3') results = graph.query(SUBCLASS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) ob = URIRef('ex:ob') class1 = URIRef('ex:class.1') class2 = URIRef('ex:class.2') class3 = URIRef('ex:class.3') nose.tools.assert_equal( results, set([(ob, class1), (ob, class2), (ob, class3)]))
def test_simple_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(BASIC_KNOWS_DATA), format='n3') results = graph.query(KNOWS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) person1 = URIRef('ex:person.1') person2 = URIRef('ex:person.2') nose.tools.assert_equal( results, set([(person1, None), (person1, Literal('person 3')), (person2, Literal('person 3'))]))
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): results = self.graph.query(QUERY, DEBUG=False, initBindings={ '?label': RDFS.label }).serialize(format='python') self.failUnless( list(results) == [doc2], "expecting : %s" % repr([doc2]))
def test_secondary_recursion(self): graph = ConjunctiveGraph() graph.load(StringIO(SUBCLASS_DATA), format='n3') results = graph.query(SUBCLASS_QUERY, DEBUG=False).serialize(format='python') results = set([tuple(result) for result in results]) ob = URIRef('ex:ob') class1 = URIRef('ex:class.1') class2 = URIRef('ex:class.2') class3 = URIRef('ex:class.3') nose.tools.assert_equal( results, set([(ob, class1), (ob, class2), (ob, class3)]))
class DateFilterTest(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_DATE_FILTER1(self): for query in [QUERY1,QUERY2,QUERY3]: print query #pQuery = Parse(query) #print RenderSPARQLAlgebra(pQuery) results = self.graph.query(query, DEBUG=False).serialize(format='python') results = list(results) self.failUnless( len(results) and results == [ANSWER1], "expecting : %s . Got: %s"%([ANSWER1],repr(results)))
def _convertRDF(self): """ Convert an RDF/XML result into an RDFLib triple store. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: RDFLib Graph """ try: from rdflib.graph import ConjunctiveGraph except ImportError: from rdflib import ConjunctiveGraph retval = ConjunctiveGraph() # this is a strange hack. If the publicID is not set, rdflib (or the underlying xml parser) makes a funny #(and, as far as I could see, meaningless) error message... retval.load(self.response, publicID=' ') return retval
def _convertRDF(self) : """ Convert an RDF/XML result into an RDFLib triple store. This method can be overwritten in a subclass for a different conversion method. @return: converted result @rtype: RDFLib Graph """ try: from rdflib.graph import ConjunctiveGraph except ImportError: from rdflib import ConjunctiveGraph retval = ConjunctiveGraph() # this is a strange hack. If the publicID is not set, rdflib (or the underlying xml parser) makes a funny #(and, as far as I could see, meaningless) error message... retval.load(self.response,publicID=' ') return retval
class DateFilterTest(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_DATE_FILTER1(self): for query in [QUERY1, QUERY2, QUERY3]: print query #pQuery = Parse(query) #print RenderSPARQLAlgebra(pQuery) results = self.graph.query(query, DEBUG=False).serialize(format='python') results = list(results) self.failUnless( len(results) and results == [ANSWER1], "expecting : %s . Got: %s" % ([ANSWER1], repr(results)))
class TestSparqlASK(unittest.TestCase): def setUp(self): self.graph = Graph() io = StringIO(""" @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix : <http://goonmill.org/2007/skill.n3#> . :Foo a rdfs:Class . :bar a :Foo . """) self.graph.load(io, format='n3') self.compliance_setting, algebra.DAWG_DATASET_COMPLIANCE = algebra.DAWG_DATASET_COMPLIANCE, False def tearDown(self): algebra.DAWG_DATASET_COMPLIANCE = self.compliance_setting def test_ask_true(self): """ Ask for a triple that exists, assert that the response is True. """ res = self.graph.query( 'ASK { <http://goonmill.org/2007/skill.n3#bar> a <http://goonmill.org/2007/skill.n3#Foo> } ' ) self.assertEquals( res.askAnswer, [True], "The answer should have been that the triple was found") test_ask_true.known_issue = True def test_ask_false(self): """ Ask for a triple that does not exist, assert that the response is False. """ res = self.graph.query( 'ASK { <http://goonmill.org/2007/skill.n3#baz> a <http://goonmill.org/2007/skill.n3#Foo> } ' ) self.assertEquals( res.askAnswer, [False], "The answer should have been that the triple was not found")
def serialize_and_load(sourceGraph, makeSerializer): stream = serialize(sourceGraph, makeSerializer, False) stream.seek(0) reparsedGraph = ConjunctiveGraph() reparsedGraph.load(stream) return reparsedGraph
#!/usr/bin/python import re from rdflib import Namespace, URIRef from rdflib.namespace import RDF from rdflib.graph import ConjunctiveGraph LOA = Namespace("http://vocab.e.gov.br/2012/08/loa2012#") LOA_I = Namespace("http://orcamento.dados.gov.br/id/") g = ConjunctiveGraph() g.bind('loa', LOA) g.bind('loa-i', LOA_I) with open("rdf_loa2012_uri_dadosgovbr.ttl", "r") as f: g.load(f, format="n3") def troca_uri(classe, padrao_uri, novo_padrao): padrao = re.compile(padrao_uri) for uri_antiga,p,o in g.triples((None, RDF['type'], classe)): match = padrao.match(uri_antiga) if match: id_instancia = match.group(1) uri_nova = URIRef(novo_padrao % id_instancia) print "Trocando %s por %s..." % (uri_antiga, uri_nova) g.remove((uri_antiga, RDF['type'], classe)) g.add((uri_nova, RDF['type'], classe)) # onde aparece como sujeito for s,p,o in g.triples((uri_antiga, None, None)): g.remove((s, p, o)) g.add((uri_nova, p, o)) # onde aparece como objeto for s,p,o in g.triples((None, None, uri_antiga)): g.remove((s, p, o))
def serialize_and_load(sourceGraph, makeSerializer): stream = serialize(sourceGraph, makeSerializer, False) stream.seek(0) reparsedGraph = ConjunctiveGraph() reparsedGraph.load(stream) return reparsedGraph
from rdflib.term import URIRef, Literal from StringIO import StringIO import rdflib testContent = """ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . <http://example.org/doc/1> rdfs:label "Document 1"@en, "Dokument 1"@sv . <http://example.org/doc/2> rdfs:label "Document 2"@en, "Dokument 2"@sv . <http://example.org/doc/3> rdfs:label "Document 3"@en, "Dokument 3"@sv . """ graph = ConjunctiveGraph() graph.load(StringIO(testContent), format='n3') doc1 = URIRef("http://example.org/doc/1") PROLOGUE = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> """ def test_filter_by_lang(): testdata = [ ("en", u'"Document 1"@en'), ("sv", u'"Dokument 1"@sv') ] query = PROLOGUE+'''
testRdf = """ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix : <tag://example.org,2007/literals-test#> . <http://example.org/thing> :plain "plain"; :integer 1; :float 1.1; :string "string"^^xsd:string; :date "2007-04-28"^^xsd:date; rdfs:label "Thing"@en, "Sak"@sv . """ graph = ConjunctiveGraph() graph.load(StringIO(testRdf), format='n3') PROLOGUE = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX t: <tag://example.org,2007/literals-test#> """ thing = URIRef("http://example.org/thing") SPARQL = PROLOGUE+" SELECT ?uri WHERE { ?uri %s . } " TEST_DATA = [ ('plain', SPARQL % 't:plain "plain"', [thing]), ('integer', SPARQL % 't:integer 1', [thing]), ('float', SPARQL % 't:float 1.1', [thing]), ('langlabel_en', SPARQL % 'rdfs:label "Thing"@en', [thing]),
testRdf = """ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix : <tag://example.org,2007/literals-test#> . <http://example.org/thing> :plain "plain"; :integer 1; :float 1.1; :string "string"^^xsd:string; :date "2007-04-28"^^xsd:date; rdfs:label "Thing"@en, "Sak"@sv . """ graph = ConjunctiveGraph() graph.load(StringIO(testRdf), format="n3") PROLOGUE = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX t: <tag://example.org,2007/literals-test#> """ thing = URIRef("http://example.org/thing") SPARQL = PROLOGUE + " SELECT ?uri WHERE { ?uri %s . } " TEST_DATA = [ ("plain", SPARQL % 't:plain "plain"', [thing]), ("integer", SPARQL % "t:integer 1", [thing]), ("float", SPARQL % "t:float 1.1", [thing]), ("langlabel_en", SPARQL % 'rdfs:label "Thing"@en', [thing]),