Prompt Tools 0.3.2
Loading...
Searching...
No Matches
prompt_options.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <exception>
5#include <rclcpp/rclcpp.hpp>
6#include <string>
7#include <vector>
8
9namespace prompt
10{
11
23std::vector<prompt::PromptOption> load_from_parameters(rclcpp::Node::SharedPtr node, std::string plugin_name)
24{
25 // init model options
26 std::vector<std::string> model_option_keys;
27
28 // declare option_keys only if not already declared
29 if (node->has_parameter(plugin_name + ".option_keys"))
30 {
31 model_option_keys = node->get_parameter(plugin_name + ".option_keys").as_string_array();
32 }
33 else
34 {
35 model_option_keys =
36 node->declare_parameter(plugin_name + ".option_keys", rclcpp::ParameterValue(std::vector<std::string>{}))
37 .get<std::vector<std::string>>();
38 }
39
40 std::vector<prompt::PromptOption> options_;
41
42 for (const auto& key : model_option_keys)
43 {
45 opt.key = key;
46
47 std::string type_param_name = plugin_name + ".options." + key + ".type";
48 if (node->has_parameter(type_param_name))
49 {
50 opt.type = node->get_parameter(type_param_name).as_string();
51 }
52 else
53 {
54 opt.type = node->declare_parameter(type_param_name, rclcpp::ParameterValue("")).get<std::string>();
55 }
56
57 // check if value is of type string
58 if (opt.type == "string")
59 {
60 std::string value_param_name = plugin_name + ".options." + key + ".value";
61 if (node->has_parameter(value_param_name))
62 {
63 opt.value = node->get_parameter(value_param_name).as_string();
64 }
65 else
66 {
67 opt.value = node->declare_parameter(value_param_name, rclcpp::ParameterValue("")).get<std::string>();
68 }
69 }
70
71 // check if value is of type bool
72 else if (opt.type == "bool")
73 {
74 std::string value_param_name = plugin_name + ".options." + key + ".value";
75 bool value;
76 if (node->has_parameter(value_param_name))
77 {
78 value = node->get_parameter(value_param_name).as_bool();
79 }
80 else
81 {
82 value = node->declare_parameter(value_param_name, rclcpp::ParameterValue(false)).get<bool>();
83 }
84 opt.value = (value) ? "true" : "false";
85 }
86
87 // check if value is of type bool
88 else if (opt.type == "int")
89 {
90 std::string value_param_name = plugin_name + ".options." + key + ".value";
91 int value;
92 if (node->has_parameter(value_param_name))
93 {
94 value = node->get_parameter(value_param_name).as_int();
95 }
96 else
97 {
98 value = node->declare_parameter(value_param_name, rclcpp::ParameterValue(0)).get<int>();
99 }
100 opt.value = std::to_string(value);
101 }
102
103 // check if value is of type bool
104 else if (opt.type == "double")
105 {
106 std::string value_param_name = plugin_name + ".options." + key + ".value";
107 double value;
108 if (node->has_parameter(value_param_name))
109 {
110 value = node->get_parameter(value_param_name).as_double();
111 }
112 else
113 {
114 value = node->declare_parameter(value_param_name, rclcpp::ParameterValue(0)).get<double>();
115 }
116 opt.value = std::to_string(value);
117 }
118
119 // check if type of value is not mentioned and take it as a string
120 else
121 {
122 std::string value_param_name = plugin_name + ".options." + key + ".value";
123 if (node->has_parameter(value_param_name))
124 {
125 opt.value = node->get_parameter(value_param_name).as_string();
126 }
127 else
128 {
129 opt.value = node->declare_parameter(value_param_name, rclcpp::ParameterValue("")).get<std::string>();
130 }
131 }
132
133 options_.push_back(opt);
134 }
135
136 return options_;
137}
138
149void ensure_model_options(rclcpp::Node::SharedPtr node, std::vector<prompt::PromptOption>& options,
150 std::vector<prompt::PromptOption>& required_options)
151{
152 // check required options are available and if not add them
153 for (const auto& required_opt : required_options)
154 {
155 bool found = false;
156 for (const auto& opt : options)
157 {
158 if (opt.key == required_opt.key)
159 {
160 found = true;
161 break;
162 }
163 }
164
165 if (!found)
166 {
167 RCLCPP_WARN(node->get_logger(), "Model option '%s' not found, Adding '%s'", required_opt.key.c_str(),
168 required_opt.value.c_str());
169 options.push_back(required_opt);
170 }
171 }
172}
173} // namespace prompt
Definition base_class.hpp:8
std::vector< prompt::PromptOption > load_from_parameters(rclcpp::Node::SharedPtr node, std::string plugin_name)
Load prompt options from ROS2 parameters.
Definition prompt_options.hpp:23
void ensure_model_options(rclcpp::Node::SharedPtr node, std::vector< prompt::PromptOption > &options, std::vector< prompt::PromptOption > &required_options)
Check and add required model options to the prompt options.
Definition prompt_options.hpp:149
Definition structs.hpp:9
std::string type
Definition structs.hpp:12
std::string value
Definition structs.hpp:11
std::string key
Definition structs.hpp:10