From dd5763bcd35388ac21469ac272ce60168939eba7 Mon Sep 17 00:00:00 2001 From: Felix Kolbe Date: Wed, 7 Aug 2013 17:52:03 +0200 Subject: [PATCH] Clean __str__ and __repr__ methods --- goap/src/goap/common_ros.py | 2 +- goap/src/goap/goap.py | 31 ++++++++++++++++++++++++++++--- goap/src/goap/inheriting.py | 20 +++++++++++++++----- goap/src/goap/planning.py | 6 ++++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/goap/src/goap/common_ros.py b/goap/src/goap/common_ros.py index 156ba55..6d51247 100644 --- a/goap/src/goap/common_ros.py +++ b/goap/src/goap/common_ros.py @@ -59,7 +59,7 @@ def __init__(self, state_name, topic, topic_class, field): self._value = None def __repr__(self): - return '' % (self._topic, self._field) + return '<%s topic=%s field=%s>' % (self.__class__.__name__, self._topic, self._field) def _callback(self, msg): self._value = self._msgeval(msg) diff --git a/goap/src/goap/goap.py b/goap/src/goap/goap.py index aee6d98..b2003d5 100644 --- a/goap/src/goap/goap.py +++ b/goap/src/goap/goap.py @@ -43,6 +43,9 @@ def __init__(self, worldstate=None): if worldstate is not None: self._condition_values.update(worldstate._condition_values) + def __str__(self): + return '%s %s' % (self.__class__.__name__, self._condition_values) + def __repr__(self): return '' % (id(self), self._condition_values) @@ -95,7 +98,10 @@ def __init__(self, state_name): self._state_name = state_name def __str__(self): - return "Condition:%s" % self._state_name + return '%s:%s' % (self.__class__.__name__, self._state_name) + + def __repr__(self): + return '<%s name=%s>' % (self.__class__.__name__, self._state_name) def get_value(self): """Returns the current value, hopefully not blocking.""" @@ -139,8 +145,11 @@ def __init__(self, condition, value, deviation=None): self._value = value self._deviation = deviation + def __str__(self): + return '%s:%s=%s~%s' % (self.__class__.__name__, self._condition._state_name, self._value, self._deviation) + def __repr__(self): - return '' % (self._condition, self._value, self._deviation) + return '<%s cond=%s value=%r dev=%s>' % (self.__class__.__name__, self._condition, self._value, self._deviation) def is_valid(self, worldstate): cond_value = worldstate.get_condition_value(self._condition) @@ -163,6 +172,12 @@ def __init__(self, condition, new_value): self._condition = condition self._new_value = new_value + def __str__(self): + return '%s:%s=%s' % (self.__class__.__name__, self._condition._state_name, self._new_value) + + def __repr__(self): + return '<%s cond=%s new_val=%s>' % (self.__class__.__name__, self._condition, self._new_value) + def apply_to(self, worldstate): # TODO: remove me as I'm only for forward planning? worldstate.set_condition_value(self._condition, self._new_value) @@ -178,6 +193,12 @@ def __init__(self, condition): # Effect.__init__(self, condition, None) self._condition = condition + def __str__(self): + return '%s:%s' % (self.__class__.__name__, self._condition._state_name) + + def __repr__(self): + return '<%s cond=%s>' % (self.__class__.__name__, self._condition) + # def apply_to(self, worldstate): # worldstate.memory.set_value(self._condition, self._new_value) @@ -214,8 +235,12 @@ def __init__(self, preconditions, effects): self._preconditions = preconditions self._effects = effects + + def __str__(self): + return self.__class__.__name__ + def __repr__(self): - return '' % self.__class__.__name__ + return '<%s preconditions=%s effects=%s>' % (self.__class__.__name__, self._preconditions, self._effects) def cost(self): return 1 diff --git a/goap/src/goap/inheriting.py b/goap/src/goap/inheriting.py index 80a24c6..2b9d4d1 100644 --- a/goap/src/goap/inheriting.py +++ b/goap/src/goap/inheriting.py @@ -80,6 +80,9 @@ def __init__(self, memory, variable, new_value, preconditions, effects): self._state_name = 'memory.' + self._variable self._memory.declare_variable(self._variable) + def __repr__(self): + return '<%s var=%s new=%s>' % (self.__class__.__name__, self._variable, self._new_value) + def run(self, next_worldstate): self._memory.set_value(self._variable, self._new_value) @@ -93,8 +96,13 @@ def __init__(self, memory, variable, old_value, new_value): ) self._old_value = old_value + def __str__(self): + return '%s:%s=%s->%s' % (self.__class__.__name__, + self._variable, self._old_value, self._new_value) + def __repr__(self): - return '' % (self._variable, self._old_value, self._new_value) + return '<%s var=%s old=%s new=%s>' % (self.__class__.__name__, + self._variable, self._old_value, self._new_value) class MemoryIncrementerAction(Action): @@ -114,8 +122,13 @@ def __init__(self, memory, variable, increment=1): self._increment = increment self._memory.declare_variable(self._variable) + def __str__(self): + return '%s:%s+=%s' % (self.__class__.__name__, + self._variable, self._increment) + def __repr__(self): - return '' % (self._variable, self._increment) + return '<%s var=%s incr=%s>' % (self.__class__.__name__, + self._variable, self._increment) def cost(self): return abs(self._increment) @@ -143,9 +156,6 @@ def __init__(self, memory, variable): self._variable = variable memory.declare_variable(self._state_name) - def __repr__(self): - return '' % self._variable - def get_value(self): return self._memory.get_value(self._state_name) diff --git a/goap/src/goap/planning.py b/goap/src/goap/planning.py index dbbcd73..ca5dcfe 100644 --- a/goap/src/goap/planning.py +++ b/goap/src/goap/planning.py @@ -54,8 +54,10 @@ def __init__(self, worldstate, action, parent_nodes_path_list, parent_actions_pa self.parent_actions_path_list = parent_actions_path_list def __repr__(self): - return '' % \ - (id(self), self.cost(), self.action, self.worldstate) +# return '' % \ +# (id(self), self.cost(), self.action, self.worldstate) + return '' % \ + (id(self), self.cost(), self.action) def cost(self): return len(self.parent_nodes_path_list) + \