Fawkes API  Fawkes Development Version
action_executor_dispatcher.cpp
1 /***************************************************************************
2  * action_executor_dispatcher.cpp - Dispatch a Golog++ activity to executors
3  *
4  * Created: Thu 03 Oct 2019 11:05:01 CEST 11:05
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 "action_executor_dispatcher.h"
22 
23 #include <core/exception.h>
24 #include <golog++/model/activity.h>
25 
26 namespace fawkes {
27 namespace gpp {
28 
29 /** @class ActionExecutorDispatcher
30  * Dispatch an activity to a number of registered executors by checking all
31  * registered executors subsequently, whether they can execute the given
32  * activity. The first suitable executor is used to execute the activity.
33  * @author Till Hofmann
34  */
35 
36 /** Determine the executor for a given activity.
37  * Check all registered executors if any of them can execute the given activity.
38  * @param activity The activity to execute.
39  * @return The executor that can execute the activity.
40  * @throws Exception If no suitable executor for the given activity exists.
41  */
42 std::shared_ptr<ActionExecutor>
43 ActionExecutorDispatcher::get_executor(std::shared_ptr<gologpp::Activity> activity)
44 {
45  for (auto &executor : action_executors_) {
46  if (executor->can_execute_activity(activity)) {
47  return executor;
48  }
49  }
50  throw Exception(std::string("No known executor for " + activity->mapped_name()).c_str());
51 }
52 
53 /** Register a new executor.
54  * @param executor The new executor
55  */
56 void
57 ActionExecutorDispatcher::register_executor(std::shared_ptr<ActionExecutor> executor)
58 {
59  action_executors_.push_back(executor);
60 }
61 
62 } // namespace gpp
63 
64 /** @class GologppDispatcherAspect
65  * An aspect that provides access to the Golog++ Action Executor Dispatcher.
66  * Use this if you implement an executor for Golog++. Your action executor
67  * should register itself by calling
68  * gpp::ActionExecutorDispatcher::register_executor().
69  * @author Till Hofmann
70  * @see gpp::ActionExecutorDispatcher
71  * @see gpp::ActionExecutor
72  */
73 
74 /** @var GologppDispatcherAspect::gologpp_dispatcher
75  * A pointer to the dispatcher that the aspect provides.
76  * Use this dispatcher to register your executor.
77  */
78 
79 /** Constructor. */
80 GologppDispatcherAspect::GologppDispatcherAspect() : gologpp_dispatcher(nullptr)
81 {
82  add_aspect("GologppDispatcherAspect");
83 }
84 
85 /** Init GologppDispatcherAspect.
86  * Initialize the aspect with the given dispatcher instance.
87  * @param dispatcher The dispatcher to use
88  */
89 void
91 {
92  gologpp_dispatcher = dispatcher;
93 }
94 
95 /** Finalize the GologppDispatcherAspect.
96  */
97 void
99 {
100 }
101 
102 } // namespace fawkes
fawkes::Aspect::add_aspect
void add_aspect(const char *name)
Add an aspect to a thread.
Definition: aspect.cpp:49
fawkes::GologppDispatcherAspect::init_GologppDispatcherAspect
void init_GologppDispatcherAspect(gpp::ActionExecutorDispatcher *dispatcher)
Init GologppDispatcherAspect.
Definition: action_executor_dispatcher.cpp:90
fawkes::GologppDispatcherAspect::GologppDispatcherAspect
GologppDispatcherAspect()
Constructor.
Definition: action_executor_dispatcher.cpp:80
fawkes::gpp::ActionExecutorDispatcher
Dispatch an activity to a number of registered executors by checking all registered executors subsequ...
Definition: action_executor_dispatcher.h:32
fawkes::GologppDispatcherAspect::gologpp_dispatcher
gpp::ActionExecutorDispatcher * gologpp_dispatcher
A pointer to the dispatcher that the aspect provides.
Definition: action_executor_dispatcher.h:51
fawkes::gpp::ActionExecutorDispatcher::get_executor
std::shared_ptr< ActionExecutor > get_executor(std::shared_ptr< gologpp::Activity >)
Determine the executor for a given activity.
Definition: action_executor_dispatcher.cpp:43
fawkes::GologppDispatcherAspect::finalize_GologppDispatcherAspect
void finalize_GologppDispatcherAspect()
Finalize the GologppDispatcherAspect.
Definition: action_executor_dispatcher.cpp:98
fawkes
Fawkes library namespace.
fawkes::gpp::ActionExecutorDispatcher::register_executor
void register_executor(std::shared_ptr< ActionExecutor > executor)
Register a new executor.
Definition: action_executor_dispatcher.cpp:57
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36