unaiverse 0.1.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. unaiverse/__init__.py +19 -0
  2. unaiverse/agent.py +2226 -0
  3. unaiverse/agent_basics.py +2389 -0
  4. unaiverse/clock.py +234 -0
  5. unaiverse/dataprops.py +1282 -0
  6. unaiverse/hsm.py +2471 -0
  7. unaiverse/modules/__init__.py +18 -0
  8. unaiverse/modules/cnu/__init__.py +17 -0
  9. unaiverse/modules/cnu/cnus.py +536 -0
  10. unaiverse/modules/cnu/layers.py +261 -0
  11. unaiverse/modules/cnu/psi.py +60 -0
  12. unaiverse/modules/hl/__init__.py +15 -0
  13. unaiverse/modules/hl/hl_utils.py +411 -0
  14. unaiverse/modules/networks.py +1509 -0
  15. unaiverse/modules/utils.py +748 -0
  16. unaiverse/networking/__init__.py +16 -0
  17. unaiverse/networking/node/__init__.py +18 -0
  18. unaiverse/networking/node/connpool.py +1332 -0
  19. unaiverse/networking/node/node.py +2752 -0
  20. unaiverse/networking/node/profile.py +446 -0
  21. unaiverse/networking/node/tokens.py +79 -0
  22. unaiverse/networking/p2p/__init__.py +188 -0
  23. unaiverse/networking/p2p/go.mod +127 -0
  24. unaiverse/networking/p2p/go.sum +548 -0
  25. unaiverse/networking/p2p/golibp2p.py +18 -0
  26. unaiverse/networking/p2p/golibp2p.pyi +136 -0
  27. unaiverse/networking/p2p/lib.go +2765 -0
  28. unaiverse/networking/p2p/lib_types.py +311 -0
  29. unaiverse/networking/p2p/message_pb2.py +50 -0
  30. unaiverse/networking/p2p/messages.py +360 -0
  31. unaiverse/networking/p2p/mylogger.py +78 -0
  32. unaiverse/networking/p2p/p2p.py +900 -0
  33. unaiverse/networking/p2p/proto-go/message.pb.go +846 -0
  34. unaiverse/stats.py +1506 -0
  35. unaiverse/streamlib/__init__.py +15 -0
  36. unaiverse/streamlib/streamlib.py +210 -0
  37. unaiverse/streams.py +804 -0
  38. unaiverse/utils/__init__.py +16 -0
  39. unaiverse/utils/lone_wolf.json +28 -0
  40. unaiverse/utils/misc.py +441 -0
  41. unaiverse/utils/sandbox.py +292 -0
  42. unaiverse/world.py +384 -0
  43. unaiverse-0.1.12.dist-info/METADATA +366 -0
  44. unaiverse-0.1.12.dist-info/RECORD +47 -0
  45. unaiverse-0.1.12.dist-info/WHEEL +5 -0
  46. unaiverse-0.1.12.dist-info/licenses/LICENSE +177 -0
  47. unaiverse-0.1.12.dist-info/top_level.txt +1 -0
