module documentation

The mutation chokepoint: the one place a Binding's nodes are made.

Declared Bindings create their nodes once at init and never move them. The three cases where the address space grows at run time (graph-driven, client-driven and content-driven) all go through here. This module renders a Projection's plan under an opensmi container (ROS2MutationMixin.render_plan) and announces each batch as one GeneralModelChangeEvent (nothing is announced at init). Nodes are created and added to, never removed.

Structure is nesting. A message becomes one complex Variable whose value is the whole structure, with a child Variable per field and a nested complex Variable per nested message, and a group (a Binding, a ROS2 parameter namespace, a Keyed member) is a variable group. A message is written once, at the root of its complex Variable, and opensmi brings the children in step. A plan with a leaf no structure field can carry (a DateTime, say) renders its levels as variable groups of plain Variables instead. Growth is capped, per Binding (ROS2MutationMixin.mutation_node_cap) and per Machine (ROS2MutationMixin.machine_mutation_node_cap). Reaching a cap refuses the whole addition and reports it as a BindingEvent. Caps count mutations only, not what a declaration asks for at init.

Class NodeAddition One plan's nodes to create in one container, on behalf of one Binding.
Class RenderedPlan The nodes one plan became, written and read by the names the plan gave its entries.
Class ROS2MutationMixin The one door through which a machinery item's Monitoring may grow.
Async Function add_plan_variable Turn one plan entry into an opensmi UaVariable, add it to a container, and type it.
Async Function ended Cancel one task and wait for it, tolerating a task that has already finished.
Async Function ensure_groups Return the variable group at the end of a chain of levels, creating those that do not exist yet.
Function plan_value Return what a variable stores in the form a projection builds a message from.
Function require_unused_name Refuse a variable name something on this container has already claimed.
Function wire_value Return a plan value in the form its variable stores.
Constant CAP_REACHED The BindingEvent code a refused addition carries, for a client that wants to know why.
Class _Complex The leaves below one path rendered as a complex Variable, which holds their whole structure.
Class _Plain A plan entry rendered as a Variable of its own.
Async Function _add Add a variable to a container, leaving nothing registered when that fails.
Function _node_added Return the model change one created node is, with the type it was instantiated as.
Function _planned_nodes Return how many nodes one addition creates: its member group, its levels and its leaves.
Function _record Note a node made and the fields below it, for the model-change event.
Function _require_derived_names Refuse a plan whose names opensmi would not derive from its field names.
Function _require_one_definition_per_name Refuse two value sets minted under one DataType name.
Function _split Sort a plan into its leaves, the entries that are Variables of their own, and the structures.
Function _structure_write_check Return the write check of a structure, or of an array of one: the codec's, then the caller's.
Async Function _write_display_name Write a node's DisplayName, which is where the raw ROS2 name, key or frame id goes.
Async Function _write_unbounded Publish an array's length as unknown. Through write_attribute, as asyncua types it as one int.
Constant _ENUM_NAMESPACE The namespace enumeration DataTypes are minted into: the server's own.
Constant _MACHINERY_STATE The AddIns a Status group references, by their browse names below MachineryBuildingBlocks.
Constant _NODE_ADDED ModelChangeStructureVerb.NodeAdded, announced for every creation here.
Constant _STATUS The Machinery Monitoring sub-structure that references the item's state machines (OPC 40001-1 section 16.1).
Constant _UNKNOWN_LENGTH The ArrayDimensions entry every array variable publishes: length unknown.
async def add_plan_variable(container: UaVariableContainer, name: str, entry: PlanVariable, datatype: ua.NodeId | None, *, writable: bool = False, write_check: Callable[[Any], Awaitable[None]] | None = None, unit: str | None = None, value_range: tuple[float, float] | None = None) -> UaVariable[Any]:

Turn one plan entry into an opensmi UaVariable, add it to a container, and type it.

This is the one place DataType, ValueRank and ArrayDimensions of a plain Variable are decided. The variable is created with an explicit Variant (an empty sequence implies no type, and opensmi writes later values with the initial VariantType) and with these attributes:

  • ValueRank OneDimension for an array, Scalar otherwise.
  • ArrayDimensions [0] for an array (length unknown), written after the add since opensmi has no parameter for it. A bounded ROS2 sequence publishes no bound.
  • DataType: the plan's own, else the built-in type of the variant type. opensmi refuses one on a variable with a unit (an AnalogUnitRangeType), so that one is written after the add.

An array of structures (plan DataType a structure class) is created empty, with the resolved DataType. Its write check refuses with BadTypeMismatch a value that is not an array of that structure, then runs the caller's write_check.

A failed add leaves nothing registered: the variable is removed from the container again and any variable it replaced is put back. The attribute writes pass no write check.

Parameters
container:UaVariableContainerThe opensmi container to add the variable to.
name:strThe variable's BrowseName.
entry:PlanVariableThe plan entry it is made from. Its DisplayName is the raw ROS2 name.
datatype:ua.NodeId | NoneThe DataType the plan names, resolved on this server, or None. Required for an array of structures; its namespace index is the structure's encoding namespace.
writable:boolWhether a client may write it.
write_check:Callable[[Any], Awaitable[None]] | NoneAn async check opensmi awaits before storing a written value.
unit:str | NoneThe engineering unit's symbol, for a variable that is not a ROS2 field. opensmi requires a range with it.
value_range:tuple[float, float] | NoneThe bounds opensmi publishes as an EURange and refuses a write outside.
Returns
UaVariable[Any]Undocumented
Raises
DeclarationErrorWhen the plan names a structure for something that is not an array, or no resolved DataType comes with it.
async def ended(task: asyncio.Task[None] | None):

