Skip to content

Commit f9715cd

Browse files
authored
Add on_pop method (#110)
2 parents e120407 + c859872 commit f9715cd

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

dynamic_stack_decider/dynamic_stack_decider/abstract_stack_element.py

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ def pop(self):
5252
"""
5353
self._dsd.pop()
5454

55+
def on_pop(self): # noqa
56+
"""
57+
This method is called when the element is popped from the stack.
58+
It can be used to clean up resources, cancel actions or similar tasks.
59+
Overload this method if you need to do something when the element is popped.
60+
"""
61+
pass
62+
5563
@abstractmethod
5664
def perform(self, reevaluate=False):
5765
"""

dynamic_stack_decider/dynamic_stack_decider/dsd.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ def update(self, reevaluate: bool = True):
259259
elif result != self.stack[self.stack_exec_index + 1][0].activation_reason:
260260
# In this case, however, the activation reason actually did change. Therefore, we have to
261261
# discard everything in the stack above the current decision and push the new result.
262-
self.stack = self.stack[0 : self.stack_exec_index + 1]
262+
for _ in range(self.stack_exec_index + 1, len(self.stack)):
263+
self.stack.pop()[1].on_pop()
263264
self.stack_reevaluate = False
264265
self.push(tree_element.get_child(result))
265266

@@ -309,8 +310,9 @@ def pop(self):
309310
if self.stack_reevaluate:
310311
# we are currently reevaluating. we shorten the stack here
311312
if self.stack_exec_index > 0:
312-
# only shorten stack if it still has one element
313-
self.stack = self.stack[0 : self.stack_exec_index]
313+
## only shorten stack if it still has one element
314+
for _ in range(self.stack_exec_index, len(self.stack)):
315+
self.stack.pop()[1].on_pop()
314316
# stop reevaluating
315317
self.stack_reevaluate = False
316318
else:
@@ -321,10 +323,10 @@ def pop(self):
321323
# only a single element of the sequence
322324
# We also do not want to reset do_not_reevaluate because an action in the sequence
323325
# may control the stack beyond its own lifetime but in the sequence element's lifetime
324-
self.stack[-1][1].pop_one()
326+
self.stack[-1][1].pop_one().on_pop()
325327
return
326328
# Remove the last element of the stack
327-
self.stack.pop()
329+
self.stack.pop()[1].on_pop()
328330

329331
# We will reevaluate even when the popped element set do_not_reevaluate
330332
# because no module should control the stack beyond its lifetime

dynamic_stack_decider/dynamic_stack_decider/sequence_element.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,22 @@ def pop_one(self):
6363
assert not self.in_last_element(), (
6464
"It is not possible to pop a single element when" "the last element of the sequence is active"
6565
)
66+
# Save the current action to return it
67+
popped_action = self.current_action
6668
# Increment the index to the next action and initialize it
6769
self.current_action_index += 1
68-
# We initilize the current action here to avoid the problem described in
70+
# We initialize the current action here to avoid the problem described in
6971
# https://github.com/bit-bots/dynamic_stack_decider/issues/107
7072
self.current_action = self._init_function(self.actions[self.current_action_index])
73+
# Return the popped action
74+
return popped_action
75+
76+
def on_pop(self):
77+
"""
78+
This method is called when the sequence is popped from the stack.
79+
This means that the last element of the sequence was also popped, so
80+
"""
81+
self.current_action.on_pop()
7182

7283
def in_last_element(self):
7384
"""Returns if the current element is the last element of the sequence"""

0 commit comments

Comments
 (0)