---
file_format: mystnb
kernelspec:
  name: python3
---

# Ontologies, WaTr vocabulary, and domain extensions

Plumb compiles standard ontologies (ASHRAE 223P, WaTr, Brick) and smaller
domain extensions into Python packages that interoperate cleanly. This page
explains how those layers relate, how the WaTr vocabulary separates
equipment concepts, and when to write a domain extension of your own.

## How layered ontologies share identity

`plumb.watr` builds on `plumb.s223` (ASHRAE 223P) without duplicating or
mutating it:

- **Unchanged S223 classes are reused directly.** `watr.Pipe is s223.Pipe`,
  and shared enumeration constants like `Fluid_Water` are the exact same
  Python objects across both packages.
- **Extended classes become typed views.** When WaTr adds properties or
  hierarchy edges to an S223 class, Plumb generates a subclass whose
  `meta.canonical` points back to the original S223 class. Both work
  interchangeably in S223 property ranges.
- **Name collisions favor the primary ontology.** Both S223 and WaTr define
  a `Pump` class: `watr.Pump` is WaTr's pump (which accepts any `Mix-Fluid`,
  including sludge), while `watr.Pump_2` is S223's pump (water, oil, or
  refrigerant only). Check `cls.meta.iri` whenever a name appears in two
  vocabularies.
- **Semantic RDF ancestry is always authoritative.** When multiple
  inheritance in an ontology cannot be linearized directly in Python's C3
  MRO, Plumb preserves the full RDF ancestry in `cls.meta.is_a(...)` and
  uses it across range checks, containment, queries, and hydration.

## The WaTr vocabulary mental model

`plumb.watr` is compiled from WaTr
[v0.3.0.rc1](https://github.com/DataDrivenCPS/water-ontology/releases/tag/v0.3.0.rc1).
WaTr separates four questions about every unit operation so you never have
to guess where a concept belongs:

| Question | Attribute | Example |
| --- | --- | --- |
| What physical unit is it? | Python class (`rdf:type`) | `GravityThickener` |
| What physical/chemical operation does it perform? | `has_process` | `Process_Settling` |
| What is that operation trying to achieve? | `has_treatment_objective` | `TreatmentObjective_Thickening` |
| What is its role in *this* plant? | `has_role` | `enums.values.Role_Primary` |

A `SedimentationTank` and a `GravityThickener` both perform
`Process_Settling`, but their objectives differ (clarifying water vs.
thickening sludge), and a unit's role depends on how you deploy it.

Processes and objectives are ontology classes used directly as values; roles
are enumeration values under `enums.values`.

### Class defaults vs. SHACL rules

When an equipment class always performs a process or objective, both Python
constructors and WaTr's `ClassDefaultsRule` know about it:

- **Python constructors** fill in the most specific class default unless you
  supply that term or a narrower subclass.
- **SHACL inference** (`validate()` or `Model.from_graph(..., infer=True)`)
  runs `ClassDefaultsRule` and `ProcessObjectiveRule` (for example,
  inferring disinfection from `Process_Chlorination`).
- **`Model.from_graph()` without `infer=True`** keeps loaded graphs exactly
  as written without injecting defaults.

### Media and mixtures

WaTr defines freshwater as containing H₂O, and seawater, brackish water, and
brine as containing H₂O and NaCl. `Fluid_Sludge` is a `Mix_Fluid` of water
and solids — it is *not* a subclass of `Fluid_Water`, so a water-only port
rejects a sludge connection. Bundled SHACL shapes also check constituent
percentages (`CompositionPercentageShape`) and infer a single missing
percentage (`CompositionComplementRule`).

## Domain extensions: Python vs. SHACL

A **domain extension** (such as `plumb.watr.drinking_water`,
`plumb.watr.asm2d`, `plumb.watr.seawater_ro`, or `plumb.s223.hvac`) is a
small ontology layered on top of WaTr or S223. It uses SHACL to specialize
generic equipment classes with named ports, required processes/roles, and
compiled `PropertySchema` value sets.

Decide how much structure you need based on how widely it will be reused:

| Goal | Best tool |
| --- | --- |
| Model one plant quickly | Base classes, `connect()`, and `.port()` |
| Reuse a group of quantities in one script or notebook | A Python `PropertySchema` subclass with `quantity_field()` |
| Reuse named equipment layouts across many models | A compiled domain extension with named ports |
| Enforce rules in SHACL validation or share definitions with non-Python RDF tools | A compiled domain extension |

### Worked example: `plumb.watr.drinking_water`

`plumb.watr.drinking_water` is compiled from `ontology/drinking-water.ttl`.
Each stage subclasses a WaTr unit and fixes its process, objective, and
exact connection points:

| Stage | WaTr parent | Named points |
| --- | --- | --- |
| `CoagulationStage` | `CoagulationBasin` | `inlet`, `outlet` |
| `FlocculationStage` | `FlocculationBasin` | `inlet`, `outlet` |
| `ClarificationStage` | `SedimentationTank` | `inlet`, `clarified_water`, `sludge` |
| `RapidSandFiltrationStage` | `RapidSandFilter` | `inlet`, `filtered_water`, `backwash_supply`, `backwash_waste` |
| `ChlorinationStage` | `ChlorinationUnit` | `inlet`, `outlet` |

Because `ClarificationStage` subclasses `SedimentationTank` in both Python
and RDF, queries for `SedimentationTank` automatically find it, and every
SHACL `sh:name` becomes a typed attribute on the instance:

```{code-cell} python3
from plumb import Model
from plumb.watr import SedimentationTank
from plumb.watr.drinking_water import ClarificationStage

with Model("urn:example:extension#"):
    clarifier = ClarificationStage("clarifier")

print("is a SedimentationTank:", isinstance(clarifier, SedimentationTank))
print("processes:", list(clarifier.has_process))
print("sludge port:", clarifier.sludge)
```

To build your own extension, follow
[Write and compile a domain package](../how-to/create-a-domain-package.md)
and consult the [Compiler and extension reference](../reference/compiler.md).
