RDF 1.2#
This document contains a short introduction to RDF 1.2 using rudof.
Preliminaries: Install and configure rudof#
The library is available as pyrudof.
!pip install pyrudof
Requirement already satisfied: pyrudof in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (0.2.9)
The main entry point if a class called Rudof through which most of the functionality is provided.
from pyrudof import Rudof, RudofConfig, ResultDataFormat
In order to initialize that class, it is possible to pass a RudofConfig instance which contains configuration parameters for customization.
rudof = Rudof(RudofConfig())
! pip install ipython # If not already installed
!pip install plantuml
from IPython.display import Image # For displaying images
Requirement already satisfied: ipython in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (9.10.1)
Requirement already satisfied: decorator>=4.3.2 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (5.2.1)
Requirement already satisfied: ipython-pygments-lexers>=1.0.0 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (1.1.1)
Requirement already satisfied: jedi>=0.18.1 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (0.19.2)
Requirement already satisfied: matplotlib-inline>=0.1.5 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (0.2.1)
Requirement already satisfied: pexpect>4.3 in /opt/hostedtoolcache/Python/3.11.15/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.15/x64/lib/python3.11/site-packages (from ipython) (3.0.52)
Requirement already satisfied: pygments>=2.11.0 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (2.20.0)
Requirement already satisfied: stack_data>=0.6.0 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (0.6.3)
Requirement already satisfied: traitlets>=5.13.0 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (5.14.3)
Requirement already satisfied: typing_extensions>=4.6 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from ipython) (4.15.0)
Requirement already satisfied: wcwidth in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython) (0.6.0)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from jedi>=0.18.1->ipython) (0.8.6)
Requirement already satisfied: ptyprocess>=0.5 in /opt/hostedtoolcache/Python/3.11.15/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.15/x64/lib/python3.11/site-packages (from stack_data>=0.6.0->ipython) (2.2.1)
Requirement already satisfied: asttokens>=2.1.0 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from stack_data>=0.6.0->ipython) (3.0.1)
Requirement already satisfied: pure-eval in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from stack_data>=0.6.0->ipython) (0.2.3)
Requirement already satisfied: plantuml in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (0.3.0)
Requirement already satisfied: httplib2 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from plantuml) (0.31.2)
Requirement already satisfied: pyparsing<4,>=3.1 in /opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages (from httplib2->plantuml) (3.3.2)
Support for RDF 1.2#
Reset previous state of rudof and remnove the temporary file out.puml if it exists.
rudof.reset_all()
Rudof has added support for RDF 1.2. It is possible, for example, to load some RDF 1.2 files and visualize them.
RDF 1.2 introduces triple terms which denote statements that can be the object of some triples.
For example, we can state that :bob is interested in :MonaLisa since 4th October 1998 using the following code:
rudof.read_data("""
prefix : <http://example.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
:aliceBelief a :Statement ;
rdf:reifies <<( :bob :knows :dave )>> ;
:since 2025 ;
:accordingTo :dave .
""")
puml_content = rudof.serialize_data(ResultDataFormat.PlantUML)
with open('out.puml', 'w') as _f:
_f.write(puml_content)
Convert the puml to an image.
print(puml_content)
@startuml
<style>
.reifier {
BackGroundColor Yellow
LineThickness 1
LineColor Black
}
.literal {
BackGroundColor Cyan
LineThickness 1
LineColor Black
}
.uri {
BackGroundColor White
LineThickness 1
LineColor Blue
RoundCorner 25
}
.bnode {
BackGroundColor Gray
LineThickness 1
LineColor Blue
RoundCorner 25
}
.asserted {
BackGroundColor White
LineThickness 2
LineColor Black
}
.non_asserted {
BackGroundColor White
LineThickness 2
LineColor Blue
RoundCorner 0
}
</style>
hide stereotype
rectangle "[[http://example.org/aliceBelief :aliceBelief]]" <<uri>> as 0
rectangle "[[http://example.org/bob :bob]]" <<uri>> as 3
rectangle "2025" <<literal>> as 1
rectangle "[[<http://example.org/knows> :knows]]" <<uri>> as 4
cloud " " <<non_asserted>> as 5
rectangle "[[http://example.org/Statement :Statement]]" <<uri>> as 6
rectangle "[[http://example.org/dave :dave]]" <<uri>> as 2
0 --> 5 : [[http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies reifies]]
0 --> 2 : [[http://example.org/accordingTo :accordingTo]]
0 --> 1 : [[http://example.org/since :since]]
0 --> 6 : [[http://www.w3.org/1999/02/22-rdf-syntax-ns#type rdf:type]]
5-->3 #line:blue;line.dashed;text:blue : subj
5-->4 #line:red;line.dashed;text:red : pred
5-->2 #line:green;line.dashed;text:green : obj
@enduml
!python -m plantuml out.puml
^C
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/plantuml.py", line 230, in <module>
main()
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/plantuml.py", line 225, in main
print(list(map(lambda filename: {'filename': filename,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/plantuml.py", line 226, in <lambda>
'gen_success': pl.processes_file(filename, directory=args.out)}, args.files)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/plantuml.py", line 199, in processes_file
content = self.processes(data)
^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/plantuml.py", line 169, in processes
response, content = self.http.request(url, **self.request_opts)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/httplib2/__init__.py", line 1727, in request
(response, content) = self._request(
^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/httplib2/__init__.py", line 1447, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/site-packages/httplib2/__init__.py", line 1399, in _conn_request
response = conn.getresponse()
^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/http/client.py", line 1415, in getresponse
response.begin()
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/http/client.py", line 330, in begin
version, status, reason = self._read_status()
^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/http/client.py", line 291, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/socket.py", line 718, in readinto
return self._sock.recv_into(b)
^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt
Image(f"out.png")
In RDF 1.2 turtle syntax, there are several possibilities to define triple terms.
Declaring triple terms directly#
rudof.reset_all()
"rm" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
A triple term can be declared enclosing it between <<( and )>>. For example:
rudof.read_data("""
PREFIX : <http://www.example.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
_:e38 :familyName "Smith" .
_:anno rdf:reifies <<( _:e38 :jobTitle "Designer" )>> .
_:anno :accordingTo :eric .
""")
puml_content = rudof.serialize_data(ResultDataFormat.PlantUML)
with open('out.puml', 'w') as _f:
_f.write(puml_content)
!python -m plantuml out.puml
[{'filename': 'out.puml', 'gen_success': True}]
Image(f"out.png")
Reifying triples#
rudof.reset_all()
Enclosing a triple between << and >>, is a syntactic sugar that declares that there is a reifier whose object is that triple and that can be used to add more declarations about that reifier.
For example:
rudof.read_data("""
PREFIX : <http://www.example.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
:employee38 :familyName "Smith" .
<< :employee38 :jobTitle "Assistant Designer" >> :accordingTo :employee22 .
""")
puml_content = rudof.serialize_data(ResultDataFormat.PlantUML)
with open('out.puml', 'w') as _f:
_f.write(puml_content)
!python -m plantuml out.puml
Image(f"out.png")
[{'filename': 'out.puml', 'gen_success': True}]
Annotation syntax#
# Clean the workspace
rudof.reset_all()
"rm" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
It is also possible to use the annotation syntax where a statement can be annotated with {| and |} as follows:
rudof.read_data("""
PREFIX : <http://example.com/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
:alice :name "Alice" ~ :t {|
:statedBy :bob ;
:recorded "2021-07-07"^^xsd:date
|} .
""")
puml_content = rudof.serialize_data(ResultDataFormat.PlantUML)
with open('out.puml', 'w') as _f:
_f.write(puml_content)
!python -m plantuml out.puml
Image(f"out.png")
[{'filename': 'out.puml', 'gen_success': True}]
Notice that in the visualization, we differentiate between a statement that is part of the graph which is represented by a box from a statement that is not part of the graph, which is represented by a cloud symbol.
Tim Berners-Lee’s example#
# @title
rudof.reset_all()
Example declaring that Tim Berners-Lee worked for the CERN between 1984 and 1994 and in 1980, and that he received the Princess of Asturias Award in 2002 together with Vinton-Cerf.
rudof.read_data("""
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#>
:timbl rdfs:label "Tim Berners Lee" ;
:employer :CERN {| :start "1984" ;
:end "1994" |}
{| :start "1980" ;
:end "1980" |} ;
:award :PA {| :time "2002" ;
:togetherWith :vint |} .
:vint rdfs:label "Vinton Cerf" .
""")
Visualization:
puml_content = rudof.serialize_data(ResultDataFormat.PlantUML)
with open('out.puml', 'w') as _f:
_f.write(puml_content)
!python -m plantuml out.puml
Image(f"out.png")
[{'filename': 'out.puml', 'gen_success': True}]