Skip to content

Commit 94afed8

Browse files
authored
Extend ComparisonExpression so we can get the value from a callable (#468)
1 parent 7bbb993 commit 94afed8

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

py_trees/behaviours.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,10 @@ def update(self) -> common.Status:
570570
"""
571571
self.logger.debug("%s.update()" % self.__class__.__name__)
572572
try:
573-
value = self.blackboard.get(self.key)
573+
lhs_value = self.blackboard.get(self.key)
574574
if self.key_attributes:
575575
try:
576-
value = operator.attrgetter(self.key_attributes)(value)
576+
lhs_value = operator.attrgetter(self.key_attributes)(lhs_value)
577577
except AttributeError:
578578
self.feedback_message = (
579579
"blackboard key-value pair exists, but the value does not "
@@ -588,20 +588,21 @@ def update(self) -> common.Status:
588588
)
589589
return common.Status.FAILURE
590590

591-
success = self.check.operator(value, self.check.value)
591+
rhs_value = self.check.value_generator()
592+
success = self.check.operator(lhs_value, rhs_value)
592593

593594
if success:
594595
self.feedback_message = "'%s' comparison succeeded [v: %s][e: %s]" % (
595596
self.check.variable,
596-
value,
597-
self.check.value,
597+
lhs_value,
598+
rhs_value,
598599
)
599600
return common.Status.SUCCESS
600601
else:
601602
self.feedback_message = "'%s' comparison failed [v: %s][e: %s]" % (
602603
self.check.variable,
603-
value,
604-
self.check.value,
604+
lhs_value,
605+
rhs_value,
605606
)
606607
return common.Status.FAILURE
607608

@@ -722,7 +723,7 @@ def update(self) -> common.Status:
722723
)
723724
)
724725
return common.Status.FAILURE
725-
results.append(check.operator(value, check.value))
726+
results.append(check.operator(value, check.value_generator()))
726727
if self.blackboard_results is not None:
727728
for counter in range(1, len(results) + 1):
728729
self.blackboard_results.set(str(counter), results[counter - 1])

py_trees/common.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class ComparisonExpression(object):
195195
196196
Args:
197197
variable: name of the variable to compare
198-
value: value to compare against
198+
value: value or callable to compare against
199199
operator: a callable comparison operator
200200
201201
.. tip::
@@ -206,7 +206,7 @@ class ComparisonExpression(object):
206206
207207
check = ComparisonExpression(
208208
variable="foo",
209-
value= 5,
209+
value=5,
210210
operator=operator.eq
211211
)
212212
success = check.operator(blackboard[check.variable], check.value)
@@ -215,18 +215,18 @@ class ComparisonExpression(object):
215215
def __init__(
216216
self,
217217
variable: str,
218-
value: ComparisonV,
218+
value: typing.Union[ComparisonV, typing.Callable[[], ComparisonV]],
219219
operator: typing.Callable[[ComparisonV, ComparisonV], bool],
220220
):
221221
"""Initialise variables.
222222
223223
Args:
224224
variable: the variable name to use
225-
value: the value to use on the RHS of the expression
225+
value: the value to use on the RHS of the expression (can also be a callable that returns the value)
226226
operator: a logical operator to enable comparisons
227227
"""
228228
self.variable = variable
229-
self.value = value
229+
self.value_generator = value if callable(value) else lambda: value
230230
self.operator = operator
231231

232232

0 commit comments

Comments
 (0)