Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Hello World example for SPARQL with RDFLib?

I would like to get as small as possible but still self-consistent and working example of using SPARQL from RDFLib. I have RDFLib version '4.0.1'.

I would like to have a code that does the following

  1. Import RDFLib.
  2. Creates a simple Graph (from 2 to 4 relations)
  3. Writes this graph into a file in the rdf format.
  4. Reads the graph from the file.
  5. Use SPARQL to extract something from the graph.

ADDED

I tried by myself (first without writing to and reading from a file) and I could not make. This is what I have:

import rdflib

g = rdflib.ConjunctiveGraph()

has_border_with = rdflib.URIRef('www.example.org/has_border_with')
located_in = rdflib.URIRef('www.example.org/located_in')

germany = rdflib.URIRef('www.example.org/country1')
france = rdflib.URIRef('www.example.org/country2')
china = rdflib.URIRef('www.example.org/country3')
mongolia = rdflib.URIRef('www.example.org/country4')

europa = rdflib.URIRef('www.example.org/part1')
asia = rdflib.URIRef('www.example.org/part2')

g.add((germany,has_border_with,france))
g.add((china,has_border_with,mongolia))
g.add((germany,located_in,europa))
g.add((france,located_in,europa))
g.add((china,located_in,asia))
g.add((mongolia,located_in,asia))

x = g.query("""select ?country where { ?country www.example.org/located_in www.example.org/part1 }""")
print x

As a result I get:

Traceback (most recent call last):
  File "hello_world.py", line 23, in <module>
    x = g.query("""select ?country where { ?country www.example.org/located_in www.example.org/part1 }""")
  File "/usr/local/lib/python2.7/dist-packages/rdflib-4.0.1-py2.7.egg/rdflib/graph.py", line 1045, in query
    query_object, initBindings, initNs, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/rdflib-4.0.1-py2.7.egg/rdflib/plugins/sparql/processor.py", line 72, in query
    parsetree = parseQuery(strOrQuery)
  File "/usr/local/lib/python2.7/dist-packages/rdflib-4.0.1-py2.7.egg/rdflib/plugins/sparql/parser.py", line 1034, in parseQuery
    return Query.parseString(q, parseAll=True)
  File "/usr/lib/python2.7/dist-packages/pyparsing.py", line 1032, in parseString
    raise exc
pyparsing.ParseException: Expected "}" (at char 24), (line:1, col:25)
like image 502
Roman Avatar asked Dec 20 '22 05:12

Roman


1 Answers

there are several issues:

  1. name your resources starting with http://
  2. URLs in SPARQL queries need <> around them
  3. use a simple Graph instead of ConjunctiveGraph
  4. you can use the Graph.serialize and Graph.parse methods for saving and reading from file (see code)

try the following modifications of your example code:

import rdflib

g = rdflib.Graph()
has_border_with = rdflib.URIRef('http://www.example.org/has_border_with')
located_in = rdflib.URIRef('http://www.example.org/located_in')

germany = rdflib.URIRef('http://www.example.org/country1')
france = rdflib.URIRef('http://www.example.org/country2')
china = rdflib.URIRef('http://www.example.org/country3')
mongolia = rdflib.URIRef('http://www.example.org/country4')

europa = rdflib.URIRef('http://www.example.org/part1')
asia = rdflib.URIRef('http://www.example.org/part2')

g.add((germany,has_border_with,france))
g.add((china,has_border_with,mongolia))
g.add((germany,located_in,europa))
g.add((france,located_in,europa))
g.add((china,located_in,asia))
g.add((mongolia,located_in,asia))

q = "select ?country where { ?country <http://www.example.org/located_in> <http://www.example.org/part1> }"
x = g.query(q)
print list(x)
# write graph to file, re-read it and query the newly created graph
g.serialize("graph.rdf")
g1 = rdflib.Graph()
g1.parse("graph.rdf", format="xml")
x1 = g1.query(q)
print list(x1)
like image 159
kr1 Avatar answered Jan 11 '23 00:01

kr1