@@ -108,6 +108,7 @@ class DSD:
108
108
stack_reevaluate = False
109
109
do_not_reevaluate = False
110
110
old_representation = ""
111
+ debug_active_action_cache : Optional [str ] = None
111
112
112
113
def __init__ (self , blackboard , debug_topic : str = None , node : Optional [Node ] = None ):
113
114
"""
@@ -142,6 +143,10 @@ def __init__(self, blackboard, debug_topic: str = None, node: Optional[Node] = N
142
143
debug_stack_topic = f"{ debug_topic } /dsd_stack"
143
144
self .debug_stack_publisher = node .create_publisher (String , debug_stack_topic , 10 )
144
145
get_logger ().debug (f"Debugging stack on '{ debug_stack_topic } '" )
146
+ # Publish the currently active action
147
+ debug_current_action_topic = f"{ debug_topic } /dsd_current_action"
148
+ self .debug_current_action_publisher = node .create_publisher (String , debug_current_action_topic , 10 )
149
+ get_logger ().debug (f"Debugging current action on '{ debug_current_action_topic } '" )
145
150
146
151
def register_actions (self , module_path ):
147
152
"""
@@ -196,7 +201,7 @@ def _bind_modules(self, element):
196
201
else :
197
202
raise ValueError (f'Unknown parser tree element type "{ type (element )} " for element "{ element } "!' )
198
203
199
- def _init_element (self , element ):
204
+ def _init_element (self , element : AbstractTreeElement ):
200
205
"""Initializes the module belonging to the given element."""
201
206
if isinstance (element , SequenceTreeElement ):
202
207
initialized_actions = list ()
@@ -206,7 +211,7 @@ def _init_element(self, element):
206
211
else :
207
212
return element .module (self .blackboard , self , element .parameters )
208
213
209
- def set_start_element (self , start_element ):
214
+ def set_start_element (self , start_element : AbstractTreeElement ):
210
215
"""
211
216
This method defines the start element on the stack, which stays always on the bottom of the stack.
212
217
It should be called in __init__.
@@ -236,6 +241,7 @@ def update(self, reevaluate: bool = True):
236
241
"""
237
242
try :
238
243
self .debug_publish_stack ()
244
+ self .debug_publish_current_action ()
239
245
240
246
if reevaluate and not self .do_not_reevaluate :
241
247
self .stack_exec_index = 0
@@ -362,3 +368,28 @@ def debug_publish_tree(self):
362
368
data = self .tree .repr_dict ()
363
369
msg = String (data = json .dumps (data ))
364
370
self .debug_tree_publisher .publish (msg )
371
+
372
+ def debug_publish_current_action (self ):
373
+ """
374
+ Publishes the name of the currently active action
375
+ """
376
+ # Check if debugging is active and if there is something on the stack
377
+ if not self .debug_active or len (self .stack ) == 0 :
378
+ return
379
+
380
+ # Get the top element
381
+ stack_top = self .stack [- 1 ][1 ]
382
+ # Check if it is an action or a sequence element and retrieve the current action
383
+ if isinstance (stack_top , AbstractActionElement ):
384
+ current_action = stack_top
385
+ elif isinstance (stack_top , SequenceElement ):
386
+ current_action = stack_top .current_action
387
+ else :
388
+ return
389
+
390
+ # Only publish if the action changed
391
+ if current_action .name != self .debug_active_action_cache :
392
+ # Publish the name of the current action
393
+ self .debug_current_action_publisher .publish (String (data = current_action .name ))
394
+ # Cache the current action name
395
+ self .debug_active_action_cache = current_action .name
0 commit comments