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
26namespace fawkes {
27namespace 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 */
42std::shared_ptr<ActionExecutor>
43ActionExecutorDispatcher::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 */
56void
57ActionExecutorDispatcher::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. */
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 */
89void
91{
92 gologpp_dispatcher = dispatcher;
93}
94
95/** Finalize the GologppDispatcherAspect.
96 */
97void
99{
100}
101
102} // namespace fawkes
void add_aspect(const char *name)
Add an aspect to a thread.
Definition: aspect.cpp:49
Base class for exceptions in Fawkes.
Definition: exception.h:36
void init_GologppDispatcherAspect(gpp::ActionExecutorDispatcher *dispatcher)
Init GologppDispatcherAspect.
void finalize_GologppDispatcherAspect()
Finalize the GologppDispatcherAspect.
gpp::ActionExecutorDispatcher * gologpp_dispatcher
A pointer to the dispatcher that the aspect provides.
Dispatch an activity to a number of registered executors by checking all registered executors subsequ...
std::shared_ptr< ActionExecutor > get_executor(std::shared_ptr< gologpp::Activity >)
Determine the executor for a given activity.
void register_executor(std::shared_ptr< ActionExecutor > executor)
Register a new executor.
Fawkes library namespace.