Skip to content

hololinked.metadata.td.interaction_affordance.EventAffordance

Bases: InteractionAffordance, EventMetadata

creates event affordance schema from events.

Schema
UML Diagram

Source code in repo/hololinked/hololinked/metadata/td/interaction_affordance.py
class EventAffordance(InteractionAffordance, EventMetadata):
    """
    creates event affordance schema from events.

    [Schema](https://www.w3.org/TR/wot-thing-description11/#eventaffordance) <br>
    [UML Diagram](https://docs.hololinked.dev/UML/PDF/InteractionAffordance.pdf) <br>
    """

    # [Supported Fields]() <br>
    subscription: str | None = None
    data: JSON | None = None

    def __init__(self):
        super().__init__()

    @property
    def what(self):  # noqa: D102
        return ResourceTypes.EVENT

    @property
    def event_unique_identifier(self) -> str:
        """Identifier this event is published under, as `<thing id>/<name>`."""
        return f"{self.thing_id}/{self.name}"

    def build(self) -> None:  # noqa: D102
        event = cast(Event, self.objekt)
        doc = event.__doc__ or event.doc
        if doc:
            title = get_summary(doc)
            description = self.format_doc(doc)
            if title and not description.startswith(title):
                self.title = title
                self.description = description
            else:
                self.description = description
        if event.schema:
            if isinstance(event.schema, dict):
                self.data = event.schema
            elif issubklass(event.schema, (BaseModel, RootModel)):
                self.data = type_to_dataschema(event.schema)
            else:
                raise ValueError(f"unknown schema definition for event data, given type: {type(event.schema)}")

    @classmethod
    def from_descriptor(cls, event: Event, owner: Thing | ThingMeta, **kwargs) -> EventAffordance:  # noqa: D102
        if not isinstance(event, Event):
            raise TypeError(f"event must be instance of Event, given type {type(event)}")
        affordance = EventAffordance()
        affordance.owner = owner
        affordance.objekt = event
        affordance.build()
        affordance.build_non_compliant_metadata()
        return affordance

    def to_descriptor(self, validator: str = "pydantic") -> Event:
        """
        Create the `Event` this affordance describes.

        Parameters
        ----------
        validator: str
            name of the validator, registered with `SchemaValidators`, the event data schema is converted for.
            The data is not validated on push, so no validator is preset.

        Returns
        -------
        Event
            the descriptor, not yet assigned to a class
        """
        schema = None
        if self.data:
            schema, _ = schema_validator(self.data, f"{self.name}_data", validator)
        return Event(doc=self.description, label=self.title, schema=schema)

Attributes

event_unique_identifier property

event_unique_identifier: str

Identifier this event is published under, as <thing id>/<name>.

Functions

to_descriptor

to_descriptor(validator: str = 'pydantic') -> Event

Create the Event this affordance describes.

Parameters:

Name Type Description Default

validator

str

name of the validator, registered with SchemaValidators, the event data schema is converted for. The data is not validated on push, so no validator is preset.

'pydantic'

Returns:

Type Description
Event

the descriptor, not yet assigned to a class

Source code in repo/hololinked/hololinked/metadata/td/interaction_affordance.py
def to_descriptor(self, validator: str = "pydantic") -> Event:
    """
    Create the `Event` this affordance describes.

    Parameters
    ----------
    validator: str
        name of the validator, registered with `SchemaValidators`, the event data schema is converted for.
        The data is not validated on push, so no validator is preset.

    Returns
    -------
    Event
        the descriptor, not yet assigned to a class
    """
    schema = None
    if self.data:
        schema, _ = schema_validator(self.data, f"{self.name}_data", validator)
    return Event(doc=self.description, label=self.title, schema=schema)
field supported meaning default usage
subscription ✔️ schema of the input required to invoke action input_schema value of action decorator
data ✔️ schema of the output returned by the action output_schema value of action decorator