Cancel one task and wait for it, tolerating a task that has already finished.

async def ensure_groups(container: UaVariableContainer, levels: Iterable[NodeLevel]) -> UaVariableContainer:

Return the variable group at the end of a chain of levels, creating those that do not exist yet.

A level's name is its browse name and its display name the raw ROS2 name. A group two plans share (a ROS2 parameter namespace) is created by the first and found by the second.

def plan_value(entry: PlanVariable, stored: object) -> object:

Return what a variable stores in the form a projection builds a message from.

The inverse of wire_value: an array of structures comes back as maps.

Raises
ValueErrorWhen an array of structures holds something else, which the write check normally refuses first.
def require_unused_name(name: str, taken: Iterable[Container[str]], where: str):

Refuse a variable name something on this container has already claimed.

opensmi's container has no such check: it files a variable with setattr, so a second one would replace the first. Shared by the chokepoint and by Skills and Methods.

Parameters
name:strThe browse name being claimed.
taken:Iterable[Container[str]]Every place a name may already be recorded, tested in order.
where:strWhat to call the container in the refusal, from a reader's point of view.
Raises
DuplicateVariableNameErrorWhen the name is taken.
def wire_value(entry: PlanVariable, value: object) -> object:

Return a plan value in the form its variable stores.

An array of structures stores instances of the structure's dataclass. Other variables store the value as is.

Raises
ValueErrorWhen the value for an array of structures is not a list.
CAP_REACHED: str =

The BindingEvent code a refused addition carries, for a client that wants to know why.

Value
'NODE_CAP_REACHED'
async def _add(container: UaVariableContainer, name: str, variable: UaVariable[Any]):

Add a variable to a container, leaving nothing registered when that fails.

opensmi files a variable before creating its node, so a failed add would leave a node-less variable that breaks later iteration, and would replace what the name held. Both are undone.

def _node_added(node: Node, affected_type: ua.NodeId | int) -> ua.ModelChangeStructureDataType:

Return the model change one created node is, with the type it was instantiated as.

def _planned_nodes(addition: NodeAddition) -> int:

Return how many nodes one addition creates: its member group, its levels and its leaves.

def _record(changes: list[ua.ModelChangeStructureDataType] | None, variable: UaVariable[Any]):

Note a node made and the fields below it, for the model-change event.

def _require_derived_names(root: str, leaves: Sequence[PlanVariable], path: NodePath):

Refuse a plan whose names opensmi would not derive from its field names.

The children of a complex Variable are named by opensmi from the field names of the structure, which are the DisplayNames: child_frame_id is ChildFrameId. A name that is not already spelled that way (a Projection that names an entry data) would browse as another name than the plan says, so it is refused instead of renamed silently.

Raises
DeclarationErrorWhen a name on a node path differs from the one opensmi derives.
def _require_one_definition_per_name(wanted: EnumDataType, minted: EnumDataType):

Refuse two value sets minted under one DataType name.

Reachable only when two ROS2 types with the same last name part are curated on one Component. Sharing the first definition would give a client another type's labels.

Raises
DuplicateProjectionErrorWhen the two do not agree.
def _split(plan: Sequence[PlanEntry], root: str | None, where: str) -> tuple[list[PlanVariable], list[tuple[str, PlanVariable]], dict[str, list[PlanVariable]]]:

Sort a plan into its leaves, the entries that are Variables of their own, and the structures.

The entries are listed with the name their Variable takes; a structure by its top-level name, which is root when there is one.

Raises
UnprojectableFieldErrorWhen an entry is an Object or has no name.
def _structure_write_check(variable: Callable[[], UaVariable[Any]], name: str, structure: type, then: Callable[[Any], Awaitable[None]] | None, *, array: bool = True) -> Callable[[Any], Awaitable[None]]:

Return the write check of a structure, or of an array of one: the codec's, then the caller's.

It refuses with BadTypeMismatch a value that is not one (or an array of them), a null field, or a fixed-size array of the wrong length, which the encoding does not publish.

Parameters
variable:Callable[[], UaVariable[Any]]The variable the check belongs to, resolved lazily since the check is made first.
name:strUndocumented
structure:typeUndocumented
then:Callable[[Any], Awaitable[None]] | NoneThe caller's check, awaited when the value passed.
array:boolWhether the variable holds an array of the structure and not one.
Returns
Callable[[Any], Awaitable[None]]Undocumented
async def _write_display_name(node: Node, text: str):

Write a node's DisplayName, which is where the raw ROS2 name, key or frame id goes.

async def _write_unbounded(node: Node):

Publish an array's length as unknown. Through write_attribute, as asyncua types it as one int.

_ENUM_NAMESPACE: int =

The namespace enumeration DataTypes are minted into: the server's own.

Value
1
_MACHINERY_STATE: tuple[str, ...] =

The AddIns a Status group references, by their browse names below MachineryBuildingBlocks.

Value
('MachineryItemState', 'MachineryOperationMode')
_NODE_ADDED =

ModelChangeStructureVerb.NodeAdded, announced for every creation here.

Value
ua.Byte(1)
_STATUS: str =

The Machinery Monitoring sub-structure that references the item's state machines (OPC 40001-1 section 16.1).

Value
'Status'
_UNKNOWN_LENGTH =

The ArrayDimensions entry every array variable publishes: length unknown.

Value
ua.UInt32(0)