|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# License: BSD |
| 4 | +# https://raw.githubusercontent.com/splintered-reality/py_trees/devel/LICENSE |
| 5 | +# |
| 6 | +############################################################################## |
| 7 | +# Documentation |
| 8 | +############################################################################## |
| 9 | + |
| 10 | +""" |
| 11 | +Trigger a 'finally'-like behaviour with the |
| 12 | +:class:`~py_trees.decorators.Finally` decorator. |
| 13 | +
|
| 14 | +.. argparse:: |
| 15 | + :module: py_trees.demos.decorator_finally |
| 16 | + :func: command_line_argument_parser |
| 17 | + :prog: py-trees-demo-finally |
| 18 | +
|
| 19 | +.. graphviz:: dot/demo-finally.dot |
| 20 | +
|
| 21 | +.. image:: images/finally.png |
| 22 | +
|
| 23 | +""" |
| 24 | + |
| 25 | +############################################################################## |
| 26 | +# Imports |
| 27 | +############################################################################## |
| 28 | + |
| 29 | +import argparse |
| 30 | +import sys |
| 31 | +import typing |
| 32 | + |
| 33 | +import py_trees |
| 34 | +import py_trees.console as console |
| 35 | + |
| 36 | +############################################################################## |
| 37 | +# Classes |
| 38 | +############################################################################## |
| 39 | + |
| 40 | + |
| 41 | +def description(root: py_trees.behaviour.Behaviour) -> str: |
| 42 | + """ |
| 43 | + Print description and usage information about the program. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + the program description string |
| 47 | + """ |
| 48 | + content = ( |
| 49 | + "Trigger python-like 'finally' behaviour with the 'Finally' decorator.\n\n" |
| 50 | + ) |
| 51 | + content += "A blackboard flag is set to false prior to commencing work. \n" |
| 52 | + content += "Once the work terminates, the decorator and it's child\n" |
| 53 | + content += "child will also terminate and toggle the flag to true.\n" |
| 54 | + content += "\n" |
| 55 | + content += "The demonstration is run twice - on the first occasion\n" |
| 56 | + content += "the work terminates with SUCCESS and on the second, it\n" |
| 57 | + content + "terminates with FAILURE\n" |
| 58 | + content += "\n" |
| 59 | + content += "EVENTS\n" |
| 60 | + content += "\n" |
| 61 | + content += " - 1 : flag is set to false, worker is running\n" |
| 62 | + content += " - 2 : worker completes (with SUCCESS||FAILURE)\n" |
| 63 | + content += " - 2 : finally is triggered, flag is set to true\n" |
| 64 | + content += "\n" |
| 65 | + if py_trees.console.has_colours: |
| 66 | + banner_line = console.green + "*" * 79 + "\n" + console.reset |
| 67 | + s = banner_line |
| 68 | + s += console.bold_white + "Finally".center(79) + "\n" + console.reset |
| 69 | + s += banner_line |
| 70 | + s += "\n" |
| 71 | + s += content |
| 72 | + s += "\n" |
| 73 | + s += banner_line |
| 74 | + else: |
| 75 | + s = content |
| 76 | + return s |
| 77 | + |
| 78 | + |
| 79 | +def epilog() -> typing.Optional[str]: |
| 80 | + """ |
| 81 | + Print a noodly epilog for --help. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + the noodly message |
| 85 | + """ |
| 86 | + if py_trees.console.has_colours: |
| 87 | + return ( |
| 88 | + console.cyan |
| 89 | + + "And his noodly appendage reached forth to tickle the blessed...\n" |
| 90 | + + console.reset |
| 91 | + ) |
| 92 | + else: |
| 93 | + return None |
| 94 | + |
| 95 | + |
| 96 | +def command_line_argument_parser() -> argparse.ArgumentParser: |
| 97 | + """ |
| 98 | + Process command line arguments. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + the argument parser |
| 102 | + """ |
| 103 | + parser = argparse.ArgumentParser( |
| 104 | + description=description(create_root(py_trees.common.Status.SUCCESS)), |
| 105 | + epilog=epilog(), |
| 106 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 107 | + ) |
| 108 | + group = parser.add_mutually_exclusive_group() |
| 109 | + group.add_argument( |
| 110 | + "-r", "--render", action="store_true", help="render dot tree to file" |
| 111 | + ) |
| 112 | + return parser |
| 113 | + |
| 114 | + |
| 115 | +def create_root( |
| 116 | + expected_work_termination_result: py_trees.common.Status, |
| 117 | +) -> py_trees.behaviour.Behaviour: |
| 118 | + """ |
| 119 | + Create the root behaviour and it's subtree. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + the root behaviour |
| 123 | + """ |
| 124 | + root = py_trees.composites.Sequence(name="root", memory=True) |
| 125 | + set_flag_to_false = py_trees.behaviours.SetBlackboardVariable( |
| 126 | + name="SetFlagFalse", |
| 127 | + variable_name="flag", |
| 128 | + variable_value=False, |
| 129 | + overwrite=True, |
| 130 | + ) |
| 131 | + set_flag_to_true = py_trees.behaviours.SetBlackboardVariable( |
| 132 | + name="SetFlagTrue", variable_name="flag", variable_value=True, overwrite=True |
| 133 | + ) |
| 134 | + parallel = py_trees.composites.Parallel( |
| 135 | + name="Parallel", |
| 136 | + policy=py_trees.common.ParallelPolicy.SuccessOnOne(), |
| 137 | + children=[], |
| 138 | + ) |
| 139 | + worker = py_trees.behaviours.TickCounter( |
| 140 | + name="Counter", duration=1, completion_status=expected_work_termination_result |
| 141 | + ) |
| 142 | + well_finally = py_trees.decorators.Finally(name="Finally", child=set_flag_to_true) |
| 143 | + parallel.add_children([worker, well_finally]) |
| 144 | + root.add_children([set_flag_to_false, parallel]) |
| 145 | + return root |
| 146 | + |
| 147 | + |
| 148 | +############################################################################## |
| 149 | +# Main |
| 150 | +############################################################################## |
| 151 | + |
| 152 | + |
| 153 | +def main() -> None: |
| 154 | + """Entry point for the demo script.""" |
| 155 | + args = command_line_argument_parser().parse_args() |
| 156 | + # py_trees.logging.level = py_trees.logging.Level.DEBUG |
| 157 | + print(description(create_root(py_trees.common.Status.SUCCESS))) |
| 158 | + |
| 159 | + #################### |
| 160 | + # Rendering |
| 161 | + #################### |
| 162 | + if args.render: |
| 163 | + py_trees.display.render_dot_tree(create_root(py_trees.common.Status.SUCCESS)) |
| 164 | + sys.exit() |
| 165 | + |
| 166 | + for status in (py_trees.common.Status.SUCCESS, py_trees.common.Status.FAILURE): |
| 167 | + py_trees.blackboard.Blackboard.clear() |
| 168 | + console.banner(f"Experiment - Terminate with {status}") |
| 169 | + root = create_root(status) |
| 170 | + root.tick_once() |
| 171 | + print(py_trees.display.unicode_tree(root=root, show_status=True)) |
| 172 | + print(py_trees.display.unicode_blackboard()) |
| 173 | + root.tick_once() |
| 174 | + print(py_trees.display.unicode_tree(root=root, show_status=True)) |
| 175 | + print(py_trees.display.unicode_blackboard()) |
| 176 | + |
| 177 | + print("\n") |
0 commit comments