Skip to content

Commit cfaa64c

Browse files
authored
[blackboard] unregister keys (splintered-reality#241)
1 parent 7439e78 commit cfaa64c

File tree

3 files changed

+75
-19
lines changed

3 files changed

+75
-19
lines changed

py_trees/blackboard.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,21 +848,37 @@ def _stringify_key_value_pairs(self, keys, key_value_dict, indent):
848848
s += console.reset
849849
return s
850850

851-
def unregister(self, clear=True):
851+
def unregister(self, clear: bool=True):
852852
"""
853853
Unregister this blackboard client and if requested, clear key-value pairs if this
854854
client is the last user of those variables.
855+
856+
Args:
857+
clear: remove key-values pairs from the blackboard
858+
"""
859+
self.unregister_all_keys(clear)
860+
del Blackboard.clients[super().__getattribute__("unique_identifier")]
861+
862+
def unregister_all_keys(self, clear: bool=True):
863+
"""
864+
Unregister all keys currently registered by this blackboard client and if requested,
865+
clear key-value pairs if this client is the last user of those variables.
866+
867+
Args:
868+
clear: remove key-values pairs from the blackboard
855869
"""
856-
print("Name: {}".format(self.name))
857870
for key in self.read:
858871
Blackboard.metadata[key].read.remove(super().__getattribute__("unique_identifier"))
859872
for key in self.write:
860873
Blackboard.metadata[key].write.remove(super().__getattribute__("unique_identifier"))
861874
if clear:
862875
for key in (set(self.read) | set(self.write)):
863876
if not (set(Blackboard.metadata[key].read) | set(Blackboard.metadata[key].write)):
864-
Blackboard.storage.pop(key, None)
865-
Blackboard.metadata.pop(key, None)
877+
try:
878+
del Blackboard.storage[key]
879+
except KeyError:
880+
pass # perfectly acceptal for a key to not exist on the blackboard yet
881+
del Blackboard.metadata[key]
866882

867883
def register_key(self, key: str, read: bool=False, write: bool=False):
868884
"""
@@ -881,6 +897,29 @@ def register_key(self, key: str, read: bool=False, write: bool=False):
881897
super().__getattribute__("write").add(key)
882898
Blackboard.metadata[key].write.add(super().__getattribute__("unique_identifier"))
883899

900+
def unregister_key(self, key: str, clear: bool=True):
901+
"""
902+
Unegister a key associated with this client.
903+
904+
Args:
905+
key: key to unregister
906+
clear: remove key-values pairs from the blackboard
907+
908+
Raises:
909+
KeyError if the key has not been previously registered
910+
"""
911+
super().__getattribute__("read").discard(key) # doesn't throw exceptions if it not present
912+
super().__getattribute__("write").discard(key)
913+
Blackboard.metadata[key].read.discard(super().__getattribute__("unique_identifier"))
914+
Blackboard.metadata[key].write.discard(super().__getattribute__("unique_identifier"))
915+
if not (Blackboard.metadata[key].read | Blackboard.metadata[key].write):
916+
del Blackboard.metadata[key]
917+
if clear:
918+
try:
919+
del Blackboard.storage[key]
920+
except KeyError:
921+
pass # perfectly legitimate for a registered key to not exist on the blackboard
922+
884923

885924
class SubBlackboard(object):
886925
"""

tests/test_blackboard.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,20 @@ def test_key_exists():
136136
console.banner("Nested Read")
137137
with create_blackboards() as (foo, unused_bar):
138138
print("foo.exists('dude') [{}][{}]".format(foo.exists("dude"), True))
139-
assert(foo.exists("dude"), True)
139+
assert(foo.exists("dude"))
140140
with nose.tools.assert_raises_regexp(AttributeError, "does not have read/write access"):
141141
print("Expecting Attribute Error with substring 'does not have read/write access'")
142142
print("foo.exists('dude_not_here') [{}][{}]".format(foo.exists("dude_not_here"), False))
143-
assert(foo.exists("dude_not_here"), False)
143+
assert(not foo.exists("dude_not_here"))
144144

145145

146146
def test_nested_exists():
147147
console.banner("Nested Read")
148148
with create_blackboards() as (foo, unused_bar):
149149
print("foo.exists('motley.nested') [{}][{}]".format(foo.exists("foo.nested"), True))
150-
assert(foo.exists("motley.nested"), True)
150+
assert(foo.exists("motley.nested"))
151151
print("foo.exists('motley.not_here') [{}][{}]".format(foo.exists("foo.not_here"), False))
152-
assert(foo.exists("motley.not_here"), False)
152+
assert(not foo.exists("motley.not_here"))
153153

154154

155155
def test_nested_read():
@@ -276,7 +276,7 @@ def test_lists():
276276
foo.dude = ["Bob", "Bill"]
277277
name = bar.dude[0]
278278
print("Read first element of the list: {} [{}]".format(name, "Bob"))
279-
assert(name, "Bob")
279+
assert(name == "Bob")
280280

281281

282282
def test_dicts():
@@ -285,7 +285,7 @@ def test_dicts():
285285
foo.dude = {"Bob": 5, "Bill": 3}
286286
value = bar.dude["Bob"]
287287
print("Read Bob's score: {} [{}]".format(value, 5))
288-
assert(value, 5)
288+
assert(value == 5)
289289

290290

291291
def test_static_get_set():
@@ -305,15 +305,32 @@ def test_static_get_set():
305305
Blackboard.set("motley", Motley())
306306
print("Set motley.nested: nooo")
307307
Blackboard.set("motley.nested", "nooo")
308-
assert(Blackboard.get("motley.nested"), "nooo")
308+
assert(Blackboard.get("motley.nested") == "nooo")
309309
print("Get motley.foo")
310310
with nose.tools.assert_raises_regexp(KeyError, "nested attributes"):
311311
print(" - Expecting a KeyError")
312312
unused_value = Blackboard.get("motley.foo")
313313
print("Set motley.other: floosie")
314314
Blackboard.set("motley.other", "floosie")
315-
assert(Blackboard.get("motley.other"), "floosie")
315+
assert(Blackboard.get("motley.other") == "floosie")
316316
print("Get missing")
317317
with nose.tools.assert_raises_regexp(KeyError, "missing"):
318318
print(" - Expecting a KeyError")
319319
unused_value = Blackboard.get("missing")
320+
321+
322+
def test_unregister_key():
323+
console.banner("Unregister Keys")
324+
with create_blackboards() as (foo, bar):
325+
print("'foo' in foo.write")
326+
assert ("foo" in foo.write)
327+
print("Foo unregisters 'foo'")
328+
foo.unregister_key("foo")
329+
print("'foo' not in foo.write")
330+
assert ("foo" not in foo.write)
331+
print("Bar unregisters 'dudette' with clearing")
332+
print("'dudette' not in bar.write")
333+
bar.unregister_key("dudette", clear=True)
334+
assert("dudette" not in bar.write)
335+
print("'dudette' not on the blackboard")
336+
assert("dudette" not in Blackboard.storage)

tests/test_blackboard_behaviours.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def test_wait_for_variable_value():
340340
assert(b.status == asserted_result)
341341

342342

343-
def untest_wait_for_blackboard_variable():
343+
def test_wait_for_blackboard_variable():
344344
console.banner("Wait for Blackboard Variable")
345345
create_blackboard()
346346

@@ -353,18 +353,18 @@ def untest_wait_for_blackboard_variable():
353353
name="check_nested_foo_exists", variable_name="nested.foo"), Status.SUCCESS))
354354
tuples.append((py_trees.behaviours.WaitForBlackboardVariable(
355355
name="check_nested_bar_exists", variable_name="nested.bar"), Status.RUNNING))
356-
tuples.append((py_trees.behaviours.WaitForBlackboardVariable(
356+
tuples.append((py_trees.behaviours.WaitForBlackboardVariableValue(
357357
name="check_foo_equals_bar", variable_name="foo", expected_value="bar"), Status.SUCCESS))
358-
tuples.append((py_trees.behaviours.WaitForBlackboardVariable(
358+
tuples.append((py_trees.behaviours.WaitForBlackboardVariableValue(
359359
name="check_foo_equals_foo", variable_name="foo", expected_value="foo"), Status.RUNNING))
360-
tuples.append((py_trees.behaviours.WaitForBlackboardVariable(
360+
tuples.append((py_trees.behaviours.WaitForBlackboardVariableValue(
361361
name="check_bar_equals_bar", variable_name="bar", expected_value="bar"), Status.RUNNING))
362-
tuples.append((py_trees.behaviours.WaitForBlackboardVariable(
362+
tuples.append((py_trees.behaviours.WaitForBlackboardVariableValue(
363363
name="check_bar_equals_foo", variable_name="bar", expected_value="foo"), Status.RUNNING))
364-
tuples.append((py_trees.behaviours.WaitForBlackboardVariable(
364+
tuples.append((py_trees.behaviours.WaitForBlackboardVariableValue(
365365
name="check_nested_foo_equals_bar", variable_name="nested.foo", expected_value="bar"),
366366
Status.SUCCESS))
367-
tuples.append((py_trees.behaviours.WaitForBlackboardVariable(
367+
tuples.append((py_trees.behaviours.WaitForBlackboardVariableValue(
368368
name="check_nested_foo_equals_foo", variable_name="nested.foo", expected_value="foo"),
369369
Status.RUNNING))
370370
for b, unused in tuples:

0 commit comments

Comments
 (0)