|
| 1 | +// Copyright 2024 Intelligent Robotics Lab - Gentlebots |
| 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 | +// http://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 "hri/dialog/get_guest_info.hpp" |
| 16 | +#include <regex> |
| 17 | + |
| 18 | +using std::placeholders::_1; |
| 19 | +using namespace std::chrono_literals; |
| 20 | + |
| 21 | + |
| 22 | +namespace dialog |
| 23 | +{ |
| 24 | + |
| 25 | +GetGuestInfo::GetGuestInfo( |
| 26 | + const std::string & xml_tag_name, const std::string & srv_name, |
| 27 | + const BT::NodeConfiguration & conf) |
| 28 | +: hri::BtServiceNode< |
| 29 | + kb_msgs::srv::Query, |
| 30 | + rclcpp_cascade_lifecycle::CascadeLifecycleNode>(xml_tag_name, srv_name, conf) |
| 31 | +{ |
| 32 | + config().blackboard->get("node", node_); |
| 33 | + this->kb_publisher_= node_->create_publisher<std_msgs::msg::String>("/kb/remove_fact", 10); |
| 34 | +} |
| 35 | + |
| 36 | +void GetGuestInfo::on_tick() |
| 37 | +{ |
| 38 | + RCLCPP_DEBUG(node_->get_logger(), "[GetGuestInfo] ticked"); |
| 39 | + rclcpp::spin_some(node_->get_node_base_interface()); |
| 40 | + |
| 41 | + // Patterns |
| 42 | + request_->patterns.push_back("robot1 oro:attends ?guest"); |
| 43 | + request_->patterns.push_back("?guest oro:hasName ?name"); |
| 44 | + request_->patterns.push_back("?guest oro:hasFavoriteDrink ?drink"); |
| 45 | + request_->patterns.push_back("?guest oro:description ?desc"); |
| 46 | + |
| 47 | + // Vars |
| 48 | + request_->vars.push_back("?guest"); |
| 49 | + request_->vars.push_back("?name"); |
| 50 | + request_->vars.push_back("?drink"); |
| 51 | + request_->vars.push_back("?desc"); |
| 52 | + |
| 53 | + |
| 54 | + setStatus(BT::NodeStatus::SUCCESS); |
| 55 | +} |
| 56 | + |
| 57 | +void GetGuestInfo::on_result() |
| 58 | +{ |
| 59 | + RCLCPP_DEBUG(node_->get_logger(), "[GetGuestInfo] result received"); |
| 60 | + |
| 61 | + if (!result_.error_msg.empty()) { |
| 62 | + RCLCPP_ERROR(node_->get_logger(), "[GetGuestInfo] error"); |
| 63 | + setStatus(BT::NodeStatus::FAILURE); |
| 64 | + } |
| 65 | + |
| 66 | + std::regex guest_regex("\"guest\"\\s*:\\s*\"([^\"]*)\""); |
| 67 | + std::regex name_regex("\"name\"\\s*:\\s*\"([^\"]*)\""); |
| 68 | + std::regex drink_regex("\"drink\"\\s*:\\s*\"([^\"]*)\""); |
| 69 | + std::regex desc_regex("\"desc\"\\s*:\\s*\"([^\"]*)\""); |
| 70 | + |
| 71 | + std::smatch match; |
| 72 | + |
| 73 | + if (std::regex_search(result_.json, match, guest_regex)) { |
| 74 | + guest_id_ = match[1]; |
| 75 | + } |
| 76 | + |
| 77 | + if (std::regex_search(result_.json, match, name_regex)) { |
| 78 | + guest_name_ = match[1]; |
| 79 | + } |
| 80 | + |
| 81 | + if (std::regex_search(result_.json, match, drink_regex)) { |
| 82 | + guest_drink_ = match[1]; |
| 83 | + } |
| 84 | + |
| 85 | + if (std::regex_search(result_.json, match, desc_regex)) { |
| 86 | + guest_description_ = match[1]; |
| 87 | + } |
| 88 | + |
| 89 | + setOutput("guest_name", guest_name_); |
| 90 | + setOutput("guest_drink", guest_drink_); |
| 91 | + setOutput("guest_description", guest_description_); |
| 92 | + |
| 93 | + RCLCPP_INFO( |
| 94 | + node_->get_logger(), "[GetGuestInfo] Guest info retrieved: Name: %s, Drink: %s, Description: %s", |
| 95 | + guest_name_.c_str(), guest_drink_.c_str(), guest_description_.c_str()); |
| 96 | + |
| 97 | + std_msgs::msg::String fact_msg; |
| 98 | + |
| 99 | + // Delete attending fact |
| 100 | + fact_msg.data = "robot1 oro:attends " + guest_id_; |
| 101 | + kb_publisher_->publish(fact_msg); |
| 102 | + |
| 103 | + setStatus(BT::NodeStatus::SUCCESS); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +} // namespace hri |
| 111 | + |
| 112 | +#include "behaviortree_cpp_v3/bt_factory.h" |
| 113 | +BT_REGISTER_NODES(factory) |
| 114 | +{ |
| 115 | + BT::NodeBuilder builder = [](const std::string & name, const BT::NodeConfiguration & config) { |
| 116 | + return std::make_unique<dialog::GetGuestInfo>( |
| 117 | + name, "/kb/query", config); |
| 118 | + }; |
| 119 | + |
| 120 | + factory.registerBuilder<dialog::GetGuestInfo>("GetGuestInfo", builder); |
| 121 | +} |
| 122 | + |
0 commit comments