AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
new_tree.cpp
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <filesystem>
16#include <fstream>
17#include <iostream>
18
19#include "auto_apms_behavior_tree/executor/executor_base.hpp"
20#include "auto_apms_behavior_tree/standard_nodes.hpp"
21#include "auto_apms_behavior_tree/util/node.hpp"
22#include "auto_apms_behavior_tree_core/builder.hpp"
23#include "auto_apms_util/filesystem.hpp"
24#include "auto_apms_util/string.hpp"
25#include "rclcpp/rclcpp.hpp"
26
27#define NEW_TREE_NAME "NewTree"
28
29using namespace auto_apms_behavior_tree;
30
31int main(int argc, char ** argv)
32{
33 bool print_help = false;
34 if (argc > 1) {
35 const std::string arg(argv[1]);
36 print_help = "-h" == arg || "--help" == arg;
37 }
38 if (print_help || argc < 2) {
39 std::cerr << "new_tree: The program accepts: \n\t1.) The path of the new behavior tree xml "
40 "file.\n\t2.) Optional: Node plugin manifest identities of installed resources used to create the "
41 "node model. Any arguments after the first one are interpreted as resource identities.\n";
42 std::cerr << "Usage: new_tree <tree_file_path> [<node_manifest_identity> ...].\n";
43 return EXIT_FAILURE;
44 }
45
46 const std::filesystem::path tree_file_path = std::filesystem::absolute(auto_apms_util::trimWhitespaces(argv[1]));
47 core::NodeManifest node_manifest;
48 for (int i = 2; i < argc; ++i) {
50 }
51
52 // Make sure path is not empty
53 if (tree_file_path.empty()) {
54 throw std::runtime_error("Argument tree_file_path is empty.");
55 }
56
57 // Require correct extensions
58 if (tree_file_path.extension() != ".xml") {
59 throw std::runtime_error("Output file '" + tree_file_path.string() + "' has wrong extension. Must be '.xml'.");
60 }
61
62 // Make sure that there is no content inside the file
63 if (std::filesystem::exists(tree_file_path)) {
64 if (!auto_apms_util::isFileEmpty(tree_file_path.string())) {
65 throw std::runtime_error("Output file '" + tree_file_path.string() + "' is not empty.");
66 }
67 }
68
69 // Prepare document
71 doc.registerNodes(node_manifest);
72 core::TreeBuilder::TreeElement tree = doc.newTree(NEW_TREE_NAME).makeRoot();
73
74 // Insert children
75 tree.insertNode<model::AlwaysSuccess>();
76
77 // Add node model
78 if (!node_manifest.empty()) {
79 doc.addNodeModel(false);
80 }
81
82 // Write tree
83 std::ofstream out_stream(tree_file_path);
84 if (out_stream.is_open()) {
85 out_stream << doc.writeToString();
86 out_stream.close();
87 } else {
88 throw std::runtime_error("Error opening behavior tree output file '" + tree_file_path.string() + "'.");
89 }
90
91 std::cout << "Wrote behavior tree to file " << tree_file_path << std::endl;
92
93 return EXIT_SUCCESS;
94}
Data structure for information about which behavior tree node plugin to load and how to configure the...
bool empty() const
Determine whether any node registration options have been added to the manifest.
NodeManifest & merge(const NodeManifest &other, bool replace=false)
Merges another NodeManifest with this one.
static NodeManifest fromResourceIdentity(const std::string &identity)
Create a node manifest from an installed resource.
NodeElement insertNode(const std::string &name, const NodeElement *before_this=nullptr)
Add a new node to the children of this node.
Handle for a single behavior tree of a TreeDocument.
TreeElement & makeRoot()
Set this behavior tree as the root tree of the parent document.
This class offers a programmatic approach for building behavior trees and stores the registration dat...
TreeDocument & addNodeModel(bool include_native=false)
Add an behavior tree node model element to the document.
virtual TreeDocument & registerNodes(const NodeManifest &tree_node_manifest, bool override=false)
Load behavior tree node plugins and register them with the internal behavior tree factory.
std::string writeToString() const
Write the XML of this tree document to a string.
TreeElement newTree(const std::string &tree_name)
Create a new behavior tree inside this document.
std::string trimWhitespaces(const std::string &str)
Trim whitespaces from both ends of a string.
Definition string.cpp:84
bool isFileEmpty(const std::string &path)
Determine if a file is empty.
Useful tooling for incorporating behavior trees for task development.
Definition builder.hpp:30