30#define STATE_NAME (demangleSymbol(typeid(MostDerived).name()).c_str())
36template <
typename TAddressee>
38 ISmaccStateMachine * stateMachine,
const std::string & stateName,
const std::string & what)
41 ev->stateName = stateName;
43 using TContainer =
typename TAddressee::context_type;
44 if constexpr (!std::is_base_of<ISmaccStateMachine, TContainer>::value)
46 ev->escalate = [stateMachine, stateName, what]()
56 class MostDerived,
class Context,
class InnerInitial = mpl::list<>,
57 sc::history_mode historyMode = sc::has_deep_history>
87 std::is_base_of<ISmaccState, Context>::value ||
88 std::is_base_of<ISmaccStateMachine, Context>::value,
89 "The context class must be a SmaccState or a SmaccStateMachine");
91 !std::is_same<MostDerived, Context>::value,
92 "The context must be a different state or state machine "
93 "than the current state");
95 logger_ = std::make_shared<rclcpp::Logger>(
106 auto & ps = this->
template context<Context>();
118 auto it = smInfo.
states.find(
typeid(MostDerived).name());
119 return it != smInfo.states.end() ? it->second.get() :
nullptr;
133 auto * derivedThis =
static_cast<MostDerived *
>(
this);
135 std::lock_guard<std::recursive_mutex> lock(this->
getStateMachine().getMutex());
145 catch (
const std::exception & e)
170 template <
typename T>
173 return base_type::outermost_context().getGlobalSMData(name, ret);
177 template <
typename T>
180 base_type::outermost_context().setGlobalSMData(name, value);
188 template <
typename TOrthogonal,
typename TBehavior>
190 std::function<
void(TBehavior & bh, MostDerived &)> initializationFunction)
195 auto bh = state->
configure<TOrthogonal, TBehavior>();
196 initializationFunction(*bh, *(
static_cast<MostDerived *
>(state)));
200 template <
typename TOrthogonal,
typename TBehavior>
202 std::function<
void(TBehavior & bh)> initializationFunction)
207 auto bh = state->
configure<TOrthogonal, TBehavior>();
208 initializationFunction(*bh);
212 template <
typename TOrthogonal,
typename TBehavior,
typename... Args>
220 typename TStateReactor,
typename TOutputEvent,
typename TInputEventList,
typename... TArgs>
224 auto srh = std::make_shared<smacc2::introspection::StateReactorHandler>(
globalNh_);
225 auto srinfo = std::make_shared<SmaccStateReactorInfo>();
230 if (srinfo->outputEventType->templateParameters.size() == 2)
232 srinfo->objectTagType = srinfo->outputEventType->templateParameters[1];
234 else if (srinfo->outputEventType->templateParameters.size() == 1)
242 "state reactor output events should have one or two parameters (SourceType, ObjectTag)");
246 using boost::mpl::_1;
247 using wrappedList =
typename boost::mpl::transform<TInputEventList, _1>::type;
249 boost::mpl::for_each<wrappedList>(op);
252 srh->srInfo_ = srinfo;
254 const std::type_info * tindex = &(
typeid(MostDerived));
258 std::vector<std::shared_ptr<SmaccStateReactorInfo>>();
260 srinfo->factoryFunction = [&, srh, args...](
ISmaccState * state)
263 state->createStateReactor<TStateReactor, TOutputEvent, TInputEventList, TArgs...>(args...);
264 srh->configureStateReactor(sr);
265 sr->initialize(state);
274 template <
typename TEventGenerator,
typename... TUArgs>
278 auto egh = std::make_shared<smacc2::introspection::EventGeneratorHandler>();
279 auto eginfo = std::make_shared<SmaccEventGeneratorInfo>();
283 egh->egInfo_ = eginfo;
285 const std::type_info * tindex = &(
typeid(MostDerived));
289 std::vector<std::shared_ptr<SmaccEventGeneratorInfo>>();
291 eginfo->factoryFunction = [&, egh, args...](
ISmaccState * state)
293 auto eg = state->createEventGenerator<TEventGenerator>(args...);
294 egh->configureEventGenerator(eg);
295 eg->initialize(state);
297 eg->template onStateOrthogonalAllocation<MostDerived, TEventGenerator>();
305 template <
typename TStateReactor,
typename... TUArgs>
309 auto srh = std::make_shared<smacc2::introspection::StateReactorHandler>(
globalNh_);
310 auto srinfo = std::make_shared<SmaccStateReactorInfo>();
314 srh->srInfo_ = srinfo;
316 const std::type_info * tindex = &(
typeid(MostDerived));
320 std::vector<std::shared_ptr<SmaccStateReactorInfo>>();
322 srinfo->factoryFunction = [&, srh, args...](
ISmaccState * state)
324 auto sr = state->createStateReactor<TStateReactor>(args...);
325 srh->configureStateReactor(sr);
326 sr->initialize(state);
337 auto * thisobject =
static_cast<MostDerived *
>(
this);
338 auto condition = boost::bind(conditionFn, thisobject);
339 bool conditionResult = condition();
375 base_type::template deep_construct_inner<inner_initial_list>(
376 pInnerContext, outermostContextBase);
383 auto state =
new MostDerived(
388 state->entryStateInternal();
391 outermostContextBase.add(pInnerContext);
392 return pInnerContext;
396 template <
typename TOrthogonal,
typename TBehavior>
398 std::function<
void(
ISmaccState * state)> initializationFunction)
406 const std::type_info * tindex = &(
typeid(MostDerived));
412 rclcpp::get_logger(
"static"),
"[states walking] State "
414 <<
"client behavior count: "
427 <<
"] creating ros subnode");
438 for (
auto & cbinfo : clientBehavior.second)
446 const std::type_info * tindex = &(
typeid(MostDerived));
456 ortho.second->initState(
this);
460 getLogger(),
"Finding static client behaviors. State Database: "
464 for (
auto & bhinfo : staticDefinedBehaviors)
469 bhinfo.factoryFunction(
this);
472 for (
auto & sr : staticDefinedStateReactors)
476 sr->stateReactorType->getFullName().c_str());
477 sr->factoryFunction(
this);
480 for (
auto & eg : staticDefinedEventGenerators)
484 eg->eventGeneratorType->getFullName().c_str());
485 eg->factoryFunction(
this);
493 auto * derivedthis =
static_cast<MostDerived *
>(
this);
503 getLogger(),
"[%s] not initialized: its container state failed to initialize",
STATE_NAME);
518 catch (
const std::exception & e)
536 static_cast<MostDerived *
>(
this)->
onEntry();
538 catch (
const std::exception & e)
554 const std::string stateName =
demangleSymbol(
typeid(MostDerived).name());
557 "[%s] exception in state initialization: %s. Its client behaviors are not started.",
558 stateName.c_str(), what.c_str());
rclcpp::Node::SharedPtr getNode()
void notifyOnRuntimeConfigurationFinished(StateType *state)
void notifyOnStateExited(StateType *state)
void notifyOnStateEntryEnd(StateType *state)
const SmaccStateMachineInfo & getStateMachineInfo()
void notifyOnStateExiting(StateType *state)
void notifyOnRuntimeConfigured(StateType *state)
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
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_
bool initializationFailed_
bool initializationFailed() const
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)
ISmaccStateMachine & getStateMachine() override
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)
static std::shared_ptr< smacc2::introspection::StateReactorHandler > static_createStateReactor_aux(TUArgs... args)
void onInitializationFailure(const std::string &what)
const smacc2::introspection::SmaccStateInfo * getStateInfo()
sc::simple_state< MostDerived, Context, InnerInitial, historyMode > base_type
base_type::inner_initial_list inner_initial_list
bool getGlobalSMData(const std::string &name, T &ret)
std::string getFullName()
static void deep_construct(const context_ptr_type &pContext, outermost_context_base_type &outermostContextBase)
InnerInitial LastDeepState
virtual ISmaccState * getParentState()
SmaccState(my_context ctx)
base_type::context_ptr_type context_ptr_type
std::string getName() override
base_type::inner_context_ptr_type inner_context_ptr_type
static void configure_orthogonal_runtime(std::function< void(TBehavior &bh)> initializationFunction)
void throwSequenceFinishedEvent()
void setGlobalSMData(const std::string &name, T value)
std::string getShortName()
static void configure_orthogonal_runtime(std::function< void(TBehavior &bh, MostDerived &)> initializationFunction)
static void configure_orthogonal(Args &&... args)
void entryStateInternal()
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
std::map< std::string, std::shared_ptr< SmaccStateInfo > > states
static TypeInfo::Ptr getTypeInfoFromType()
rclcpp::Node::SharedPtr globalNh_
std::string demangleSymbol()
std::string cleanShortTypeName()
void postStInitializationFailure(ISmaccStateMachine *stateMachine, const std::string &stateName, const std::string &what)
boost::intrusive_ptr< EventType > makeEvent(Args &&... args)
void standardOnExit(TState &st, std::true_type)
my_context(typename base_type::context_ptr_type pContext)
base_type::context_ptr_type pContext_
const std::type_info * behaviorType
const std::type_info * orthogonalType
std::function< void(smacc2::ISmaccState *)> factoryFunction
smacc2_state_onRuntimeConfigure_start
smacc2_state_onRuntimeConfigure_end
smacc2_state_onEntry_start
smacc2_state_onExit_start