unaiverse/hsm.py ADDED
@@ -0,0 +1,2471 @@
1
+ """
2
+ █████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
3
+ ░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
4
+ ░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
5
+ ░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
6
+ ░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
7
+ ░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
8
+ ░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
9
+ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
10
+ A Collectionless AI Project (https://collectionless.ai)
11
+ Registration/Login: https://unaiverse.io
12
+ Code Repositories: https://github.com/collectionlessai/
13
+ Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
14
+ """
15
+ import io
16
+ import os
17
+ import re
18
+ import json
19
+ import copy
20
+ import html
21
+ import sys
22
+ import time
23
+ import inspect
24
+ import graphviz
25
+ import importlib.resources
26
+ from collections.abc import Callable
27
+
28
+
29
+ class ActionRequest:
30
+ def __init__(self, requester: object, action: 'Action',
31
+ args: dict | None = None, timestamp: float = -1., uuid: str | None = None):
32
+ self.requester = requester
33
+ self.action = action
34
+ self.args = args
35
+ self.timestamp = timestamp
36
+ self.uuid = uuid
37
+ self.by_insertion_order_id = -1
38
+ self.by_requester_insertion_order_id = -1
39
+ self.mark = None
40
+ self.hidden = False
41
+
42
+ def set_mark(self, mark: object):
43
+ self.mark = mark
44
+
45
+ def get_mark(self):
46
+ return self.mark
47
+
48
+ def get_order_id(self, by_requester: bool = False) -> int:
49
+ if not by_requester:
50
+ return self.by_insertion_order_id
51
+ else:
52
+ return self.by_requester_insertion_order_id
53
+
54
+ def is_valid(self):
55
+ return self.by_insertion_order_id >= 0 and self.by_requester_insertion_order_id >= 0
56
+
57
+ def has_dummy_requester(self):
58
+ return self.requester is None
59
+
60
+ def to_str(self):
61
+ return json.dumps([self.requester, self.args, self.timestamp, self.uuid])
62
+
63
+ def alter_arg(self, arg_name: str, arg_value: object):
64
+ if arg_name in self.args:
65
+ self.args[arg_name] = arg_value
66
+ return True
67
+ else:
68
+ return False
69
+
70
+ def set_arg(self, arg_name: str, arg_value: object):
71
+ self.args[arg_name] = arg_value
72
+
73
+ def get_arg(self, arg_name):
74
+ return self.args[arg_name] if arg_name in self.args else None
75
+
76
+ def __str__(self):
77
+ """Provides a string representation of the `ActionRequest` instance.
78
+
79
+ Returns:
80
+ A string containing a formatted summary of the instance.
81
+ """
82
+ return (f"(action={self.action.name}, args={self.args}, "
83
+ f"requester={self.requester}, timestamp={self.timestamp}, uuid={self.uuid})")
84
+
85
+
86
+ class ActionRequestList:
87
+ def __init__(self, max_per_requester: int = -1):
88
+ self.by_insertion_order = []
89
+ self.by_requester_and_by_insertion_order = {}
90
+ self.max_per_requester = max_per_requester
91
+ self.by_insertion_order_entering_time = []
92
+
93
+ def add(self, req: ActionRequest):
94
+
95
+ # Updating by-requester index
96
+ if req.requester not in self.by_requester_and_by_insertion_order:
97
+ self.by_requester_and_by_insertion_order[req.requester] = []
98
+
99
+ # Searching for UUID = None, if already there - do not accumulate multiple requests with UUID None
100
+ if req.uuid is None:
101
+ existing_request_same_uuid = self.get_request_by_uuid(req.requester, req.uuid)
102
+ if existing_request_same_uuid:
103
+ return
104
+
105
+ if 0 < self.max_per_requester <= len(self.by_requester_and_by_insertion_order[req.requester]):
106
+ self.remove(self.get_oldest_request(req.requester))
107
+ by_requester_insertion_order_id = len(self.by_requester_and_by_insertion_order[req.requester])
108
+ self.by_requester_and_by_insertion_order[req.requester].append(req)
109
+
110
+ # Updating direct global index
111
+ insertion_order_id = len(self.by_insertion_order)
112
+ self.by_insertion_order.append(req)
113
+
114
+ # Updating reverse indices
115
+ req.by_insertion_order_id = insertion_order_id
116
+ req.by_requester_insertion_order_id = by_requester_insertion_order_id
117
+
118
+ # Saving joining time
119
+ self.by_insertion_order_entering_time.append(time.perf_counter())
120
+
121
+ def remove(self, req: ActionRequest):
122
+ if req.is_valid():
123
+ for i in range(req.by_insertion_order_id + 1, len(self.by_insertion_order)):
124
+ self.by_insertion_order[i].by_insertion_order_id -= 1
125
+ del self.by_insertion_order[req.by_insertion_order_id]
126
+ del self.by_insertion_order_entering_time[req.by_insertion_order_id]
127
+
128
+ d = self.by_requester_and_by_insertion_order[req.requester]
129
+ for i in range(req.by_requester_insertion_order_id + 1, len(d)):
130
+ d[i].by_requester_insertion_order_id -= 1
131
+ del d[req.by_requester_insertion_order_id]
132
+ if len(d) == 0:
133
+ del self.by_requester_and_by_insertion_order[req.requester]
134
+ req.by_insertion_order_id = -1
135
+ req.by_requester_insertion_order_id = -1
136
+
137
+ def remove_due_to_timeout(self, timeout_secs: float):
138
+ to_remove = []
139
+ for i, req in enumerate(self.by_insertion_order):
140
+ if (time.perf_counter() - self.by_insertion_order_entering_time[i]) >= timeout_secs:
141
+ to_remove.append(req)
142
+ for req in to_remove:
143
+ self.remove(req)
144
+
145
+ def move_request_to_back(self, req: ActionRequest):
146
+ if req.is_valid():
147
+ entering_time = self.by_insertion_order_entering_time[req.by_insertion_order_id]
148
+ self.remove(req)
149
+ self.add(req)
150
+ self.by_insertion_order_entering_time[req.by_insertion_order_id] = entering_time
151
+
152
+ def move_requester_to_back(self, requester: object):
153
+ requests = self.get_requests(requester)
154
+ if requests is not None and len(requests) > 0:
155
+ requests_copy = []
156
+ entering_times = []
157
+ for req in requests:
158
+ if req.is_valid():
159
+ requests_copy.append(req)
160
+ entering_times.append(self.by_insertion_order_entering_time[req.by_insertion_order_id])
161
+ self.remove(req)
162
+ for i, req in enumerate(requests_copy):
163
+ self.add(req)
164
+ self.by_insertion_order_entering_time[req.by_insertion_order_id] = entering_times[i]
165
+
166
+ def get_request(self, req_order_id: int, requester: object | None = None):
167
+ if req_order_id < 0 and req_order_id != -1:
168
+ return None
169
+ if requester is None:
170
+ return self.by_insertion_order[req_order_id] if req_order_id < len(self.by_insertion_order) else None
171
+ else:
172
+ if requester not in self.by_requester_and_by_insertion_order:
173
+ return None
174
+ return self.by_requester_and_by_insertion_order[requester][req_order_id] \
175
+ if req_order_id < len(self.by_requester_and_by_insertion_order[requester]) else None
176
+
177
+ def get_oldest_request(self, requester: object | None = None):
178
+ return self.get_request(0, requester)
179
+
180
+ def get_most_recent_request(self, requester: object | None = None):
181
+ return self.get_request(-1, requester)
182
+
183
+ def get_request_by_uuid(self, requester: object, uuid: str | None) -> None | ActionRequest:
184
+ requests = self.get_requests(requester)
185
+ if requests is None or len(requests) == 0:
186
+ return None
187
+
188
+ for req in requests:
189
+ if req.uuid == uuid:
190
+ return req
191
+
192
+ def keep_only_the_most_recent_request(self):
193
+ req = self.get_most_recent_request()
194
+ entering_time = self.by_insertion_order_entering_time[req.by_insertion_order_id]
195
+ self.clear()
196
+ self.add(req)
197
+ self.by_insertion_order_entering_time[req.by_insertion_order_id] = entering_time
198
+
199
+ def get_requests(self, requester: object | None = None, to_str: bool = False):
200
+ if requester is None:
201
+ if not to_str:
202
+ return self.by_insertion_order
203
+ else:
204
+ return json.dumps([req.to_str() for req in self.by_insertion_order])
205
+ else:
206
+ if requester in self.by_requester_and_by_insertion_order:
207
+ if not to_str:
208
+ return self.by_requester_and_by_insertion_order[requester]
209
+ else:
210
+ reqs = self.by_requester_and_by_insertion_order[requester]
211
+ return json.dumps([req.to_str() for req in reqs])
212
+ else:
213
+ if not to_str:
214
+ return []
215
+ else:
216
+ return json.dumps([])
217
+
218
+ def clear(self):
219
+ self.by_insertion_order.clear()
220
+ self.by_requester_and_by_insertion_order.clear()
221
+ self.by_insertion_order_entering_time.clear()
222
+
223
+ def is_requester_known(self, requester: object):
224
+ return requester in self.by_requester_and_by_insertion_order
225
+
226
+ def __len__(self):
227
+ return len(self.by_insertion_order)
228
+
229
+ def __iter__(self):
230
+ return iter(self.by_insertion_order)
231
+
232
+ def __str__(self):
233
+ """Provides a string representation of the `ActionRequestList` instance.
234
+
235
+ Returns:
236
+ A string containing a formatted summary of the instance.
237
+ """
238
+ return "{" + ", ".join([str(r) for r in self.by_insertion_order]) + "}"
239
+
240
+
241
+ class Action:
242
+
243
+ # Candidate argument names (when calling an action) that tells that such an action is multi-steps
244
+ STEPS_ARG_NAMES = {'steps', 'samples'}
245
+ SECONDS_ARG_NAMES = {'time'}
246
+ TIMEOUT_ARG_NAMES = {'timeout'}
247
+ DELAY_ARG_NAMES = {'delay'}
248
+ COMPLETED_NAMES = {'_completed'}
249
+ REQUESTER_ARG_NAMES = {'_requester'}
250
+ REQUEST_TIME_NAMES = {'_request_time'}
251
+ REQUEST_UUID_NAMES = {'_request_uuid'}
252
+ NOT_READY_PREFIXES = ('get_', 'got_', 'do_', 'done_')
253
+ KNOWN_SINGLE_STEP_ACTION_PREFIXES = ('ask_',)
254
+
255
+ # Completion reasons
256
+ MAX_STEPS_REACHED = 0 # Single-step actions always complete due to this reason
257
+ MAX_TIME_REACHED = 1
258
+ MAX_TIMEOUT_DURING_ATTEMPTS_REACHED = 2
259
+
260
+ def __init__(self, name: str, args: dict, actionable: object,
261
+ idx: int = -1,
262
+ ready: bool = True,
263
+ msg: str | None = None,
264
+ avoid_changing_ready: bool = False, out_fcn: Callable = print):
265
+ """Initializes an `Action` object, which encapsulates a method to be executed on a given object (`actionable`)
266
+ with specified arguments. It sets up various properties for managing multistep actions, including
267
+ `total_steps`, `total_time`, and `timeout`. It also handles wildcard argument replacement and checks for the
268
+ existence of required parameters. It identifies if the action is a 'not ready' type (e.g., `do_`, `get_`) and
269
+ sets its initial status accordingly.
270
+
271
+ Args:
272
+ name: The name of the method to call.
273
+ args: A dictionary of arguments for the method.
274
+ actionable: The object on which the method will be executed.
275
+ idx: A unique ID for the action.
276
+ ready: A boolean indicating if the action is ready to be executed.
277
+ msg: An optional human-readable message.
278
+ avoid_changing_ready: A boolean indicating that the selected ready state should not be changed by
279
+ internal rules.
280
+ out_fcn: Output print function.
281
+ """
282
+ # Basic properties
283
+ self.name = name # Name of the action (name of the corresponding method)
284
+ self.args = args # Dictionary of arguments to pass to the action
285
+ self.actionable = actionable # Object on which the method whose name is self.name is searched
286
+ self.ready = ready # Boolean flag telling if the action can considered ready to be executed
287
+ self.requests = ActionRequestList() # List of requests to make this action ready to be executed (customizable)
288
+ self.id = idx # Unique ID of the action (-1 if not needed)
289
+ self.msg = msg # Human-readable message associated to this instance of action
290
+ self.out_fcn = out_fcn
291
+
292
+ # Fix UNICODE chars
293
+ if self.msg is not None:
294
+ self.msg = html.unescape(self.msg)
295
+
296
+ # Reference elements
297
+ self.args_with_wildcards = copy.deepcopy(self.args) # Backup of the originally provided arguments
298
+ self.msg_with_wildcards = self.msg
299
+ self.__fcn = self.__action_name_to_callable(name) # The real method to be called
300
+ self.__sig = inspect.signature(self.__fcn) # Signature of the method for argument inspection
301
+
302
+ # Parameter names and default values
303
+ self.param_list = [] # Full list of the parameters that the action supports
304
+ self.param_to_default_value = {} # From parameter to its default value, if any
305
+ self.__get_action_params() # This will fill the two attributes above
306
+ self.__check_if_args_exist(self.args, exception=True) # Checking arguments
307
+
308
+ # Argument values replaced by wildcards (commonly assumed to be in the format <value>)
309
+ self.wildcards = {} # Value-to-value (es: <playlist> to this:and:this)
310
+
311
+ # Number of steps of this function
312
+ self.__step = -1 # Default initial step index (remark: "step INDEX", so when it is 0 it means a step was done)
313
+ self.__total_steps = 1 # Total step of an action (a multi-steps action has != 1 steps)
314
+ self.__guess_total_steps(self.get_actual_params({})) # This will "guess" the value of self.__total_steps
315
+
316
+ # Time-based metrics
317
+ self.__starting_time = 0
318
+ self.__total_time = 0 # A total time <= 0 means "no total time at all"
319
+ self.__guess_total_time(self.get_actual_params({})) # This will "guess" the value of self.__total_time
320
+
321
+ # Time-based metrics
322
+ self.__timeout_starting_time = 0
323
+ self.__timeout = 0 # A timeout <= 0 means "no total time at all"
324
+ self.__guess_timeout(self.get_actual_params({})) # This will "guess" the value of self.__timeout
325
+
326
+ # Time-based metrics
327
+ self.__delay = 0
328
+ self.__guess_delay(self.get_actual_params({})) # This will "guess" the value of self.__delay
329
+
330
+ # Fixing (if no options are specified, assuming a single-step action)
331
+ if self.__total_steps <= 0 and self.__total_time <= 0:
332
+ self.__total_steps = 1
333
+
334
+ # Fixing (forcing NOT-ready on some actions)
335
+ if not avoid_changing_ready:
336
+ for prefix in Action.NOT_READY_PREFIXES:
337
+ if self.name.startswith(prefix):
338
+ self.ready = False
339
+
340
+ self.__has_completion_step = False
341
+ for completed_name in Action.COMPLETED_NAMES:
342
+ if completed_name in self.param_list:
343
+ self.__has_completion_step = True
344
+ break
345
+
346
+ # Status
347
+ self.__cannot_be_run_anymore = False
348
+
349
+ async def __call__(self, request: ActionRequest | None = None):
350
+ """Executes the action's associated method. This is the main entry point for running an action. It handles
351
+ multistep logic by updating the step counter and checking for completion based on steps, time, or timeout.
352
+ It also injects dynamic arguments like the `requester`, `request_time`, and `request_uuid` into the method's
353
+ arguments before execution. If the action is a multistep action and has a completion step, it handles that
354
+ callback as well (async).
355
+
356
+ Args:
357
+ request: The ActionRequest object or None, if the action was not requested by other agents.
358
+
359
+ Returns:
360
+ A boolean indicating whether the action was executed successfully.
361
+ """
362
+ request_args = request.args if request is not None else {}
363
+ self.__check_if_args_exist(request_args, exception=True)
364
+ actual_args = self.get_actual_params(request_args) # Getting the actual values of the arguments
365
+
366
+ if self.msg is not None:
367
+ self.out_fcn(self.msg, request, self.requests)
368
+
369
+ if actual_args is not None:
370
+
371
+ # Getting the values for the main involved measures: total steps, total time, timeout
372
+ self.__guess_total_steps(actual_args)
373
+ self.__guess_total_time(actual_args)
374
+ self.__guess_timeout(actual_args)
375
+
376
+ # Storing the time index that is related to the timeout (do this before calling self.is_timed_out())
377
+ if self.__timeout_starting_time <= 0:
378
+ self.__timeout_starting_time = time.perf_counter()
379
+
380
+ # Storing the starting time (do this before calling self.was_last_step_done())
381
+ if self.__starting_time <= 0:
382
+ self.__starting_time = time.perf_counter()
383
+
384
+ # Setting up the flag that tells if the action reached a point in which it cannot be run anymore
385
+ self.__cannot_be_run_anymore = self.is_timed_out() or self.was_last_step_done()
386
+
387
+ if HybridStateMachine.DEBUG:
388
+ if self.__cannot_be_run_anymore:
389
+ print(f"[DEBUG HSM] Cannot-be-run-anymore set to True, "
390
+ f"due to self.is_timed_out()={self.is_timed_out()} or "
391
+ f"self.was_last_step_done()={self.was_last_step_done()}")
392
+
393
+ if self.__cannot_be_run_anymore and not self.is_multi_steps():
394
+ return False
395
+
396
+ # Setting up the information on whether a multistep action is completed
397
+ # (for example, to tell that now it is time for a callback)
398
+ calling_completion_step = False
399
+ for completed_name in Action.COMPLETED_NAMES:
400
+ if completed_name in actual_args:
401
+ calling_completion_step = self.__cannot_be_run_anymore and self.get_step() >= 0
402
+ actual_args[completed_name] = calling_completion_step
403
+ break
404
+
405
+ # We are done, no need to call the action again
406
+ if self.__cannot_be_run_anymore and not calling_completion_step:
407
+ return True
408
+
409
+ # Setting up the requester
410
+ for req_arg_name in Action.REQUESTER_ARG_NAMES:
411
+ if req_arg_name in actual_args:
412
+ actual_args[req_arg_name] = request.requester if request is not None else None
413
+ break
414
+
415
+ # Setting up the request time
416
+ for req_time_name in Action.REQUEST_TIME_NAMES:
417
+ if req_time_name in actual_args:
418
+ actual_args[req_time_name] = request.timestamp if request is not None else -1.
419
+ break
420
+
421
+ # Setting up the request uuid
422
+ for req_uuid_name in Action.REQUEST_UUID_NAMES:
423
+ if req_uuid_name in actual_args:
424
+ actual_args[req_uuid_name] = request.uuid if request is not None else None
425
+ break
426
+
427
+ # Fixing (if no options are specified, assuming a single-step action)
428
+ if self.__total_steps == 0 and self.__total_time == 0:
429
+ self.__total_steps = 1
430
+
431
+ # Fixing the single step case: in this case, time does not matter, so we force it to zero
432
+ if self.__total_steps == 1:
433
+ self.__total_time = 0
434
+
435
+ # Increasing the step index
436
+ self.__step += 1 # This is a step index, so self.__step == 0 means "done 1 step"
437
+
438
+ if HybridStateMachine.DEBUG:
439
+ if request is None or request.requester is None:
440
+ requester_str = "nobody"
441
+ else:
442
+ requester_str = request.requester
443
+ print(f"[DEBUG HSM] Calling function {self.name} (multi_steps: {self.is_multi_steps()}), "
444
+ f"requested by {requester_str}, with actual params: {actual_args}")
445
+
446
+ # Calling the method here
447
+ ret = await self.__fcn(**actual_args)
448
+
449
+ if HybridStateMachine.DEBUG:
450
+ print(f"[DEBUG HSM] Returned: {ret}")
451
+
452
+ # If action failed, be sure to reduce the step counter (only if it was actually incremented)
453
+ if not ret:
454
+ self.__step -= 1
455
+
456
+ # If it went OK, we reset the time counter that is related to the timeout
457
+ else:
458
+ self.__timeout_starting_time = 0
459
+
460
+ return ret
461
+ else:
462
+ if HybridStateMachine.DEBUG:
463
+ print(f"[DEBUG HSM] Tried and failed (missing actual param): {self}")
464
+ return False
465
+
466
+ def __str__(self):
467
+ """Provides a string representation of the `Action` instance.
468
+
469
+ Returns:
470
+ A string containing a formatted summary of the instance.
471
+ """
472
+ return (f"[Action: {self.name}] id: {self.id}, args: {self.args}, param_list: {self.param_list}, "
473
+ f"total_steps: {self.__total_steps}, "
474
+ f"total_time: {self.__total_time}, timeout: {self.__timeout}, "
475
+ f"ready: {self.ready}, requests: {str(self.requests)}, msg: {str(self.msg)}]")
476
+
477
+ def set_as_ready(self):
478
+ """Sets the action's ready flag to `True`, indicating it can now be executed."""
479
+ self.ready = True
480
+
481
+ def set_as_not_ready(self):
482
+ """Sets the action's ready flag to `False`, preventing it from being executed."""
483
+ self.ready = False
484
+
485
+ def set_msg(self, msg):
486
+ """Sets the message associated to this action."""
487
+
488
+ if msg is not None:
489
+ self.msg = html.unescape(msg)
490
+ self.msg_with_wildcards = self.msg
491
+ else:
492
+ self.msg = None
493
+ self.msg_with_wildcards = None
494
+
495
+ def is_ready(self, consider_requests: bool = True):
496
+ """Checks if the action is ready to be executed. It returns `True` if the `ready` flag is set or if there are
497
+ any pending requests.
498
+
499
+ Args:
500
+ consider_requests: A boolean flag to include pending requests in the readiness check.
501
+
502
+ Returns:
503
+ A boolean indicating the action's readiness.
504
+ """
505
+ return self.ready or (consider_requests and len(self.requests) > 0)
506
+
507
+ def was_last_step_done(self):
508
+ """Determines if the action has reached its completion criteria, either by reaching the total number of steps
509
+ or by exceeding the maximum allowed execution time.
510
+
511
+ Returns:
512
+ True if the action is completed, False otherwise.
513
+ """
514
+ return ((self.__total_steps > 0 and self.__step == self.__total_steps - 1) or
515
+ (self.__total_time > 0 and ((time.perf_counter() - self.__starting_time) >= self.__total_time)))
516
+
517
+ def cannot_be_run_anymore(self):
518
+ """Checks if the action has reached a state where it cannot be executed further, for instance, due to
519
+ completion or a timeout.
520
+
521
+ Returns:
522
+ A boolean indicating if the action can no longer be run.
523
+ """
524
+ return self.__cannot_be_run_anymore
525
+
526
+ def has_completion_step(self):
527
+ """Checks if the action is designed to have a completion step, which is a final execution pass after the main
528
+ action logic has finished.
529
+
530
+ Returns:
531
+ A boolean indicating the presence of a completion step.
532
+ """
533
+ return self.__has_completion_step
534
+
535
+ def is_multi_steps(self):
536
+ """Determines if the action is configured to be a multistep action (i.e., not a single-step action).
537
+
538
+ Returns:
539
+ A boolean indicating if the action is multistep.
540
+ """
541
+ return self.__total_steps != 1
542
+
543
+ def has_a_timeout(self):
544
+ """Checks if a timeout has been configured for the action.
545
+
546
+ Returns:
547
+ A boolean indicating if a timeout is set.
548
+ """
549
+ return self.__timeout > 0
550
+
551
+ def is_delayed(self, starting_time: float):
552
+ """Checks if the action is currently in a delayed state and cannot be executed yet, based on a defined delay
553
+ period.
554
+
555
+ Args:
556
+ starting_time: The time the delay period began.
557
+
558
+ Returns:
559
+ True if the action is delayed, False otherwise.
560
+ """
561
+ return self.__delay > 0 and (time.perf_counter() - starting_time) <= self.__delay
562
+
563
+ def is_timed_out(self):
564
+ """Checks if the action has exceeded its configured timeout period since the last successful execution attempt.
565
+
566
+ Returns:
567
+ True if the action has timed out, False otherwise.
568
+ """
569
+ if self.__timeout <= 0 or self.__timeout_starting_time <= 0:
570
+ return False
571
+ else:
572
+ if HybridStateMachine.DEBUG:
573
+ print(f"[DEBUG HSM] checking if {self.name} is timed out:"
574
+ f" {(time.perf_counter() - self.__timeout_starting_time)} >= {self.__timeout}")
575
+ if (time.perf_counter() - self.__timeout_starting_time) >= self.__timeout:
576
+ if HybridStateMachine.DEBUG:
577
+ print(f"[DEBUG HSM] Timeout for {self.name}!")
578
+ return True
579
+ else:
580
+ return False
581
+
582
+ def to_list(self, minimal=False):
583
+ """Converts the action's properties into a list for easy serialization. It can generate either a full or a
584
+ minimal representation.
585
+
586
+ Args:
587
+ minimal: A boolean flag to return a minimal list representation.
588
+
589
+ Returns:
590
+ A list containing the action's properties.
591
+ """
592
+ if not minimal:
593
+ if self.msg is not None:
594
+ msg = self.msg.encode("ascii", "xmlcharrefreplace").decode("ascii")
595
+ else:
596
+ msg = None
597
+ return [self.name, self.args, self.ready, self.id] + ([msg] if msg is not None else [])
598
+ else:
599
+ return [self.name, self.args]
600
+
601
+ def same_as(self, name: str, args: dict | None):
602
+ """Compares the current action to a target action by name and arguments. It returns `True` if they are
603
+ considered the same, ignoring specific arguments like time or timeout.
604
+
605
+ Args:
606
+ name: The name of the target action.
607
+ args: The arguments of the target action.
608
+
609
+ Returns:
610
+ A boolean indicating if the actions are a match.
611
+ """
612
+ if args is None:
613
+ args = {}
614
+
615
+ # The current action is the same of another action called with some arguments "args" if:
616
+ # 1) it has the same name of the other action
617
+ # 2) the name of the arguments in "args" are known and valid
618
+ # 3) the values of the arguments in "args" matches the ones of the current action, being them default or not
619
+ # the values of those arguments that are not in "args" are assumed to the equivalent to the ones in the current
620
+ # action, so:
621
+ # - if the current action is act(a=3, b=4), then it is the same_as(name='act', args={'a': 3})
622
+ # - if the current action is act(a=3, b=4), then it is the same_as(name='act', args={'a': 3, 'b': 4, 'c': 5})
623
+ args_to_exclude = Action.SECONDS_ARG_NAMES | Action.TIMEOUT_ARG_NAMES | Action.DELAY_ARG_NAMES
624
+ return (name == self.name and
625
+ self.__check_if_args_exist(args) and
626
+ all(k in args_to_exclude or k not in self.args or self.args[k] == v for k, v in args.items()))
627
+
628
+ def __check_if_args_exist(self, args: dict, exception: bool = False):
629
+ """A private helper method to validate that all provided arguments for an action exist in the action's
630
+ parameter list. It can either raise a `ValueError` or return a boolean.
631
+
632
+ Args:
633
+ args: The dictionary of arguments to check.
634
+ exception: If `True`, a `ValueError` is raised on failure.
635
+
636
+ Returns:
637
+ True if all arguments are valid, False otherwise (if `exception` is `False`).
638
+ """
639
+ if args is not None:
640
+ for param_name in args.keys():
641
+ if param_name not in self.param_list:
642
+ if exception:
643
+ raise ValueError(f"Unknown parameter {param_name} for action {self.name}")
644
+ else:
645
+ return False
646
+ return True
647
+
648
+ def set_wildcards(self, wildcards: dict[str, str | float | int] | None):
649
+ """Replaces wildcard values in the action's arguments with actual values. This method is used to dynamically
650
+ configure actions with context-specific data.
651
+
652
+ Args:
653
+ wildcards: A dictionary mapping wildcard placeholders to their concrete values.
654
+ """
655
+ self.wildcards = wildcards if wildcards is not None else {}
656
+ self.__replace_wildcard_values()
657
+
658
+ def add_request(self, signature: object, args: dict, timestamp: float, uuid: str):
659
+ """Adds a new request to the action's internal list. This is used to track pending requests that might make the
660
+ action ready to be executed.
661
+
662
+ Args:
663
+ signature: The object making the request.
664
+ args: The arguments associated with the request.
665
+ timestamp: The time the request was made.
666
+ uuid: A unique ID for the request.
667
+ """
668
+ req = ActionRequest(signature, self, args, timestamp, uuid)
669
+ self.requests.add(req)
670
+
671
+ def clear_requests(self, signature: object | None = None):
672
+ """Clears all pending requests from the action's list."""
673
+ if signature is None:
674
+ self.requests = ActionRequestList()
675
+ else:
676
+ if self.requests.is_requester_known(signature):
677
+ requests = self.requests.get_requests(signature)
678
+ for req in requests:
679
+ self.requests.remove(req)
680
+
681
+ def clear_request(self, signature: object, req_id: int):
682
+ req = self.requests.get_request(req_id, signature)
683
+ if req is not None:
684
+ self.requests.remove(req)
685
+
686
+ def get_list_of_requests(self) -> ActionRequestList:
687
+ """Retrieves the list of pending requests.
688
+
689
+ Returns:
690
+ The list of pending requests, i.e., an object of type ActionRequestList.
691
+ """
692
+ return self.requests
693
+
694
+ def reset_step(self):
695
+ """Resets the action's state, including the step counter and timing metrics, allowing it to be re-run from the
696
+ beginning.
697
+ """
698
+ self.__step = -1
699
+ self.__starting_time = 0.
700
+ self.__timeout_starting_time = 0.
701
+ self.__cannot_be_run_anymore = False
702
+
703
+ def get_step(self):
704
+ """Retrieves the current step index of the multistep action.
705
+
706
+ Returns:
707
+ An integer representing the current step index.
708
+ """
709
+ return self.__step
710
+
711
+ def get_total_steps(self):
712
+ """Retrieves the total number of steps configured for the action.
713
+
714
+ Returns:
715
+ An integer representing the total steps.
716
+ """
717
+ return self.__total_steps
718
+
719
+ def get_starting_time(self):
720
+ """Retrieves the timestamp when the action's current execution started.
721
+
722
+ Returns:
723
+ A float representing the starting time.
724
+ """
725
+ return self.__starting_time
726
+
727
+ def get_total_time(self):
728
+ """Retrieves the total time configured for the action's execution.
729
+
730
+ Returns:
731
+ A float representing the total time.
732
+ """
733
+ return self.__total_time
734
+
735
+ def get_actual_params(self, additional_args: dict | None):
736
+ """A helper method that resolves all parameters for an action's execution. It combines the action's
737
+ default arguments, initial arguments, and any additional arguments provided during the call, ensuring all
738
+ necessary parameters have a value.
739
+
740
+ Args:
741
+ additional_args: A dictionary of arguments to be combined with the action's defaults.
742
+
743
+ Returns:
744
+ A dictionary of all resolved arguments, or `None` if a required parameter is missing.
745
+ """
746
+ actual_params = {}
747
+ params = self.param_list
748
+ defaults = self.param_to_default_value
749
+ for param_name in params:
750
+ if param_name in self.args:
751
+ actual_params[param_name] = self.args[param_name]
752
+ elif additional_args is not None and param_name in additional_args:
753
+ actual_params[param_name] = additional_args[param_name]
754
+ elif param_name in defaults:
755
+ actual_params[param_name] = defaults[param_name]
756
+ else:
757
+ if HybridStateMachine.DEBUG:
758
+ print(f"[DEBUG HSM] Getting actual params for {self.name}; missing param: {param_name}")
759
+ return None
760
+ return actual_params
761
+
762
+ def __action_name_to_callable(self, action_name: str):
763
+ """A private helper method that resolves a string action name into a callable method on the `actionable`
764
+ object. It raises a `ValueError` if the method is not found.
765
+
766
+ Args:
767
+ action_name: The name of the method to retrieve.
768
+
769
+ Returns:
770
+ A callable function or method.
771
+ """
772
+ if self.actionable is not None:
773
+ action_fcn = getattr(self.actionable, action_name)
774
+ if action_fcn is None:
775
+ raise ValueError("Cannot find function/method: " + str(action_name))
776
+ return action_fcn
777
+ else:
778
+ return None
779
+
780
+ def __get_action_params(self):
781
+ """A private helper method that inspects the signature of the action's method to populate the list of
782
+ supported parameters and their default values.
783
+ """
784
+ self.param_list = [param_name for param_name in self.__sig.parameters.keys()]
785
+ self.param_to_default_value = {param.name: param.default for param in self.__sig.parameters.values() if
786
+ param.default is not inspect.Parameter.empty}
787
+
788
+ def __replace_wildcard_values(self):
789
+ """A private helper method that replaces placeholder values (wildcards) in the action's arguments with their
790
+ actual, concrete values. It handles both single-value and list-based wildcards.
791
+ """
792
+ if self.args_with_wildcards is None:
793
+ self.args_with_wildcards = copy.deepcopy(self.args) # Backup before applying wildcards (first time only)
794
+ else:
795
+ self.args = copy.deepcopy(self.args_with_wildcards) # Restore a backup before applying wildcards
796
+
797
+ if self.msg_with_wildcards is None:
798
+ self.msg_with_wildcards = self.msg
799
+ else:
800
+ self.msg = self.msg_with_wildcards
801
+
802
+ for wildcard_from, wildcard_to in self.wildcards.items():
803
+ for k, v in self.args.items():
804
+ if not isinstance(wildcard_to, str):
805
+ if wildcard_from == v:
806
+ self.args[k] = wildcard_to
807
+ else:
808
+ if isinstance(v, list):
809
+ for i, vv in enumerate(v):
810
+ if isinstance(vv, str) and wildcard_from in vv:
811
+ v[i] = vv.replace(wildcard_from, wildcard_to)
812
+ elif isinstance(v, str):
813
+ if wildcard_from in v:
814
+ self.args[k] = v.replace(wildcard_from, wildcard_to)
815
+
816
+ if self.msg is not None:
817
+ self.msg = self.msg.replace(wildcard_from, str(wildcard_to))
818
+
819
+ def __guess_total_steps(self, args):
820
+ """A private helper method that attempts to determine the total number of steps for a multistep action by
821
+ looking for specific keyword arguments like 'steps' or 'samples'.
822
+
823
+ Args:
824
+ args: The dictionary of arguments to inspect.
825
+ """
826
+ for prefix in Action.KNOWN_SINGLE_STEP_ACTION_PREFIXES:
827
+ if self.name.startswith(prefix):
828
+ return
829
+ for arg_name in Action.STEPS_ARG_NAMES:
830
+ if arg_name in args:
831
+ if isinstance(args[arg_name], int):
832
+ self.__total_steps = max(float(args[arg_name]), 1.)
833
+ break
834
+
835
+ def __guess_total_time(self, args):
836
+ """A private helper method that attempts to determine the total execution time for an action by looking for a
837
+ 'time' or 'seconds' argument.
838
+
839
+ Args:
840
+ args: The dictionary of arguments to inspect.
841
+ """
842
+ for prefix in Action.KNOWN_SINGLE_STEP_ACTION_PREFIXES:
843
+ if self.name.startswith(prefix):
844
+ return
845
+ for arg_name in Action.SECONDS_ARG_NAMES:
846
+ if arg_name in args:
847
+ if isinstance(args[arg_name], int) or isinstance(args[arg_name], float):
848
+ try:
849
+ self.__total_time = max(float(args[arg_name]), 0.)
850
+ except ValueError:
851
+ self.__total_time = -1.
852
+ pass
853
+ break
854
+
855
+ def __guess_timeout(self, args):
856
+ """A private helper method that attempts to determine the timeout duration for an action by looking for a
857
+ 'timeout' argument.
858
+
859
+ Args:
860
+ args: The dictionary of arguments to inspect.
861
+ """
862
+ for prefix in Action.KNOWN_SINGLE_STEP_ACTION_PREFIXES:
863
+ if self.name.startswith(prefix):
864
+ return
865
+ for arg_name in Action.TIMEOUT_ARG_NAMES:
866
+ if arg_name in args:
867
+ try:
868
+ self.__timeout = max(float(args[arg_name]), 0.)
869
+ except ValueError:
870
+ self.__timeout = -1.
871
+ pass
872
+ break
873
+
874
+ def __guess_delay(self, args):
875
+ """A private helper method that attempts to determine a delay duration for an action by looking for a 'delay'
876
+ argument.
877
+
878
+ Args:
879
+ args: The dictionary of arguments to inspect.
880
+ """
881
+ for arg_name in Action.DELAY_ARG_NAMES:
882
+ if arg_name in args:
883
+ try:
884
+ self.__delay = max(float(args[arg_name]), 0.)
885
+ except ValueError:
886
+ self.__delay = -1.
887
+ pass
888
+ break
889
+
890
+
891
+ class State:
892
+
893
+ def __init__(self, name: str, idx: int = -1, action: Action | None = None, waiting_time: float = 0.,
894
+ blocking: bool = True, msg: str | None = None, out_fcn: Callable = print):
895
+ """Initializes a `State` object, which is a fundamental component of a Hybrid State Machine. A state can be
896
+ associated with an optional `Action` to be performed, a unique name, and various properties like waiting time
897
+ and blocking behavior. It also stores a human-readable message.
898
+
899
+ Args:
900
+ name: The unique name of the state.
901
+ idx: A unique ID for the state.
902
+ action: An optional `Action` object to be executed when the state is entered.
903
+ waiting_time: The number of seconds to wait before the state can transition.
904
+ blocking: A boolean indicating if the state blocks execution until a condition is met.
905
+ msg: An optional message associated with the state.
906
+ out_fcn: Output print function.
907
+ """
908
+ self.name = name # Name of the state (must be unique)
909
+ self.action = action # Inner state action (it can be None)
910
+ self.id = idx # Unique ID of the state (-1 if not needed)
911
+ self.waiting_time = waiting_time # Number of seconds to wait in the current state before acting
912
+ self.starting_time = 0.
913
+ self.blocking = blocking
914
+ self.msg = msg # Human-readable message associated to this instance of state
915
+ self.out_fcn = out_fcn
916
+
917
+ # Fix UNICODE chars
918
+ if self.msg is not None:
919
+ self.msg = html.unescape(self.msg)
920
+
921
+ # Message parts replaced by wildcards (commonly assumed to be in the format <value>)
922
+ self.wildcards = {} # Value-to-value (es: <playlist> to this:and:this)
923
+ self.msg_with_wildcards = self.msg
924
+
925
+ async def __call__(self, *args, **kwargs):
926
+ """Executes the state's logic. If a `waiting_time` is set, it starts a timer. If an `action` is associated with
927
+ the state, it resets the action's step counter and then executes the action by calling it. It returns the
928
+ result of the action's execution (async).
929
+
930
+ Args:
931
+ *args: Positional arguments to pass to the action's `__call__` method.
932
+ **kwargs: Keyword arguments to pass to the action's `__call__` method.
933
+
934
+ Returns:
935
+ The return value of the action's `__call__` method, or `None` if no action is set.
936
+ """
937
+ if self.starting_time <= 0.:
938
+ self.starting_time = time.perf_counter()
939
+
940
+ if self.msg is not None:
941
+ self.out_fcn(self.msg)
942
+
943
+ if self.action is not None:
944
+ if HybridStateMachine.DEBUG:
945
+ print("[DEBUG HSM] Running action on state: " + self.action.name)
946
+ self.action.reset_step()
947
+ return await self.action(*args, **kwargs)
948
+ else:
949
+ return None
950
+
951
+ def __str__(self):
952
+ """Provides a string representation of the `State` object. This is useful for debugging and logging, as it
953
+ summarizes the state's properties, including its name, ID, waiting time, blocking status, and its associated
954
+ action (if any).
955
+
956
+ Returns:
957
+ A string containing a formatted summary of the state's instance.
958
+ """
959
+ return (f"[State: {self.name}] id: {self.id}, waiting_time: {self.waiting_time}, blocking: {self.blocking}, "
960
+ f"action -> {self.action if self.action is not None else 'none'}, msg: {self.msg}")
961
+
962
+ def set_msg(self, msg):
963
+ """Sets the message associated to this state."""
964
+
965
+ if msg is not None:
966
+ self.msg = html.unescape(msg)
967
+ self.msg_with_wildcards = self.msg
968
+ else:
969
+ self.msg = None
970
+ self.msg_with_wildcards = None
971
+
972
+ def must_wait(self):
973
+ """Checks if the state needs to wait before it can transition. It compares the current elapsed time since
974
+ entering the state with the configured `waiting_time`. If the elapsed time is less than the waiting time,
975
+ it returns `True`, indicating the state is still in a waiting period.
976
+
977
+ Returns:
978
+ A boolean indicating whether the state is currently waiting.
979
+ """
980
+ if self.waiting_time > 0.:
981
+ if (time.perf_counter() - self.starting_time) >= self.waiting_time:
982
+ return False
983
+ else:
984
+ if HybridStateMachine.DEBUG:
985
+ print(f"[DEBUG HSM] Time passing: {(time.perf_counter() - self.starting_time)} seconds")
986
+ return True
987
+ else:
988
+ return False
989
+
990
+ def to_list(self):
991
+ """Converts the state's properties into a list. This method is useful for serialization, allowing the state to
992
+ be easily stored or transmitted. It includes the action's minimal list representation, the state's ID,
993
+ blocking status, waiting time, and message.
994
+
995
+ Returns:
996
+ A list containing the state's properties.
997
+ """
998
+ if self.msg is not None:
999
+ msg = self.msg.encode("ascii", "xmlcharrefreplace").decode("ascii")
1000
+ else:
1001
+ msg = None
1002
+ return ((self.action.to_list(minimal=True) if self.action is not None else [None, None]) +
1003
+ ([self.id, self.blocking, self.waiting_time] + ([msg] if msg is not None else [])))
1004
+
1005
+ def has_action(self):
1006
+ """A simple getter that checks if an action is associated with the state.
1007
+
1008
+ Returns:
1009
+ True if an action is set, False otherwise.
1010
+ """
1011
+ return self.action is not None
1012
+
1013
+ def get_starting_time(self):
1014
+ """Retrieves the timestamp when the state's execution began. This is used to calculate the elapsed waiting time.
1015
+
1016
+ Returns:
1017
+ A float representing the starting time.
1018
+ """
1019
+ return self.starting_time
1020
+
1021
+ def reset(self):
1022
+ """Resets the state's internal counters. This method is typically called when re-entering a state. It sets the
1023
+ `starting_time` to zero and also resets the associated action's step counter if an action exists.
1024
+ """
1025
+ self.starting_time = 0.
1026
+ if self.action is not None:
1027
+ self.action.reset_step()
1028
+
1029
+ def set_blocking(self, blocking: bool):
1030
+ """Sets the blocking status of the state. A blocking state will prevent the state machine from transitioning to
1031
+ the next state until the action is fully completed.
1032
+
1033
+ Args:
1034
+ blocking: A boolean value to set the blocking status.
1035
+ """
1036
+ self.blocking = blocking
1037
+
1038
+ def set_wildcards(self, wildcards: dict[str, str | float | int] | None):
1039
+ """Replaces wildcard values in the state messages. This method is used to dynamically
1040
+ configure state messages with context-specific data.
1041
+
1042
+ Args:
1043
+ wildcards: A dictionary mapping wildcard placeholders to their concrete values.
1044
+ """
1045
+ self.wildcards = wildcards if wildcards is not None else {}
1046
+ self.__replace_wildcard_values()
1047
+
1048
+ def __replace_wildcard_values(self):
1049
+ """A private helper method that replaces placeholder values (wildcards) in the state message.
1050
+ It handles both single-value and list-based wildcards.
1051
+ """
1052
+ if self.msg_with_wildcards is None:
1053
+ self.msg_with_wildcards = self.msg
1054
+ else:
1055
+ self.msg = self.msg_with_wildcards
1056
+
1057
+ if self.msg is not None:
1058
+ for wildcard_from, wildcard_to in self.wildcards.items():
1059
+ self.msg = self.msg.replace(wildcard_from, str(wildcard_to))
1060
+
1061
+
1062
+ class HybridStateMachine:
1063
+ DEBUG = True
1064
+ DEFAULT_WILDCARDS = {'<world>': '<world>', '<agent>': '<agent>', '<partner>': '<partner>', '<role>': '<role>'}
1065
+ REQUEST_VALIDITY_TIMEOUT = 5 * 60.0 # Seconds
1066
+ ACTION_TICKS_PER_STATUS = [" ✅ ", " 🔄 ", " ❌ "] # Keep the final spaces
1067
+
1068
+ def __init__(self, actionable: object, wildcards: dict[str, str | float | int] | None = None,
1069
+ request_signature_checker: Callable[[object], bool] | None = None,
1070
+ policy: Callable[[list[Action]], tuple[int, ActionRequest | None]] | None = None):
1071
+ """Initializes a `HybridStateMachine` object, which orchestrates states and transitions. It manages a set of
1072
+ states and actions, and handles the logic for transitions between states based on conditions and a defined
1073
+ policy. It sets up initial and current states, wildcards for dynamic arguments, and references to an
1074
+ `actionable` object whose methods are the actions to be called. It also includes debug and output settings.
1075
+
1076
+ Args:
1077
+ actionable: The object on which actions (methods) are to be executed.
1078
+ wildcards: A dictionary of key-value pairs for dynamic argument substitution.
1079
+ request_signature_checker: An optional callable to validate incoming action requests.
1080
+ policy: An optional callable that determines which action to execute from a list of feasible actions.
1081
+ """
1082
+
1083
+ # States are identified by strings, and then handled as State object with possibly and integer ID and action
1084
+ self.initial_state: str | None = None # Initial state of the machine
1085
+ self.prev_state: str | None = None # Previous state
1086
+ self.limbo_state: str | None = None # When an action takes more than a step to complete, we are in "limbo"
1087
+ self.state: str | None = None # Current state
1088
+ self.role: str | None = None # Role of the agent in the state machine (e.g., teacher, student, etc.)
1089
+ self.enabled: bool = True
1090
+ self.states: dict[str, State] = {} # State name to State object
1091
+
1092
+ # Actions (transitions) are handled as Action objects in-between state strings
1093
+ self.transitions: dict[str, dict[str, list[Action]]] = {} # Pair-of-states to the actions between them
1094
+ self.actionable: object = actionable # The object on whose methods are actions that the machine calls
1095
+ self.wildcards: dict[str, str | float | int] | None = wildcards \
1096
+ if wildcards is not None else {} # From a wildcards string to a specific value (used in action arguments)
1097
+ self.policy = policy if policy is not None else self.__policy_first_requested_or_first_ready
1098
+ self.policy_filter = None
1099
+ self.policy_filter_opts = {}
1100
+ self.welcome_msg = None
1101
+ self.welcome_msg_with_wildcards = None
1102
+ self.show_blocking_states = False
1103
+ self.show_action_completion = False
1104
+ self.show_action_request_info = False
1105
+
1106
+ # Actions can be requested from the "outside": each request if checked by this function, if any
1107
+ self.request_signature_checker: Callable[[object], bool] | None = request_signature_checker
1108
+
1109
+ # Running data
1110
+ self.__action: Action | None = None # Action that is being executed (could take more than a step to complete)
1111
+ self.__last_completed_action: Action | None = None
1112
+ self.__cur_feasible_actions_status: dict | None = None # Store info of the executed action (for multi-steps)
1113
+ self.__id_to_state: list[State] = [] # Map from state ID to State object
1114
+ self.__id_to_action: list[Action] = [] # Map from action ID to Action object
1115
+ self.__state_changed = False # Internal flag
1116
+ self.__id_to_original_state_msg: list[tuple[str | None, str | None]] = []
1117
+ self.__id_to_original_action_msg: list[str | None] = []
1118
+
1119
+ # Forcing default wildcards
1120
+ self.add_wildcards(HybridStateMachine.DEFAULT_WILDCARDS)
1121
+
1122
+ # Forcing output function
1123
+ self.__last_printed_msg = None
1124
+ self.__last_printed_tick = None
1125
+ self.__debug_messages_active = False
1126
+ self.print_stream = sys.stdout
1127
+ self.print_start = ""
1128
+ self.print_ending = "\n"
1129
+ self.print_fcn = print
1130
+ self.print_fcn_supports_html = False
1131
+
1132
+ def wrapped_out_fcn(msg: str, is_state: bool,
1133
+ action_request: ActionRequest | None, action_requests: ActionRequestList | None = None):
1134
+ if msg is not None:
1135
+ must_print = False
1136
+ if msg in HybridStateMachine.ACTION_TICKS_PER_STATUS:
1137
+ if msg != self.__last_printed_tick:
1138
+ self.__last_printed_tick = msg
1139
+ must_print = True
1140
+ elif msg != self.__last_printed_msg:
1141
+ self.__last_printed_msg = msg
1142
+ self.__last_printed_tick = None
1143
+ must_print = True
1144
+
1145
+ if must_print:
1146
+ if not self.print_fcn_supports_html:
1147
+
1148
+ # Handle a bit of HTML (<br/>, <a href=...>...</a>, <strong>...</strong>)
1149
+ msg = (msg.replace('<br/>', '\n').replace('<strong>', '')
1150
+ .replace('</strong>', ''))
1151
+ pattern = r'<a\s+href=[\'"](.*?)[\'"][^>]*>(.*?)</a>'
1152
+ msg = re.sub(pattern, r'\2 (\1)', msg)
1153
+
1154
+ if is_state and self.show_blocking_states:
1155
+ if self.states[self.state].blocking:
1156
+ msg += " 🔴"
1157
+ else:
1158
+ msg += " 🟢"
1159
+
1160
+ if not is_state and self.show_action_request_info and action_request is not None:
1161
+ msg += (f" (requester: {action_request.requester}, uuid: {action_request.uuid}, "
1162
+ f"#requests: {len(action_requests)})")
1163
+
1164
+ self.print_fcn(self.print_start + msg, end=self.print_ending, file=self.print_stream)
1165
+
1166
+ def wrapped_state_out_fcn(msg: str):
1167
+ wrapped_out_fcn(msg, True, None, None)
1168
+
1169
+ def wrapped_action_out_fcn(msg: str,
1170
+ request: ActionRequest | None = None, requests: ActionRequestList | None = None):
1171
+ wrapped_out_fcn(msg, False, request, requests)
1172
+
1173
+ self.state_out_fcn = wrapped_state_out_fcn
1174
+ self.action_out_fcn = wrapped_action_out_fcn
1175
+
1176
+ def set_print_fcn(self, print_fcn, supports_html):
1177
+ self.print_fcn = print_fcn
1178
+ self.print_fcn_supports_html = supports_html
1179
+
1180
+ def show_ticks_in_action_messages(self, do_it: bool = True):
1181
+ self.show_action_completion = do_it
1182
+
1183
+ def show_marks_in_blocking_state_messages(self, do_it: bool = True):
1184
+ self.show_blocking_states = do_it
1185
+
1186
+ def show_request_info_in_action_messages(self, do_it: bool = True):
1187
+ self.show_action_request_info = do_it
1188
+
1189
+ def set_welcome_message(self, msg):
1190
+ """Sets a message that will be printed only once when the initial state is reached."""
1191
+
1192
+ if msg is not None:
1193
+ self.welcome_msg = html.unescape(msg)
1194
+ self.welcome_msg_with_wildcards = self.welcome_msg
1195
+ else:
1196
+ self.welcome_msg = None
1197
+ self.welcome_msg_with_wildcards = None
1198
+
1199
+ def to_dict(self):
1200
+ """Serializes the state machine's current configuration into a dictionary. This includes its states,
1201
+ transitions, roles, and the current action being executed. It is useful for saving the state of the machine or
1202
+ for logging its status in a structured format.
1203
+
1204
+ Returns:
1205
+ A dictionary representation of the state machine's properties.
1206
+ """
1207
+ return {
1208
+ 'initial_state': self.initial_state,
1209
+ 'state': self.state,
1210
+ 'role': self.role,
1211
+ 'prev_state': self.prev_state,
1212
+ 'limbo_state': self.limbo_state,
1213
+ 'welcome_msg':
1214
+ self.welcome_msg_with_wildcards.encode("ascii", "xmlcharrefreplace").decode(
1215
+ "ascii") if self.welcome_msg_with_wildcards is not None else None,
1216
+ 'highlight_blocking_states_in_messages': self.show_blocking_states,
1217
+ 'show_action_ticks_after_messages': self.show_action_completion,
1218
+ 'show_action_request_after_messages': self.show_action_request_info,
1219
+ 'state_actions': {state.name: state.to_list() for state in self.__id_to_state},
1220
+ 'transitions': {from_state: {to_state: [act.to_list() for act in action_list] for to_state, action_list in
1221
+ to_states.items()} for from_state, to_states in self.transitions.items() if
1222
+ len(to_states) > 0},
1223
+ 'cur_action': self.__action.to_list() if self.__action is not None else None
1224
+ }
1225
+
1226
+ def __str__(self):
1227
+ """Generates a human-readable string representation of the state machine. It uses the `to_dict` method to get
1228
+ the machine's data and then formats it as a compact JSON string, making it easy to inspect for debugging
1229
+ purposes.
1230
+
1231
+ Returns:
1232
+ A formatted JSON string representing the state machine.
1233
+ """
1234
+ hsm_data = self.to_dict()
1235
+
1236
+ def custom_serializer(obj):
1237
+ if not isinstance(obj, (int, str, float, bool, list, tuple, dict, set)):
1238
+ return "_non_basic_type_removed_"
1239
+ else:
1240
+ return obj
1241
+
1242
+ json_str = json.dumps(hsm_data, indent=4, default=custom_serializer)
1243
+
1244
+ # Compacting lists
1245
+ def remove_newlines_in_lists(json_string):
1246
+ stack = []
1247
+ output = []
1248
+ i = 0
1249
+ while i < len(json_string):
1250
+ char = json_string[i]
1251
+ if char == '[':
1252
+ stack.append('[')
1253
+ output.append(char)
1254
+ elif char == ']':
1255
+ stack.pop()
1256
+ output.append(char)
1257
+ elif char == '\n' and stack: # Skipping newline
1258
+ i += 1
1259
+ while i < len(json_string) and json_string[i] in ' \t':
1260
+ i += 1
1261
+ if output[-1] == ",":
1262
+ output.append(" ")
1263
+ continue # Do not output newline or following spaces
1264
+ else:
1265
+ output.append(char)
1266
+ i += 1
1267
+ return ''.join(output)
1268
+
1269
+ return remove_newlines_in_lists(json_str)
1270
+
1271
+ def set_actionable(self, obj: object):
1272
+ """Sets the object on which the state machine's actions will be performed. This allows the same state machine
1273
+ logic to be applied to different objects. It updates the `actionable` reference for all states and actions
1274
+ within the machine.
1275
+
1276
+ Args:
1277
+ obj: The object instance to be set as the new `actionable`.
1278
+ """
1279
+ self.actionable = obj
1280
+
1281
+ for state_obj in self.states.values():
1282
+ if state_obj.action is not None:
1283
+ state_obj.action.actionable = obj
1284
+
1285
+ def set_policy(self, policy_fcn: Callable[[list[Action]], tuple[int, ActionRequest | None]]):
1286
+ """Sets the policy to be used in selecting what action to perform in the current state.
1287
+
1288
+ Args:
1289
+ policy_fcn: A function that takes a list of `Action` objects that are candidates for execution, and returns
1290
+ the index of the selected action and an ActionRequest object with the action-requester details (object,
1291
+ arguments, time, and UUID), or -1 and None if no action is selected.
1292
+ """
1293
+ self.policy = policy_fcn
1294
+
1295
+ def set_policy_filter(self, filter_fcn: Callable[
1296
+ [int, ActionRequest | None, list[Action], dict], tuple[int, ActionRequest | None]],
1297
+ filter_fcn_opts: dict):
1298
+ """Sets the filter function that will overload the decision of the policy.
1299
+
1300
+ Args:
1301
+ filter_fcn: A function that takes the decision of the policy, a list of `Action` objects
1302
+ that are candidates for execution, and a customizable dict of options, and returns the index of the
1303
+ selected action and an ActionRequest with the requested details (requests, arguments, time, and UUID),
1304
+ or -1 and None if no action is selected.
1305
+ filter_fcn_opts: A reference to the dictionary of custom options that will be passed to the filter function.
1306
+ """
1307
+ self.policy_filter = filter_fcn
1308
+ self.policy_filter_opts = filter_fcn_opts
1309
+ self.policy_filter_opts.clear()
1310
+
1311
+ def set_wildcards(self, wildcards: dict[str, str | float | int] | None):
1312
+ """Sets the dictionary of wildcards that are used to dynamically replace placeholder values in action
1313
+ arguments. It updates all actions with the new wildcard dictionary.
1314
+
1315
+ Args:
1316
+ wildcards: A dictionary containing wildcard key-value pairs.
1317
+ """
1318
+ self.wildcards = wildcards if wildcards is not None else {}
1319
+ for action in self.__id_to_action:
1320
+ action.set_wildcards(self.wildcards)
1321
+ for state in self.__id_to_state:
1322
+ state.set_wildcards(self.wildcards)
1323
+ if self.welcome_msg is not None:
1324
+ if self.welcome_msg_with_wildcards is None:
1325
+ self.welcome_msg_with_wildcards = self.welcome_msg
1326
+ else:
1327
+ self.welcome_msg = self.welcome_msg_with_wildcards
1328
+ for wildcard_from, wildcard_to in self.wildcards.items():
1329
+ self.welcome_msg = self.welcome_msg.replace(wildcard_from, str(wildcard_to))
1330
+
1331
+ def set_role(self, role: str):
1332
+ """Sets the role of the agent associated with this state machine. This can be used to influence state machine
1333
+ behavior based on the agent's role (e.g., 'teacher', 'student').
1334
+
1335
+ Args:
1336
+ role: The string representation of the new role.
1337
+ """
1338
+ self.role = role
1339
+ self.update_wildcard("<role>", self.role)
1340
+
1341
+ def get_wildcards(self):
1342
+ """Retrieves the dictionary of wildcards currently used by the state machine.
1343
+
1344
+ Returns:
1345
+ A dictionary of the wildcards.
1346
+ """
1347
+ return self.wildcards
1348
+
1349
+ def add_wildcards(self, wildcards: dict[str, str | float | int | list[str]]):
1350
+ """Adds new key-value pairs to the existing wildcard dictionary. It also triggers an update to all actions with
1351
+ the new combined dictionary.
1352
+
1353
+ Args:
1354
+ wildcards: A dictionary of new wildcards to add.
1355
+ """
1356
+ self.wildcards.update(wildcards)
1357
+ self.set_wildcards(self.wildcards)
1358
+
1359
+ def update_wildcard(self, wildcard_key: str, wildcard_value: str | float | int):
1360
+ """Updates the value of a single existing wildcard. It raises an error if the key does not exist. This method
1361
+ is useful for changing a single dynamic value without redefining all wildcards.
1362
+
1363
+ Args:
1364
+ wildcard_key: The key of the wildcard to update.
1365
+ wildcard_value: The new value for the wildcard.
1366
+ """
1367
+ assert wildcard_key in self.wildcards, f"{wildcard_key} is not a valid wildcard"
1368
+ self.wildcards[wildcard_key] = wildcard_value
1369
+ self.set_wildcards(self.wildcards)
1370
+
1371
+ def get_action_step(self):
1372
+ """Retrieves the current step index of the action being executed. This is particularly useful for tracking the
1373
+ progress of multistep actions.
1374
+
1375
+ Returns:
1376
+ An integer representing the current step, or -1 if no action is running.
1377
+ """
1378
+ return self.__action.get_step() if self.__action is not None else -1
1379
+
1380
+ def is_busy_acting(self):
1381
+ """Checks if the state machine is currently executing an action. This is determined by checking if the action
1382
+ step index is greater than or equal to 0.
1383
+
1384
+ Returns:
1385
+ True if an action is running, False otherwise.
1386
+ """
1387
+ return self.get_action_step() >= 0
1388
+
1389
+ def add_state(self, state: str, action: str = None, args: dict | None = None, state_id: int | None = None,
1390
+ waiting_time: float | None = None, blocking: bool | None = None,
1391
+ msg: str | None = None, msg_action: str | None = None):
1392
+ """Adds a new state to the state machine. This method can create a new state with an optional inner action or
1393
+ update an existing state. It assigns a unique ID to the state and its action.
1394
+
1395
+ Args:
1396
+ state: The name of the state to add.
1397
+ action: The name of the action to associate with the state.
1398
+ args: A dictionary of arguments for the action.
1399
+ state_id: An optional unique ID for the state.
1400
+ waiting_time: A float representing a delay before the state can transition.
1401
+ blocking: A boolean indicating if the state is blocking.
1402
+ msg: A human-readable message for the state.
1403
+ msg_action: A human-readable message for the action running on this state.
1404
+ """
1405
+ if args is None:
1406
+ args = {}
1407
+ sta_obj = None
1408
+ if state_id is None:
1409
+ if state not in self.states:
1410
+ state_id = len(self.__id_to_state)
1411
+ else:
1412
+ sta_obj = self.states[state]
1413
+ state_id = sta_obj.id
1414
+ if action is None:
1415
+ act = sta_obj.action if sta_obj is not None else None
1416
+ else:
1417
+ act = Action(name=action, args=args, idx=len(self.__id_to_action),
1418
+ actionable=self.actionable, avoid_changing_ready=True,
1419
+ msg=msg_action, out_fcn=self.action_out_fcn)
1420
+ act.set_wildcards(self.wildcards)
1421
+ self.__id_to_action.append(act)
1422
+ if waiting_time is None:
1423
+ waiting_time = sta_obj.waiting_time if sta_obj is not None else 0. # Default waiting time
1424
+ if blocking is None:
1425
+ blocking = sta_obj.blocking if sta_obj is not None else True # Default blocking
1426
+ if msg is None:
1427
+ msg = sta_obj.msg_with_wildcards if sta_obj is not None else None
1428
+
1429
+ sta = State(name=state, idx=state_id, action=act, waiting_time=waiting_time, blocking=blocking, msg=msg,
1430
+ out_fcn=self.state_out_fcn)
1431
+ sta.set_wildcards(self.wildcards)
1432
+ if state not in self.states:
1433
+ self.__id_to_state.append(sta)
1434
+ else:
1435
+ self.__id_to_state[state_id] = sta
1436
+ self.states[state] = sta
1437
+
1438
+ if len(self.__id_to_state) == 1 and self.state is None:
1439
+ self.set_state(sta.name)
1440
+
1441
+ def get_state_name(self):
1442
+ """Retrieves the name of the current state of the state machine.
1443
+
1444
+ Returns:
1445
+ A string with the state's name, or `None` if no state is set.
1446
+ """
1447
+
1448
+ return self.state
1449
+
1450
+ def get_state(self):
1451
+ """Retrieves the current `State` object of the state machine.
1452
+
1453
+ Returns:
1454
+ A `State` object or `None`.
1455
+ """
1456
+ return self.states[self.state] if self.state is not None else None
1457
+
1458
+ def get_all_states(self):
1459
+ """Retrieves the list of all `State` objects.
1460
+
1461
+ Returns:
1462
+ List of `State` objects.
1463
+ """
1464
+ return self.__id_to_state
1465
+
1466
+ def get_all_actions(self):
1467
+ """Retrieves the list of all `Action` objects.
1468
+
1469
+ Returns:
1470
+ List of `Action` objects.
1471
+ """
1472
+ return self.__id_to_action
1473
+
1474
+ def get_action(self):
1475
+ """Retrieves the `Action` object that is currently being executed.
1476
+
1477
+ Returns:
1478
+ An `Action` object or `None`.
1479
+ """
1480
+ return self.__action
1481
+
1482
+ def get_action_name(self):
1483
+ """Retrieves the name of the action currently being executed.
1484
+
1485
+ Returns:
1486
+ A string with the action's name, or `None` if no action is running.
1487
+ """
1488
+ return self.__action.name if self.__action is not None else None
1489
+
1490
+ def get_last_completed_action_name(self):
1491
+ """Retrieves the name of the last action that was correctly executed.
1492
+
1493
+ Returns:
1494
+ A string with the action's name, or `None` if no actions were executed before.
1495
+ """
1496
+ return self.__last_completed_action.name if self.__last_completed_action is not None else None
1497
+
1498
+ def reset_state(self):
1499
+ """Resets the state machine to its initial state. This clears the current action, the previous state, and
1500
+ the limbo state. It also resets the step counters for all actions within the machine.
1501
+ """
1502
+ self.state = self.initial_state
1503
+ self.limbo_state = None
1504
+ self.prev_state = None
1505
+ self.__action = None
1506
+ for act in self.__id_to_action:
1507
+ act.reset_step()
1508
+ for s in self.__id_to_state:
1509
+ if s.action is not None:
1510
+ s.action.reset_step()
1511
+
1512
+ def get_states(self):
1513
+ """Returns an iterable of all state names defined in the state machine.
1514
+
1515
+ Returns:
1516
+ An iterable of state names.
1517
+ """
1518
+ return list(set(list(self.transitions.keys()) + self.__id_to_state))
1519
+
1520
+ def set_state(self, state: str):
1521
+ """Sets the current state of the state machine to a new, specified state. It also handles the transition logic
1522
+ by resetting the current action and updating the previous state. Raises an error if the new state is not known
1523
+ to the machine.
1524
+
1525
+ Args:
1526
+ state: The name of the state to transition to.
1527
+ """
1528
+ if state in self.transitions or state in self.states:
1529
+ self.prev_state = self.state
1530
+ self.state = state
1531
+ if self.__action is not None:
1532
+ self.__action.reset_step()
1533
+ self.__action = None
1534
+ if self.initial_state is None:
1535
+ self.initial_state = state
1536
+ else:
1537
+ raise ValueError("Unknown state: " + str(state))
1538
+
1539
+ def are_debug_messages_active(self):
1540
+ return self.__debug_messages_active
1541
+
1542
+ def set_debug_messages_active(self, yes: bool):
1543
+ self.__debug_messages_active = yes
1544
+
1545
+ if yes:
1546
+ self.show_ticks_in_action_messages(True)
1547
+ self.show_marks_in_blocking_state_messages(True)
1548
+ self.show_request_info_in_action_messages(True)
1549
+
1550
+ # Replace original messages
1551
+ self.generate_auto_messages(force=True)
1552
+ else:
1553
+ self.show_ticks_in_action_messages(False)
1554
+ self.show_marks_in_blocking_state_messages(False)
1555
+ self.show_request_info_in_action_messages(False)
1556
+
1557
+ # Restore original messages
1558
+ if len(self.__id_to_original_state_msg) > 0:
1559
+ for i, state in enumerate(self.__id_to_state):
1560
+ state.set_msg(self.__id_to_original_state_msg[i][0])
1561
+ if state.action is not None:
1562
+ state.action.set_msg(self.__id_to_original_state_msg[i][1])
1563
+ if len(self.__id_to_original_action_msg) > 0:
1564
+ for i, action in enumerate(self.__id_to_action):
1565
+ action.set_msg(self.__id_to_original_action_msg[i])
1566
+ self.__id_to_original_state_msg.clear()
1567
+ self.__id_to_original_action_msg.clear()
1568
+
1569
+ def generate_auto_messages(self, states: bool = True, actions: bool = True, force: bool = False):
1570
+ if states is True and len(self.__id_to_original_state_msg) == 0:
1571
+ for state in self.__id_to_state:
1572
+ original1 = state.msg_with_wildcards
1573
+ original2 = state.action.msg_with_wildcards if state.action is not None else None
1574
+
1575
+ if state.msg_with_wildcards is None:
1576
+ state.set_msg("📍 " + state.name.replace('_', ' ').capitalize())
1577
+ elif force is True:
1578
+ state.set_msg("📍 " + state.name.replace('_', ' ').capitalize() +
1579
+ " [" + state.msg_with_wildcards + "]")
1580
+ if state.action is not None:
1581
+ if state.action.msg_with_wildcards is None:
1582
+ state.action.set_msg("📍 " + state.action.name.replace('_', ' ').capitalize())
1583
+ elif force is True:
1584
+ state.action.set_msg("📍 " + state.action.name.replace('_', ' ').capitalize() +
1585
+ " [" + state.action.msg_with_wildcards + "]")
1586
+
1587
+ self.__id_to_original_state_msg.append((original1, original2))
1588
+ if actions is True and len(self.__id_to_original_action_msg) == 0:
1589
+ for action in self.__id_to_action:
1590
+ original = action.msg_with_wildcards
1591
+
1592
+ if action.msg_with_wildcards is None:
1593
+ action.set_msg("🚀 " + action.name.replace('_', ' ').capitalize())
1594
+ elif force:
1595
+ action.set_msg("🚀 " + action.name.replace('_', ' ').capitalize() +
1596
+ " [" + action.msg_with_wildcards + "]")
1597
+
1598
+ self.__id_to_original_action_msg.append(original)
1599
+
1600
+ def add_transit(self, from_state: str, to_state: str,
1601
+ action: str, args: dict | None = None, ready: bool = True,
1602
+ act_id: int | None = None, msg: str | None = None, avoid_changing_ready: bool = False):
1603
+ """Defines a transition between two states with an associated action. This method is central to building the
1604
+ state machine's logic. It can also handle loading and integrating a complete state machine from a file,
1605
+ resolving any state name clashes.
1606
+
1607
+ Args:
1608
+ from_state: The name of the starting state.
1609
+ to_state: The name of the destination state (can be a file path to load another HSM).
1610
+ action: The name of the action to trigger the transition.
1611
+ args: A dictionary of arguments for the action.
1612
+ ready: A boolean indicating if the action is ready by default.
1613
+ act_id: An optional unique ID for the action.
1614
+ msg: An optional human-readable message for the action.
1615
+ avoid_changing_ready: A boolean indicating that the selected ready state should not be changed by
1616
+ internal rules.
1617
+ """
1618
+
1619
+ # Plugging a previously loaded HSM
1620
+ if to_state.lower().endswith(".json"):
1621
+ if not os.path.exists(to_state):
1622
+ raise FileNotFoundError(f"Cannot find {to_state}")
1623
+
1624
+ file_name = to_state
1625
+ hsm = HybridStateMachine(self.actionable).load(file_name)
1626
+
1627
+ # First, we avoid name clashes, renaming already-used-state-names in original_name~1 (or ~2, or ~3, ...)
1628
+ hsm_states = list(hsm.states.keys()) # Keep the list(...) thing, since we need a copy here (it will change)
1629
+ for state in hsm_states:
1630
+ renamed_state = state
1631
+ i = 1
1632
+ while renamed_state in self.states or (i > 1 and renamed_state in hsm.states):
1633
+ renamed_state = state + "." + str(i)
1634
+ i += 1
1635
+
1636
+ if hsm.initial_state == state:
1637
+ hsm.initial_state = renamed_state
1638
+ if hsm.prev_state == state:
1639
+ hsm.prev_state = renamed_state
1640
+ if hsm.state == state:
1641
+ hsm.state = renamed_state
1642
+ if hsm.limbo_state == state:
1643
+ hsm.limbo_state = renamed_state
1644
+
1645
+ hsm.states[renamed_state] = hsm.states[state]
1646
+ if renamed_state != state:
1647
+ del hsm.states[state]
1648
+ hsm.transitions[renamed_state] = hsm.transitions[state]
1649
+ if renamed_state != state:
1650
+ del hsm.transitions[state]
1651
+
1652
+ for to_states in hsm.transitions.values():
1653
+ if state in to_states:
1654
+ to_states[renamed_state] = to_states[state]
1655
+ if renamed_state != state:
1656
+ del to_states[state]
1657
+
1658
+ # Saving
1659
+ initial_state_was_set = self.initial_state is not None
1660
+ state_was_set = self.state is not None
1661
+
1662
+ # Include actions/states from another HSM
1663
+ self.include(hsm)
1664
+
1665
+ # Adding a transition to the initial state of the given HSM
1666
+ self.add_transit(from_state=from_state, to_state=hsm.initial_state, action=action, args=args,
1667
+ ready=ready, act_id=None, msg=msg)
1668
+
1669
+ # Restoring
1670
+ self.initial_state = from_state if not initial_state_was_set else self.initial_state
1671
+ self.state = from_state if not state_was_set else self.state
1672
+ return
1673
+
1674
+ # Adding a new transition
1675
+ if from_state not in self.transitions:
1676
+ if from_state not in self.states:
1677
+ self.add_state(from_state, action=None)
1678
+ self.transitions[from_state] = {}
1679
+ if to_state not in self.transitions:
1680
+ if to_state not in self.states:
1681
+ self.add_state(to_state, action=None)
1682
+ self.transitions[to_state] = {}
1683
+ if args is None:
1684
+ args = {}
1685
+ if act_id is None:
1686
+ act_id = len(self.__id_to_action)
1687
+
1688
+ # Clearing
1689
+ if to_state not in self.transitions[from_state]:
1690
+ self.transitions[from_state][to_state] = []
1691
+
1692
+ # Checking
1693
+ existing_action_list = self.transitions[from_state][to_state]
1694
+ for existing_action in existing_action_list:
1695
+ if existing_action.same_as(name=action, args=args):
1696
+ raise ValueError(f"Repeated transition from {from_state} to {to_state}: "
1697
+ f"{existing_action.to_list()}")
1698
+
1699
+ # Adding the new action
1700
+ new_action = Action(name=action, args=args, idx=act_id, actionable=self.actionable, ready=ready, msg=msg,
1701
+ avoid_changing_ready=avoid_changing_ready, out_fcn=self.action_out_fcn)
1702
+ self.transitions[from_state][to_state].append(new_action)
1703
+ self.__id_to_action.append(new_action)
1704
+
1705
+ def include(self, hsm, make_a_copy=False):
1706
+ """Integrates the states and transitions of another state machine (`hsm`) into the current one. This is a
1707
+ crucial method for composing complex state machines from smaller, reusable components. It copies wildcards,
1708
+ states, and transitions, ensuring that all actions and states are properly added and linked. This method also
1709
+ handles an optional `make_a_copy` flag to completely replicate the source machine's state (e.g., current state,
1710
+ initial state).
1711
+
1712
+ Args:
1713
+ hsm: The `HybridStateMachine` object to include.
1714
+ make_a_copy: A boolean to indicate whether the current state machine should adopt the state (e.g.,
1715
+ current state, initial state) of the included one.
1716
+ """
1717
+
1718
+ # Copying wildcards
1719
+ self.add_wildcards(hsm.get_wildcards())
1720
+
1721
+ # Adding states before adding transitions, so that we also add inner state actions, if any
1722
+ for _state in hsm.states.values():
1723
+ self.add_state(state=_state.name,
1724
+ action=_state.action.name if _state.action is not None else None,
1725
+ waiting_time=_state.waiting_time,
1726
+ args=copy.deepcopy(_state.action.args_with_wildcards) if _state.action is not None else None,
1727
+ state_id=None,
1728
+ blocking=_state.blocking,
1729
+ msg=_state.msg_with_wildcards)
1730
+
1731
+ # Copy all the transitions of the HSM
1732
+ for _from_state, _to_states in hsm.transitions.items():
1733
+ for _to_state, _action_list in _to_states.items():
1734
+ for _action in _action_list:
1735
+ self.add_transit(from_state=_from_state, to_state=_to_state, action=_action.name,
1736
+ args=copy.deepcopy(_action.args_with_wildcards), ready=_action.ready,
1737
+ act_id=None, msg=_action.msg_with_wildcards, avoid_changing_ready=True)
1738
+
1739
+ if make_a_copy:
1740
+ self.state = hsm.state
1741
+ self.prev_state = hsm.state
1742
+ self.initial_state = hsm.initial_state
1743
+ self.limbo_state = hsm.limbo_state
1744
+ self.set_welcome_message(hsm.welcome_msg_with_wildcards)
1745
+ self.show_blocking_states = hsm.show_blocking_states
1746
+ self.show_action_completion = hsm.show_blocking_states
1747
+ self.show_action_request_info = hsm.show_action_request_info
1748
+
1749
+ def must_wait(self):
1750
+ """Checks if the current state is in a waiting period before any transitions can occur.
1751
+
1752
+ Returns:
1753
+ A boolean indicating if the state machine must wait.
1754
+ """
1755
+ if self.state is not None:
1756
+ return self.states[self.state].must_wait()
1757
+ else:
1758
+ return False
1759
+
1760
+ def is_enabled(self):
1761
+ """A simple getter to check if the state machine is currently enabled to run.
1762
+
1763
+ Returns:
1764
+ True if the state machine is enabled, False otherwise.
1765
+ """
1766
+ return self.enabled
1767
+
1768
+ def enable(self, yes_or_not: bool):
1769
+ """Enables or disables the state machine. When disabled, the `act_states` and `act_transitions` methods will
1770
+ not perform any actions.
1771
+
1772
+ Args:
1773
+ yes_or_not: A boolean to enable (`True`) or disable (`False`) the state machine.
1774
+ """
1775
+ self.enabled = yes_or_not
1776
+
1777
+ async def act_states(self):
1778
+ """Executes the inner action of the current state, if one exists. This method is for actions that occur upon
1779
+ entering a state but do not cause an immediate transition. It only runs if the state machine is enabled (async).
1780
+ """
1781
+ if not self.enabled:
1782
+ return
1783
+
1784
+ if self.state is not None: # When in the middle of an action, the state is Nones
1785
+ await self.states[self.state]() # Run the action (if any)
1786
+
1787
+ async def act_transitions(self, requested_only: bool = False):
1788
+ """This is the core execution loop for transitions. It finds all feasible actions from the current state and,
1789
+ using a policy, selects and executes one. It handles single-step and multistep actions, managing state changes,
1790
+ timeouts, and failed executions. It returns an integer status code indicating the outcome (e.g., transition
1791
+ done, try again, move to next action) (async).
1792
+
1793
+ Args:
1794
+ requested_only: A boolean to consider only actions that have pending requests.
1795
+
1796
+ Returns:
1797
+ An integer status code: `0` for a successful transition, `1` to retry the same action, `2` to move to the
1798
+ next action, or `-1` if no actions were found.
1799
+ """
1800
+ if not self.enabled:
1801
+ return -1
1802
+
1803
+ # Collecting list of feasible actions, wait flags, etc. (from the current state)
1804
+ if self.__cur_feasible_actions_status is None:
1805
+ if self.state is None:
1806
+ return -1
1807
+
1808
+ actions_list = []
1809
+ to_state_list = []
1810
+ attempts_to_serve_a_request_list = []
1811
+
1812
+ for to_state, action_list in self.transitions[self.state].items():
1813
+ for i, action in enumerate(action_list):
1814
+
1815
+ # Pruning too old requests
1816
+ action.requests.remove_due_to_timeout(HybridStateMachine.REQUEST_VALIDITY_TIMEOUT)
1817
+
1818
+ if (action.is_ready() and (not requested_only or len(action.requests) > 0) and
1819
+ not action.is_delayed(self.states[self.state].starting_time)):
1820
+ actions_list.append(action)
1821
+ to_state_list.append(to_state)
1822
+ attempts_to_serve_a_request_list.append(0)
1823
+
1824
+ if len(actions_list) > 0:
1825
+ self.__cur_feasible_actions_status = {
1826
+ 'actions_list': actions_list,
1827
+ 'to_state_list': to_state_list,
1828
+ 'selected_idx': 0,
1829
+ 'selected_request': None,
1830
+ 'attempts_to_serve_a_request_list': attempts_to_serve_a_request_list
1831
+ }
1832
+ else:
1833
+
1834
+ # Reloading the already computed set of actions, wait flags, etc. (when in the middle of an action)
1835
+ actions_list = self.__cur_feasible_actions_status['actions_list']
1836
+ to_state_list = self.__cur_feasible_actions_status['to_state_list']
1837
+ attempts_to_serve_a_request_list = self.__cur_feasible_actions_status['attempts_to_serve_a_request_list']
1838
+
1839
+ # Pruning too old requests
1840
+ idx_to_remove = []
1841
+ for i, action in enumerate(actions_list):
1842
+ if len(action.requests) > 0:
1843
+ action.requests.remove_due_to_timeout(HybridStateMachine.REQUEST_VALIDITY_TIMEOUT)
1844
+ if len(action.requests) == 0:
1845
+ idx_to_remove.append(i)
1846
+ for i in idx_to_remove:
1847
+ del actions_list[i]
1848
+ del to_state_list[i]
1849
+ del attempts_to_serve_a_request_list[i]
1850
+
1851
+ # Using the selected policy to decide what action to apply
1852
+ while len(actions_list) > 0:
1853
+
1854
+ # It there was an already selected action (for example a multistep action), then continue with it,
1855
+ # otherwise, select a new one following a certain policy (actually, first-come first-served)
1856
+ if self.__action is None:
1857
+
1858
+ if HybridStateMachine.DEBUG:
1859
+ print(f"[DEBUG HSM] Considering the following list of actions: "
1860
+ f"{[a.__str__() for a in actions_list]}")
1861
+
1862
+ # Naive policy: take the first action that is ready
1863
+ _idx, _request = self.policy(actions_list)
1864
+
1865
+ if _idx < 0:
1866
+ if HybridStateMachine.DEBUG:
1867
+ print(f"[DEBUG HSM, {self.role}] Policy selected no actions")
1868
+
1869
+ # No actions were applied
1870
+ self.__cur_feasible_actions_status = None
1871
+ self.__state_changed = False
1872
+ return -1 # Early stop
1873
+ else:
1874
+ if HybridStateMachine.DEBUG:
1875
+ if _request is not None:
1876
+ print(f"[DEBUG HSM, {self.role}] Policy selected {actions_list[_idx].__str__()}"
1877
+ f" whose request is {_request.__str__()}")
1878
+ print(f"[DEBUG HSM, {self.role}] (request in to_str format: {_request.to_str()})")
1879
+ else:
1880
+ print(f"[DEBUG HSM, {self.role}] Policy selected {actions_list[_idx].__str__()}")
1881
+
1882
+ # Revisiting decisions due to the policy filter
1883
+ if self.policy_filter is not None:
1884
+ _idx_f, _request_f = self.policy_filter(_idx, _request, actions_list, self.policy_filter_opts)
1885
+
1886
+ if _idx_f != _idx or _request_f != _request:
1887
+ _idx = _idx_f
1888
+ _request = _request_f
1889
+ if _idx < 0:
1890
+ if HybridStateMachine.DEBUG:
1891
+ print(f"[DEBUG HSM, {self.role}] After the policy filter, policy selected no-actions")
1892
+ if _request is not None:
1893
+ print(f"[DEBUG HSM, {self.role}] Request is not None (unexpected) "
1894
+ f"{_request.__str__()}")
1895
+ print(f"[DEBUG HSM, {self.role}] (request in to_str format: {_request.to_str()})")
1896
+ else:
1897
+ print(f"[DEBUG HSM, {self.role}] Request is None (as expected)")
1898
+
1899
+ # No actions were applied
1900
+ self.__cur_feasible_actions_status = None
1901
+ self.__state_changed = False
1902
+ return -1 # Early stop
1903
+ else:
1904
+ if HybridStateMachine.DEBUG:
1905
+ if _request is not None:
1906
+ print(f"[DEBUG HSM, {self.role}] After the policy filter, policy selected "
1907
+ f"{actions_list[_idx].__str__()}"
1908
+ f" whose request is {_request.__str__()}")
1909
+ print(f"[DEBUG HSM, {self.role}] (request in to_str format: {_request.to_str()})")
1910
+ else:
1911
+ print(f"[DEBUG HSM, {self.role}] After the policy filter, policy selected "
1912
+ f"{actions_list[_idx].__str__()}")
1913
+ else:
1914
+ if HybridStateMachine.DEBUG:
1915
+ print(f"[DEBUG HSM, {self.role}] After the policy filter, the policy decision was not "
1916
+ f"changed")
1917
+ if _request is not None:
1918
+ print(f"[DEBUG HSM, {self.role}] In particular, after the policy filter, policy "
1919
+ f"selected "
1920
+ f"{actions_list[_idx].__str__()}"
1921
+ f" whose request is {_request.__str__()}")
1922
+ print(f"[DEBUG HSM, {self.role}] (request in to_str format: {_request.to_str()})")
1923
+ else:
1924
+ print(f"[DEBUG HSM, {self.role}] In particular, after the policy filter, policy "
1925
+ f"selected "
1926
+ f"{actions_list[_idx].__str__()}")
1927
+
1928
+ # Saving current action
1929
+ self.limbo_state = self.state
1930
+ self.state = None
1931
+ self.__action = actions_list[_idx]
1932
+ self.__action.reset_step() # Resetting
1933
+ self.__cur_feasible_actions_status['selected_idx'] = _idx
1934
+ self.__cur_feasible_actions_status['selected_request'] = _request
1935
+
1936
+ # References
1937
+ action = self.__action
1938
+ idx = self.__cur_feasible_actions_status['selected_idx']
1939
+ request = self.__cur_feasible_actions_status['selected_request']
1940
+
1941
+ # Call action
1942
+ action_call_returned_true = await action(request=request)
1943
+
1944
+ # Status can be one of these:
1945
+ # 0: action fully done;
1946
+ # 1: try again this action;
1947
+ # 2: move to next action.
1948
+ if action_call_returned_true:
1949
+ if not action.is_multi_steps():
1950
+
1951
+ # Single-step actions
1952
+ status = 0 # Done
1953
+ else:
1954
+
1955
+ # multistep actions
1956
+ if action.cannot_be_run_anymore(): # Timeout, max time reached, max steps reached
1957
+ if HybridStateMachine.DEBUG:
1958
+ print(f"[DEBUG HSM] multistep action {self.__action.name} returned True and "
1959
+ f"cannot-be-run-anymore "
1960
+ f"(step: {action.get_step()}, "
1961
+ f"has_completion_step: {action.has_completion_step()})")
1962
+ if self.__action.has_completion_step() and action.get_step() == 0:
1963
+ status = 1 # Try again (next step, it will trigger the completion step)
1964
+ else:
1965
+ if action.get_step() >= 0:
1966
+ status = 0 # Done, the action is fully completed
1967
+ else:
1968
+ status = 2 # Move to the next action (or to the next request of the same action)
1969
+ else:
1970
+ if HybridStateMachine.DEBUG:
1971
+ print(f"[DEBUG HSM] multistep action {self.__action.name} can still be run")
1972
+ status = 1 # Try again (next step)
1973
+ else:
1974
+ if not action.is_multi_steps():
1975
+
1976
+ # Single-step actions
1977
+ if not action.has_a_timeout() or action.is_timed_out():
1978
+ status = 2 # Move to the next action (or to the next request of the same action)
1979
+ else:
1980
+ status = 1 # Try again (one more time, until timeout is reached)
1981
+ else:
1982
+
1983
+ # multistep actions
1984
+ if action.cannot_be_run_anymore(): # Timeout, max time reached, max steps reached
1985
+ if HybridStateMachine.DEBUG:
1986
+ print(f"[DEBUG HSM] multistep action {self.__action.name} returned False and "
1987
+ f"cannot-be-run-anymore "
1988
+ f"(step: {action.get_step()}, "
1989
+ f"has_completion_step: {self.__action.has_completion_step()})")
1990
+ status = 2 # Move to the next action, since the final communication failed
1991
+ else:
1992
+ status = 1 # Try again (same step)
1993
+
1994
+ if HybridStateMachine.DEBUG:
1995
+ print(f"[DEBUG HSM] Action {self.__action.name}, after being called, leaded to status: {status}")
1996
+
1997
+ if action.msg is not None and self.show_action_completion:
1998
+ action.out_fcn(HybridStateMachine.ACTION_TICKS_PER_STATUS[status], None, None)
1999
+
2000
+ # Post-call operations
2001
+ if status == 0: # Done
2002
+
2003
+ # Clearing request
2004
+ if request is not None:
2005
+ self.__action.get_list_of_requests().remove(request)
2006
+
2007
+ # State transition
2008
+ self.prev_state = self.limbo_state
2009
+ self.state = to_state_list[idx]
2010
+ self.limbo_state = None
2011
+
2012
+ # Update status
2013
+ self.__state_changed = self.state != self.prev_state # Checking if we are on a self-loop or not
2014
+ self.__last_completed_action = self.__action # This will be set also if the state does not change
2015
+
2016
+ # If we moved to another state
2017
+ # (this is not true anymore: "clearing all the pending annotations for the next possible actions")
2018
+ if self.__state_changed:
2019
+ if HybridStateMachine.DEBUG:
2020
+ print(f"[DEBUG HSM] Moving to state: {self.state}")
2021
+ # for to_state, action_list in self.transitions[self.state].items():
2022
+ # for i, act in enumerate(action_list):
2023
+ # act.clear_requests()
2024
+
2025
+ # Propagating (trying to propagate forward the residual requests)
2026
+ list_of_residual_requests = self.__action.get_list_of_requests()
2027
+ propagated_requests = []
2028
+ for req in list_of_residual_requests:
2029
+ if self.request_action(req.requester, action_name=self.__action.name, args=req.args,
2030
+ from_state=None, to_state=None, timestamp=req.timestamp,
2031
+ uuid=req.uuid):
2032
+ propagated_requests.append(req)
2033
+ for req in propagated_requests:
2034
+ list_of_residual_requests.remove(req) # Clearing propagated requests
2035
+
2036
+ self.states[self.prev_state].reset() # Reset starting time (only if state changed!)
2037
+
2038
+ if HybridStateMachine.DEBUG:
2039
+ print(f"[DEBUG HSM] Correctly completed action: {self.__action.name}")
2040
+
2041
+ self.__action.reset_step()
2042
+ self.__action = None # Clearing
2043
+ self.__cur_feasible_actions_status = None
2044
+
2045
+ return 0 # Transition done, no need to check other actions!
2046
+
2047
+ elif status == 1: # Try again the same action (either a new step or an already done-and-failed one)
2048
+
2049
+ # Update status
2050
+ self.__state_changed = False
2051
+ if self.prev_state is not None:
2052
+ self.states[self.prev_state].reset() # Reset starting time
2053
+
2054
+ return 1 # Transition not-done: no need to check other actions, the current one will be run again
2055
+
2056
+ elif status == 2: # Move to the next action (or to the next request of the same action)
2057
+
2058
+ # Clearing request
2059
+ if request is not None:
2060
+ self.__action.requests.move_request_to_back(request) # Rotating to avoid starvation
2061
+ attempts_to_serve_a_request_list[idx] += 1
2062
+
2063
+ # Back to the original state
2064
+ self.state = self.limbo_state
2065
+ self.limbo_state = None
2066
+ if HybridStateMachine.DEBUG:
2067
+ print(f"[DEBUG HSM] Tried and failed (failed execution): {action.name}")
2068
+
2069
+ # Purging action from the current list
2070
+ if request is None or attempts_to_serve_a_request_list[idx] >= len(self.__action.requests):
2071
+ del actions_list[idx]
2072
+ del to_state_list[idx]
2073
+
2074
+ # Update status
2075
+ self.__state_changed = False
2076
+ self.__action.reset_step()
2077
+ self.__action = None # Clearing
2078
+
2079
+ continue # Move to the next action
2080
+ else:
2081
+ raise ValueError("Unexpected status: " + str(status))
2082
+
2083
+ # No actions were applied
2084
+ self.__cur_feasible_actions_status = None
2085
+ self.__state_changed = False
2086
+ return -1
2087
+
2088
+ async def act(self):
2089
+ """A high-level method that combines `act_states` and `act_transitions` to run the state machine. It repeatedly
2090
+ processes states and transitions until a blocking state is reached or all feasible actions have been tried,
2091
+ thus ensuring a complete processing cycle in one call (async).
2092
+ """
2093
+
2094
+ # It keeps processing states and actions, until all the current feasible actions fail
2095
+ # (also when a step of a multistep action is executed) or a blocking state is reached
2096
+ while True:
2097
+ if self.welcome_msg is not None and self.state is not None and self.state == self.initial_state:
2098
+ self.state_out_fcn(self.welcome_msg)
2099
+ self.set_welcome_message(None)
2100
+
2101
+ await self.act_states()
2102
+ ret = await self.act_transitions(self.must_wait())
2103
+ if ret != 0 or (self.state is not None and self.states[self.state].blocking):
2104
+ break
2105
+
2106
+ def get_state_changed(self):
2107
+ """Returns an internal flag that indicates if a state transition has occurred in the last execution cycle.
2108
+ This can be used by an external loop to know when to re-evaluate the state machine's context.
2109
+
2110
+ Returns:
2111
+ True if the state has changed, False otherwise.
2112
+ """
2113
+ return self.__state_changed
2114
+
2115
+ def request_action(self, signature: object, action_name: str, args: dict | None = None,
2116
+ from_state: str | None = None, to_state: str | None = None,
2117
+ timestamp: float | None = None, uuid: str | None = None):
2118
+ """Allows an external entity to request a specific action. The request is validated by a signature checker
2119
+ (if one exists) and then queued on the corresponding action. This method enables dynamic, external triggers for
2120
+ state machine transitions.
2121
+
2122
+ Args:
2123
+ signature: An object used for validating the request's origin.
2124
+ action_name: The name of the requested action.
2125
+ args: Arguments for the requested action.
2126
+ from_state: The optional starting state for the requested transition.
2127
+ to_state: The optional destination state for the requested transition.
2128
+ timestamp: The time the request was made.
2129
+ uuid: A unique identifier for the request.
2130
+
2131
+ Returns:
2132
+ True if the request was accepted and queued, False otherwise.
2133
+ """
2134
+ if HybridStateMachine.DEBUG:
2135
+ print(f"[DEBUG HSM] Received a request signed as {signature}, "
2136
+ f"asking for action {action_name}, with args: {args}, "
2137
+ f"from_state: {from_state}, to_state: {to_state}, uuid: {uuid}")
2138
+
2139
+ # Discard suggestions if they are not trusted
2140
+ if self.request_signature_checker is not None and not self.request_signature_checker(signature):
2141
+ if HybridStateMachine.DEBUG:
2142
+ print("[DEBUG HSM] Request signature check failed")
2143
+ return False
2144
+
2145
+ # If state is not provided, the current state is assumed
2146
+ if from_state is None:
2147
+ # If the request arrives in the middle of a multistep action, we need to check limbo state
2148
+ from_state = self.state if self.state is not None else self.limbo_state
2149
+ if from_state not in self.transitions:
2150
+ if HybridStateMachine.DEBUG:
2151
+ print(f"[DEBUG HSM] Request not accepted: not valid source state ({from_state})")
2152
+ return False
2153
+
2154
+ # If the destination state is not provided, all the possible destination from the current state are considered
2155
+ if to_state is not None and to_state not in self.transitions[from_state]:
2156
+ if HybridStateMachine.DEBUG:
2157
+ print(f"[DEBUG HSM] Request not accepted: not valid destination state ({to_state})")
2158
+ return False
2159
+ to_states = self.transitions[from_state].keys() if to_state is None else [to_state]
2160
+
2161
+ for to_state in to_states:
2162
+ action_list = self.transitions[from_state][to_state]
2163
+ for i, action in enumerate(action_list):
2164
+ if HybridStateMachine.DEBUG:
2165
+ print(f"[DEBUG HSM] Comparing with action: {str(action)}")
2166
+ if action.same_as(name=action_name, args=args):
2167
+ if HybridStateMachine.DEBUG:
2168
+ print(f"[DEBUG HSM] Requested action found in state {from_state}, adding request to the queue")
2169
+
2170
+ # Action found, let's save the suggestion
2171
+ action.add_request(signature, args, timestamp=timestamp, uuid=uuid)
2172
+ return True
2173
+
2174
+ # If the action was not found
2175
+ if HybridStateMachine.DEBUG:
2176
+ print("[DEBUG HSM] Requested action not found")
2177
+ return False
2178
+
2179
+ def wait_for_all_actions_that_start_with(self, prefix):
2180
+ """Sets the `ready` flag to `False` for all actions whose name begins with a given prefix. This method is used
2181
+ to programmatically disable a group of actions, effectively pausing them.
2182
+
2183
+ Args:
2184
+ prefix: The string prefix to match against action names.
2185
+ """
2186
+ for state, to_states in self.transitions.items():
2187
+ for to_state, action_list in to_states.items():
2188
+ for i, action in enumerate(action_list):
2189
+ if action.name.startswith(prefix):
2190
+ action.set_as_not_ready()
2191
+
2192
+ def wait_for_all_actions_that_include_an_arg(self, arg_name):
2193
+ """Sets the `ready` flag to `False` for all actions that include a specific argument name in their signature.
2194
+ This provides another way to programmatically disable actions.
2195
+
2196
+ Args:
2197
+ arg_name: The name of the argument to look for.
2198
+ """
2199
+ for state, to_states in self.transitions.items():
2200
+ for to_state, action_list in to_states.items():
2201
+ for i, action in enumerate(action_list):
2202
+ if arg_name in action.args:
2203
+ action.set_as_not_ready()
2204
+
2205
+ def wait_for_actions(self, from_state: str, to_state: str, wait: bool = True):
2206
+ """Sets the `ready` flag for a specific action (or group of actions) between two states. This allows for
2207
+ fine-grained control over which transitions are active.
2208
+
2209
+ Args:
2210
+ from_state: The name of the starting state.
2211
+ to_state: The name of the destination state.
2212
+ wait: A boolean flag to either set the action as not ready (`True`) or ready (`False`).
2213
+
2214
+ Returns:
2215
+ True if the specified action was found, False otherwise.
2216
+ """
2217
+ if from_state not in self.transitions or to_state not in self.transitions[from_state]:
2218
+ return False
2219
+
2220
+ for action in self.transitions[from_state][to_state]:
2221
+ if wait:
2222
+ action.set_as_not_ready()
2223
+ else:
2224
+ action.set_as_ready()
2225
+ return True
2226
+
2227
+ def save(self, filename: str, only_if_changed: object | None = None):
2228
+ """Saves the state machine's current configuration to a JSON file. It can optionally check if the configuration
2229
+ has changed before saving to avoid redundant file writes.
2230
+
2231
+ Args:
2232
+ filename: The path to the file to save to.
2233
+ only_if_changed: An optional object to compare against for changes. If a change is not detected, the file
2234
+ is not written.
2235
+
2236
+ Returns:
2237
+ True if the file was written, False otherwise.
2238
+ """
2239
+ if only_if_changed is not None and os.path.exists(filename):
2240
+ try:
2241
+ existing = HybridStateMachine(actionable=only_if_changed).load(filename)
2242
+ if str(existing) == str(self):
2243
+ return False
2244
+ except Exception: # If load fails, we assume it changed
2245
+ if HybridStateMachine.DEBUG:
2246
+ print(f"[DEBUG HSM] Error while reloading the exising machine from {filename}, assuming it changed")
2247
+
2248
+ with open(filename, 'w', encoding='utf-8') as file:
2249
+ file.write(str(self))
2250
+ return True
2251
+
2252
+ def load(self, filename_or_hsm_as_string: str | io.TextIOWrapper):
2253
+ """Loads a state machine's configuration from a JSON file or a JSON string. It reconstructs the states,
2254
+ actions, and transitions from the serialized data. This method is critical for persistence and for loading
2255
+ pre-defined state machine models.
2256
+
2257
+ Args:
2258
+ filename_or_hsm_as_string: The path to the JSON file or a JSON string representation of the state machine.
2259
+
2260
+ Returns:
2261
+ The loaded `HybridStateMachine` object (self).
2262
+ """
2263
+
2264
+ # Loading the whole file
2265
+ if (isinstance(filename_or_hsm_as_string, importlib.resources.abc.Traversable) or
2266
+ isinstance(filename_or_hsm_as_string, io.TextIOWrapper)):
2267
+
2268
+ # Safe way to load when this file is packed in a pip package
2269
+ hsm_data = json.load(filename_or_hsm_as_string)
2270
+ else:
2271
+
2272
+ # Ordinary case
2273
+ if os.path.exists(filename_or_hsm_as_string) and os.path.isfile(filename_or_hsm_as_string):
2274
+ with open(filename_or_hsm_as_string, 'r', encoding="utf-8") as file:
2275
+ hsm_data = json.load(file)
2276
+ else:
2277
+
2278
+ # Assuming it is a string
2279
+ hsm_data = json.loads(filename_or_hsm_as_string)
2280
+
2281
+ # Getting state info
2282
+ self.initial_state = hsm_data['initial_state']
2283
+ self.state = hsm_data['state']
2284
+ self.prev_state = hsm_data['prev_state']
2285
+ self.limbo_state = hsm_data['limbo_state']
2286
+ self.set_role(hsm_data.get('role', None))
2287
+ self.set_welcome_message(hsm_data.get('welcome_msg', None))
2288
+ self.show_blocking_states = hsm_data.get('highlight_blocking_states_in_messages', False)
2289
+ self.show_action_completion = hsm_data.get('show_action_ticks_after_messages', False)
2290
+ self.show_action_request_info = hsm_data.get('show_action_request_after_messages', False)
2291
+
2292
+ # Getting states
2293
+ self.states = {}
2294
+ if 'state_actions' in hsm_data:
2295
+ for state, state_action_list in hsm_data['state_actions'].items():
2296
+ if len(state_action_list) == 3: # Backward compatibility
2297
+ act_name, act_args, state_id = state_action_list
2298
+ waiting_time = 0.
2299
+ blocking = True
2300
+ msg = None
2301
+ elif len(state_action_list) == 4: # Backward compatibility
2302
+ act_name, act_args, state_id, waiting_time = state_action_list
2303
+ blocking = True
2304
+ msg = None
2305
+ elif len(state_action_list) == 5: # Backward compatibility
2306
+ act_name, act_args, state_id, blocking, waiting_time = state_action_list
2307
+ msg = None
2308
+ else:
2309
+ act_name, act_args, state_id, blocking, waiting_time, msg = state_action_list
2310
+
2311
+ # Recall that state_id can be set to -1 in the original file, meaning "automatically set the state_id"
2312
+ self.add_state(state, action=act_name, args=act_args,
2313
+ state_id=state_id if state_id >= 0 else None,
2314
+ waiting_time=waiting_time, blocking=blocking, msg=msg)
2315
+
2316
+ # Getting transitions
2317
+ self.transitions = {}
2318
+ for from_state, to_states in hsm_data['transitions'].items():
2319
+ for to_state, action_list in to_states.items():
2320
+ for action_list_tuple in action_list:
2321
+ if len(action_list_tuple) == 4:
2322
+ act_name, act_args, act_ready, act_id = action_list_tuple
2323
+ msg = None
2324
+ else:
2325
+ act_name, act_args, act_ready, act_id, msg = action_list_tuple
2326
+
2327
+ # Recall that act_id can be set to -1 in the original file, meaning "automatically set the act_id"
2328
+ self.add_transit(from_state, to_state,
2329
+ action=act_name, args=act_args, ready=act_ready,
2330
+ act_id=act_id if act_id >= 0 else None, msg=msg,
2331
+ avoid_changing_ready=True)
2332
+
2333
+ return self
2334
+
2335
+ def to_graphviz(self):
2336
+ """Generates a Graphviz `Digraph` object representing the state machine's structure. This method visualizes
2337
+ states as nodes and transitions as edges. It includes details such as node shapes (diamond for initial state,
2338
+ oval for others), styles (filled for blocking states), and labels for both states and transitions. The labels
2339
+ for actions include their names and arguments, formatted to wrap lines for readability.
2340
+
2341
+ Returns:
2342
+ A `graphviz.Digraph` object ready for rendering.
2343
+ """
2344
+ graph = graphviz.Digraph()
2345
+ graph.attr('node', fontsize='8')
2346
+ for state, state_obj in self.states.items():
2347
+ action = state_obj.action
2348
+ if action is not None:
2349
+ s = "("
2350
+ for i, (k, v) in enumerate(action.args.items()):
2351
+ s += str(k) + "=" + (str(v) if not isinstance(v, str) else ("'" + v + "'"))
2352
+ if i < len(action.args) - 1:
2353
+ s += ", "
2354
+ s += ")"
2355
+ label = action.name + s
2356
+ if len(label) > 40:
2357
+ tokens = label.split(" ")
2358
+ z = ""
2359
+ i = 0
2360
+ done = False
2361
+ while i < len(tokens):
2362
+ z += (" " if i > 0 else "") + tokens[i]
2363
+ if not done and i < (len(tokens) - 1) and len(z + tokens[i + 1]) > 40:
2364
+ z += "\n "
2365
+ done = True
2366
+ i += 1
2367
+ label = z
2368
+ suffix = "\n" + label
2369
+ else:
2370
+ suffix = ""
2371
+ if state == self.initial_state:
2372
+ graph.attr('node', shape='diamond')
2373
+ else:
2374
+ graph.attr('node', shape='oval')
2375
+ if self.states[state].blocking:
2376
+ graph.attr('node', style='filled')
2377
+ else:
2378
+ graph.attr('node', style='solid')
2379
+ graph.node(state, state + suffix, _attributes={'id': "node" + str(state_obj.id)})
2380
+
2381
+ for from_state, to_states in self.transitions.items():
2382
+ for to_state, action_list in to_states.items():
2383
+ for action in action_list:
2384
+ s = "("
2385
+ for i, (k, v) in enumerate(action.args.items()):
2386
+ s += str(k) + "=" + (str(v) if not isinstance(v, str) else ("'" + v + "'"))
2387
+ if i < len(action.args) - 1:
2388
+ s += ", "
2389
+ s += ")"
2390
+ label = action.name + s
2391
+ if len(label) > 40:
2392
+ tokens = label.split(" ")
2393
+ z = ""
2394
+ i = 0
2395
+ done = False
2396
+ while i < len(tokens):
2397
+ z += (" " if i > 0 else "") + tokens[i]
2398
+ if not done and i < (len(tokens) - 1) and len(z + tokens[i + 1]) > 40:
2399
+ z += "\n"
2400
+ done = True
2401
+ i += 1
2402
+ label = z
2403
+ graph.edge(from_state, to_state, label=" " + label + " ", fontsize='8',
2404
+ style='dashed' if not action.is_ready() else 'solid',
2405
+ _attributes={'id': "edge" + str(action.id)})
2406
+ return graph
2407
+
2408
+ def save_pdf(self, filename: str):
2409
+ """Saves the state machine's Graphviz representation as a PDF file. It calls `to_graphviz()` to create the
2410
+ graph and then uses the Graphviz library's `render` method to generate the PDF.
2411
+
2412
+ Args:
2413
+ filename: The path and name of the PDF file to save.
2414
+
2415
+ Returns:
2416
+ True if the file was successfully saved, False otherwise.
2417
+ """
2418
+ if filename.lower().endswith(".pdf"):
2419
+ filename = filename[0:-4]
2420
+
2421
+ try:
2422
+ self.to_graphviz().render(filename, format='pdf', cleanup=True)
2423
+ return True
2424
+ except Exception as e:
2425
+ if HybridStateMachine.DEBUG:
2426
+ print(f"[DEBUG HSM] Error while saving to PDF {e}")
2427
+ return False
2428
+
2429
+ def print_actions(self, state: str | None = None):
2430
+ """Prints a list of all transitions and their associated actions from a given state. If no state is provided,
2431
+ it defaults to the current state. This method is useful for quickly inspecting the available transitions from
2432
+ a specific point in the state machine's flow.
2433
+
2434
+ Args:
2435
+ state: The name of the state from which to print actions. Defaults to the current state.
2436
+ """
2437
+ state = (self.state if self.state is not None else self.limbo_state) if state is None else state
2438
+ for to_state, action_list in self.transitions[state].items():
2439
+ if action_list is None or len(action_list) == 0:
2440
+ print(f"{state}, no actions")
2441
+ for action in action_list:
2442
+ print(f"{state} --> {to_state} {action}")
2443
+
2444
+ # Noinspection PyMethodMayBeStatic
2445
+ def __policy_first_requested_or_first_ready(self, actions_list: list[Action]) -> tuple[int, ActionRequest | None]:
2446
+ """This is the default policy for selecting which action to execute from a list of feasible actions.
2447
+ It prioritizes actions that have been explicitly requested (i.e., have pending requests) on a first-come,
2448
+ first-served basis. If no requested actions are found, it then selects the first action in the list that is
2449
+ marked as `ready`.
2450
+
2451
+ Args:
2452
+ actions_list: A list of `Action` objects that are candidates for execution.
2453
+
2454
+ Returns:
2455
+ The index of the selected action and the ActionRequest object with the requester details (object,
2456
+ arguments, time, and UUID), or -1 and the None if no action is selected.
2457
+ """
2458
+ for i, action in enumerate(actions_list):
2459
+ _list_of_requests = action.get_list_of_requests()
2460
+ if len(_list_of_requests) > 0:
2461
+ _selected_action_idx = i
2462
+ _selected_request = _list_of_requests.get_oldest_request()
2463
+ return _selected_action_idx, _selected_request
2464
+ for i, action in enumerate(actions_list):
2465
+ if action.is_ready(consider_requests=False):
2466
+ _selected_action_idx = i
2467
+ _selected_request = None
2468
+ return _selected_action_idx, _selected_request
2469
+ _selected_action_idx = -1
2470
+ _selected_request = None
2471
+ return _selected_action_idx, _selected_request