Create objects and set attributes

Create objects and set attributes#

A Model owns a namespace and the objects created in it. The examples use WaTr; S223 works the same way.

Create objects#

Objects created inside with model: join the model. The name becomes a local name in the namespace:

from plumb import Model
from plumb.watr import Pump, SedimentationTank, Tank, enums

model = Model("urn:example:plant#")
with model:
    intake = Pump("intake-pump")
    clarifier = SedimentationTank("clarifier")

model["clarifier"]
<SedimentationTank 'clarifier'>

Unnamed objects are numbered per class:

with model:
    spare = Pump()
    another_spare = Pump()
spare.name, another_spare.name
('pump_1', 'pump_2')

Outside a with block, pass model=:

clearwell = Tank("clearwell", model=model)
clearwell.meta.instance_iri
'urn:example:plant#clearwell'

Methods on existing objects, such as .port() and .sensor(), use the object’s own model.

Set attributes#

A property the shapes require is a required keyword argument:

from plumb.watr import DomainSpace

try:
    DomainSpace("zone", model=model)
except TypeError as error:
    print(error)
DomainSpace.__init__() missing 1 required keyword-only argument: 'has_domain'

A requirement that the class itself fixes is filled in. Every WaTr sedimentation tank performs settling:

list(clarifier.has_process)
[plumb.watr.Process_Settling]

A property with at most one value is a plain attribute. Others are set-like collections with add(), remove(), and clear(); assignment replaces the contents and takes an iterable:

clearwell.label = "Clearwell"
clearwell.has_role.add(enums.values.Role_Storage)
clearwell.label, list(clearwell.has_role)
('Clearwell', [<enum Role-Storage>])

Values are type-checked when set:

from plumb.watr import Process_Settling

try:
    clearwell.has_role.add(Process_Settling)
except Exception as error:
    print(type(error).__name__, error)
RangeError Tank.has_role expects an enum constant (from the generated enums module), got type (<class 'plumb.watr.Process_Settling'>)

Every object also has name, type, label, and comment. Inverse properties stay in sync. An ontology property whose name collides with the object API, such as contains or walk, gets a trailing underscore: contains_.

Next#