Save, load, and edit existing RDF graphs#
Save a Plumb model as RDF, load existing RDF graphs back as typed Python objects, edit attributes, and extend loaded models with new equipment and deferred connections — while preserving untouched RDF triples.
Save a model#
save() resolves the model and serializes it to RDF. The format is inferred
from the file extension (.ttl, .nt, .xml/.rdf, .jsonld) or set
with format=. graph() returns a fresh rdflib.Graph.
from rdflib import OWL
from plumb import Model, connect
from plumb.watr import Pump, Tank
from plumb.watr.drinking_water import ClarificationStage, CoagulationStage
with Model("urn:example:plant#") as authored:
intake = Pump("intake-pump", label="Intake pump")
coagulation = CoagulationStage("coagulation")
clarifier = ClarificationStage("clarifier")
holding = Tank("sludge-holding")
connect(intake, coagulation.inlet, name="raw-water")
connect(coagulation.outlet, clarifier.inlet, name="coagulated-water")
connect(clarifier.sludge, holding, name="sludge-line")
authored.save("plant.ttl")
# Saved graphs declare an owl:Ontology header importing every vocabulary used.
graph = authored.graph()
for _, _, dep in graph.triples((None, OWL.imports, None)):
print(dep, graph.value(dep, OWL.versionInfo))
http://data.ashrae.org/standard223/1.0/model/all git:master@6fdc11c60b930ccb042d775b66a9115547fd22a7 date:2026-10-06 20:01:58 UTC
https://watermetadata.org/ontology/watr 0.3.rc1
https://watermetadata.org/plumb/drinking-water# None
To customize ontology metadata on a new model, pass name=,
ontology_iri=, imports=, or prefixes= to Model(...).
If you share a model that uses a domain package (such as drinking_water)
with a tool that cannot fetch the extension’s ontology by IRI, call
export_ontology() to write the extension schema alongside your data:
schema = authored.export_ontology("plant-schema.ttl")
len(schema)
185
Load an RDF graph#
Model.from_graph() accepts a file path, URL, or rdflib.Graph. It reads
the graph’s owl:imports to select the right installed packages lazily,
verifies any pinned owl:versionInfo, and hydrates every recognized resource
as its most specific Python class:
loaded = Model.from_graph("plant.ttl")
loaded.by_class()
{plumb.watr.drinking_water.ClarificationStage: [<ClarificationStage 'clarifier'>],
plumb.s223.OutletConnectionPoint: [<OutletConnectionPoint 'clarifier-clarified_water'>,
<OutletConnectionPoint 'clarifier-sludge'>,
<OutletConnectionPoint 'coagulation-outlet'>,
<OutletConnectionPoint 'intake-pump-out'>],
plumb.s223.InletConnectionPoint: [<InletConnectionPoint 'clarifier-inlet'>,
<InletConnectionPoint 'coagulation-inlet'>,
<InletConnectionPoint 'intake-pump-in'>,
<InletConnectionPoint 'sludge-holding-in'>],
plumb.s223.Pipe: [<Pipe 'coagulated-water'>,
<Pipe 'raw-water'>,
<Pipe 'sludge-line'>],
plumb.watr.drinking_water.CoagulationStage: [<CoagulationStage 'coagulation'>],
plumb.watr.Pump: [<Pump 'intake-pump'>],
plumb.watr.Tank: [<Tank 'sludge-holding'>]}
Named ports come back automatically from RDF facts, so
loaded["clarifier"].sludgeworks just as it did when authored.Typed blank nodes are hydrated too, keeping their original blank-node identity across edits, snapshots, and validation.
Existing concrete pipes and points load as regular objects, so flow walks (
loaded["intake-pump"].find_all_next()) work immediately without re-solving.
When owl:imports is missing or uses a custom package#
Plumb does not guess packages from rdf:type namespaces. If a graph lacks
owl:imports, or uses a package you compiled yourself, pass registries=:
from rdflib import Graph
from plumb.watr import drinking_water
bare = Graph().parse("plant.ttl")
bare.remove((None, OWL.imports, None))
loaded_bare = Model.from_graph(bare, registries=drinking_water.REGISTRY)
type(loaded_bare["clarifier"]).__name__
'ClarificationStage'
Pass a list when combining independent packages, e.g.
registries=[watr.REGISTRY, s223.hvac.REGISTRY]. Custom packages compiled
with plumb gen can also opt into automatic discovery by registering a
plumb.ontologies Python entry point mapping the ontology IRI to
mypkg:REGISTRY.
Unknown classes, permissive loading, and inference#
Unknown RDF classes stay safely in the model’s backing RDF graph without becoming Python objects.
If a generated attribute points at an unknown resource or conflicts with scalar cardinality,
strict=True(the default) raisesModelingError. Passstrict=Falseto skip unconvertible values while preserving their original triples inmodel.graph().Pass
infer=Trueto run SHACL-AF inference rules before hydrating Python objects (passshapes=to override the bundled rules).
Edit and extend a loaded model#
A loaded model owns a private copy of its imported RDF — editing the model
never mutates a rdflib.Graph you passed in, and each call to
model.graph() returns a fresh graph.
Editing generated attributes updates only the affected predicates, preserving
untouched triples, literal datatypes, and language tags. Introducing a new
vocabulary automatically adds its owl:imports entry and a readable prefix.
You can also extend a loaded model with new equipment, deferred
connect(), .port(), and model.system() — Plumb resolves the newly
added intentions into a snapshot alongside all original RDF:
from plumb.watr import enums
from plumb.watr.drinking_water import RapidSandFiltrationStage
loaded = Model.from_graph("plant.ttl")
loaded["clarifier"].label = "Clarifier 1"
loaded["clarifier"].has_role.add(enums.values.Role_Primary)
# Add a new stage and connect it to the loaded clarifier's free outlet
with loaded:
sand_filter = RapidSandFiltrationStage("sand-filter")
connect(loaded["clarifier"].clarified_water, sand_filter.inlet, name="clarified-water")
resolved = loaded.compile()
resolved["intake-pump"].find_all_next()
[<CoagulationStage 'coagulation'>,
<ClarificationStage 'clarifier'>,
<Tank 'sludge-holding'>,
<RapidSandFiltrationStage 'sand-filter'>]
Save the extended graph back out:
loaded.save("plant-extended.ttl")