SPARQL 1.2#
This document contains a short introduction to RDF using rudof.
Preliminaries: Install and configure rudof#
The library is available as pyrudof.
!pip install pyrudof # Pyrudof
! pip install ipython
! pip install plantuml # To visualize graphs
from IPython.display import Image
from pyrudof import Rudof, RudofConfig
rudof = Rudof(RudofConfig())
Requirement already satisfied: pyrudof in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (0.1.135)
Requirement already satisfied: ipython in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (9.6.0)
Requirement already satisfied: decorator in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (5.2.1)
Requirement already satisfied: ipython-pygments-lexers in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (1.1.1)
Requirement already satisfied: jedi>=0.16 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (0.19.2)
Requirement already satisfied: matplotlib-inline in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (0.2.1)
Requirement already satisfied: pexpect>4.3 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (4.9.0)
Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (3.0.52)
Requirement already satisfied: pygments>=2.4.0 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (2.19.2)
Requirement already satisfied: stack_data in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (0.6.3)
Requirement already satisfied: traitlets>=5.13.0 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (5.14.3)
Requirement already satisfied: typing_extensions>=4.6 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from ipython) (4.15.0)
Requirement already satisfied: wcwidth in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython) (0.2.14)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from jedi>=0.16->ipython) (0.8.5)
Requirement already satisfied: ptyprocess>=0.5 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from pexpect>4.3->ipython) (0.7.0)
Requirement already satisfied: executing>=1.2.0 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from stack_data->ipython) (2.2.1)
Requirement already satisfied: asttokens>=2.1.0 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from stack_data->ipython) (3.0.0)
Requirement already satisfied: pure-eval in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from stack_data->ipython) (0.2.3)
Requirement already satisfied: plantuml in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (0.3.0)
Requirement already satisfied: httplib2 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from plantuml) (0.31.0)
Requirement already satisfied: pyparsing<4,>=3.0.4 in /opt/hostedtoolcache/Python/3.11.13/x64/lib/python3.11/site-packages (from httplib2->plantuml) (3.2.5)
Tim Berners-Lee exampl in RDF 1.2#
Rudof supports RDF 1.2.
RDF 1.2 introduces triple terms which denote statements that can be the object of some triples.
The following example declares some information about a node :timbl that indicates that he worked at CERN between 1984 and 1994, and in 1980, and that he obtained the Princess of Asturias Award in 2002 together with Vinton Cerf,
rudof.read_data_str("""
prefix : <http://example.org/>
prefix sh: <http://www.w3.org/ns/shacl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
:timbl rdfs:label "Tim Berners Lee" .
_:r rdf:reifies <<( :timbl :employer :CERN )>> ;
:start "1984" ;
:end "1994" .
_:s rdf:reifies <<( :timbl :employer :CERN )>> ;
:start "1980" ;
:end "1980" .
_:t rdf:reifies <<( :timbl :award :PA )>> ;
:time "2002" ;
:togetherWith :vint, :bob, :lawrence .
:vint rdfs:label "Vinton Cerf" .
:bob rdfs:label "Bob kahn" .
:lawrence rdfs:label "Lawrence Roberts" .
:PA rdfs:label "Princess of Asturias Award" .
:CERN rdfs:label "CERN" .
""")
SPARQL 1.2#
results = rudof.run_query_str("""
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?person ?employer ?start ?end WHERE {
?r rdf:reifies <<( ?person :employer ?employer )>> ;
:start ?start ;
:end ?end .
}
""")
print(results.show())
╭───┬─────────┬───────────┬────────┬────────╮
│ │ ?person │ ?employer │ ?start │ ?end │
├───┼─────────┼───────────┼────────┼────────┤
│ 1 │ :timbl │ :CERN │ "1980" │ "1980" │
├───┼─────────┼───────────┼────────┼────────┤
│ 2 │ :timbl │ :CERN │ "1984" │ "1994" │
╰───┴─────────┴───────────┴────────┴────────╯
results = rudof.run_query_str("""
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?person ?award ?togetherPerson ?time WHERE {
?r rdf:reifies <<( ?person :award ?award )>> ;
OPTIONAL {?r :time ?time }
OPTIONAL {
?r :togetherWith ?togetherWith .
?togetherWith rdfs:label ?togetherPerson
}
}
""")
print(results.show())
╭───┬─────────┬────────┬────────────────────┬────────╮
│ │ ?person │ ?award │ ?togetherPerson │ ?time │
├───┼─────────┼────────┼────────────────────┼────────┤
│ 1 │ :timbl │ :PA │ "Lawrence Roberts" │ "2002" │
├───┼─────────┼────────┼────────────────────┼────────┤
│ 2 │ :timbl │ :PA │ "Bob kahn" │ "2002" │
├───┼─────────┼────────┼────────────────────┼────────┤
│ 3 │ :timbl │ :PA │ "Vinton Cerf" │ "2002" │
╰───┴─────────┴────────┴────────────────────┴────────╯