Fawkes API Fawkes Development Version
print_action_executor.cpp
1/***************************************************************************
2 * print_action_executor.cpp - A simple action executor for printing
3 *
4 * Created: Tue 05 Nov 2019 14:38:48 CET 14:38
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 "print_action_executor.h"
22
23#include "utils.h"
24
25#include <logging/logger.h>
26
27namespace fawkes {
28namespace gpp {
29/** @class PrintActionExecutor
30 * A Golog++ action executor that just prints a message.
31 * @author Till Hofmann
32 */
33
34/** Constructor.
35 * Initialize the executor to print with the given logger.
36 * @param logger The logger to send messages to
37 */
39{
40}
41
42/** Destructor. */
44{
45}
46
47bool
48PrintActionExecutor::can_execute_activity(std::shared_ptr<gologpp::Activity> activity) const
49{
50 return activity->mapped_name() == "print";
51}
52
53void
54PrintActionExecutor::start(std::shared_ptr<gologpp::Activity> activity)
55{
56 if (!can_execute_activity(activity)) {
57 throw Exception("Cannot execute activity '%s' with PrintActionExecutor",
58 activity->mapped_name().c_str());
59 }
60 activity->update(gologpp::Transition::Hook::START);
61 std::map<std::string, Logger::LogLevel> log_levels = {{"debug", Logger::LL_DEBUG},
62 {"info", Logger::LL_INFO},
63 {"warn", Logger::LL_WARN},
64 {"error", Logger::LL_ERROR},
65 {"none", Logger::LL_NONE}};
67 if (activity->target()->mapping().arg_mapping().count("level") > 0) {
68 std::string level_str = static_cast<std::string>(activity->mapped_arg_value("level"));
69 if (log_levels.count(level_str) > 0) {
70 log_level = log_levels[level_str];
71 }
72 }
73
74 logger_->log(log_level,
75 "Golog++",
76 "%s",
77 static_cast<std::string>(activity->mapped_arg_value("message")).c_str());
78 activity->update(gologpp::Transition::Hook::FINISH);
79}
80
81void
82PrintActionExecutor::stop(std::shared_ptr<gologpp::Grounding<gologpp::Action>> activity)
83{
84 logger_->log_error("PrintActionExecutor", "Cannot stop printing a message!");
85}
86} // namespace gpp
87} // namespace fawkes
Base class for exceptions in Fawkes.
Definition: exception.h:36
Interface for logging.
Definition: logger.h:42
virtual void log(LogLevel level, const char *component, const char *format,...)
Log message of given log level.
Definition: logger.cpp:326
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
LogLevel
Log level.
Definition: logger.h:51
@ LL_INFO
informational output about normal procedures
Definition: logger.h:53
@ LL_WARN
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:54
@ LL_NONE
use this to disable log output
Definition: logger.h:60
@ LL_ERROR
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:57
@ LL_DEBUG
debug output, relevant only when tracking down problems
Definition: logger.h:52
Abstract class to execute a Golog++ activity.
Logger * logger_
The logger to use for logging messages.
PrintActionExecutor(Logger *logger)
Constructor.
void start(std::shared_ptr< gologpp::Activity > activity) override
Start the given activity.
void stop(std::shared_ptr< gologpp::Grounding< gologpp::Action > > activity) override
Stop the given activity.
bool can_execute_activity(std::shared_ptr< gologpp::Activity > activity) const override
Determine if this executor can execute the given activity.
Fawkes library namespace.