AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
create_node_model_for_manifest_resource.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_core/node/node_manifest.hpp"
20#include "auto_apms_behavior_tree_core/tree/tree_document.hpp"
21#include "auto_apms_util/string.hpp"
22
23using namespace auto_apms_behavior_tree;
24
25int main(int argc, char ** argv)
26{
27 if (argc < 2 || argc > 3) {
28 std::cerr << "create_node_model_for_manifest_resource: Wrong number of arguments!\n";
29 std::cerr << "Usage: create_node_model_for_manifest_resource <output_file>\n";
30 std::cerr << " create_node_model_for_manifest_resource <output_file> <node_manifest_resource_identity>\n";
31 std::cerr << "\nIf only output_file is provided, generates model for native BehaviorTree.CPP nodes.\n";
32 std::cerr << "If both arguments are provided, generates model for the specified node manifest resource.\n";
33 return EXIT_FAILURE;
34 }
35
36 try {
37 std::filesystem::path output_file;
38 std::string manifest_resource_identity;
39 bool use_native_only = false;
40
41 // First argument is always the output file
42 output_file = std::filesystem::absolute(auto_apms_util::trimWhitespaces(argv[1]));
43
44 if (argc == 2) {
45 // Single argument - just output file, use native nodes only
46 use_native_only = true;
47 } else {
48 // Two arguments - output file and manifest resource identity
49 manifest_resource_identity = auto_apms_util::trimWhitespaces(argv[2]);
50 }
51
52 // Ensure correct extension
53 if (output_file.extension() != ".xml") {
54 throw std::runtime_error("Output file '" + output_file.string() + "' has wrong extension. Must be '.xml'.");
55 }
56
57 // Create output directory if it doesn't exist
58 std::filesystem::create_directories(output_file.parent_path());
59
60 // Create TreeDocument for the node model
62
63 if (use_native_only) {
64 // Just use native nodes - no need to register anything
65 } else {
66 // Register all nodes with the document
67 doc.registerNodes(core::NodeManifest::fromResource(manifest_resource_identity));
68 }
69
70 // Get the node model and add it to the document, then write to file
71 doc.addNodeModel(doc.getNodeModel(use_native_only));
72 doc.writeToFile(output_file.string());
73
74 } catch (const std::exception & e) {
75 std::cerr << "ERROR (create_node_model_for_manifest_resource): " << e.what() << "\n";
76 return EXIT_FAILURE;
77 }
78
79 return EXIT_SUCCESS;
80}
static NodeManifest fromResource(const NodeManifestResourceIdentity &search_identity)
Create a node manifest from an installed resource.
Document Object Model (DOM) for the behavior tree XML schema. This class offers a programmatic approa...
void writeToFile(const std::string &path) const
Write the XML of this tree document to a file.
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.
TreeDocument & addNodeModel(NodeModelMap model_map)
Add a behavior tree node model element to the document by parsing the contents of model_map.
static NodeModelMap getNodeModel(tinyxml2::XMLDocument &doc, const NodeManifest &manifest)
Convert a behavior tree node model document to the corresponding data structure.
std::string trimWhitespaces(const std::string &str)
Trim whitespaces from both ends of a string.
Definition string.cpp:84
Powerful tooling for incorporating behavior trees for task development.
Definition behavior.hpp:32