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

Feature to plot XML to image #14

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion bt_view/src/bt_view/bt_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _make_history_image(
def draw_pygraphviz(
g: nx.Graph,
fname: str,
modifier,
modifier: Optional[callable] = None,
):
A = nx.nx_agraph.to_agraph(g) # convert to a graphviz graph
for node in A.nodes():
Expand Down
5 changes: 4 additions & 1 deletion bt_view/src/bt_view/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import sys

try:
from bt_view import draw_pygraphviz # type: ignore
from bt_view import draw_pygraphviz_w_history # type: ignore
from bt_view import draw_pygraphviz_w_returnstates # type: ignore
from bt_view import draw_pygraphviz_w_valuemod # type: ignore
except ImportError:
from .bt_view import draw_pygraphviz
from .bt_view import draw_pygraphviz_w_history
from .bt_view import draw_pygraphviz_w_returnstates
from .bt_view import draw_pygraphviz_w_valuemod
Expand Down Expand Up @@ -76,7 +78,7 @@ def main(args=sys.argv[1:]):
parser.print_help()
sys.exit(1)

if arguments.bt_xml_fname and arguments.bt_log_fbl_fname:
if arguments.bt_xml_fname and arguments.bt_log_fbl_fnames:
print('When reading FBL log file, XML file is not needed '
'and will be ignored')
arguments.bt_xml_fname = None
Expand All @@ -93,6 +95,7 @@ def main(args=sys.argv[1:]):
g, _ = xml_to_networkx(bt_xml_fname)
# if arguments.assemble_subtrees:
# g, xpi = assemble_subtrees(g, xpi)
draw_pygraphviz(g, os.path.splitext(bt_xml_fname)[0])

# read fbl log file
if arguments.bt_log_fbl_fnames:
Expand Down
10 changes: 10 additions & 0 deletions bt_view/test/_test_data/behavior_tree.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<root main_tree_to_execute="MainTree" >
<BehaviorTree ID="MainTree">
<Sequence name="root_sequence">
<SaySomething name="action_hello" message="Hello"/>
<OpenGripper name="open_gripper"/>
<ApproachObject name="approach_object"/>
<CloseGripper name="close_gripper"/>
</Sequence>
</BehaviorTree>
</root>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions bt_view/test/_test_data/reference/behavior_tree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 32 additions & 3 deletions bt_view/test/systemtests/test_bt_view_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_bt_view_main_single_fbl(self):
fname = os.path.join(
TEST_DATA_DIR, f'bt_trace{no}_fbl_log_{data}.svg')
# load svg file
with open(fname, 'r') as f:
with open(fname, 'r', encoding='utf-8') as f:
svg = f.read()
svg_hashes_fbl[fname] = hash(svg)
self._check_svg_for_nodenames(svg)
Expand Down Expand Up @@ -230,14 +230,14 @@ def test_bt_view_main_multiple_fbl_files_merge(self):
fname = os.path.join(
TEST_DATA_DIR, f'bt_trace{no}_fbl_log_{data}.svg')
# load svg file
with open(fname, 'r') as f:
with open(fname, 'r', encoding='utf-8') as f:
svg = f.read()
svg_hashes_fbl[fname] = hash(svg)
self._check_svg_for_nodenames(svg)

# also hash the combined svg files
for fname in fnames_combined_svg:
with open(fname, 'r') as f:
with open(fname, 'r', encoding='utf-8') as f:
svg = f.read()
svg_hashes_fbl[fname] = hash(svg)
self._check_svg_for_nodenames(svg)
Expand Down Expand Up @@ -273,3 +273,32 @@ def test_bt_view_main_coverage(self):
main(['--bt_log_fbl_fname', fname_2,
'--coverage-threshold', '0.8'])
self.assertEqual(cm.exception.code, 1)

def test_bt_view_main_xml(self):
"""Test if the correct images are created for XML input."""
base_name: str = 'behavior_tree'
xml_fname = os.path.join(
TEST_DATA_DIR,
f'{base_name}.xml')
main(['--bt_xml_fname', xml_fname])

# make sure the files have been created
for ext in self.img_exts:
fname = os.path.join(
TEST_DATA_DIR, f'{base_name}.{ext}')
self.assertTrue(os.path.isfile(fname))

# make sure they match the reference
for ext in self.img_exts:
with open(os.path.join(
TEST_DATA_DIR,
f'{base_name}.{ext}'
), 'rb') as f:
out = f.read()
with open(os.path.join(
TEST_DATA_DIR,
'reference',
f'{base_name}.{ext}'
), 'rb') as f:
ref = f.read()
self.assertEqual(out, ref)
Loading