Skip to content

Commit

Permalink
[Action Designator] Changed pre and post perform to decorator hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigul committed Jan 23, 2025
1 parent f173104 commit c157804
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/pycram/designator.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class ActionDesignatorDescription(DesignatorDescription, Language):
Reference to the performable class that is used to execute the action.
"""



@dataclass
class Action:
"""
Expand All @@ -175,6 +177,9 @@ class Action:
"""
The type of the robot at the start of the action.
"""
_pre_perform_callbacks = []

_post_perform_callbacks = []

def __post_init__(self):
self.robot_position = World.robot.get_pose()
Expand All @@ -190,9 +195,11 @@ def perform(self) -> Any:
:return: The result of the action in the plan
"""
self.pre_perform()
for pre_perform in ActionDesignatorDescription.Action._pre_perform_callbacks:
pre_perform(self)
result = self.plan()
self.post_perform()
for post_perform in ActionDesignatorDescription.Action._post_perform_callbacks:
post_perform(self)
return result

@with_tree
Expand All @@ -204,18 +211,6 @@ def plan(self) -> Any:
"""
raise NotImplementedError()

def pre_perform(self):
"""
This method is called before the perform method is executed. To be overridden by subclasses.
"""
pass

def post_perform(self):
"""
This method is called after the perform method is executed. To be overridden by subclasses.
"""
pass

def to_sql(self) -> ORMAction:
"""
Create an ORM object that corresponds to this description.
Expand Down Expand Up @@ -262,6 +257,24 @@ def get_type_hints(cls) -> Dict[str, Any]:
"""
return get_type_hints(cls)

@classmethod
def pre_perform(cls, func):
cls._pre_perform_callbacks.append(func)

def wrapper(*args, **kwargs):
func(*args, **kwargs)

return wrapper

@classmethod
def post_perform(cls, func):
cls._post_perform_callbacks.append(func)

def wrapper(*args, **kwargs):
func(*args, **kwargs)

return wrapper

def __init__(self):
"""
Base of all action designator_description descriptions.
Expand Down Expand Up @@ -566,3 +579,5 @@ def __post_init__(self):
# if missing != [] or wrong_type != {}:
# raise ResolutionError(missing, wrong_type, current_type, self.__class__)
#


0 comments on commit c157804

Please sign in to comment.