Fawkes API Fawkes Development Version
exog_manager.h
1/***************************************************************************
2 * exog_manager.h - Insert exog actions into Golog++
3 *
4 * Created: Mon 26 Aug 2019 CEST 15:38
5 * Copyright 2019 Victor Mataré <matare@fh-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#ifndef FAWKES_GOLOGPP_BLACKBOARD_H_
22#define FAWKES_GOLOGPP_BLACKBOARD_H_
23
24#include <blackboard/blackboard.h>
25#include <blackboard/interface_listener.h>
26#include <blackboard/interface_observer.h>
27#include <config/config.h>
28#include <core/threading/thread.h>
29#include <golog++/model/action.h>
30#include <golog++/model/execution.h>
31#include <logging/logger.h>
32
33namespace gologpp {
34class Type;
35}
36
37namespace fawkes {
38namespace gpp {
39
40class GologppThread;
41
42///////////////////////////////////////////////////////////////////////////////
43class ConfigError : public Exception
44{
45public:
46 ConfigError(const std::string &);
47};
48
49///////////////////////////////////////////////////////////////////////////////
51{
52public:
53 ExogManager(GologppThread *exec_thread,
55 const std::string &cfg_prefix,
56 BlackBoard *,
57 Logger *);
58
59 const char *name();
60
61private:
62 void exog_queue_push(gologpp::shared_ptr<gologpp::ExogEvent>);
63 gologpp::shared_ptr<gologpp::ExogAction> find_mapped_exog(const std::string &mapped_name);
64
65 ///////////////////////////////////////////////////////////////////
66 class BlackboardEventHandler
67 {
68 public:
69 BlackboardEventHandler(BlackBoard * bb,
70 gologpp::shared_ptr<gologpp::ExogAction> exog,
71 ExogManager & exog_mgr);
72 BlackboardEventHandler(const BlackboardEventHandler &) = delete;
73 BlackboardEventHandler &operator=(const BlackboardEventHandler &) = delete;
74 BlackboardEventHandler(BlackboardEventHandler &&) = default;
75
76 gologpp::shared_ptr<gologpp::ExogEvent> make_exog_event(Interface *) const;
77
78 static std::string extract_type_name(const std::string &iface_uid);
79 static std::string extract_id(const std::string &iface_uid);
80
81 protected:
82 BlackBoard * blackboard_;
83 gologpp::shared_ptr<gologpp::ExogAction> target_exog_;
84 std::unordered_map<std::string, gologpp::arity_t> fields_order_;
85 ExogManager & exog_manager_;
86 };
87
88 ///////////////////////////////////////////////////////////////////
89 class InterfaceWatcher : public BlackboardEventHandler, public BlackBoardInterfaceListener
90 {
91 public:
92 InterfaceWatcher(BlackBoard *,
93 const std::string &id,
94 gologpp::shared_ptr<gologpp::ExogAction>,
95 ExogManager &exog_mgr);
96 virtual ~InterfaceWatcher() override;
97
98 virtual void bb_interface_data_refreshed(Interface *) noexcept override;
99
100 private:
101 Interface *iface_;
102 };
103
104 //////////////////////////////////////////////////////////////////
105 class PatternObserver : public BlackboardEventHandler, public BlackBoardInterfaceObserver
106 {
107 public:
108 PatternObserver(BlackBoard *,
109 const std::string &pattern,
110 gologpp::shared_ptr<gologpp::ExogAction>,
111 ExogManager &exog_mgr);
112 virtual ~PatternObserver() override;
113
114 virtual void bb_interface_created(const char *type, const char *id) noexcept override;
115
116 private:
117 std::string pattern_;
118 std::mutex handler_mutex_;
119 };
120
121 //////////////////////////////////////////////////////////////////
122 std::unordered_map<std::string, gologpp::shared_ptr<gologpp::ExogAction>> mapped_exogs_;
123 std::vector<std::unique_ptr<InterfaceWatcher>> watchers_;
124 std::vector<std::unique_ptr<PatternObserver>> observers_;
125 GologppThread * exec_thread_;
126 Configuration * config_;
127 BlackBoard * blackboard_;
128 Logger * logger_;
129
130 static const std::unordered_map<interface_fieldtype_t, std::string> iface_type_to_golog_type_;
131};
132
133} // namespace gpp
134} // namespace fawkes
135
136#endif
BlackBoard interface listener.
BlackBoard interface observer.
The BlackBoard abstract class.
Definition: blackboard.h:46
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
Interface for logging.
Definition: logger.h:42
Thrown if the config is somehow inconsistent with the agent program.
Definition: exog_manager.h:44
ConfigError(const std::string &)
Construct a ConfigError.
Watch/observe blackboard interfaces according to the mappings specified for exogenous actions in the ...
Definition: exog_manager.h:51
const char * name()
Get the ExogManager's thread name.
ExogManager(GologppThread *exec_thread, Configuration *, const std::string &cfg_prefix, BlackBoard *, Logger *)
Constructor.
Main golog++ thread that handles program execution, i.e.
Fawkes library namespace.