hsm
hsm
¶
Classes:
| Name | Description |
|---|---|
Action |
|
State |
|
HybridStateMachine |
|
Classes¶
Action
¶
Action(name: str, args: dict, actionable: object, idx: int = -1, ready: bool = True, wildcards: dict[str, str | float | int] | None = None, msg: str | None = None)
Initializes an Action object, which encapsulates a method to be executed on a given object (actionable)
with specified arguments. It sets up various properties for managing multistep actions, including
total_steps, total_time, and timeout. It also handles wildcard argument replacement and checks for the
existence of required parameters. It identifies if the action is a 'not ready' type (e.g., do_, get_) and
sets its initial status accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the method to call. |
required |
args
|
dict
|
A dictionary of arguments for the method. |
required |
actionable
|
object
|
The object on which the method will be executed. |
required |
idx
|
int
|
A unique ID for the action. |
-1
|
ready
|
bool
|
A boolean indicating if the action is ready to be executed. |
True
|
wildcards
|
dict[str, str | float | int] | None
|
A dictionary for replacing placeholder values in arguments. |
None
|
msg
|
str | None
|
An optional human-readable message. |
None
|
Methods:
| Name | Description |
|---|---|
set_as_ready |
Sets the action's ready flag to |
set_as_not_ready |
Sets the action's ready flag to |
is_ready |
Checks if the action is ready to be executed. It returns |
was_last_step_done |
Determines if the action has reached its completion criteria, either by reaching the total number of steps |
cannot_be_run_anymore |
Checks if the action has reached a state where it cannot be executed further, for instance, due to |
has_completion_step |
Checks if the action is designed to have a completion step, which is a final execution pass after the main |
is_multi_steps |
Determines if the action is configured to be a multistep action (i.e., not a single-step action). |
has_a_timeout |
Checks if a timeout has been configured for the action. |
is_delayed |
Checks if the action is currently in a delayed state and cannot be executed yet, based on a defined delay |
is_timed_out |
Checks if the action has exceeded its configured timeout period since the last successful execution attempt. |
to_list |
Converts the action's properties into a list for easy serialization. It can generate either a full or a |
same_as |
Compares the current action to a target action by name and arguments. It returns |
set_wildcards |
Replaces wildcard values in the action's arguments with actual values. This method is used to dynamically |
add_request |
Adds a new request to the action's internal list. This is used to track pending requests that might make the |
clear_requests |
Clears all pending requests from the action's list. |
get_requests |
Retrieves the dictionary of pending requests. Each entry in the dictionary maps a requester to its |
reset_step |
Resets the action's state, including the step counter and timing metrics, allowing it to be re-run from the |
get_step |
Retrieves the current step index of the multistep action. |
get_total_steps |
Retrieves the total number of steps configured for the action. |
get_starting_time |
Retrieves the timestamp when the action's current execution started. |
get_total_time |
Retrieves the total time configured for the action's execution. |
Source code in unaiverse/hsm.py
Methods:¶
set_as_ready
¶
set_as_not_ready
¶
is_ready
¶
Checks if the action is ready to be executed. It returns True if the ready flag is set or if there are
any pending requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consider_requests
|
bool
|
A boolean flag to include pending requests in the readiness check. |
True
|
Returns:
| Type | Description |
|---|---|
|
A boolean indicating the action's readiness. |
Source code in unaiverse/hsm.py
was_last_step_done
¶
Determines if the action has reached its completion criteria, either by reaching the total number of steps or by exceeding the maximum allowed execution time.
Returns:
| Type | Description |
|---|---|
|
True if the action is completed, False otherwise. |
Source code in unaiverse/hsm.py
cannot_be_run_anymore
¶
Checks if the action has reached a state where it cannot be executed further, for instance, due to completion or a timeout.
Returns:
| Type | Description |
|---|---|
|
A boolean indicating if the action can no longer be run. |
Source code in unaiverse/hsm.py
has_completion_step
¶
Checks if the action is designed to have a completion step, which is a final execution pass after the main action logic has finished.
Returns:
| Type | Description |
|---|---|
|
A boolean indicating the presence of a completion step. |
Source code in unaiverse/hsm.py
is_multi_steps
¶
Determines if the action is configured to be a multistep action (i.e., not a single-step action).
Returns:
| Type | Description |
|---|---|
|
A boolean indicating if the action is multistep. |
has_a_timeout
¶
Checks if a timeout has been configured for the action.
Returns:
| Type | Description |
|---|---|
|
A boolean indicating if a timeout is set. |
is_delayed
¶
Checks if the action is currently in a delayed state and cannot be executed yet, based on a defined delay period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
starting_time
|
float
|
The time the delay period began. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the action is delayed, False otherwise. |
Source code in unaiverse/hsm.py
is_timed_out
¶
Checks if the action has exceeded its configured timeout period since the last successful execution attempt.
Returns:
| Type | Description |
|---|---|
|
True if the action has timed out, False otherwise. |
Source code in unaiverse/hsm.py
to_list
¶
Converts the action's properties into a list for easy serialization. It can generate either a full or a minimal representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
minimal
|
A boolean flag to return a minimal list representation. |
False
|
Returns:
| Type | Description |
|---|---|
|
A list containing the action's properties. |
Source code in unaiverse/hsm.py
same_as
¶
Compares the current action to a target action by name and arguments. It returns True if they are
considered the same, ignoring specific arguments like time or timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the target action. |
required |
args
|
dict | None
|
The arguments of the target action. |
required |
Returns:
| Type | Description |
|---|---|
|
A boolean indicating if the actions are a match. |
Source code in unaiverse/hsm.py
set_wildcards
¶
Replaces wildcard values in the action's arguments with actual values. This method is used to dynamically configure actions with context-specific data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wildcards
|
dict[str, str | float | int] | None
|
A dictionary mapping wildcard placeholders to their concrete values. |
required |
Source code in unaiverse/hsm.py
add_request
¶
Adds a new request to the action's internal list. This is used to track pending requests that might make the action ready to be executed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generic_request_obj
|
object
|
The object making the request. |
required |
args
|
dict
|
The arguments associated with the request. |
required |
timestamp
|
float
|
The time the request was made. |
required |
uuid
|
str
|
A unique ID for the request. |
required |
Source code in unaiverse/hsm.py
clear_requests
¶
get_requests
¶
Retrieves the dictionary of pending requests. Each entry in the dictionary maps a requester to its arguments, timestamp, and UUID.
Returns:
| Type | Description |
|---|---|
|
A dictionary of pending requests. |
reset_step
¶
Resets the action's state, including the step counter and timing metrics, allowing it to be re-run from the beginning.
Source code in unaiverse/hsm.py
get_step
¶
Retrieves the current step index of the multistep action.
Returns:
| Type | Description |
|---|---|
|
An integer representing the current step index. |
get_total_steps
¶
Retrieves the total number of steps configured for the action.
Returns:
| Type | Description |
|---|---|
|
An integer representing the total steps. |
get_starting_time
¶
Retrieves the timestamp when the action's current execution started.
Returns:
| Type | Description |
|---|---|
|
A float representing the starting time. |
get_total_time
¶
Retrieves the total time configured for the action's execution.
Returns:
| Type | Description |
|---|---|
|
A float representing the total time. |
State
¶
State(name: str, idx: int = -1, action: Action | None = None, waiting_time: float = 0.0, blocking: bool = True, msg: str | None = None)
Initializes a State object, which is a fundamental component of a Hybrid State Machine. A state can be
associated with an optional Action to be performed, a unique name, and various properties like waiting time
and blocking behavior. It also stores a human-readable message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The unique name of the state. |
required |
idx
|
int
|
A unique ID for the state. |
-1
|
action
|
Action | None
|
An optional |
None
|
waiting_time
|
float
|
The number of seconds to wait before the state can transition. |
0.0
|
blocking
|
bool
|
A boolean indicating if the state blocks execution until a condition is met. |
True
|
msg
|
str | None
|
An optional message associated with the state. |
None
|
Methods:
| Name | Description |
|---|---|
must_wait |
Checks if the state needs to wait before it can transition. It compares the current elapsed time since |
to_list |
Converts the state's properties into a list. This method is useful for serialization, allowing the state to |
has_action |
A simple getter that checks if an action is associated with the state. |
get_starting_time |
Retrieves the timestamp when the state's execution began. This is used to calculate the elapsed waiting time. |
reset |
Resets the state's internal counters. This method is typically called when re-entering a state. It sets the |
set_blocking |
Sets the blocking status of the state. A blocking state will prevent the state machine from transitioning to |
Source code in unaiverse/hsm.py
Methods:¶
must_wait
¶
Checks if the state needs to wait before it can transition. It compares the current elapsed time since
entering the state with the configured waiting_time. If the elapsed time is less than the waiting time,
it returns True, indicating the state is still in a waiting period.
Returns:
| Type | Description |
|---|---|
|
A boolean indicating whether the state is currently waiting. |
Source code in unaiverse/hsm.py
to_list
¶
Converts the state's properties into a list. This method is useful for serialization, allowing the state to be easily stored or transmitted. It includes the action's minimal list representation, the state's ID, blocking status, waiting time, and message.
Returns:
| Type | Description |
|---|---|
|
A list containing the state's properties. |
Source code in unaiverse/hsm.py
has_action
¶
A simple getter that checks if an action is associated with the state.
Returns:
| Type | Description |
|---|---|
|
True if an action is set, False otherwise. |
get_starting_time
¶
Retrieves the timestamp when the state's execution began. This is used to calculate the elapsed waiting time.
Returns:
| Type | Description |
|---|---|
|
A float representing the starting time. |
reset
¶
Resets the state's internal counters. This method is typically called when re-entering a state. It sets the
starting_time to zero and also resets the associated action's step counter if an action exists.
Source code in unaiverse/hsm.py
set_blocking
¶
Sets the blocking status of the state. A blocking state will prevent the state machine from transitioning to the next state until the action is fully completed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocking
|
bool
|
A boolean value to set the blocking status. |
required |
Source code in unaiverse/hsm.py
HybridStateMachine
¶
HybridStateMachine(actionable: object, wildcards: dict[str, str | float | int] | None = None, request_signature_checker: Callable[[object], bool] | None = None, policy: Callable[[list[Action]], int] | None = None)
Initializes a HybridStateMachine object, which orchestrates states and transitions. It manages a set of
states and actions, and handles the logic for transitions between states based on conditions and a defined
policy. It sets up initial and current states, wildcards for dynamic arguments, and references to an
actionable object whose methods are the actions to be called. It also includes debug and output settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actionable
|
object
|
The object on which actions (methods) are to be executed. |
required |
wildcards
|
dict[str, str | float | int] | None
|
A dictionary of key-value pairs for dynamic argument substitution. |
None
|
request_signature_checker
|
Callable[[object], bool] | None
|
An optional callable to validate incoming action requests. |
None
|
policy
|
Callable[[list[Action]], int] | None
|
An optional callable that determines which action to execute from a list of feasible actions. |
None
|
Methods:
| Name | Description |
|---|---|
to_dict |
Serializes the state machine's current configuration into a dictionary. This includes its states, |
set_actionable |
Sets the object on which the state machine's actions will be performed. This allows the same state machine |
set_wildcards |
Sets the dictionary of wildcards that are used to dynamically replace placeholder values in action |
set_role |
Sets the role of the agent associated with this state machine. This can be used to influence state machine |
get_wildcards |
Retrieves the dictionary of wildcards currently used by the state machine. |
add_wildcards |
Adds new key-value pairs to the existing wildcard dictionary. It also triggers an update to all actions with |
update_wildcard |
Updates the value of a single existing wildcard. It raises an error if the key does not exist. This method |
get_action_step |
Retrieves the current step index of the action being executed. This is particularly useful for tracking the |
is_busy_acting |
Checks if the state machine is currently executing an action. This is determined by checking if the action |
add_state |
Adds a new state to the state machine. This method can create a new state with an optional inner action or |
get_state_name |
Retrieves the name of the current state of the state machine. |
get_state |
Retrieves the current |
get_action |
Retrieves the |
get_action_name |
Retrieves the name of the action currently being executed. |
reset_state |
Resets the state machine to its initial state. This clears the current action, the previous state, and |
get_states |
Returns an iterable of all state names defined in the state machine. |
set_state |
Sets the current state of the state machine to a new, specified state. It also handles the transition logic |
add_transit |
Defines a transition between two states with an associated action. This method is central to building the |
include |
Integrates the states and transitions of another state machine ( |
must_wait |
Checks if the current state is in a waiting period before any transitions can occur. |
is_enabled |
A simple getter to check if the state machine is currently enabled to run. |
enable |
Enables or disables the state machine. When disabled, the |
act_states |
Executes the inner action of the current state, if one exists. This method is for actions that occur upon |
act_transitions |
This is the core execution loop for transitions. It finds all feasible actions from the current state and, |
act |
A high-level method that combines |
get_state_changed |
Returns an internal flag that indicates if a state transition has occurred in the last execution cycle. |
request_action |
Allows an external entity to request a specific action. The request is validated by a signature checker |
wait_for_all_actions_that_start_with |
Sets the |
wait_for_all_actions_that_include_an_arg |
Sets the |
wait_for_actions |
Sets the |
save |
Saves the state machine's current configuration to a JSON file. It can optionally check if the configuration |
load |
Loads a state machine's configuration from a JSON file or a JSON string. It reconstructs the states, |
to_graphviz |
Generates a Graphviz |
save_pdf |
Saves the state machine's Graphviz representation as a PDF file. It calls |
print_actions |
Prints a list of all transitions and their associated actions from a given state. If no state is provided, |
Source code in unaiverse/hsm.py
Methods:¶
to_dict
¶
Serializes the state machine's current configuration into a dictionary. This includes its states, transitions, roles, and the current action being executed. It is useful for saving the state of the machine or for logging its status in a structured format.
Returns:
| Type | Description |
|---|---|
|
A dictionary representation of the state machine's properties. |
Source code in unaiverse/hsm.py
set_actionable
¶
Sets the object on which the state machine's actions will be performed. This allows the same state machine
logic to be applied to different objects. It updates the actionable reference for all states and actions
within the machine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
The object instance to be set as the new |
required |
Source code in unaiverse/hsm.py
set_wildcards
¶
Sets the dictionary of wildcards that are used to dynamically replace placeholder values in action arguments. It updates all actions with the new wildcard dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wildcards
|
dict[str, str | float | int] | None
|
A dictionary containing wildcard key-value pairs. |
required |
Source code in unaiverse/hsm.py
set_role
¶
Sets the role of the agent associated with this state machine. This can be used to influence state machine behavior based on the agent's role (e.g., 'teacher', 'student').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
The string representation of the new role. |
required |
Source code in unaiverse/hsm.py
get_wildcards
¶
Retrieves the dictionary of wildcards currently used by the state machine.
Returns:
| Type | Description |
|---|---|
|
A dictionary of the wildcards. |
add_wildcards
¶
Adds new key-value pairs to the existing wildcard dictionary. It also triggers an update to all actions with the new combined dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wildcards
|
dict[str, str | float | int | list[str]]
|
A dictionary of new wildcards to add. |
required |
Source code in unaiverse/hsm.py
update_wildcard
¶
Updates the value of a single existing wildcard. It raises an error if the key does not exist. This method is useful for changing a single dynamic value without redefining all wildcards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wildcard_key
|
str
|
The key of the wildcard to update. |
required |
wildcard_value
|
str | float | int
|
The new value for the wildcard. |
required |
Source code in unaiverse/hsm.py
get_action_step
¶
Retrieves the current step index of the action being executed. This is particularly useful for tracking the progress of multistep actions.
Returns:
| Type | Description |
|---|---|
|
An integer representing the current step, or -1 if no action is running. |
Source code in unaiverse/hsm.py
is_busy_acting
¶
Checks if the state machine is currently executing an action. This is determined by checking if the action step index is greater than or equal to 0.
Returns:
| Type | Description |
|---|---|
|
True if an action is running, False otherwise. |
Source code in unaiverse/hsm.py
add_state
¶
add_state(state: str, action: str = None, args: dict | None = None, state_id: int | None = None, waiting_time: float | None = None, blocking: bool | None = None, msg: str | None = None)
Adds a new state to the state machine. This method can create a new state with an optional inner action or update an existing state. It assigns a unique ID to the state and its action.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
The name of the state to add. |
required |
action
|
str
|
The name of the action to associate with the state. |
None
|
args
|
dict | None
|
A dictionary of arguments for the action. |
None
|
state_id
|
int | None
|
An optional unique ID for the state. |
None
|
waiting_time
|
float | None
|
A float representing a delay before the state can transition. |
None
|
blocking
|
bool | None
|
A boolean indicating if the state is blocking. |
None
|
msg
|
str | None
|
A human-readable message for the state. |
None
|
Source code in unaiverse/hsm.py
get_state_name
¶
Retrieves the name of the current state of the state machine.
Returns:
| Type | Description |
|---|---|
|
A string with the state's name, or |
get_state
¶
Retrieves the current State object of the state machine.
Returns:
| Type | Description |
|---|---|
|
A |
get_action
¶
Retrieves the Action object that is currently being executed.
Returns:
| Type | Description |
|---|---|
|
An |
get_action_name
¶
Retrieves the name of the action currently being executed.
Returns:
| Type | Description |
|---|---|
|
A string with the action's name, or |
Source code in unaiverse/hsm.py
reset_state
¶
Resets the state machine to its initial state. This clears the current action, the previous state, and the limbo state. It also resets the step counters for all actions within the machine.
Source code in unaiverse/hsm.py
get_states
¶
Returns an iterable of all state names defined in the state machine.
Returns:
| Type | Description |
|---|---|
|
An iterable of state names. |
set_state
¶
Sets the current state of the state machine to a new, specified state. It also handles the transition logic by resetting the current action and updating the previous state. Raises an error if the new state is not known to the machine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str
|
The name of the state to transition to. |
required |
Source code in unaiverse/hsm.py
add_transit
¶
add_transit(from_state: str, to_state: str, action: str, args: dict | None = None, ready: bool = True, act_id: int | None = None, msg: str | None = None)
Defines a transition between two states with an associated action. This method is central to building the state machine's logic. It can also handle loading and integrating a complete state machine from a file, resolving any state name clashes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_state
|
str
|
The name of the starting state. |
required |
to_state
|
str
|
The name of the destination state (can be a file path to load another HSM). |
required |
action
|
str
|
The name of the action to trigger the transition. |
required |
args
|
dict | None
|
A dictionary of arguments for the action. |
None
|
ready
|
bool
|
A boolean indicating if the action is ready by default. |
True
|
act_id
|
int | None
|
An optional unique ID for the action. |
None
|
msg
|
str | None
|
An optional human-readable message for the action. |
None
|
Source code in unaiverse/hsm.py
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 | |
include
¶
Integrates the states and transitions of another state machine (hsm) into the current one. This is a
crucial method for composing complex state machines from smaller, reusable components. It copies wildcards,
states, and transitions, ensuring that all actions and states are properly added and linked. This method also
handles an optional make_a_copy flag to completely replicate the source machine's state (e.g., current state,
initial state).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hsm
|
The |
required | |
make_a_copy
|
A boolean to indicate whether the current state machine should adopt the state (e.g., current state, initial state) of the included one. |
False
|
Source code in unaiverse/hsm.py
must_wait
¶
Checks if the current state is in a waiting period before any transitions can occur.
Returns:
| Type | Description |
|---|---|
|
A boolean indicating if the state machine must wait. |
Source code in unaiverse/hsm.py
is_enabled
¶
A simple getter to check if the state machine is currently enabled to run.
Returns:
| Type | Description |
|---|---|
|
True if the state machine is enabled, False otherwise. |
enable
¶
Enables or disables the state machine. When disabled, the act_states and act_transitions methods will
not perform any actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yes_or_not
|
bool
|
A boolean to enable ( |
required |
Source code in unaiverse/hsm.py
act_states
¶
Executes the inner action of the current state, if one exists. This method is for actions that occur upon entering a state but do not cause an immediate transition. It only runs if the state machine is enabled.
Source code in unaiverse/hsm.py
act_transitions
¶
This is the core execution loop for transitions. It finds all feasible actions from the current state and, using a policy, selects and executes one. It handles single-step and multistep actions, managing state changes, timeouts, and failed executions. It returns an integer status code indicating the outcome (e.g., transition done, try again, move to next action).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requested_only
|
bool
|
A boolean to consider only actions that have pending requests. |
False
|
Returns:
| Type | Description |
|---|---|
|
An integer status code: |
|
|
next action, or |
Source code in unaiverse/hsm.py
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 | |
act
¶
A high-level method that combines act_states and act_transitions to run the state machine. It repeatedly
processes states and transitions until a blocking state is reached or all feasible actions have been tried,
thus ensuring a complete processing cycle in one call.
Source code in unaiverse/hsm.py
get_state_changed
¶
Returns an internal flag that indicates if a state transition has occurred in the last execution cycle. This can be used by an external loop to know when to re-evaluate the state machine's context.
Returns:
| Type | Description |
|---|---|
|
True if the state has changed, False otherwise. |
Source code in unaiverse/hsm.py
request_action
¶
request_action(signature: object, action_name: str, args: dict | None = None, from_state: str | None = None, to_state: str | None = None, timestamp: float | None = None, uuid: str | None = None)
Allows an external entity to request a specific action. The request is validated by a signature checker (if one exists) and then queued on the corresponding action. This method enables dynamic, external triggers for state machine transitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
object
|
An object used for validating the request's origin. |
required |
action_name
|
str
|
The name of the requested action. |
required |
args
|
dict | None
|
Arguments for the requested action. |
None
|
from_state
|
str | None
|
The optional starting state for the requested transition. |
None
|
to_state
|
str | None
|
The optional destination state for the requested transition. |
None
|
timestamp
|
float | None
|
The time the request was made. |
None
|
uuid
|
str | None
|
A unique identifier for the request. |
None
|
Returns:
| Type | Description |
|---|---|
|
True if the request was accepted and queued, False otherwise. |
Source code in unaiverse/hsm.py
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 | |
wait_for_all_actions_that_start_with
¶
Sets the ready flag to False for all actions whose name begins with a given prefix. This method is used
to programmatically disable a group of actions, effectively pausing them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
The string prefix to match against action names. |
required |
Source code in unaiverse/hsm.py
wait_for_all_actions_that_include_an_arg
¶
Sets the ready flag to False for all actions that include a specific argument name in their signature.
This provides another way to programmatically disable actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg_name
|
The name of the argument to look for. |
required |
Source code in unaiverse/hsm.py
wait_for_actions
¶
Sets the ready flag for a specific action (or group of actions) between two states. This allows for
fine-grained control over which transitions are active.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_state
|
str
|
The name of the starting state. |
required |
to_state
|
str
|
The name of the destination state. |
required |
wait
|
bool
|
A boolean flag to either set the action as not ready ( |
True
|
Returns:
| Type | Description |
|---|---|
|
True if the specified action was found, False otherwise. |
Source code in unaiverse/hsm.py
save
¶
Saves the state machine's current configuration to a JSON file. It can optionally check if the configuration has changed before saving to avoid redundant file writes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the file to save to. |
required |
only_if_changed
|
object | None
|
An optional object to compare against for changes. If a change is not detected, the file is not written. |
None
|
Returns:
| Type | Description |
|---|---|
|
True if the file was written, False otherwise. |
Source code in unaiverse/hsm.py
load
¶
Loads a state machine's configuration from a JSON file or a JSON string. It reconstructs the states, actions, and transitions from the serialized data. This method is critical for persistence and for loading pre-defined state machine models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename_or_hsm_as_string
|
str | TextIOWrapper
|
The path to the JSON file or a JSON string representation of the state machine. |
required |
Returns:
| Type | Description |
|---|---|
|
The loaded |
Source code in unaiverse/hsm.py
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 | |
to_graphviz
¶
Generates a Graphviz Digraph object representing the state machine's structure. This method visualizes
states as nodes and transitions as edges. It includes details such as node shapes (diamond for initial state,
oval for others), styles (filled for blocking states), and labels for both states and transitions. The labels
for actions include their names and arguments, formatted to wrap lines for readability.
Returns:
| Type | Description |
|---|---|
|
A |
Source code in unaiverse/hsm.py
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 | |
save_pdf
¶
Saves the state machine's Graphviz representation as a PDF file. It calls to_graphviz() to create the
graph and then uses the Graphviz library's render method to generate the PDF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path and name of the PDF file to save. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the file was successfully saved, False otherwise. |
Source code in unaiverse/hsm.py
print_actions
¶
Prints a list of all transitions and their associated actions from a given state. If no state is provided, it defaults to the current state. This method is useful for quickly inspecting the available transitions from a specific point in the state machine's flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str | None
|
The name of the state from which to print actions. Defaults to the current state. |
None
|