From faa684ce6852476d0b7004d056ab6ce2d1fb2730 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Sat, 19 Nov 2022 18:03:43 +0100 Subject: [PATCH 001/475] fix error #473 and change name of CMake options --- .../workflows/{cmake.yml => cmake_ubuntu.yml} | 10 ++- .github/workflows/cmake_windows.yml | 47 ++++++++++++ CMakeLists.txt | 71 ++++++++++--------- tests/CMakeLists.txt | 4 +- tools/CMakeLists.txt | 1 + 5 files changed, 92 insertions(+), 41 deletions(-) rename .github/workflows/{cmake.yml => cmake_ubuntu.yml} (91%) create mode 100644 .github/workflows/cmake_windows.yml diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake_ubuntu.yml similarity index 91% rename from .github/workflows/cmake.yml rename to .github/workflows/cmake_ubuntu.yml index d192e3515..3ca2bf3c4 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake_ubuntu.yml @@ -1,4 +1,4 @@ -name: cmake +name: cmake Ubuntu on: [push, pull_request] @@ -15,14 +15,13 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, windows-latest] + os: [ubuntu-20.04] steps: - uses: actions/checkout@v2 - name: Install Dependencies (Linux) run: sudo apt-get install libboost-dev libzmq3-dev - if: matrix.os == 'ubuntu-latest' - name: Create Build Environment # Some projects don't allow in-source building, so create a separate build directory @@ -34,8 +33,8 @@ jobs: # access regardless of the host operating system shell: bash working-directory: ${{github.workspace}}/build - # Note the current convention is to use the -S and -B options here to specify source - # and build directories, but this is only available with CMake 3.13 and higher. + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON @@ -46,7 +45,6 @@ jobs: run: cmake --build . --config $BUILD_TYPE - name: run test (Linux) - if: matrix.os == 'ubuntu-latest' working-directory: ${{github.workspace}}/build run: ./tests/behaviortree_cpp_test diff --git a/.github/workflows/cmake_windows.yml b/.github/workflows/cmake_windows.yml new file mode 100644 index 000000000..95007302e --- /dev/null +++ b/.github/workflows/cmake_windows.yml @@ -0,0 +1,47 @@ +name: cmake Windows + +on: [push, pull_request] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally + # well on Windows or Mac. You can convert this to a matrix build if you need + # cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest] + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + # Use a bash shell so we can use the same syntax for environment variable + # access regardless of the host operating system + shell: bash + working-directory: ${{github.workspace}}/build + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. + # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON + + - name: Build + working-directory: ${{github.workspace}}/build + shell: bash + # Execute the build. You can specify a specific target with "--target " + run: cmake --build . --config $BUILD_TYPE + + - name: run test (Windows) + working-directory: ${{github.workspace}}/build + run: ./tests/Release/behaviortree_cpp_test + diff --git a/CMakeLists.txt b/CMakeLists.txt index 31cbd30d4..d03b8a734 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,17 +18,24 @@ endif() set(CMAKE_POSITION_INDEPENDENT_CODE ON) #---- project configuration ---- -option(BUILD_EXAMPLES "Build tutorials and examples" ON) -option(BUILD_SAMPLES "Build sample nodes" ON) -option(BUILD_UNIT_TESTS "Build the unit tests" ON) -option(BUILD_TOOLS "Build commandline tools" ON) -option(BUILD_SHARED_LIBS "Build shared libraries" ON) -option(BUILD_MANUAL_SELECTOR "Build manual selector node" ON) -option(ENABLE_COROUTINES "Enable boost coroutines" ON) +option(BTCPP_SHARED_LIBS "Build shared libraries" ON) +option(BTCPP_ENABLE_COROUTINES "Enable boost coroutines" ON) +option(BTCPP_MANUAL_SELECTOR "Build manual selector node" ON) + +if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR} ) + option(BTCPP_EXAMPLES "Build tutorials and examples" ON) + option(BTCPP_UNIT_TESTS "Build the unit tests" ON) + option(BTCPP_BUILD_TOOLS "Build commandline tools" ON) +else() + option(BTCPP_EXAMPLES "Build tutorials and examples" OFF) + option(BTCPP_UNIT_TESTS "Build the unit tests" OFF) + option(BTCPP_BUILD_TOOLS "Build commandline tools" OFF) +endif() + option(USE_V3_COMPATIBLE_NAMES "Use some alias to compile more easily old 3.x code" OFF) #---- Include boost to add coroutines ---- -if(ENABLE_COROUTINES) +if(BTCPP_ENABLE_COROUTINES) find_package(Boost COMPONENTS coroutine QUIET) if(Boost_FOUND) @@ -36,23 +43,23 @@ if(ENABLE_COROUTINES) if(NOT Boost_VERSION_NODOT VERSION_LESS 105900) message(STATUS "Found boost::coroutine2.") add_definitions(-DBT_BOOST_COROUTINE2) - set(BT_COROUTINES true) + set(BT_COROUTINES_FOUND true) elseif(NOT Boost_VERSION_NODOT VERSION_LESS 105300) message(STATUS "Found boost::coroutine.") add_definitions(-DBT_BOOST_COROUTINE) - set(BT_COROUTINES true) + set(BT_COROUTINES_FOUND true) endif() include_directories(${Boost_INCLUDE_DIRS}) endif() - if(NOT DEFINED BT_COROUTINES) + if(NOT DEFINED BT_COROUTINES_FOUND) message(STATUS "Boost coroutines disabled. Install Boost (version 1.59+ recommended).") endif() else() message(STATUS "Boost coroutines disabled by CMake option.") endif() -if(NOT DEFINED BT_COROUTINES) +if(NOT DEFINED BT_COROUTINES_FOUND) add_definitions(-DBT_NO_COROUTINES) endif() @@ -116,7 +123,7 @@ elseif( CATKIN_DEVEL_PREFIX OR CATKIN_BUILD_BINARY_PACKAGE) list(APPEND BEHAVIOR_TREE_PUBLIC_LIBRARIES ${catkin_LIBRARIES}) set(BUILD_TOOL_INCLUDE_DIRS ${catkin_INCLUDE_DIRS}) -elseif(BUILD_UNIT_TESTS) +elseif(BTCPP_UNIT_TESTS) if(${CMAKE_VERSION} VERSION_LESS "3.11.0") find_package(GTest REQUIRED) else() @@ -175,7 +182,7 @@ list(APPEND BT_SOURCE 3rdparty/minitrace/minitrace.cpp ) -if(BUILD_MANUAL_SELECTOR) +if(BTCPP_MANUAL_SELECTOR) find_package(Curses QUIET) if(CURSES_FOUND) list(APPEND BT_SOURCE @@ -211,13 +218,13 @@ if( ZMQ_FOUND ) list(APPEND BUILD_TOOL_INCLUDE_DIRS ${ZMQ_INCLUDE_DIRS}) endif() -target_link_libraries(${BEHAVIOR_TREE_LIBRARY} PUBLIC - ${BEHAVIOR_TREE_PUBLIC_LIBRARIES}) - -target_link_libraries(${BEHAVIOR_TREE_LIBRARY} PRIVATE - ${Boost_LIBRARIES} - ${ZMQ_LIBRARIES} - foonathan::lexy +target_link_libraries(${BEHAVIOR_TREE_LIBRARY} + PUBLIC + ${BEHAVIOR_TREE_PUBLIC_LIBRARIES} + PRIVATE + ${Boost_LIBRARIES} + ${ZMQ_LIBRARIES} + $ ) #get_target_property(my_libs ${BEHAVIOR_TREE_LIBRARY} INTERFACE_LINK_LIBRARIES) @@ -229,12 +236,15 @@ target_link_libraries(${BEHAVIOR_TREE_LIBRARY} PRIVATE target_compile_definitions(${BEHAVIOR_TREE_LIBRARY} PRIVATE $<$:TINYXML2_DEBUG>) target_include_directories(${BEHAVIOR_TREE_LIBRARY} PUBLIC - $ - $ $ $ ${BUILD_TOOL_INCLUDE_DIRS}) +target_include_directories(${BEHAVIOR_TREE_LIBRARY} PRIVATE + $ + $ + ) + if( ZMQ_FOUND ) target_compile_definitions(${BEHAVIOR_TREE_LIBRARY} PUBLIC ZMQ_FOUND) endif() @@ -271,18 +281,13 @@ endif() message( STATUS "BEHAVIOR_TREE_LIB_DESTINATION: ${BEHAVIOR_TREE_LIB_DESTINATION} " ) message( STATUS "BEHAVIOR_TREE_BIN_DESTINATION: ${BEHAVIOR_TREE_BIN_DESTINATION} " ) -message( STATUS "BUILD_UNIT_TESTS: ${BUILD_UNIT_TESTS} " ) +message( STATUS "BTCPP_UNIT_TESTS: ${BTCPP_UNIT_TESTS} " ) - -###################################################### -# Samples -if (BUILD_SAMPLES) - add_subdirectory(sample_nodes) -endif() +add_subdirectory(sample_nodes) ###################################################### # Test -if (BUILD_UNIT_TESTS AND BUILD_SAMPLES) +if (BTCPP_UNIT_TESTS) add_subdirectory(tests) endif() @@ -333,10 +338,10 @@ install( ###################################################### # EXAMPLES and TOOLS -if(BUILD_TOOLS) +if(BTCPP_BUILD_TOOLS) add_subdirectory(tools) endif() -if(BUILD_EXAMPLES AND BUILD_SAMPLES) +if(BTCPP_EXAMPLES) add_subdirectory(examples) endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ff624fa27..fa4a3b68c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,7 +24,7 @@ set(BT_TESTS script_parser_test.cpp ) -if( BT_COROUTINES ) +if( BTCPP_ENABLE_COROUTINES AND BT_COROUTINES_FOUND ) LIST( APPEND BT_TESTS gtest_coroutines.cpp) endif() @@ -60,7 +60,7 @@ elseif(catkin_FOUND AND CATKIN_ENABLE_TESTING) target_include_directories(${BEHAVIOR_TREE_LIBRARY}_test PRIVATE gtest/include) -elseif(BUILD_UNIT_TESTS) +elseif(BTCPP_UNIT_TESTS) enable_testing() diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 2ad33b35c..163e70348 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.5) +include_directories(${CMAKE_SOURCE_DIR}/3rdparty) add_executable(bt3_log_cat bt_log_cat.cpp ) target_link_libraries(bt3_log_cat ${BEHAVIOR_TREE_LIBRARY} ) From a851c79c12b4dcad95eedd9dfe7e33808b22f2b9 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Sat, 19 Nov 2022 18:17:40 +0100 Subject: [PATCH 002/475] overwrite previous tree definitions --- src/xml_parsing.cpp | 5 ++--- tests/gtest_factory.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/xml_parsing.cpp b/src/xml_parsing.cpp index 37cac3345..7585bfa48 100644 --- a/src/xml_parsing.cpp +++ b/src/xml_parsing.cpp @@ -222,7 +222,7 @@ void XMLParser::Pimpl::loadDocImpl(tinyxml2::XMLDocument* doc, bool add_includes tree_name = "BehaviorTree_" + std::to_string(suffix_count++); } - tree_roots.insert({tree_name, bt_node}); + tree_roots[tree_name] = bt_node; } } @@ -430,10 +430,9 @@ void VerifyXML(const std::string& xml_text, } Tree XMLParser::instantiateTree(const Blackboard::Ptr& root_blackboard, - std::string main_tree_to_execute) + std::string main_tree_ID) { Tree output_tree; - std::string main_tree_ID = main_tree_to_execute; // use the main_tree_to_execute argument if it was provided by the user // or the one in the FIRST document opened diff --git a/tests/gtest_factory.cpp b/tests/gtest_factory.cpp index 372307899..ab68cf6ab 100644 --- a/tests/gtest_factory.cpp +++ b/tests/gtest_factory.cpp @@ -341,3 +341,34 @@ TEST( Tree tree = factory.createTreeFromFile(path); ASSERT_EQ(NodeStatus::SUCCESS, tree.tickWhileRunning()); } + +TEST(BehaviorTreeReload, ReloadSameTree) +{ + const char* xmlA = R"( + + + + + )"; + + const char* xmlB = R"( + + + + + )"; + + BehaviorTreeFactory factory; + + factory.registerBehaviorTreeFromText(xmlA); + { + auto tree = factory.createTree("MainTree"); + ASSERT_EQ(NodeStatus::SUCCESS, tree.tickWhileRunning()); + } + + factory.registerBehaviorTreeFromText(xmlB); + { + auto tree = factory.createTree("MainTree"); + ASSERT_EQ(NodeStatus::FAILURE, tree.tickWhileRunning()); + } +} From 92564dfac20420d98d03f0e2d41eefdba766d52e Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Sat, 19 Nov 2022 18:58:18 +0100 Subject: [PATCH 003/475] removing "main_tree_to_execute" from tutorials and adding "BTCPP_format" --- examples/ex01_wrap_legacy.cpp | 2 +- examples/ex02_runtime_ports.cpp | 5 ++-- examples/ex03_ncurses_manual_selector.cpp | 2 +- examples/ex04_waypoints.cpp | 6 ++-- examples/t01_build_your_first_tree.cpp | 6 ++-- examples/t02_basic_ports.cpp | 2 +- examples/t03_generic_ports.cpp | 5 ++-- examples/t04_reactive_sequence.cpp | 5 ++-- examples/t05_crossdoor.cpp | 10 +++++-- examples/t06_subtree_port_remapping.cpp | 11 +++---- examples/t07_load_multiple_xml.cpp | 6 ++-- examples/t08_additional_node_args.cpp | 2 +- examples/t09_scripting.cpp | 2 +- examples/test_files/subtree_test.xml | 2 +- examples/test_files/subtrees/Talk.xml | 2 +- include/behaviortree_cpp/behavior_tree.h | 1 + .../decorators/subtree_node.h | 4 +-- src/bt_factory.cpp | 2 +- src/xml_parsing.cpp | 7 +++++ tests/gtest_blackboard.cpp | 10 +++---- tests/gtest_factory.cpp | 10 +++---- tests/gtest_ports.cpp | 9 +++--- tests/gtest_postconditions.cpp | 2 +- tests/gtest_preconditions.cpp | 8 ++--- tests/gtest_skipping.cpp | 4 +-- tests/gtest_subtree.cpp | 30 +++++++++++-------- tests/gtest_switch.cpp | 2 +- tests/script_parser_test.cpp | 8 ++--- 28 files changed, 94 insertions(+), 71 deletions(-) diff --git a/examples/ex01_wrap_legacy.cpp b/examples/ex01_wrap_legacy.cpp index 622d0f7d1..80abf64f7 100644 --- a/examples/ex01_wrap_legacy.cpp +++ b/examples/ex01_wrap_legacy.cpp @@ -51,7 +51,7 @@ Point3D convertFromString(StringView key) // clang-format off static const char* xml_text = R"( - + diff --git a/examples/ex02_runtime_ports.cpp b/examples/ex02_runtime_ports.cpp index aea5dd1c4..d6e645dc2 100644 --- a/examples/ex02_runtime_ports.cpp +++ b/examples/ex02_runtime_ports.cpp @@ -3,7 +3,7 @@ using namespace BT; // clang-format off static const char* xml_text = R"( - + @@ -62,7 +62,8 @@ int main() PortsList say_ports = {BT::InputPort("message")}; factory.registerNodeType("SayRuntimePort", say_ports); - auto tree = factory.createTreeFromText(xml_text); + factory.registerBehaviorTreeFromText(xml_text); + auto tree = factory.createTree("MainTree"); tree.tickWhileRunning(); return 0; diff --git a/examples/ex03_ncurses_manual_selector.cpp b/examples/ex03_ncurses_manual_selector.cpp index 28172b074..3ce3c1379 100644 --- a/examples/ex03_ncurses_manual_selector.cpp +++ b/examples/ex03_ncurses_manual_selector.cpp @@ -10,7 +10,7 @@ using namespace BT; // clang-format off static const char* xml_text = R"( - + diff --git a/examples/ex04_waypoints.cpp b/examples/ex04_waypoints.cpp index 98e7edb22..536fe69d5 100644 --- a/examples/ex04_waypoints.cpp +++ b/examples/ex04_waypoints.cpp @@ -126,7 +126,7 @@ class UseWaypoint : public ThreadedAction // clang-format off static const char* xml_implicit = R"( - + @@ -140,7 +140,7 @@ static const char* xml_implicit = R"( static const char* xml_A = R"( - + @@ -157,7 +157,7 @@ static const char* xml_A = R"( )"; static const char* xml_B = R"( - + diff --git a/examples/t01_build_your_first_tree.cpp b/examples/t01_build_your_first_tree.cpp index 03d2b1752..24b507caa 100644 --- a/examples/t01_build_your_first_tree.cpp +++ b/examples/t01_build_your_first_tree.cpp @@ -20,7 +20,7 @@ using namespace BT; // clang-format off static const char* xml_text = R"( - + @@ -73,11 +73,13 @@ int main() // it automated the registering step. factory.registerFromPlugin("./libdummy_nodes_dyn.so"); #endif + // register the XML description + factory.registerBehaviorTreeFromText(xml_text); // Trees are created at deployment-time (i.e. at run-time, but only once at the beginning). // The currently supported format is XML. // IMPORTANT: when the object "tree" goes out of scope, all the TreeNodes are destroyed - auto tree = factory.createTreeFromText(xml_text); + auto tree = factory.createTree("MainTree"); // To "execute" a Tree you need to "tick" it. // The tick is propagated to the children based on the logic of the tree. diff --git a/examples/t02_basic_ports.cpp b/examples/t02_basic_ports.cpp index 74c8508c4..da3405740 100644 --- a/examples/t02_basic_ports.cpp +++ b/examples/t02_basic_ports.cpp @@ -28,7 +28,7 @@ using namespace BT; // clang-format off static const char* xml_text = R"( - + diff --git a/examples/t03_generic_ports.cpp b/examples/t03_generic_ports.cpp index c7521f63e..db1964f34 100644 --- a/examples/t03_generic_ports.cpp +++ b/examples/t03_generic_ports.cpp @@ -93,7 +93,7 @@ class PrintTarget : public SyncActionNode * 2) Call PrintTarget. The input "target" will be read from the Blackboard * entry "GoalPosition". * -* 3) Use the built-in action SetBlackboard to write the key "OtherGoal". +* 3) Use the built-in action Script to write the key "OtherGoal". * A conversion from string to Position2D will be done under the hood. * * 4) Call PrintTarget. The input "goal" will be read from the Blackboard @@ -103,7 +103,7 @@ class PrintTarget : public SyncActionNode // clang-format off static const char* xml_text = R"( - + @@ -126,6 +126,7 @@ int main() factory.registerNodeType("PrintTarget"); auto tree = factory.createTreeFromText(xml_text); + tree.tickWhileRunning(); /* Expected output: diff --git a/examples/t04_reactive_sequence.cpp b/examples/t04_reactive_sequence.cpp index fe15284f0..27465352c 100644 --- a/examples/t04_reactive_sequence.cpp +++ b/examples/t04_reactive_sequence.cpp @@ -16,7 +16,7 @@ using namespace BT; static const char* xml_text_sequence = R"( - + @@ -32,7 +32,7 @@ static const char* xml_text_sequence = R"( static const char* xml_text_reactive = R"( - + @@ -55,6 +55,7 @@ using namespace DummyNodes; int main() { BehaviorTreeFactory factory; + factory.registerSimpleCondition("BatteryOK", std::bind(CheckBattery)); factory.registerNodeType("MoveBase"); factory.registerNodeType("SaySomething"); diff --git a/examples/t05_crossdoor.cpp b/examples/t05_crossdoor.cpp index a64807c4c..58a2c6214 100644 --- a/examples/t05_crossdoor.cpp +++ b/examples/t05_crossdoor.cpp @@ -10,7 +10,7 @@ // clang-format off static const char* xml_text = R"( - + @@ -46,8 +46,12 @@ int main() CrossDoor cross_door; cross_door.registerNodes(factory); - // Load from text or file... - auto tree = factory.createTreeFromText(xml_text); + // In this example a single XML contains multiple tags + // To determine which one is the "main one", we should first register + // the XML and then allocate a specific tree, using its ID + + factory.registerBehaviorTreeFromText(xml_text); + auto tree = factory.createTree("MainTree"); // helper function to print the tree BT::printTreeRecursively(tree.rootNode()); diff --git a/examples/t06_subtree_port_remapping.cpp b/examples/t06_subtree_port_remapping.cpp index fbbfb8e54..d9f2b0a07 100644 --- a/examples/t06_subtree_port_remapping.cpp +++ b/examples/t06_subtree_port_remapping.cpp @@ -21,11 +21,11 @@ // clang-format off static const char* xml_text = R"( - + -