Work with PROV-O (RDF)¶
PROV-O support needs the optional rdflib dependency:
python -m pip install "prov[rdf]"
The serialization format is selected with format="rdf". A second, RDF-specific keyword,
rdf_format, chooses the concrete RDF syntax ("trig" by default) — do not confuse the
two.
Serialize to a file¶
import prov.model as pm
document = pm.ProvDocument()
document.set_default_namespace("http://example.org/")
e = document.entity("e1")
a = document.activity("a1")
document.wasGeneratedBy(e, a)
document.serialize("document.trig", format="rdf") # rdf_format="trig" is the default
Choose a different RDF syntax¶
rdf_format accepts anything rdflib can serialize to, e.g. "turtle", "xml"
(RDF/XML), "nt", "nquads":
turtle_str = document.serialize(format="rdf", rdf_format="turtle")
print(turtle_str)
Important
Only quad-based syntaxes ("trig" — the default — and "nquads") preserve bundles as
separate named graphs. Triple-based syntaxes such as "turtle" or "xml" flatten every
bundle’s statements into a single graph, discarding which bundle each statement came from.
Stick with the default TriG if your document has bundles.
Serialize to a string¶
trig_str = document.serialize(format="rdf")
Deserialize from a file or stream¶
loaded = pm.ProvDocument.deserialize("document.trig", format="rdf")
assert loaded == document
Pass the matching rdf_format if the input is not TriG:
loaded = pm.ProvDocument.deserialize(content=turtle_str, format="rdf", rdf_format="turtle")
Deserialize from a string¶
loaded = pm.ProvDocument.deserialize(content=trig_str, format="rdf")
Auto-detect the format with prov.read()¶
PROV-O/RDF is the second format prov.read() tries (after PROV-JSON), so genuine RDF
content auto-detects reliably:
import prov
loaded = prov.read("document.trig")
assert loaded == document
Common errors¶
Malformed RDF raises rdflib’s own parser error:
try:
pm.ProvDocument.deserialize(content="not rdf", format="rdf")
except Exception as e:
print(f"{type(e).__name__}")
BadSyntax