Skip to content

Commit

Permalink
binary ids
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Henkel <christian.henkel2@de.bosch.com>
  • Loading branch information
Christian Henkel authored and ct2034 committed Aug 13, 2024
1 parent c43aaa5 commit 101d5d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
21 changes: 13 additions & 8 deletions btlib/src/btlib/bts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions btlib/test/systemtests/test_bt_to_fsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion btlib/test/unittests/test_unittest_bts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 101d5d2

Please sign in to comment.