StateUtil.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::Statechart
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 #pragma once
25 
28 
29 #include "StateController.h"
30 
31 namespace armarx
32 {
33  class StatechartContext;
35 
36  class ChannelRef;
38 
39  /**
40  * \class StateUtility
41  * \ingroup StatechartGrp
42  * This class provides utility functions for statechart-implementing-users to communicate
43  * with other distributed components like observers. These functions should only be used
44  * in the onEnter(), onBreak() and onExit() functions.
45  */
46  class StateUtility : virtual public StateController
47  {
48  std::string contextString;
49 
50  bool checkEventIsHandled(const EventPtr& event) const;
51 
52  public:
53  //! Struct for the return value of setCounter/TimerEvent
55  {
56  ChannelRefBasePtr actionId;
57  ConditionIdentifier conditionId;
58  };
59 
60  //! Utility function to create a new Event
61  template <class EventClass>
62  EventPtr
64  {
65  EventPtr event = new EventClass(this->stateName);
66  return event;
67  }
68 
69  //! Utility function to create a new Event
71  const std::string& eventName,
72  const StringVariantContainerBaseMap& properties = StringVariantContainerBaseMap());
73  /*! \brief Utility function to install a condition on the distributed conditionhandler
74  @param condition Smartpointer to the condition that should be installed on the conditionhandler
75  @param evt The event that the statecharts wants to receive if the condition is fulfilled
76  */
77 
78  protected:
79  ConditionIdentifier
80  installCondition(const Term& condition, const EventPtr evt, const std::string& desc = "");
81 
82  /*! \brief Utility function to install a condition on the distributed conditionhandler
83  @param condition Smartpointer to the condition that should be installed on the conditionhandler
84  @param Eventclass The eventtype that the statecharts wants to receive if the condition is fulfilled
85  */
86  template <class Eventclass>
87  ConditionIdentifier
88  installCondition(const Term& condition, const std::string& desc = "")
89  {
90  return installCondition(condition, createEvent<Eventclass>(), desc);
91  }
92 
93  /*! \brief Utility function to remove an installed condition on the distributed conditionhandler
94  @param conditionId The ConditionIdentifier that was returned from installCondition
95  */
96  void removeCondition(ConditionIdentifier conditionId);
97  /*! \brief Utility function to start a timer on the systemObserver and register an event on the conditionHandler.
98  The condition is automatically removed, when the state is left.
99  @param Eventclass Eventtype that the state wants to receive
100  @param timeoutDurationMs Duration until the event should be send back
101  @return Identifier for this timeoutevent to be able to remove/reset the condition/timer
102  */
103  ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs, const EventPtr& evt);
104 
105  template <class Eventclass>
106  ActionEventIdentifier
107  setTimeoutEvent(int timeoutDurationMs)
108  {
109  return setTimeoutEvent(timeoutDurationMs, createEvent<Eventclass>());
110  }
111 
112  ActionEventIdentifier
113  setCounterEvent(int counterThreshold, const EventPtr& evt, int initialValue = 0);
114 
115  template <class Eventclass>
116  ActionEventIdentifier
117  setCounterEvent(int counterThreshold, int initialValue = 0)
118  {
119  return setCounterEvent(counterThreshold, createEvent<Eventclass>(), initialValue);
120  }
121 
122  void removeTimeoutEvent(const ActionEventIdentifier& id);
123  void removeCounterEvent(const ActionEventIdentifier& id);
124 
125  /*! \brief Function to send an event to a specific state from an onEnter()-function. Must not be called anywhere else.
126  The event is processed after the onEnter()-function of this and all initial substates have returned.<br/>
127  Use of this function should be avoided since this statemachine is blocked until this event and all subsequent sendEvents() are processed.
128  @param event Event that is to be sent
129  @param eventProcessor Pointer to the state, that should process this event. If set to NULL, the event it sent to the parent state.
130  If you want to leave the current state with this event, set this parameter to NULL.
131  */
132  void sendEvent(const EventPtr event, StateBasePtr eventProcessor = nullptr);
133 
134  template <class Event>
135  void
136  sendEvent(StateBasePtr eventProcessor = nullptr)
137  {
138  sendEvent(createEvent<Event>(), eventProcessor);
139  }
140 
141  // //! Function for identifying this state
142  // std::string getScope() const;
143  // //! same function as getScope(), only shorter
144  // std::string s() const
145  // {
146  // return getScope();
147  // }
148 
149  //void startWorkerThread();
150 
151 
152  private:
153  std::vector<ConditionIdentifier> __installedConditionIdentifiers;
154  std::vector<ChannelRefBasePtr> __installedTimerChannelRefs;
155  std::vector<ChannelRefBasePtr> __installedCounterChannelRefs;
156 
157  void _removeInstalledConditions() override;
158  };
159 
160 } // namespace armarx
armarx::ChannelRefPtr
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition: ChannelRef.h:40
armarx::StateUtility::removeCondition
void removeCondition(ConditionIdentifier conditionId)
Utility function to remove an installed condition on the distributed conditionhandler.
Definition: StateUtil.cpp:148
armarx::StateController
Definition: StateController.h:55
armarx::StateUtility::sendEvent
void sendEvent(StateBasePtr eventProcessor=nullptr)
Definition: StateUtil.h:136
armarx::Term
Definition: Term.h:111
armarx::StateUtility::setCounterEvent
ActionEventIdentifier setCounterEvent(int counterThreshold, int initialValue=0)
Definition: StateUtil.h:117
armarx::StateUtility::setCounterEvent
ActionEventIdentifier setCounterEvent(int counterThreshold, const EventPtr &evt, int initialValue=0)
Definition: StateUtil.cpp:216
Term.h
armarx::StateUtility::ActionEventIdentifier::actionId
ChannelRefBasePtr actionId
Definition: StateUtil.h:56
IceInternal::Handle
Definition: forward_declarations.h:8
StateController.h
armarx::StateUtility::ActionEventIdentifier
Struct for the return value of setCounter/TimerEvent.
Definition: StateUtil.h:54
armarx::StateUtility::setTimeoutEvent
ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs, const EventPtr &evt)
Utility function to start a timer on the systemObserver and register an event on the conditionHandler...
Definition: StateUtil.cpp:162
Event.h
armarx::VariantType::ChannelRef
const VariantTypeId ChannelRef
Definition: ChannelRef.h:169
armarx::StateUtility::installCondition
ConditionIdentifier installCondition(const Term &condition, const EventPtr evt, const std::string &desc="")
Utility function to install a condition on the distributed conditionhandler.
Definition: StateUtil.cpp:102
armarx::StateUtility::installCondition
ConditionIdentifier installCondition(const Term &condition, const std::string &desc="")
Utility function to install a condition on the distributed conditionhandler.
Definition: StateUtil.h:88
armarx::StateUtility::setTimeoutEvent
ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs)
Definition: StateUtil.h:107
armarx::StateUtility
Definition: StateUtil.h:46
armarx::StateUtility::ActionEventIdentifier::conditionId
ConditionIdentifier conditionId
Definition: StateUtil.h:57
armarx::StateUtility::removeCounterEvent
void removeCounterEvent(const ActionEventIdentifier &id)
Definition: StateUtil.cpp:257
armarx::StateUtility::sendEvent
void sendEvent(const EventPtr event, StateBasePtr eventProcessor=nullptr)
Function to send an event to a specific state from an onEnter()-function. Must not be called anywhere...
Definition: StateUtil.cpp:40
armarx::StateUtility::createEvent
EventPtr createEvent()
Utility function to create a new Event.
Definition: StateUtil.h:63
armarx::StateUtility::removeTimeoutEvent
void removeTimeoutEvent(const ActionEventIdentifier &id)
Definition: StateUtil.cpp:203
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::StatechartContextPtr
IceInternal::Handle< StatechartContext > StatechartContextPtr
Definition: StatechartContext.h:206