scenario_simulator_v2 C++ API
storyboard_element.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
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 #ifndef OPENSCENARIO_INTERPRETER__SYNTAX__STORYBOARD_ELEMENT_HPP_
16 #define OPENSCENARIO_INTERPRETER__SYNTAX__STORYBOARD_ELEMENT_HPP_
17 
18 #include <cstddef>
19 #include <limits>
26 #include <string>
27 #include <type_traits>
28 #include <unordered_map>
29 #include <unordered_set>
30 #include <utility>
31 
33 {
34 inline namespace syntax
35 {
37 {
38 protected:
40 
41  const std::size_t maximum_execution_count = 1;
42 
43  std::size_t current_execution_count = 0;
44 
46 
48 
50 
51 private:
52  std::unordered_map<
53  StoryboardElementState::value_type, std::vector<std::function<void(const StoryboardElement &)>>>
54  callbacks;
55 
56 public:
57  // Storyboard
58  explicit StoryboardElement(const Trigger & stop_trigger) //
60  {
61  }
62 
63  // Act
66  {
67  }
68 
69  // Event
71  const std::size_t maximum_execution_count, const Trigger & start_trigger)
73  {
74  }
75 
76  explicit StoryboardElement(const std::size_t maximum_execution_count = 1)
78  {
79  }
80 
81  auto state() const -> const auto & { return current_state; }
82 
83  template <StoryboardElementState::value_type State>
84  auto is() const
85  {
86  return current_state.as<StoryboardElementState>() == State;
87  }
88 
89  auto override()
90  {
91  if (
95  }
96  return current_state;
97  }
98 
99 private:
100  virtual auto accomplished() const -> bool
101  {
102  return std::all_of(std::begin(elements), std::end(elements), [](auto && element) {
103  assert(element.template is_also<StoryboardElement>());
104  return element.template as<StoryboardElement>()
106  });
107  }
108 
109  virtual auto run() -> void
110  {
111  for (auto && element : elements) {
112  assert(element.is_also<StoryboardElement>());
113  element.evaluate();
114  }
115  }
116 
117  virtual auto start() -> void {}
118 
119  virtual auto stop() -> void
120  {
121  for (auto && element : elements) {
122  assert(element.is_also<StoryboardElement>());
123  element.as<StoryboardElement>().override();
124  element.evaluate();
125  }
126  }
127 
128 protected:
129  auto rename(const std::string & name) const
130  {
131  static std::size_t id = 0;
132  return name.empty() ? std::string("anonymous-") + std::to_string(++id) : name;
133  }
134 
135  std::unordered_set<std::string> names;
136 
137  auto unique(const std::string & name)
138  {
139  [[maybe_unused]] auto [iter, success] = names.emplace(name);
140  return success;
141  }
142 
143  template <typename U, typename Node, typename... Ts>
144  auto readStoryboardElement(const Node & node, Scope & inner_scope, Ts &&... xs)
145  {
146  if (const auto name = rename(readAttribute<String>("name", node, inner_scope)); unique(name)) {
147  auto element = make<U>(node, inner_scope, std::forward<decltype(xs)>(xs)...);
148  inner_scope.insert(name, element);
149  return element;
150  } else {
151  throw SyntaxError(
152  "Detected redefinition of StoryboardElement named ", std::quoted(name), " (class ",
153  makeTypename(typeid(U)), ")");
154  }
155  }
156 
157  template <typename U, typename Node, typename... Ts>
158  auto readCatalogedStoryboardElement(const Node & node, Scope & inner_scope, Ts &&... xs)
159  {
160  if (auto element = CatalogReference(node, inner_scope).make();
161  not unique(element.template as<U>().name)) {
162  throw SyntaxError(
163  "Detected redefinition of StoryboardElement named ",
164  std::quoted(element.template as<U>().name), " (class ", makeTypename(typeid(U)), ")");
165  } else {
166  inner_scope.insert(element.template as<U>().name, element);
167  return element;
168  }
169  }
170 
171 public:
174  std::function<void(const StoryboardElement &)> callback)
175  {
176  callbacks[transition].push_back(callback);
177  }
178 
179  auto transitionTo(const Object & state) -> bool
180  {
182  for (auto && callback : callbacks[current_state.as<StoryboardElementState>()]) {
183  callback(std::as_const(*this));
184  }
185  return current_state == state;
186  }
187 
188  virtual auto evaluate() -> Object
189  {
190  if (stop_trigger.evaluate().as<Boolean>()) {
191  override();
192  }
193 
194  // NOTE: https://releases.asam.net/OpenSCENARIO/1.0.0/ASAM_OpenSCENARIO_BS-1-2_User-Guide_V1-0-0.html#_states_and_transitions_of_storyboardelements
195 
196  // NOTE: The fooTransition state must not return from case fooTransition
197  // because it is a waypoint in the transition to barState.
198 
199  dispatch:
200  switch (state().as<StoryboardElementState>()) {
201  case StoryboardElementState::standbyState: /* ----------------------------
202  *
203  * This is the default initialization state of a StoryboardElement.
204  * When it is in this state, the runtime instantiation of the
205  * StoryboardElement is ready to execute once given a startTrigger. A
206  * runtime instantiation of any StoryboardElement is created once its
207  * parent element is in the standbyState. From the standbyState, the
208  * Story element instantaneously transitions into the runningState.
209  *
210  * ------------------------------------------------------------------- */
212  goto dispatch;
213  } else {
214  return current_state;
215  }
216 
217  case StoryboardElementState::startTransition: /* -------------------------
218  *
219  * The startTransition symbolizes that the execution of the runtime
220  * instantiation is now starting. The startTransition can be used in
221  * conditions to trigger based on this transition.
222  *
223  * ------------------------------------------------------------------- */
224  start();
226  goto dispatch;
227 
228  case StoryboardElementState::runningState: /* ----------------------------
229  *
230  * The runningState symbolizes that the execution of the runtime
231  * instantiation is now ongoing and has not yet accomplished its goal.
232  *
233  * The concept of accomplishing a goal varies depending on the type of
234  * StoryboardElement under consideration:
235  *
236  * Action
237  * An Action's goal is a function of the Action type and cannot be
238  * generalized. Accomplishing an Action's goal will involve meeting
239  * some arbitrary prerequisites related with the Action type (for
240  * example, a SpeedAction accomplishes its goal when the considered
241  * Entity is travelling at the prescribed speed). If an Action is
242  * acting on an EntitySelection, all instances of Entity within the
243  * selection have to complete in order to reach the completeState of
244  * the Action.
245  *
246  * Event
247  * An Event's goal is accomplished when all its Actions are in the
248  * completeState.
249  *
250  * Maneuver
251  * A Maneuver's goal is accomplished when all its Events are in the
252  * completeState.
253  *
254  * ManeuverGroup
255  * A ManeuverGroup's goal is accomplished when all its Maneuvers are
256  * in the completeState.
257  *
258  * Act
259  * An Act's goal is accomplished when all its ManeuverGroups are in
260  * the completeState.
261  *
262  * Story
263  * A Story's goal is accomplished when all its Acts are in the
264  * completeState.
265  *
266  * ------------------------------------------------------------------- */
267  if (run(), accomplished()) {
269  goto dispatch;
270  } else {
271  return current_state;
272  }
273 
274  case StoryboardElementState::endTransition: /* ---------------------------
275  *
276  * The endTransition occurs when the runtime instantiation of the
277  * StoryboardElement accomplishes its goal. Once the endTransition
278  * occurs, a check for completeness is made. A positive outcome moves
279  * the state machine to the completeState, whereas a negative outcome
280  * moves the state machine to the standbyState. The endTransition can
281  * be used in conditions to trigger based on this transition.
282  *
283  * -------------------------------------------------------------------- */
284  transitionTo(
286  goto dispatch;
287 
288  case StoryboardElementState::completeState: /* ---------------------------
289  *
290  * The completeState signals that the runtime instantiation of the
291  * StoryboardElement cannot reach a running state without external
292  * interference. If the affected runtime instantiation of the
293  * StoryboardElement is defined with a maximumExecutionCount, to be
294  * complete implies that there are no more executions left to run, or a
295  * stopTransition has occurred.
296  *
297  * Checking for completeness involves verifying if the given runtime
298  * instantiation of the StoryboardElement still has executions left
299  * upon finishing the runningState. This check returns false if there
300  * are executions left. This check returns true if there are no
301  * executions left, or if the maximumExecutionCount is not defined in
302  * the StoryboardElement.
303  *
304  * Resetting the completeState can only be achieved externally by the
305  * parent StoryboardElement whose child is in the completeState. This
306  * may only occur if the parent initiates a new execution.
307  *
308  * -------------------------------------------------------------------- */
309  return current_state;
310 
311  case StoryboardElementState::skipTransition: /* --------------------------
312  *
313  * Transition marking the moment an element is asked to move to the
314  * runningState but is instead skipped so it remains in the
315  * standbyState (only for Event instances). The skipTransition can be
316  * used in conditions to trigger based on this transition.
317  *
318  * ------------------------------------------------------------------- */
319  throw Error("UNIMPLEMENTED!");
320 
321  default:
322  case StoryboardElementState::stopTransition: /* --------------------------
323  *
324  * The stopTransition marks the reception of a stopTrigger or the
325  * storyboard element is overridden (applicable for Event and Action).
326  * This implies that the stopTransition cannot be reached other than
327  * with an external intervention to the runtime instantiation of the
328  * StoryboardElement.
329  *
330  * When a runtime instantiation of a StoryboardElement goes through a
331  * stopTransition, all of its child elements are also forced to go
332  * through the same transition. The stopTransition can be used in
333  * conditions to trigger based on this transition.
334  *
335  * ------------------------------------------------------------------- */
336  if (not accomplished()) {
337  stop();
338  }
339 
341  goto dispatch;
342  }
343  }
344 };
345 } // namespace syntax
346 } // namespace openscenario_interpreter
347 
348 #endif // OPENSCENARIO_INTERPRETER__SYNTAX__STORYBOARD_ELEMENT_HPP_
auto as() const -> U &
Definition: pointer.hpp:109
Definition: scope.hpp:154
auto insert(const Name &, const Object &) -> void
Definition: scope.cpp:129
Definition: storyboard_element.hpp:37
Object current_state
Definition: storyboard_element.hpp:45
Elements elements
Definition: storyboard_element.hpp:47
auto readStoryboardElement(const Node &node, Scope &inner_scope, Ts &&... xs)
Definition: storyboard_element.hpp:144
auto readCatalogedStoryboardElement(const Node &node, Scope &inner_scope, Ts &&... xs)
Definition: storyboard_element.hpp:158
auto state() const -> const auto &
Definition: storyboard_element.hpp:81
Trigger stop_trigger
Definition: storyboard_element.hpp:39
Trigger start_trigger
Definition: storyboard_element.hpp:49
StoryboardElement(const Trigger &start_trigger, const Trigger &stop_trigger)
Definition: storyboard_element.hpp:64
auto unique(const std::string &name)
Definition: storyboard_element.hpp:137
StoryboardElement(const std::size_t maximum_execution_count=1)
Definition: storyboard_element.hpp:76
StoryboardElement(const std::size_t maximum_execution_count, const Trigger &start_trigger)
Definition: storyboard_element.hpp:70
auto rename(const std::string &name) const
Definition: storyboard_element.hpp:129
auto transitionTo(const Object &state) -> bool
Definition: storyboard_element.hpp:179
virtual auto evaluate() -> Object
Definition: storyboard_element.hpp:188
void addTransitionCallback(StoryboardElementState::value_type transition, std::function< void(const StoryboardElement &)> callback)
Definition: storyboard_element.hpp:172
auto is() const
Definition: storyboard_element.hpp:84
const std::size_t maximum_execution_count
Definition: storyboard_element.hpp:41
StoryboardElement(const Trigger &stop_trigger)
Definition: storyboard_element.hpp:58
std::size_t current_execution_count
Definition: storyboard_element.hpp:43
std::unordered_set< std::string > names
Definition: storyboard_element.hpp:135
const Object standby_state
Definition: storyboard_element_state.cpp:79
const Object end_transition
Definition: storyboard_element_state.cpp:76
const Object running_state
Definition: storyboard_element_state.cpp:80
const Object complete_state
Definition: storyboard_element_state.cpp:81
const Object start_transition
Definition: storyboard_element_state.cpp:75
const Object stop_transition
Definition: storyboard_element_state.cpp:77
auto makeTypename(Ts &&... xs)
Definition: demangle.hpp:30
Definition: escape_sequence.hpp:22
std::list< Object > Elements
Definition: object.hpp:56
constexpr auto make(Ts &&... xs) -> decltype(auto)
Definition: object.hpp:59
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
Definition: object.hpp:42
Definition: object.hpp:30
Definition: catalog_reference.hpp:45
Definition: condition_group.hpp:41
Definition: storyboard_element_state.hpp:48
value_type
Definition: storyboard_element_state.hpp:49
@ completeState
Definition: storyboard_element_state.hpp:126
@ startTransition
Definition: storyboard_element_state.hpp:135
@ stopTransition
Definition: storyboard_element_state.hpp:163
@ runningState
Definition: storyboard_element_state.hpp:103
@ skipTransition
Definition: storyboard_element_state.hpp:173
@ standbyState
Definition: storyboard_element_state.hpp:63
@ endTransition
Definition: storyboard_element_state.hpp:147
auto evaluate() -> Object
Definition: trigger.cpp:27