Orocos Real-Time Toolkit  2.8.3
StateMachineService.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Wed Jan 18 14:11:40 CET 2006 StateMachineService.cpp
3 
4  StateMachineService.cpp - description
5  -------------------
6  begin : Wed January 18 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@mech.kuleuven.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 
40 #include "StateMachineService.hpp"
41 
42 #include "../Attribute.hpp"
43 #include "../FactoryExceptions.hpp"
44 #include "../TaskContext.hpp"
45 #include "../OperationCaller.hpp"
46 
47 namespace RTT
48 {
49 
50  using namespace detail;
51 
52  void StateMachineService::createOperationFactory() {
53  // Add the state specific methods :
54  // Special trick : we store the 'this' pointer in a DataSource, such that when
55  // the created commands are copied, they also get the new this pointer.
56  // This requires template specialisations on the TemplateFactory level.
57  DataSource<StateMachinePtr>* ptr = _this.get();
58 
59  // I had to make activate() a command because the entry {}
60  // may contain commands upon which the state machine is
61  // not strictly active (entry executed and no transition
62  // in progress) when activate() returns, hence activate()
63  // takes time and is thus a method. This is however in
64  // violation with the concept of 'initialisation of the
65  // SM' which may contain non-rt safe code. When activate() is
66  // called directly upon the SM in C++, it _is_ a method, but
67  // with the same deficiencies.
68  addOperationDS("trace", &StateMachine::trace,ptr).doc("Trace the execution of this StateMachine. *Not* Real-Time.");
69  addOperationDS("activate", &StateMachine::activate,ptr).doc("Activate this StateMachine to initial state and enter request Mode.");
70  addOperationDS("deactivate", &StateMachine::deactivate,ptr).doc("Deactivate this StateMachine");
71  addOperationDS("start", &StateMachine::automatic,ptr).doc("Start this StateMachine, enter automatic Mode.");
72  addOperationDS("automatic", &StateMachine::automatic,ptr).doc("Start this StateMachine, enter automatic Mode.");
73  addOperationDS("pause", &StateMachine::pause,ptr).doc("Pause this StateMachine, enter paused Mode.");
74  addOperationDS("step", &StateMachine::step,ptr).doc(
75  "Step this StateMachine. When paused, step a single instruction or transition evaluation. \n"
76  "When in reactive mode, evaluate transitions and go to a next state, or if none, run handle.");
77  addOperationDS("reset", &StateMachine::reset,ptr).doc("Reset this StateMachine to the initial state");
78  addOperationDS("stop", &StateMachine::stop,ptr).doc("Stop this StateMachine to the final state and enter request Mode.");
79  addOperationDS("reactive", &StateMachine::reactive,ptr).doc("Enter reactive mode (see requestState() and step() ).\n OperationCaller is done if ready for requestState() or step() method.");
80  addOperationDS("requestState", &StateMachine::requestState,ptr).doc("Request to go to a particular state. Will succeed if there exists a valid transition from this state to the requested state.").arg("State", "The state to make the transition to.");
81 
82  addOperationDS("inState", &StateMachine::inState,ptr).doc("Is the StateMachine in a given state ?").arg("State", "State Name");
83  addOperationDS("inError", &StateMachine::inError,ptr).doc("Is this StateMachine in error ?");
84  addOperationDS("getState", &StateMachine::getCurrentStateName,ptr).doc("The name of the current state. An empty string if not active.");
85  addOperationDS("isActive", &StateMachine::isActive,ptr).doc("Is this StateMachine activated (possibly in transition) ?");
86  addOperationDS("isRunning", &StateMachine::isAutomatic,ptr).doc("Is this StateMachine running in automatic mode ?");
87  addOperationDS("isReactive", &StateMachine::isReactive,ptr).doc("Is this StateMachine ready and waiting for requests or events ?");
88  addOperationDS("isPaused", &StateMachine::isPaused,ptr).doc("Is this StateMachine paused ?");
89  addOperationDS("inInitialState", &StateMachine::inInitialState,ptr).doc("Is this StateMachine in the initial state ?");
90  addOperationDS("inFinalState", &StateMachine::inFinalState,ptr).doc("Is this StateMachine in the final state ?");
91  addOperationDS("inTransition", &StateMachine::inTransition,ptr).doc("Is this StateMachine executing a entry|handle|exit program ?");
92  }
93 
94  StateMachineServicePtr StateMachineService::copy(ParsedStateMachinePtr newsc, std::map<const DataSourceBase*, DataSourceBase*>& replacements, bool instantiate )
95  {
96  // if this gets copied, all created methods will use the new instance of StateMachineService to
97  // call the member functions. Further more, all future methods for the copy will also call the new instance
98  // while future methods for the original will still call the original.
99  StateMachineServicePtr tmp( new StateMachineService( newsc, this->mtc ) );
100  replacements[ _this.get() ] = tmp->_this.get(); // put 'newsc' in map
101 
102  ConfigurationInterface* dummy = ConfigurationInterface::copy( replacements, instantiate );
103  tmp->loadValues( dummy->getValues());
104  delete dummy;
105 
106  return tmp;
107  }
108 
110  : Service( statem->getName() ),
111  _this( new ValueDataSource<StateMachinePtr>( statem ) ),
112  statemachine(statem),
113  mtc(tc)
114  {
115  this->createOperationFactory();
116  this->setOwner( tc );
117  }
118 
120  {
121  // When the this Service is deleted, make sure the program does not reference us.
122  if ( statemachine ) {
123  statemachine->setService( StateMachineServicePtr() );
124  }
125  }
126  //ExecutionEngine* StateMachineService::engine() const { return mtc->engine(); }
127 
128 }
129 
const std::string & getName() const
Returns the name of this service instance.
Definition: Service.hpp:139
boost::shared_ptr< StateMachineService > StateMachineServicePtr
boost::shared_ptr< ParsedStateMachine > ParsedStateMachinePtr
bool isAutomatic() const
Query if the state machine is reacting to events and evaluating transition conditions.
bool inState(const std::string &state) const
Check if the state machine is in a given state.
bool deactivate()
Stop this StateMachine.
bool reset()
Reset the state machine from the final state to the initial state and wait for events or requests...
StateMachineServicePtr copy(ParsedStateMachinePtr newsc, std::map< const base::DataSourceBase *, base::DataSourceBase * > &replacements, bool instantiate)
bool pause()
Pause the state machine.
This class allows storage and retrieval of operations, ports, attributes and properties provided by a...
Definition: Service.hpp:93
bool inInitialState() const
Inspect if we are in the initial state.
bool step()
Execute a single action if the state machine is paused or evaluate the transition conditions if the s...
StateMachineService(ParsedStateMachinePtr statemachine, TaskContext *tc)
By constructing this object, a stateMachine can be added to a taskcontext as a Service, with its operations.
const std::string & getCurrentStateName() const
Return name of current state, empty string if not active.
AttributeObjects const & getValues() const
Returns all attributes in this repository.
bool inError() const
Get the error status of this StateMachine.
bool requestState(const std::string &statename)
Request a transition to a given state.
void trace(bool on_off)
Turn log(Debug) messages on or off to track state transitions.
boost::shared_ptr< StateMachine > StateMachinePtr
A class for keeping track of Attribute, Constant and Property objects of a TaskContext.
bool inFinalState() const
Inspect if we are in the final state.
bool activate()
Start this StateMachine.
bool isReactive() const
Query if the state machine is currently reacting only to events.
DataSource< T >::result_t get() const
Return the data as type T.
Definition: DataSources.hpp:78
The TaskContext is the C++ representation of an Orocos component.
Definition: TaskContext.hpp:93
Operation< typename internal::GetSignatureDS< Func >::Signature > & addOperationDS(const std::string &name, Func func, internal::DataSource< boost::shared_ptr< ObjT > > *sp, ExecutionThread et=ClientThread)
For internal use only.
Definition: Service.hpp:506
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
ConfigurationInterface * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &repl, bool instantiate) const
Return a new copy of this repository with the copy operation semantics.
void setOwner(TaskContext *new_owner)
Sets the owning TaskContext that will execute the operations in this service.
Definition: Service.cpp:261
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
bool isActive() const
Returns true if the state machine is activated.
bool isPaused() const
Query if the state machine is paused.
bool reactive()
Switch to reactive mode from automatic mode.
bool inTransition() const
Inspect if the StateMachine is performing a state transition.
bool automatic()
Enter automatic mode: evaluating the transition conditions continuously.
bool stop()
Bring the state machine to the safe final state and wait for events or requests.