Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using binary IDs #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading