Validate a model

Validate a model#

validate() checks the resolved RDF against the SHACL shapes bundled with each package, using shifty. It applies S223’s and WaTr’s SHACL-AF rules first and maps each result back to a Python object.

from plumb import Model, connect
from plumb.watr import Pump, Tank
from plumb.watr.drinking_water import ClarificationStage

model = Model("urn:example:validate#")
with model:
    intake = Pump("intake-pump")
    clarifier = ClarificationStage("clarifier")
    sludge_tank = Tank("sludge-tank")
connect(intake, clarifier.inlet, name="raw-water")

report = model.validate()
report.ok
False

Read the report#

for issue in report.violations:
    print(issue.entity, issue.path)
    print("  ", issue.message)
<Tank 'sludge-tank'> http://data.ashrae.org/standard223#hasConnectionPoint
   A Tank must have at least one fluid connection point; a bidirectional port is sufficient.

The sludge tank has no connection point. Connect it:

connect(clarifier.sludge, sludge_tank, name="sludge-draw")
model.validate().ok
True

report.issues also holds warnings and informational notices, which do not affect report.ok. An unconnected boundary point is a notice.

Add your own shapes#

Pass a Turtle path or an rdflib.Graph:

model.validate("site-rules.ttl")

infer=False skips the SHACL-AF rules.

Compile#

compile() resolves, validates, and returns the snapshot, or raises ValidationError:

resolved = model.compile()
resolved["sludge-draw"]
<Pipe 'sludge-draw'>

compile(validate=False) returns a snapshot without validating.

Next#

save() and graph() resolve but do not validate; validate first when the file must conform. See Save, load, and edit models.