Fawkes API  Fawkes Development Version
sleep_action_executor.cpp
1 /***************************************************************************
2  * sleep_action_executor.cpp - A simple sleep action
3  *
4  * Created: Tue 12 Nov 2019 14:09:55 CET 14:09
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 "sleep_action_executor.h"
22 
23 #include <golog++/model/activity.h>
24 #include <logging/logger.h>
25 
26 #include <chrono>
27 #include <thread>
28 
29 namespace fawkes {
30 namespace gpp {
31 
32 /** @class SleepActionExecutor
33  * A Golog++ action executor that just sleeps for a certain amount of time.
34  * The action executor sleeps asynchronously and sets the activity to finished
35  * after the given time.
36  * @author Till Hofmann
37  */
38 
39 /** Constructor.
40  * Initialize the executor.
41  * @param logger A logger to use for logging messages
42  */
44 {
45 }
46 
47 /** Destructor.
48  * Notify all running activities to cancel and wait for them before destruction.
49  */
51 {
52  wait_cond_.notify_all();
53  for (auto &fut : running_sleeps_) {
54  fut.wait();
55  }
56 }
57 
58 bool
59 SleepActionExecutor::can_execute_activity(std::shared_ptr<gologpp::Activity> activity) const
60 {
61  return activity->mapped_name() == "sleep";
62 }
63 
64 void
65 SleepActionExecutor::start(std::shared_ptr<gologpp::Activity> activity)
66 {
67  if (!can_execute_activity(activity)) {
68  throw Exception("Cannot execute activity '%s' with SleepActionExecutor",
69  activity->mapped_name().c_str());
70  }
71  activity->update(gologpp::Transition::Hook::START);
72  const std::chrono::duration sleep_duration =
73  std::chrono::seconds(static_cast<long>(activity->mapped_arg_value("seconds")));
74  running_sleeps_.push_back(std::async(std::launch::async, ([this, sleep_duration, activity] {
75  std::unique_lock<std::mutex> lock(cancel_mutex_);
76  auto status = wait_cond_.wait_for(lock, sleep_duration);
77  if (status == std::cv_status::timeout
78  && activity->state()
79  == gologpp::Activity::State::RUNNING) {
80  activity->update(gologpp::Transition::Hook::FINISH);
81  }
82  })));
83  running_sleeps_.remove_if([&](auto &fut) {
84  return fut.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready;
85  });
86 }
87 
88 void
89 SleepActionExecutor::stop(std::shared_ptr<gologpp::Grounding<gologpp::Action>> activity)
90 {
91 }
92 
93 } // namespace gpp
94 } // namespace fawkes
Base class for exceptions in Fawkes.
Definition: exception.h:36
Interface for logging.
Definition: logger.h:42
Abstract class to execute a Golog++ activity.
void start(std::shared_ptr< gologpp::Activity > activity) override
Start the given activity.
bool can_execute_activity(std::shared_ptr< gologpp::Activity > activity) const override
Determine if this executor can execute the given activity.
void stop(std::shared_ptr< gologpp::Grounding< gologpp::Action >> activity) override
Stop the given activity.
SleepActionExecutor(Logger *logger)
Constructor.
Fawkes library namespace.