SMACC2
Loading...
Searching...
No Matches
smacc_state_base.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
28
29#define STATE_NAME (demangleSymbol(typeid(MostDerived).name()).c_str())
30
31namespace smacc2
32{
33using namespace smacc2::introspection;
34using namespace smacc2::default_events;
35
36template <
37 class MostDerived, class Context, class InnerInitial = mpl::list<>,
38 sc::history_mode historyMode = sc::has_deep_history>
39class SmaccState : public sc::simple_state<MostDerived, Context, InnerInitial, historyMode>,
40 public ISmaccState
41{
43
44public:
45 typedef Context TContext;
46 typedef typename Context::inner_context_type context_type;
47 typedef typename context_type::state_iterator state_iterator;
48
49 typedef InnerInitial LastDeepState;
50
52 InnerInitial * smacc_inner_type;
53
56 {
57 my_context(typename base_type::context_ptr_type pContext) : pContext_(pContext) {}
58
59 typename base_type::context_ptr_type pContext_;
60 };
61
62 SmaccState() = delete;
63
64 // Constructor that initializes the state ros node handle
66 {
67 static_assert(
68 std::is_base_of<ISmaccState, Context>::value ||
69 std::is_base_of<ISmaccStateMachine, Context>::value,
70 "The context class must be a SmaccState or a SmaccStateMachine");
71 static_assert(
72 !std::is_same<MostDerived, Context>::value,
73 "The context must be a different state or state machine "
74 "than the current state");
75
76 logger_.reset(new rclcpp::Logger(
77 rclcpp::get_logger(smacc2::utils::cleanShortTypeName(typeid(MostDerived)))));
78
79 RCLCPP_INFO(getLogger(), "[%s] creating state ", STATE_NAME);
80 this->set_context(ctx.pContext_);
81
82 node_ = this->getStateMachine().getNode();
83
84 this->stateInfo_ = getStateInfo();
85
86 // storing a reference to the parent state
87 auto & ps = this->template context<Context>();
88 parentState_ = dynamic_cast<ISmaccState *>(&ps);
89 finishStateThrown = false;
90 }
91
92 virtual ~SmaccState() {}
93
95 {
96 auto smInfo = this->getStateMachine().getStateMachineInfo();
97
98 auto key = typeid(MostDerived).name();
99 if (smInfo.states.count(key))
100 {
101 return smInfo.states[key].get();
102 }
103 else
104 {
105 return nullptr;
106 }
107 }
108
109 std::string getName() override { return getShortName(); }
110
111 std::string getFullName() { return demangleSymbol(typeid(MostDerived).name()); }
112
113 std::string getShortName() { return smacc2::utils::cleanShortTypeName(typeid(MostDerived)); }
114
116 {
117 // auto* ctx = dynamic_cast<ISmaccState*>(this->template context<Context *>());
118
119 return parentState_;
120 }
121
122 // this function is called by boot statechart before the destructor call
123 void exit()
124 {
125 auto * derivedThis = static_cast<MostDerived *>(this);
126 // this->getStateMachine().notifyOnStateExitting(derivedThis);
127 {
128 std::lock_guard<std::recursive_mutex> lock(this->getStateMachine().getMutex());
129 this->getStateMachine().notifyOnStateExitting(derivedThis);
130 try
131 {
133 // static_cast<MostDerived *>(this)->onExit();
134 standardOnExit(*derivedThis);
136 }
137 catch (...)
138 {
139 }
140 this->getStateMachine().notifyOnStateExited(derivedThis);
141 }
142 }
143
144public:
145 // This method is static-polymorphic because of the curiously recurring template pattern. It
146 // calls to the most derived class onEntry method if declared on smacc state construction
148
149 // This method is static-polymorphic because of the curiously recurring template pattern. It
150 // calls to the most derived class onEntry method if declared on smacc state construction
151 void onEntry() {}
152
153 // this method is static-polimorphic because of the curiously recurreing pattern. It
154 // calls to the most derived class onExit method if declared on smacc state destruction
155 void onExit() {}
156
157 template <typename T>
158 bool getGlobalSMData(std::string name, T & ret)
159 {
160 return base_type::outermost_context().getGlobalSMData(name, ret);
161 }
162
163 // Store globally in this state machine. (By value parameter )
164 template <typename T>
165 void setGlobalSMData(std::string name, T value)
166 {
167 base_type::outermost_context().setGlobalSMData(name, value);
168 }
169
170 template <typename SmaccComponentType>
171 void requiresComponent(SmaccComponentType *& storage)
172 {
173 base_type::outermost_context().requiresComponent(storage);
174 }
175
176 virtual ISmaccStateMachine & getStateMachine() { return base_type::outermost_context(); }
177
178 template <typename TOrthogonal, typename TBehavior>
180 std::function<void(TBehavior & bh, MostDerived &)> initializationFunction)
181 {
183 [=](ISmaccState * state)
184 {
185 // auto bh = std::make_shared<TBehavior>(args...);
186 auto bh = state->configure<TOrthogonal, TBehavior>();
187 initializationFunction(*bh, *(static_cast<MostDerived *>(state)));
188 });
189 }
190
191 template <typename TOrthogonal, typename TBehavior>
193 std::function<void(TBehavior & bh)> initializationFunction)
194 {
196 [=](ISmaccState * state)
197 {
198 // auto bh = std::make_shared<TBehavior>(args...);
199 auto bh = state->configure<TOrthogonal, TBehavior>();
200 initializationFunction(*bh);
201 });
202 }
203
204 template <typename TOrthogonal, typename TBehavior, typename... Args>
205 static void configure_orthogonal(Args &&... args)
206 {
208 [=](ISmaccState * state)
209 {
210 // auto bh = std::make_shared<TBehavior>(args...);
211 state->configure<TOrthogonal, TBehavior>(args...);
212 });
213 }
214
215 template <
216 typename TStateReactor, typename TOutputEvent, typename TInputEventList, typename... TArgs>
217 static std::shared_ptr<smacc2::introspection::StateReactorHandler> static_createStateReactor(
218 TArgs... args)
219 {
220 auto srh = std::make_shared<smacc2::introspection::StateReactorHandler>(globalNh_);
221 auto srinfo = std::make_shared<SmaccStateReactorInfo>();
222
223 srinfo->stateReactorType = TypeInfo::getTypeInfoFromType<TStateReactor>();
224 srinfo->outputEventType = TypeInfo::getTypeInfoFromType<TOutputEvent>();
225
226 if (srinfo->outputEventType->templateParameters.size() == 2)
227 {
228 srinfo->objectTagType = srinfo->outputEventType->templateParameters[1];
229 }
230 else if (srinfo->outputEventType->templateParameters.size() == 1)
231 {
233 }
234 else
235 {
236 assert(
237 false &&
238 "state reactor output events should have one or two parameters (SourceType, ObjectTag)");
239 }
240
241 // iterate statically on all event sources
242 using boost::mpl::_1;
243 using wrappedList = typename boost::mpl::transform<TInputEventList, _1>::type;
245 boost::mpl::for_each<wrappedList>(op);
246
247 srinfo->srh = srh;
248 srh->srInfo_ = srinfo;
249
250 const std::type_info * tindex = &(typeid(MostDerived)); // get identifier of the current state
251
252 if (!SmaccStateInfo::stateReactorsInfo.count(tindex))
254 std::vector<std::shared_ptr<SmaccStateReactorInfo>>();
255
256 srinfo->factoryFunction = [&, srh, args...](ISmaccState * state)
257 {
258 auto sr =
259 state->createStateReactor<TStateReactor, TOutputEvent, TInputEventList, TArgs...>(args...);
260 srh->configureStateReactor(sr);
261 sr->initialize(state);
262 return sr;
263 };
264
265 SmaccStateInfo::stateReactorsInfo[tindex].push_back(srinfo);
266
267 return srh;
268 }
269
270 template <typename TEventGenerator, typename... TUArgs>
271 static std::shared_ptr<smacc2::introspection::EventGeneratorHandler> static_createEventGenerator(
272 TUArgs... args)
273 {
274 auto egh = std::make_shared<smacc2::introspection::EventGeneratorHandler>();
275 auto eginfo = std::make_shared<SmaccEventGeneratorInfo>();
276 eginfo->eventGeneratorType = TypeInfo::getTypeInfoFromType<TEventGenerator>();
277
278 eginfo->egh = egh;
279 egh->egInfo_ = eginfo;
280
281 const std::type_info * tindex = &(typeid(MostDerived)); // get identifier of the current state
282
283 if (!SmaccStateInfo::eventGeneratorsInfo.count(tindex))
285 std::vector<std::shared_ptr<SmaccEventGeneratorInfo>>();
286
287 eginfo->factoryFunction = [&, egh, args...](ISmaccState * state)
288 {
289 auto eg = state->createEventGenerator<TEventGenerator>(args...);
290 egh->configureEventGenerator(eg);
291 eg->initialize(state);
292
293 eg->template onOrthogonalAllocation<
294 MostDerived, TEventGenerator>(); // deprecated to extinguish in future
295 eg->template onStateOrthogonalAllocation<MostDerived, TEventGenerator>();
296 return eg;
297 };
298
299 SmaccStateInfo::eventGeneratorsInfo[tindex].push_back(eginfo);
300 return egh;
301 }
302
303 template <typename TStateReactor, typename... TUArgs>
304 static std::shared_ptr<smacc2::introspection::StateReactorHandler> static_createStateReactor_aux(
305 TUArgs... args)
306 {
307 auto srh = std::make_shared<smacc2::introspection::StateReactorHandler>(globalNh_);
308 auto srinfo = std::make_shared<SmaccStateReactorInfo>();
309
310 srinfo->stateReactorType = TypeInfo::getTypeInfoFromType<TStateReactor>();
311 srinfo->srh = srh;
312 srh->srInfo_ = srinfo;
313
314 const std::type_info * tindex = &(typeid(MostDerived)); // get identifier of the current state
315
316 if (!SmaccStateInfo::stateReactorsInfo.count(tindex))
318 std::vector<std::shared_ptr<SmaccStateReactorInfo>>();
319
320 srinfo->factoryFunction = [&, srh, args...](ISmaccState * state)
321 {
322 auto sr = state->createStateReactor<TStateReactor>(args...);
323 srh->configureStateReactor(sr);
324 sr->initialize(state);
325 return sr;
326 };
327
328 SmaccStateInfo::stateReactorsInfo[tindex].push_back(srinfo);
329
330 return srh;
331 }
332
333 void checkWhileLoopConditionAndThrowEvent(bool (MostDerived::*conditionFn)())
334 {
335 auto * thisobject = static_cast<MostDerived *>(this);
336 auto condition = boost::bind(conditionFn, thisobject);
337 bool conditionResult = condition();
338
339 // RCLCPP_INFO("LOOP EVENT CONDITION: %d", conditionResult);
340 if (conditionResult)
341 {
343 }
344 else
345 {
347 }
348 RCLCPP_INFO(getLogger(), "[%s] POST THROW CONDITION", STATE_NAME);
349 }
350
352
354 // The following declarations should be private.
355 // They are only public because many compilers lack template friends.
357 // See base class for documentation
358 typedef typename base_type::outermost_context_base_type outermost_context_base_type;
359 typedef typename base_type::inner_context_ptr_type inner_context_ptr_type;
360 typedef typename base_type::context_ptr_type context_ptr_type;
361 typedef typename base_type::inner_initial_list inner_initial_list;
362
363 static void initial_deep_construct(outermost_context_base_type & outermostContextBase)
364 {
365 deep_construct(&outermostContextBase, outermostContextBase);
366 }
367
368 // See base class for documentation
369 static void deep_construct(
370 const context_ptr_type & pContext, outermost_context_base_type & outermostContextBase)
371 {
372 const inner_context_ptr_type pInnerContext(shallow_construct(pContext, outermostContextBase));
373 base_type::template deep_construct_inner<inner_initial_list>(
374 pInnerContext, outermostContextBase);
375 }
376
378 const context_ptr_type & pContext, outermost_context_base_type & outermostContextBase)
379 {
380 // allocating in memory
381 auto state = new MostDerived(
383 const inner_context_ptr_type pInnerContext(state);
384
386 state->entryStateInternal();
388
389 outermostContextBase.add(pInnerContext);
390 return pInnerContext;
391 }
392
393private:
394 template <typename TOrthogonal, typename TBehavior>
396 std::function<void(ISmaccState * state)> initializationFunction)
397 {
399 bhinfo.factoryFunction = initializationFunction;
400
401 bhinfo.behaviorType = &(typeid(TBehavior));
402 bhinfo.orthogonalType = &(typeid(TOrthogonal));
403
404 const std::type_info * tindex = &(typeid(MostDerived));
405 if (!SmaccStateInfo::staticBehaviorInfo.count(tindex))
406 SmaccStateInfo::staticBehaviorInfo[tindex] = std::vector<ClientBehaviorInfoEntry>();
407
408 SmaccStateInfo::staticBehaviorInfo[tindex].push_back(bhinfo);
409 RCLCPP_INFO_STREAM(
410 rclcpp::get_logger("static"), "[states walking] State "
412 << "client behavior count: "
413 << SmaccStateInfo::staticBehaviorInfo[tindex].size());
414 }
415
417 {
418 // finally we go to the derived state onEntry Function
419
420 RCLCPP_DEBUG(getLogger(), "[%s] State object created. Initializating...", STATE_NAME);
421 this->getStateMachine().notifyOnStateEntryStart(static_cast<MostDerived *>(this));
422
423 RCLCPP_DEBUG_STREAM(
424 getLogger(), "[" << smacc2::utils::cleanShortTypeName(typeid(MostDerived)).c_str()
425 << "] creating ros subnode");
426
427 // before dynamic runtimeConfigure, we execute the staticConfigure behavior configurations
428 {
429 RCLCPP_DEBUG(getLogger(), "[%s] -- STATIC STATE DESCRIPTION --", STATE_NAME);
430
431 for (const auto & clientBehavior : SmaccStateInfo::staticBehaviorInfo)
432 {
433 RCLCPP_DEBUG(
434 getLogger(), "[%s] client behavior info: %s", STATE_NAME,
435 demangleSymbol(clientBehavior.first->name()).c_str());
436 for (auto & cbinfo : clientBehavior.second)
437 {
438 RCLCPP_DEBUG(
439 getLogger(), "[%s] client behavior: %s", STATE_NAME,
440 demangleSymbol(cbinfo.behaviorType->name()).c_str());
441 }
442 }
443
444 const std::type_info * tindex = &(typeid(MostDerived));
445 auto & staticDefinedBehaviors = SmaccStateInfo::staticBehaviorInfo[tindex];
446 auto & staticDefinedStateReactors = SmaccStateInfo::stateReactorsInfo[tindex];
447 auto & staticDefinedEventGenerators = SmaccStateInfo::eventGeneratorsInfo[tindex];
448
449 for (auto & ortho : this->getStateMachine().getOrthogonals())
450 {
451 RCLCPP_INFO(
452 getLogger(), "[%s] Initializing orthogonal: %s", STATE_NAME,
453 demangleSymbol(typeid(*ortho.second).name()).c_str());
454 ortho.second->initState(this);
455 }
456
457 RCLCPP_DEBUG_STREAM(
458 getLogger(), "finding static client behaviors. State Database: "
459 << SmaccStateInfo::staticBehaviorInfo.size() << ". Current state "
460 << cleanShortTypeName(*tindex)
461 << " cbs: " << SmaccStateInfo::staticBehaviorInfo[tindex].size());
462 for (auto & bhinfo : staticDefinedBehaviors)
463 {
464 RCLCPP_DEBUG(
465 getLogger(), "[%s] Creating static client behavior: %s", STATE_NAME,
466 demangleSymbol(bhinfo.behaviorType->name()).c_str());
467 bhinfo.factoryFunction(this);
468 }
469
470 for (auto & sr : staticDefinedStateReactors)
471 {
472 RCLCPP_DEBUG(
473 getLogger(), "[%s] Creating static state reactor: %s", STATE_NAME,
474 sr->stateReactorType->getFullName().c_str());
475 sr->factoryFunction(this);
476 }
477
478 for (auto & eg : staticDefinedEventGenerators)
479 {
480 RCLCPP_DEBUG(
481 getLogger(), "[%s] Creating static event generator: %s", STATE_NAME,
482 eg->eventGeneratorType->getFullName().c_str());
483 eg->factoryFunction(this);
484 }
485
486 RCLCPP_DEBUG(getLogger(), "[%s] ---- END STATIC DESCRIPTION", STATE_NAME);
487 }
488
489 RCLCPP_DEBUG(getLogger(), "[%s] State runtime configuration", STATE_NAME);
490
491 auto * derivedthis = static_cast<MostDerived *>(this);
492
493 // second the orthogonals are internally configured
494 this->getStateMachine().notifyOnRuntimeConfigured(derivedthis);
495
497 // first we runtime configure the state, where we create client behaviors
498 static_cast<MostDerived *>(this)->runtimeConfigure();
500
502
503 RCLCPP_DEBUG(getLogger(), "[%s] State OnEntry", STATE_NAME);
504
505 static_cast<MostDerived *>(this)->onEntry();
506
507 // here orthogonals and client behaviors are entered OnEntry
508 this->getStateMachine().notifyOnStateEntryEnd(derivedthis);
509 }
510};
511} // namespace smacc2
rclcpp::Node::SharedPtr getNode()
void notifyOnStateExitting(StateType *state)
void notifyOnRuntimeConfigurationFinished(StateType *state)
void notifyOnStateExited(StateType *state)
void notifyOnStateEntryEnd(StateType *state)
const SmaccStateMachineInfo & getStateMachineInfo()
void notifyOnRuntimeConfigured(StateType *state)
void notifyOnStateEntryStart(StateType *state)
std::shared_ptr< rclcpp::Logger > logger_
rclcpp::Logger getLogger()
std::shared_ptr< TBehavior > configure(Args &&... args)
rclcpp::Node::SharedPtr node_
ISmaccState * parentState_
const smacc2::introspection::SmaccStateInfo * stateInfo_
base_type::outermost_context_base_type outermost_context_base_type
static inner_context_ptr_type shallow_construct(const context_ptr_type &pContext, outermost_context_base_type &outermostContextBase)
static std::shared_ptr< smacc2::introspection::StateReactorHandler > static_createStateReactor(TArgs... args)
static void initial_deep_construct(outermost_context_base_type &outermostContextBase)
InnerInitial * smacc_inner_type
void checkWhileLoopConditionAndThrowEvent(bool(MostDerived::*conditionFn)())
static void configure_orthogonal_internal(std::function< void(ISmaccState *state)> initializationFunction)
static std::shared_ptr< smacc2::introspection::EventGeneratorHandler > static_createEventGenerator(TUArgs... args)
virtual ISmaccStateMachine & getStateMachine()
static std::shared_ptr< smacc2::introspection::StateReactorHandler > static_createStateReactor_aux(TUArgs... args)
const smacc2::introspection::SmaccStateInfo * getStateInfo()
sc::simple_state< MostDerived, Context, InnerInitial, historyMode > base_type
bool getGlobalSMData(std::string name, T &ret)
base_type::inner_initial_list inner_initial_list
static void deep_construct(const context_ptr_type &pContext, outermost_context_base_type &outermostContextBase)
virtual ISmaccState * getParentState()
SmaccState(my_context ctx)
base_type::context_ptr_type context_ptr_type
std::string getName() override
void setGlobalSMData(std::string name, T value)
base_type::inner_context_ptr_type inner_context_ptr_type
static void configure_orthogonal_runtime(std::function< void(TBehavior &bh)> initializationFunction)
void requiresComponent(SmaccComponentType *&storage)
static void configure_orthogonal_runtime(std::function< void(TBehavior &bh, MostDerived &)> initializationFunction)
static void configure_orthogonal(Args &&... args)
context_type::state_iterator state_iterator
Context::inner_context_type context_type
static std::map< const std::type_info *, std::vector< std::shared_ptr< SmaccStateReactorInfo > > > stateReactorsInfo
static std::map< const std::type_info *, std::vector< std::shared_ptr< SmaccEventGeneratorInfo > > > eventGeneratorsInfo
static std::map< const std::type_info *, std::vector< ClientBehaviorInfoEntry > > staticBehaviorInfo
static TypeInfo::Ptr getTypeInfoFromType()
rclcpp::Node::SharedPtr globalNh_
std::string demangleSymbol()
std::string cleanShortTypeName()
Definition common.hpp:62
void standardOnExit(TState &st, std::true_type)
#define STATE_NAME
void TRACEPOINT(spinOnce)
my_context(typename base_type::context_ptr_type pContext)
base_type::context_ptr_type pContext_
std::function< void(smacc2::ISmaccState *)> factoryFunction
smacc2_state_onExit_end
smacc2_state_onRuntimeConfigure_start
smacc2_state_onEntry_end
smacc2_state_onRuntimeConfigure_end
smacc2_state_onEntry_start
smacc2_state_onExit_start