-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
344 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
digraph pastafarianism { | ||
ordering=out; | ||
graph [fontname="times-roman"]; | ||
node [fontname="times-roman"]; | ||
edge [fontname="times-roman"]; | ||
root [fillcolor=orange, fontcolor=black, fontsize=9, label="Ⓜ root", shape=box, style=filled]; | ||
SetFlagFalse [fillcolor=gray, fontcolor=black, fontsize=9, label=SetFlagFalse, shape=ellipse, style=filled]; | ||
root -> SetFlagFalse; | ||
Parallel [fillcolor=gold, fontcolor=black, fontsize=9, label="Parallel\nSuccessOnOne", shape=parallelogram, style=filled]; | ||
root -> Parallel; | ||
Counter [fillcolor=gray, fontcolor=black, fontsize=9, label=Counter, shape=ellipse, style=filled]; | ||
Parallel -> Counter; | ||
Finally [fillcolor=ghostwhite, fontcolor=black, fontsize=9, label=Finally, shape=ellipse, style=filled]; | ||
Parallel -> Finally; | ||
SetFlagTrue [fillcolor=gray, fontcolor=black, fontsize=9, label=SetFlagTrue, shape=ellipse, style=filled]; | ||
Finally -> SetFlagTrue; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
#!/usr/bin/env python | ||
# | ||
# License: BSD | ||
# https://raw.githubusercontent.com/splintered-reality/py_trees/devel/LICENSE | ||
# | ||
############################################################################## | ||
# Documentation | ||
############################################################################## | ||
|
||
""" | ||
Trigger a 'finally'-like behaviour with the | ||
:class:`~py_trees.decorators.Finally` decorator. | ||
.. argparse:: | ||
:module: py_trees.demos.decorator_finally | ||
:func: command_line_argument_parser | ||
:prog: py-trees-demo-finally | ||
.. graphviz:: dot/demo-finally.dot | ||
.. image:: images/finally.png | ||
""" | ||
|
||
############################################################################## | ||
# Imports | ||
############################################################################## | ||
|
||
import argparse | ||
import sys | ||
import typing | ||
|
||
import py_trees | ||
import py_trees.console as console | ||
|
||
############################################################################## | ||
# Classes | ||
############################################################################## | ||
|
||
|
||
def description(root: py_trees.behaviour.Behaviour) -> str: | ||
""" | ||
Print description and usage information about the program. | ||
Returns: | ||
the program description string | ||
""" | ||
content = ( | ||
"Trigger python-like 'finally' behaviour with the 'Finally' decorator.\n\n" | ||
) | ||
content += "A blackboard flag is set to false prior to commencing work. \n" | ||
content += "Once the work terminates, the decorator and it's child\n" | ||
content += "child will also terminate and toggle the flag to true.\n" | ||
content += "\n" | ||
content += "The demonstration is run twice - on the first occasion\n" | ||
content += "the work terminates with SUCCESS and on the second, it\n" | ||
content + "terminates with FAILURE\n" | ||
content += "\n" | ||
content += "EVENTS\n" | ||
content += "\n" | ||
content += " - 1 : flag is set to false, worker is running\n" | ||
content += " - 2 : worker completes (with SUCCESS||FAILURE)\n" | ||
content += " - 2 : finally is triggered, flag is set to true\n" | ||
content += "\n" | ||
if py_trees.console.has_colours: | ||
banner_line = console.green + "*" * 79 + "\n" + console.reset | ||
s = banner_line | ||
s += console.bold_white + "Finally".center(79) + "\n" + console.reset | ||
s += banner_line | ||
s += "\n" | ||
s += content | ||
s += "\n" | ||
s += banner_line | ||
else: | ||
s = content | ||
return s | ||
|
||
|
||
def epilog() -> typing.Optional[str]: | ||
""" | ||
Print a noodly epilog for --help. | ||
Returns: | ||
the noodly message | ||
""" | ||
if py_trees.console.has_colours: | ||
return ( | ||
console.cyan | ||
+ "And his noodly appendage reached forth to tickle the blessed...\n" | ||
+ console.reset | ||
) | ||
else: | ||
return None | ||
|
||
|
||
def command_line_argument_parser() -> argparse.ArgumentParser: | ||
""" | ||
Process command line arguments. | ||
Returns: | ||
the argument parser | ||
""" | ||
parser = argparse.ArgumentParser( | ||
description=description(create_root(py_trees.common.Status.SUCCESS)), | ||
epilog=epilog(), | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument( | ||
"-r", "--render", action="store_true", help="render dot tree to file" | ||
) | ||
return parser | ||
|
||
|
||
def create_root( | ||
expected_work_termination_result: py_trees.common.Status, | ||
) -> py_trees.behaviour.Behaviour: | ||
""" | ||
Create the root behaviour and it's subtree. | ||
Returns: | ||
the root behaviour | ||
""" | ||
root = py_trees.composites.Sequence(name="root", memory=True) | ||
set_flag_to_false = py_trees.behaviours.SetBlackboardVariable( | ||
name="SetFlagFalse", | ||
variable_name="flag", | ||
variable_value=False, | ||
overwrite=True, | ||
) | ||
set_flag_to_true = py_trees.behaviours.SetBlackboardVariable( | ||
name="SetFlagTrue", variable_name="flag", variable_value=True, overwrite=True | ||
) | ||
parallel = py_trees.composites.Parallel( | ||
name="Parallel", | ||
policy=py_trees.common.ParallelPolicy.SuccessOnOne(), | ||
children=[], | ||
) | ||
worker = py_trees.behaviours.TickCounter( | ||
name="Counter", duration=1, completion_status=expected_work_termination_result | ||
) | ||
well_finally = py_trees.decorators.Finally(name="Finally", child=set_flag_to_true) | ||
parallel.add_children([worker, well_finally]) | ||
root.add_children([set_flag_to_false, parallel]) | ||
return root | ||
|
||
|
||
############################################################################## | ||
# Main | ||
############################################################################## | ||
|
||
|
||
def main() -> None: | ||
"""Entry point for the demo script.""" | ||
args = command_line_argument_parser().parse_args() | ||
# py_trees.logging.level = py_trees.logging.Level.DEBUG | ||
print(description(create_root(py_trees.common.Status.SUCCESS))) | ||
|
||
#################### | ||
# Rendering | ||
#################### | ||
if args.render: | ||
py_trees.display.render_dot_tree(create_root(py_trees.common.Status.SUCCESS)) | ||
sys.exit() | ||
|
||
for status in (py_trees.common.Status.SUCCESS, py_trees.common.Status.FAILURE): | ||
py_trees.blackboard.Blackboard.clear() | ||
console.banner(f"Experiment - Terminate with {status}") | ||
root = create_root(status) | ||
root.tick_once() | ||
print(py_trees.display.unicode_tree(root=root, show_status=True)) | ||
print(py_trees.display.unicode_blackboard()) | ||
root.tick_once() | ||
print(py_trees.display.unicode_tree(root=root, show_status=True)) | ||
print(py_trees.display.unicode_blackboard()) | ||
|
||
print("\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.