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 
33 namespace gologpp {
34 class Type;
35 }
36 
37 namespace fawkes {
38 namespace gpp {
39 
40 class GologppThread;
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 class ConfigError : public Exception
44 {
45 public:
46  ConfigError(const std::string &);
47 };
48 
49 ///////////////////////////////////////////////////////////////////////////////
51 {
52 public:
53  ExogManager(GologppThread *exec_thread,
54  Configuration *,
55  const std::string &cfg_prefix,
56  BlackBoard *,
57  Logger *);
58 
59  const char *name();
60 
61 private:
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_changed(Interface *) throw() 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) throw() 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
fawkes::BlackBoardInterfaceListener
BlackBoard interface listener.
Definition: interface_listener.h:42
fawkes::BlackBoard
The BlackBoard abstract class.
Definition: blackboard.h:46
fawkes::gpp::ExogManager::name
const char * name()
Get the ExogManager's thread name.
Definition: exog_manager.cpp:113
fawkes::Configuration
Interface for configuration handling.
Definition: config.h:65
fawkes::Logger
Interface for logging.
Definition: logger.h:42
fawkes
Fawkes library namespace.
fawkes::BlackBoardInterfaceObserver
BlackBoard interface observer.
Definition: interface_observer.h:37
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
fawkes::gpp::ConfigError
Thrown if the config is somehow inconsistent with the agent program.
Definition: exog_manager.h:44
fawkes::gpp::ConfigError::ConfigError
ConfigError(const std::string &)
Construct a ConfigError.
Definition: exog_manager.cpp:66
fawkes::gpp::GologppThread
Main golog++ thread that handles program execution, i.e.
Definition: execution_thread.h:47
fawkes::gpp::ExogManager
Watch/observe blackboard interfaces according to the mappings specified for exogenous actions in the ...
Definition: exog_manager.h:51
fawkes::gpp::ExogManager::ExogManager
ExogManager(GologppThread *exec_thread, Configuration *, const std::string &cfg_prefix, BlackBoard *, Logger *)
Constructor.
Definition: exog_manager.cpp:78
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36