Fawkes API Fawkes Development Version
message_action_executor.cpp
1/***************************************************************************
2 * message_action_executor.cpp - Action executor for blackboard messages
3 *
4 * Created: Wed 30 Oct 2019 13:06:51 CET 13:06
5 * Copyright 2019 Till Hofmann <hofmann@kbsg.rwth-aachen.de>
6 ****************************************************************************/
7
8/* This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
17 *
18 * Read the full text in the LICENSE.GPL file in the doc directory.
19 */
20
21#include "message_action_executor.h"
22
23#include "utils.h"
24
25#include <blackboard/blackboard.h>
26#include <config/config.h>
27#include <golog++/model/activity.h>
28#include <logging/logger.h>
29
30#include <algorithm>
31
32namespace fawkes {
33namespace gpp {
34
35/** @class BBMessageActionExecutor
36 * A Golog++ action executor that sends a message to a blackboard interface.
37 * @author Till Hofmann
38 */
39
40/** Constructor.
41 * Initializes the executor with all the action mappings specified in the config.
42 * @param logger A logger to use for logging messages
43 * @param blackboard The blackboard to use to send messages to
44 * @param config The configuration to read the mapping from
45 * @param cfg_prefix The config prefix to use for the message mapping
46 */
48 BlackBoard * blackboard,
49 Configuration * config,
50 const std::string &cfg_prefix)
51: ActionExecutor(logger), blackboard_(blackboard), config_(config), cfg_prefix_(cfg_prefix)
52{
53}
54
55/** Destructor.
56 * Clean up and close all interfaces.
57 */
59{
60 for (auto interface : open_interfaces_) {
61 blackboard_->close(interface.second);
62 }
63}
64
65bool
66BBMessageActionExecutor::can_execute_activity(std::shared_ptr<gologpp::Activity> activity) const
67{
68 return activity->mapped_name() == "send_message";
69}
70
71void
72BBMessageActionExecutor::start(std::shared_ptr<gologpp::Activity> activity)
73{
74 if (!can_execute_activity(activity)) {
75 throw Exception("Cannot execute activity '%s' with BBMessageActionExecutor",
76 activity->mapped_name().c_str());
77 }
78 activity->update(gologpp::Transition::Hook::START);
79 std::string interface_type =
80 static_cast<std::string>(activity->mapped_arg_value("interface_type"));
81 std::string interface_id = static_cast<std::string>(activity->mapped_arg_value("interface_id"));
82 std::string message_type = static_cast<std::string>(activity->mapped_arg_value("message_type"));
83 if (open_interfaces_.find(interface_id) == open_interfaces_.end()) {
84 open_interfaces_[interface_id] =
85 blackboard_->open_for_reading(interface_type.c_str(), interface_id.c_str());
86 }
87 Interface *interface = open_interfaces_[interface_id];
88 auto msg = interface->create_message(message_type.c_str());
89
90 for (auto field = msg->fields(); field != msg->fields_end(); field++) {
91 if (activity->target()->mapping().is_mapped(field.get_name())) {
92 auto value = activity->mapped_arg_value(field.get_name());
93 try {
94 value_to_field(value, &field);
95 } catch (boost::bad_get &e) {
96 logger_->log_error("BBMessageActionExecutor",
97 "Failed to convert value '%s' of field '%s' with type '%s': '%s'",
98 value.string_representation().c_str(),
99 field.get_name(),
100 field.get_typename(),
101 e.what());
102 activity->update(gologpp::Transition::Hook::FAIL);
103 return;
104 }
105 }
106 }
107 interface->msgq_enqueue(msg);
108 activity->update(gologpp::Transition::Hook::FINISH);
109 return;
110}
111
112void
113BBMessageActionExecutor::stop(std::shared_ptr<gologpp::Grounding<gologpp::Action>> activity)
114{
115 logger_->log_error("BBMessageActionExecutor",
116 "Cannot stop a message that has already been sent!");
117}
118
119} // namespace gpp
120} // namespace fawkes
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual void close(Interface *interface)=0
Close interface.
Interface for configuration handling.
Definition: config.h:68
Base class for exceptions in Fawkes.
Definition: exception.h:36
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
InterfaceFieldIterator fields_end()
Invalid iterator.
Definition: interface.cpp:1240
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
Definition: interface.cpp:1231
Interface for logging.
Definition: logger.h:42
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
Abstract class to execute a Golog++ activity.
Logger * logger_
The logger to use for logging messages.
bool can_execute_activity(std::shared_ptr< gologpp::Activity > activity) const override
Determine if this executor can execute the given activity.
BBMessageActionExecutor(Logger *logger, BlackBoard *blackboard, Configuration *config, const std::string &cfg_prefix)
Constructor.
void stop(std::shared_ptr< gologpp::Grounding< gologpp::Action > > activity) override
Stop the given activity.
void start(std::shared_ptr< gologpp::Activity > activity) override
Start the given activity.
Fawkes library namespace.