# Write and compile a domain package

A **domain package** is a small ontology layered on WaTr or S223 that
pre-defines reusable equipment classes (with named ports and required roles
or processes) and property schemas (groups of validated quantities). Write
one when a pattern outlives a single script or needs to be enforced by SHACL
across models.

## 1. Write the source ontology in Turtle

Save the file below as `pumps.ttl`. It uses four pieces:

1. An `owl:Ontology` header with `codegen:baseModule` naming the installed
   Plumb package it extends (`"plumb.s223"` or `"plumb.watr"`).
2. A subclass (`ex:SupplyPump`) paired with a SHACL `NodeShape` fixing its
   role (`sh:hasValue`) and two named connection points (`sh:name` +
   `sh:qualifiedValueShape`).
3. A `sh:NodeShape` marked `codegen:projection codegen:PropertySchema`, where
   each `s223:hasProperty` field specifies a QUDT quantity kind, unit
   default/choices, datatype, and numeric bounds.

```turtle
@prefix ex: <urn:example:pumps#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix s223: <http://data.ashrae.org/standard223#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix qudt: <http://qudt.org/schema/qudt/> .
@prefix kind: <http://qudt.org/vocab/quantitykind/> .
@prefix unit: <http://qudt.org/vocab/unit/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix codegen: <https://watermetadata.org/plumb/codegen#> .

ex: a owl:Ontology ;
    owl:imports <http://data.ashrae.org/standard223/1.0/model/all> ;
    codegen:baseModule "plumb.s223" .

ex:SupplyPump a owl:Class ;
    rdfs:subClassOf s223:Pump .

ex:SupplyPumpShape a sh:NodeShape ;
    sh:targetClass ex:SupplyPump ;
    sh:node ex:PressureDesignShape ;
    sh:property
        [ sh:path s223:hasRole ; sh:hasValue s223:Role-Supply ],
        [ sh:path s223:hasConnectionPoint ; sh:name "inlet" ;
          sh:qualifiedMinCount 1 ; sh:qualifiedMaxCount 1 ;
          sh:qualifiedValueShape ex:Inlet ],
        [ sh:path s223:hasConnectionPoint ; sh:name "outlet" ;
          sh:qualifiedMinCount 1 ; sh:qualifiedMaxCount 1 ;
          sh:qualifiedValueShape ex:Outlet ],
        [ sh:path s223:hasConnectionPoint ; sh:minCount 2 ; sh:maxCount 2 ] .

ex:Inlet sh:class s223:InletConnectionPoint ;
    sh:property
        [ sh:path s223:hasMedium ; sh:hasValue s223:Fluid-Water ],
        [ sh:path rdfs:label ; sh:hasValue "inlet" ] .

ex:Outlet sh:class s223:OutletConnectionPoint ;
    sh:property
        [ sh:path s223:hasMedium ; sh:hasValue s223:Fluid-Water ],
        [ sh:path rdfs:label ; sh:hasValue "outlet" ] .

ex:PressureDesignShape a sh:NodeShape ;
    codegen:projection codegen:PropertySchema ;
    codegen:pythonName "PressureDesign" ;
    sh:property [ sh:path s223:hasProperty ; sh:name "pressure" ;
        sh:qualifiedMinCount 1 ; sh:qualifiedMaxCount 1 ;
        sh:qualifiedValueShape ex:Pressure ] .

ex:Pressure sh:class s223:QuantifiableProperty ;
    sh:property
        [ sh:path s223:hasValue ; sh:datatype xsd:double ;
          sh:minCount 1 ; sh:maxCount 1 ; sh:minInclusive 0.0 ],
        [ sh:path qudt:hasQuantityKind ; sh:hasValue kind:Pressure ],
        [ sh:path qudt:hasUnit ; sh:defaultValue unit:PA ;
          sh:in (unit:PA unit:KiloPA unit:BAR) ],
        [ sh:path s223:hasAspect ; sh:hasValue s223:Aspect-Nominal ] .
```

Key SHACL patterns compiled into Python:

- `sh:hasValue` inserts a required value and protects it from removal.
- `sh:defaultValue` supplies a replaceable default at construction time.
- `sh:in` restricts values to a finite set of allowed choices.
- Each named port needs a fixed direction, exact medium, exact count, and
  distinguishing facts (such as `rdfs:label`) when two ports share a
  direction and medium.

See the [Compiler and extension reference](../reference/compiler.md) for the
complete list of supported constructs.

## 2. Compile the package

Run `plumb gen` to generate a self-contained Python package with its bundled
`_shapes.ttl`, compiler symbols, and support report:

```bash
plumb gen --file pumps.ttl --name Pumps --out pumps
```

## 3. Use and validate it

```python
from plumb import Model
from plumb.qudt import QuantityAmount as Q, units
from pumps import PressureDesign, SupplyPump

with Model("urn:example:plant#") as model:
    pump = SupplyPump("pump")
    pump.apply_properties(PressureDesign(pressure=Q(7.0, units.BAR)))

assert model.validate().ok
```

A bare number (`PressureDesign(pressure=700_000)`) uses `PA`, the source
default. Validation automatically checks both `pumps/_shapes.ttl` and the
base S223 shapes.

## 4. Load saved models cleanly

Pass your package's `REGISTRY` to `Model.from_graph()`:

```python
from plumb import Model
import pumps

model.save("plant.ttl")
loaded = Model.from_graph("plant.ttl", registries=pumps.REGISTRY)
assert isinstance(loaded["pump"], SupplyPump)
```

If you distribute your generated package as an installable Python wheel, you
can register an entry point in `pyproject.toml` so `Model.from_graph()`
discovers it automatically from `owl:imports`:

```toml
[project.entry-points."plumb.ontologies"]
"urn:example:pumps" = "pumps:REGISTRY"
```

## 5. Review ontology upgrades

When you revise `pumps.ttl` later, save a copy of the previous generated
directory, recompile, and compare with `plumb diff --check`:

```bash
plumb diff pumps-prev pumps --check
```

`plumb diff` separates contract changes from source/build metadata, and
`--check` exits with status `1` if any surviving constraint lost
constructor, runtime, or solver enforcement.

To regenerate Plumb's own bundled packages (`s223`, `watr`, `drinking_water`,
`asm2d`, `seawater_ro`, `hvac`, and `qudt`), run `make regen`.
