SMACC2
Loading...
Searching...
No Matches
smacc_state_info.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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
22#include <functional>
23#include <memory>
26#include <vector>
27namespace smacc2
28{
29namespace introspection
30{
31// Ownership of the introspection graph. The SmaccStateMachineInfo owns every SmaccStateInfo
32// through its `states` map; a SmaccStateInfo owns its `children_` and its `transitions_`; the
33// static maps SmaccStateInfo::stateReactorsInfo and eventGeneratorsInfo own the reactor and
34// generator infos, which own their handlers. Every pointer in the other direction (a state to its
35// machine or parent, a transition to its states, a handler to its info) is a std::weak_ptr, so the
36// graph is released when the state machine releases its SmaccStateMachineInfo. Readers of a weak
37// pointer lock it; all of them run while the state machine is alive.
38//-------------------------------------------------------------
40{
41 std::function<void(smacc2::ISmaccState *)> factoryFunction;
42 const std::type_info * behaviorType;
43 const std::type_info * orthogonalType;
44};
45
46//---------------------------------------------
47// contains information about an specific event in the statechart
49{
50 SmaccEventInfo(std::shared_ptr<TypeInfo> eventType);
51
52 std::string getEventTypeName();
53
54 std::string getEventSourceName();
55
56 // AKA orthogonal
57 std::string getOrthogonalName();
58
59 std::string label;
60
61 std::shared_ptr<TypeInfo> eventType;
62
63private:
64};
65
66// contains metainformation about some specific transition in the statechart
68{
70
71 // Default-initialized: the startup transition-log entry in SmaccStateMachineBase::initiate_impl
72 // builds this struct with make_shared and never assigns these two, and the user-provided
73 // constructor leaves them indeterminate otherwise.
74 bool historyNode = false;
75 int index = 0;
76 // Weak: the source state owns this transition, and the destiny state may own a transition back.
77 std::weak_ptr<const SmaccStateInfo> sourceState;
78 std::weak_ptr<const SmaccStateInfo> destinyState;
79
80 std::string transitionTag;
81 std::string transitionType;
82 std::shared_ptr<SmaccEventInfo> eventInfo;
83
85};
86//---------------------------------------------
87
89{
90 std::function<void(std::shared_ptr<smacc2::StateReactor>)> fn;
91};
92
93// This object is used in a static context (when the runtime state reactor is not built yet)
95{
96private:
97 std::vector<StateReactorCallbackFunctor> callbacks_;
98
99public:
100 // Only the node's logger is kept. This object is owned by a static map and outlives the state
101 // machine; holding the node here would make a static the node's last owner and destroy it after
102 // rclcpp::shutdown() has finalized the DDS participant.
103 explicit StateReactorHandler(rclcpp::Node::SharedPtr nh)
104 : logger_(nh ? nh->get_logger() : rclcpp::get_logger("StateReactorHandler"))
105 {
106 }
107
108 void configureStateReactor(std::shared_ptr<smacc2::StateReactor> sr);
109
110 template <typename TEv>
111 void addInputEvent();
112
113 template <typename TEv>
114 void setOutputEvent();
115
116 // Weak: the SmaccStateReactorInfo owns this handler (and its factoryFunction captures it).
117 std::weak_ptr<smacc2::introspection::SmaccStateReactorInfo> srInfo_;
118
119private:
120 rclcpp::Logger logger_;
121};
122//-------------------------------------------------------------
123
124// This object is used in a static context (when the runtime client behavior is not built yet)
125template <class T>
127{
128public:
130};
131//-------------------------------------------------------------
132// contains metainformation about some specific state reactor in the statechart
134{
135 // Never assigned or read by the core; kept weak so that it can never own a state.
136 std::weak_ptr<SmaccStateInfo> ownerState;
137 std::function<void(smacc2::ISmaccState *)> factoryFunction;
138
139 std::shared_ptr<TypeInfo> stateReactorType;
140 std::shared_ptr<TypeInfo> outputEventType;
141 std::shared_ptr<TypeInfo> objectTagType;
142 std::vector<std::shared_ptr<SmaccEventInfo>> sourceEventTypes;
143 std::shared_ptr<StateReactorHandler> srh;
144};
145
146//-------------------------------------------------------------
148{
149 std::function<void(std::shared_ptr<smacc2::SmaccEventGenerator>)> fn;
150};
151
153{
154private:
155 std::vector<EventGeneratorCallbackFunctor> callbacks_;
156
157public:
158 void configureEventGenerator(std::shared_ptr<smacc2::SmaccEventGenerator> eg);
159
160 template <typename TEv>
162
163 // Weak: the SmaccEventGeneratorInfo owns this handler (and its factoryFunction captures it).
164 std::weak_ptr<smacc2::introspection::SmaccEventGeneratorInfo> egInfo_;
165};
166
167// contains metainformation about some specific event generator in the statechart
169{
170 // Never assigned or read by the core; kept weak so that it can never own a state.
171 std::weak_ptr<SmaccStateInfo> ownerState;
172 std::function<void(smacc2::ISmaccState *)> factoryFunction;
173
174 std::shared_ptr<TypeInfo> eventGeneratorType;
175 std::shared_ptr<TypeInfo> outputEventType;
176 std::shared_ptr<TypeInfo> objectTagType;
177 std::vector<std::shared_ptr<SmaccEventInfo>> sourceEventTypes;
178 std::shared_ptr<EventGeneratorHandler> egh;
179};
180//-----------------------------------------------------------------------
182{
183 SUPERSTATE = 2,
184 STATE = 1,
186};
187//-----------------------------------------------------------------------
188class SmaccStateInfo : public std::enable_shared_from_this<SmaccStateInfo>
189{
190public:
191 typedef std::shared_ptr<SmaccStateInfo> Ptr;
192
193 static std::map<const std::type_info *, std::vector<ClientBehaviorInfoEntry>> staticBehaviorInfo;
194 static std::map<const std::type_info *, std::vector<std::shared_ptr<SmaccStateReactorInfo>>>
196 static std::map<const std::type_info *, std::vector<std::shared_ptr<SmaccEventGeneratorInfo>>>
198
199 int stateIndex_ = 0;
200 std::string fullStateName;
202
203 // Weak back-references: the machine info owns this state through `states`, and the parent owns
204 // it through `children_`. Use getStateMachineInfo() and getParentState().
205 std::weak_ptr<SmaccStateMachineInfo> stateMachine_;
206 std::weak_ptr<SmaccStateInfo> parentState_;
207 std::vector<SmaccTransitionInfo> transitions_;
208
209 std::vector<std::shared_ptr<SmaccStateInfo>> children_;
210 int depth_ = 0; // root state (no parent) is depth 0; the constructor adds 1 per ancestor
211 const std::type_info * tid_;
212
214 const std::type_info * tid, std::shared_ptr<SmaccStateInfo> parentState,
215 std::shared_ptr<SmaccStateMachineInfo> stateMachineInfo);
216
218
219 // Null once the SmaccStateMachineInfo has been destroyed, which only happens after the state
220 // machine itself is gone.
221 inline std::shared_ptr<SmaccStateMachineInfo> getStateMachineInfo() const
222 {
223 return stateMachine_.lock();
224 }
225
226 // Null for the root state, and once the parent has been destroyed.
227 inline std::shared_ptr<SmaccStateInfo> getParentState() const { return parentState_.lock(); }
228
229 inline int depth() const { return depth_; }
230
231 void getAncestors(std::list<const SmaccStateInfo *> & ancestorsList) const;
232
233 std::string getFullPath();
234
235 template <typename StateType>
236 std::shared_ptr<SmaccStateInfo> createChildState();
237
238 template <typename EvType>
240 std::shared_ptr<SmaccStateInfo> & dstState, const std::string & transitionTag,
241 const std::string & transitionType, bool history, TypeInfo::Ptr transitionTypeInfo);
242
243 const std::string & toShortName() const;
244
245 std::string getDemangledFullName() const;
246 rclcpp::Node::SharedPtr getNode();
247 inline rclcpp::Logger getLogger() { return getNode()->get_logger(); }
248};
249} // namespace introspection
250} // namespace smacc2
std::weak_ptr< smacc2::introspection::SmaccEventGeneratorInfo > egInfo_
std::vector< EventGeneratorCallbackFunctor > callbacks_
void configureEventGenerator(std::shared_ptr< smacc2::SmaccEventGenerator > eg)
std::weak_ptr< SmaccStateInfo > parentState_
std::shared_ptr< SmaccStateInfo > createChildState()
void declareTransition(std::shared_ptr< SmaccStateInfo > &dstState, const std::string &transitionTag, const std::string &transitionType, bool history, TypeInfo::Ptr transitionTypeInfo)
SmaccStateInfo(const std::type_info *tid, std::shared_ptr< SmaccStateInfo > parentState, std::shared_ptr< SmaccStateMachineInfo > stateMachineInfo)
static std::map< const std::type_info *, std::vector< std::shared_ptr< SmaccStateReactorInfo > > > stateReactorsInfo
void getAncestors(std::list< const SmaccStateInfo * > &ancestorsList) const
const std::string & toShortName() const
std::weak_ptr< SmaccStateMachineInfo > stateMachine_
std::shared_ptr< SmaccStateInfo > Ptr
std::vector< std::shared_ptr< SmaccStateInfo > > children_
std::shared_ptr< SmaccStateInfo > getParentState() const
std::shared_ptr< SmaccStateMachineInfo > getStateMachineInfo() const
std::vector< SmaccTransitionInfo > transitions_
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
void configureStateReactor(std::shared_ptr< smacc2::StateReactor > sr)
std::vector< StateReactorCallbackFunctor > callbacks_
StateReactorHandler(rclcpp::Node::SharedPtr nh)
std::weak_ptr< smacc2::introspection::SmaccStateReactorInfo > srInfo_
std::shared_ptr< TypeInfo > Ptr
std::function< void(smacc2::ISmaccState *)> factoryFunction
std::function< void(std::shared_ptr< smacc2::SmaccEventGenerator >)> fn
std::shared_ptr< EventGeneratorHandler > egh
std::function< void(smacc2::ISmaccState *)> factoryFunction
std::vector< std::shared_ptr< SmaccEventInfo > > sourceEventTypes
SmaccEventInfo(std::shared_ptr< TypeInfo > eventType)
std::shared_ptr< TypeInfo > eventType
std::function< void(smacc2::ISmaccState *)> factoryFunction
std::shared_ptr< StateReactorHandler > srh
std::vector< std::shared_ptr< SmaccEventInfo > > sourceEventTypes
std::weak_ptr< const SmaccStateInfo > sourceState
std::shared_ptr< SmaccEventInfo > eventInfo
std::weak_ptr< const SmaccStateInfo > destinyState
smacc2::introspection::TypeInfo::Ptr transitionTypeInfo
std::function< void(std::shared_ptr< smacc2::StateReactor >)> fn