unaiverse.hsm.state¶
This module holds State, a node of a HybridStateMachine. A state has a unique name and three optional properties that change how the machine behaves while it sits there: (1) an inner Action that runs at every pass of act(), (2) a blocking flag that decides whether act() stops when the machine enters the state and (3) a waiting time during which only requested actions can fire. A state can also carry a message that the machine prints on the user log channel when it enters the state. The JSON form of a state is described in the states section of the behavior format and the role of states in a behavior in Behaviors.
A developer rarely creates a State directly. HybridStateMachine.add_state(), add_transit() and load() create the states and keep them in hsm.states. Reading a state is common in custom code that inspects the machine:
state = agent.behav.get_state()
if state is not None and state.must_wait():
print(f"{state.name}: {state.get_time_passed():.1f} s of {state.waiting_time} s")
A world can also adjust a state after building it in code, as the example worlds do:
State
¶
State(name: str, idx: int = -1, action: Action | None = None, waiting_time: float = 0.0, blocking: bool = True, msg: str | None = None)
Source code in unaiverse/hsm/state.py
A node of a state machine. The machine creates the object and links it to itself with set_state_machine().
| Argument | Type | Default | Meaning |
|---|---|---|---|
name |
str |
required | The unique name of the state in the machine. A name in Custom.NOT_ALLOWED_STATE_NAMES (that is "all") raises GenException. |
idx |
int |
-1 |
The numeric id of the state. The machine passes the index of the state in its list. |
action |
Action \| None |
None |
The inner action. |
waiting_time |
float |
0. |
The JSON field time_to_wait_before_acting in seconds. |
blocking |
bool |
True |
The JSON field blocking. |
msg |
str \| None |
None |
The message of the state. HTML entities are decoded (👋 becomes the character it encodes). |
| Attribute | Type | Meaning |
|---|---|---|
name, id, action, waiting_time, blocking, msg |
The constructor arguments. msg holds the message after the replacement of the wildcards. |
|
msg_with_wildcards |
str \| None |
The message as written in the behavior. |
starting_time |
float |
The time.perf_counter() value at the first call after entering the state. It is 0. until then. |
msg_printed |
bool |
Whether the message was printed since the machine entered the state. |
state_machine |
HybridStateMachine \| None |
The machine that owns the state. |
wildcards |
dict |
The same dictionary object that holds the wildcards of the machine. |
The machine awaits the state itself (await state()) at every pass of act_states(). At the first call after entering the state the call records starting_time and prints the message once through the user log channel. The message ends with 🔴 for a blocking state and with 🟢 for a non-blocking one when the machine has the option highlight_blocking_states_in_messages. Every call then runs the inner action on the system interaction of the agent after resetting the progress of that interaction. The call returns the code of the action (0, 1 or 2) and None for a state without action. The machine ignores this value.
set_state_machine
¶
Source code in unaiverse/hsm/state.py
Links the state to the machine hsm, gives it the wildcards of hsm and links the inner action to the same machine. add_state() calls it. Returns None.
set_msg
¶
Source code in unaiverse/hsm/state.py
Replaces the message of the state with msg after decoding its HTML entities. Both msg and msg_with_wildcards take the new value and None removes the message. The machine calls it when it generates or restores messages in debug mode. Returns None.
must_wait
¶
Source code in unaiverse/hsm/state.py
Returns True while less than waiting_time seconds have passed since starting_time. A state with waiting_time equal to 0 or less never waits. HybridStateMachine.must_wait() calls it for the current state.
to_dict
¶
Source code in unaiverse/hsm/state.py
Returns the JSON form of the state as a dictionary with the keys action, action_kwargs, msg, blocking and time_to_wait_before_acting. The message is ASCII with the other characters written as HTML entities. The arguments are those of the action after the replacement of the wildcards. The message of the inner action is not part of the result.
{"action": "nop", "action_kwargs": {"message": "Exam started"}, "msg": "Exam 📝",
"blocking": True, "time_to_wait_before_acting": 3.0}
has_action
¶
Returns True when the state has an inner action.
get_starting_time
¶
Returns starting_time, the time.perf_counter() value recorded at the first call after entering the state. The value is 0. before that call.
get_time_passed
¶
Returns the seconds passed since starting_time. The result is -1. when the state has not been entered yet.
reset
¶
Source code in unaiverse/hsm/state.py
Clears starting_time and resets the progress of the system interaction of the inner action. The default reset_message_printing=True also lets the message of the state and of its inner action be printed again. The machine calls it on the state it leaves after each completed transition and passes False for a self-loop. Returns None.
set_blocking
¶
Source code in unaiverse/hsm/state.py
Changes the attribute blocking to the given value. Returns None.
set_wildcards
¶
Source code in unaiverse/hsm/state.py
Replaces the dictionary of wildcards of the state and of its inner action with wildcards (an empty dictionary for None). The replacement itself happens in apply_wildcards(). Returns None.
apply_wildcards
¶
Source code in unaiverse/hsm/state.py
Rebuilds msg from msg_with_wildcards and replaces in it every wildcard with str() of its value. The method then calls apply_wildcards() on the inner action. HybridStateMachine.apply_wildcards() calls it on every state. Returns None.