From 101d5d297b99589b890dd6e749e37f6f98feb5b8 Mon Sep 17 00:00:00 2001 From: Christian Henkel Date: Tue, 13 Aug 2024 12:16:01 +0200 Subject: [PATCH] binary ids Signed-off-by: Christian Henkel --- btlib/src/btlib/bts.py | 21 +++++++++++++-------- btlib/test/systemtests/test_bt_to_fsm.py | 12 ++++++------ btlib/test/unittests/test_unittest_bts.py | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/btlib/src/btlib/bts.py b/btlib/src/btlib/bts.py index 0f9b10d..9c98e0e 100644 --- a/btlib/src/btlib/bts.py +++ b/btlib/src/btlib/bts.py @@ -34,15 +34,20 @@ def _get_siblings(elm): return [ch for ch in elm.parent.children if isinstance(ch, Tag)] -def _get_unique_id(elm: BeautifulSoup, tree_id: int) -> int: +def _get_unique_id(elm: BeautifulSoup, tree_id: int) -> str: + """ + Get a unique id for the node. + + This is a byte string that is unique for the node in the graph. It is used to identify the node + in the graph. + """ if elm.name == 'BehaviorTree': - # we need a one in at first position, because 00 evaluates to 0 - return tree_id + 10 - parents_ch_list = _get_siblings(elm) - n_chars_of_nr = floor(log10(len(parents_ch_list)) + 1) - return int( - str(_get_unique_id(elm.parent, tree_id)) + str( - parents_ch_list.index(elm)).zfill(n_chars_of_nr)) + return f'{tree_id:02x}' + # which number sibling is this node + sibling_no: int = _get_siblings(elm).index(elm) + this_id: str = _get_unique_id(elm.parent, tree_id) + this_id += f'{sibling_no:02x}' + return this_id def _get_attrs(elm: BeautifulSoup) -> dict: diff --git a/btlib/test/systemtests/test_bt_to_fsm.py b/btlib/test/systemtests/test_bt_to_fsm.py index 9072f84..f7a2662 100644 --- a/btlib/test/systemtests/test_bt_to_fsm.py +++ b/btlib/test/systemtests/test_bt_to_fsm.py @@ -38,14 +38,14 @@ def test_inverter(self): # check the existence of the edges for port in ['success', 'failure', 'running']: - self.assertTrue(fsm.has_edge('1000_ServiceBtCondition', port)) - self.assertTrue(fsm.has_edge('tick', '1000_ServiceBtCondition')) + self.assertTrue(fsm.has_edge('000000_ServiceBtCondition', port)) + self.assertTrue(fsm.has_edge('tick', '000000_ServiceBtCondition')) # check the labels of the edges - self.assertEqual(fsm.edges['1000_ServiceBtCondition', 'success']['label'], 'on_failure') - self.assertEqual(fsm.edges['1000_ServiceBtCondition', 'failure']['label'], 'on_success') - self.assertEqual(fsm.edges['1000_ServiceBtCondition', 'running']['label'], 'on_running') - self.assertEqual(fsm.edges['tick', '1000_ServiceBtCondition']['label'], 'on_tick') + self.assertEqual(fsm.edges['000000_ServiceBtCondition', 'success']['label'], 'on_failure') + self.assertEqual(fsm.edges['000000_ServiceBtCondition', 'failure']['label'], 'on_success') + self.assertEqual(fsm.edges['000000_ServiceBtCondition', 'running']['label'], 'on_running') + self.assertEqual(fsm.edges['tick', '000000_ServiceBtCondition']['label'], 'on_tick') def test_simple(self): """Test that the conversion from a Behavior Tree to a FSM works.""" diff --git a/btlib/test/unittests/test_unittest_bts.py b/btlib/test/unittests/test_unittest_bts.py index b347f59..5e20051 100644 --- a/btlib/test/unittests/test_unittest_bts.py +++ b/btlib/test/unittests/test_unittest_bts.py @@ -63,7 +63,7 @@ def test_xml_to_networkx(self): # all nodes are in the graph and the xpi self.assertEqual(g.number_of_nodes(), 4) self.assertEqual(len(xpi), 4) - for x in [10, 100, 1000, 1001]: + for x in ['00', '0000', '000000', '000001']: self.assertIn(x, g.nodes) self.assertIn(x, xpi.keys())