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 #include "StateController.h"
29 
30 
31 namespace armarx
32 {
33  class StatechartContext;
35 
36  class ChannelRef;
38 
39 
40  /**
41  * \class StateUtility
42  * \ingroup StatechartGrp
43  * This class provides utility functions for statechart-implementing-users to communicate
44  * with other distributed components like observers. These functions should only be used
45  * in the onEnter(), onBreak() and onExit() functions.
46  */
47  class StateUtility :
48  virtual public StateController
49  {
50  std::string contextString;
51 
52  bool checkEventIsHandled(const EventPtr& event) const;
53  public:
54  //! Struct for the return value of setCounter/TimerEvent
56  {
57  ChannelRefBasePtr actionId;
58  ConditionIdentifier conditionId;
59  };
60 
61 
62  //! Utility function to create a new Event
63  template <class EventClass>
65  {
66  EventPtr event = new EventClass(this->stateName);
67  return event;
68  }
69  //! Utility function to create a new Event
70  EventPtr createEvent(const std::string& eventName, const StringVariantContainerBaseMap& properties = StringVariantContainerBaseMap());
71  /*! \brief Utility function to install a condition on the distributed conditionhandler
72  @param condition Smartpointer to the condition that should be installed on the conditionhandler
73  @param evt The event that the statecharts wants to receive if the condition is fulfilled
74  */
75 
76  protected:
77  ConditionIdentifier installCondition(const Term& condition, const EventPtr evt, const std::string& desc = "");
78  /*! \brief Utility function to install a condition on the distributed conditionhandler
79  @param condition Smartpointer to the condition that should be installed on the conditionhandler
80  @param Eventclass The eventtype that the statecharts wants to receive if the condition is fulfilled
81  */
82  template <class Eventclass>
83  ConditionIdentifier installCondition(const Term& condition, const std::string& desc = "")
84  {
85  return installCondition(condition, createEvent<Eventclass>(), desc);
86  }
87 
88  /*! \brief Utility function to remove an installed condition on the distributed conditionhandler
89  @param conditionId The ConditionIdentifier that was returned from installCondition
90  */
91  void removeCondition(ConditionIdentifier conditionId);
92  /*! \brief Utility function to start a timer on the systemObserver and register an event on the conditionHandler.
93  The condition is automatically removed, when the state is left.
94  @param Eventclass Eventtype that the state wants to receive
95  @param timeoutDurationMs Duration until the event should be send back
96  @return Identifier for this timeoutevent to be able to remove/reset the condition/timer
97  */
98  ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs, const EventPtr& evt);
99  template <class Eventclass>
100  ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs)
101  {
102  return setTimeoutEvent(timeoutDurationMs, createEvent<Eventclass>());
103  }
104  ActionEventIdentifier setCounterEvent(int counterThreshold, const EventPtr& evt, int initialValue = 0);
105  template <class Eventclass>
106  ActionEventIdentifier setCounterEvent(int counterThreshold, int initialValue = 0)
107  {
108  return setCounterEvent(counterThreshold, createEvent<Eventclass>(), initialValue);
109  }
110 
111  void removeTimeoutEvent(const ActionEventIdentifier& id);
112  void removeCounterEvent(const ActionEventIdentifier& id);
113 
114  /*! \brief Function to send an event to a specific state from an onEnter()-function. Must not be called anywhere else.
115  The event is processed after the onEnter()-function of this and all initial substates have returned.<br/>
116  Use of this function should be avoided since this statemachine is blocked until this event and all subsequent sendEvents() are processed.
117  @param event Event that is to be sent
118  @param eventProcessor Pointer to the state, that should process this event. If set to NULL, the event it sent to the parent state.
119  If you want to leave the current state with this event, set this parameter to NULL.
120  */
121  void sendEvent(const EventPtr event, StateBasePtr eventProcessor = nullptr);
122  template <class Event>
123  void sendEvent(StateBasePtr eventProcessor = nullptr)
124  {
125  sendEvent(createEvent<Event>(), eventProcessor);
126  }
127 
128  // //! Function for identifying this state
129  // std::string getScope() const;
130  // //! same function as getScope(), only shorter
131  // std::string s() const
132  // {
133  // return getScope();
134  // }
135 
136  //void startWorkerThread();
137 
138 
139  private:
140  std::vector< ConditionIdentifier > __installedConditionIdentifiers;
141  std::vector< ChannelRefBasePtr > __installedTimerChannelRefs;
142  std::vector< ChannelRefBasePtr > __installedCounterChannelRefs;
143 
144  void _removeInstalledConditions() override;
145  };
146 
147 }
148 
armarx::ChannelRefPtr
IceInternal::Handle< ChannelRef > ChannelRefPtr
Definition: ChannelRef.h:41
armarx::StateUtility::removeCondition
void removeCondition(ConditionIdentifier conditionId)
Utility function to remove an installed condition on the distributed conditionhandler.
Definition: StateUtil.cpp:131
armarx::StateController
Definition: StateController.h:55
armarx::StateUtility::sendEvent
void sendEvent(StateBasePtr eventProcessor=nullptr)
Definition: StateUtil.h:123
armarx::Term
Definition: Term.h:111
armarx::StateUtility::setCounterEvent
ActionEventIdentifier setCounterEvent(int counterThreshold, int initialValue=0)
Definition: StateUtil.h:106
armarx::StateUtility::setCounterEvent
ActionEventIdentifier setCounterEvent(int counterThreshold, const EventPtr &evt, int initialValue=0)
Definition: StateUtil.cpp:192
Term.h
armarx::StateUtility::ActionEventIdentifier::actionId
ChannelRefBasePtr actionId
Definition: StateUtil.h:57
IceInternal::Handle
Definition: forward_declarations.h:8
StateController.h
armarx::StateUtility::ActionEventIdentifier
Struct for the return value of setCounter/TimerEvent.
Definition: StateUtil.h:55
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:144
Event.h
armarx::VariantType::ChannelRef
const VariantTypeId ChannelRef
Definition: ChannelRef.h:162
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:97
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:83
armarx::StateUtility::setTimeoutEvent
ActionEventIdentifier setTimeoutEvent(int timeoutDurationMs)
Definition: StateUtil.h:100
armarx::StateUtility
Definition: StateUtil.h:47
armarx::StateUtility::ActionEventIdentifier::conditionId
ConditionIdentifier conditionId
Definition: StateUtil.h:58
armarx::StateUtility::removeCounterEvent
void removeCounterEvent(const ActionEventIdentifier &id)
Definition: StateUtil.cpp:226
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:38
armarx::StateUtility::createEvent
EventPtr createEvent()
Utility function to create a new Event.
Definition: StateUtil.h:64
armarx::StateUtility::removeTimeoutEvent
void removeTimeoutEvent(const ActionEventIdentifier &id)
Definition: StateUtil.cpp:178
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StatechartContextPtr
IceInternal::Handle< StatechartContext > StatechartContextPtr
Definition: StatechartContext.h:194