scenario_simulator_v2 C++ API
condition.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__CONDITION_HPP_
16 #define OPENSCENARIO_INTERPRETER__SYNTAX__CONDITION_HPP_
17 
18 #include <algorithm>
19 #include <boost/json.hpp>
20 #include <cstddef>
21 #include <functional>
22 #include <list>
30 #include <pugixml.hpp>
31 #include <tuple>
32 
34 {
35 inline namespace syntax
36 {
37 /* ---- Condition --------------------------------------------------------------
38  *
39  * <xsd:complexType name="Condition">
40  * <xsd:choice>
41  * <xsd:element name="ByEntityCondition" type="ByEntityCondition"/>
42  * <xsd:element name="ByValueCondition" type="ByValueCondition"/>
43  * </xsd:choice>
44  * <xsd:attribute name="name" type="String" use="required"/>
45  * <xsd:attribute name="delay" type="Double" use="required"/>
46  * <xsd:attribute name="conditionEdge" type="ConditionEdge" use="required"/>
47  * </xsd:complexType>
48  *
49  * -------------------------------------------------------------------------- */
51 {
52  const String name;
53 
54  const Double delay;
55 
57 
59 
60 private:
61  struct History
62  {
63  double time;
64  bool result;
65  };
66 
67  std::list<History> histories;
68 
69 public:
70  explicit Condition(const pugi::xml_node & node, Scope & scope);
71 
72  auto evaluate() -> Object;
73 
74 private:
75  template <typename... Booleans>
76  auto update_condition(std::function<bool(Booleans...)> condition) -> Object
77  {
78  histories.push_back({evaluateSimulationTime(), ComplexType::evaluate().as<Boolean>()});
79  if (auto iterator = std::find_if(
80  std::begin(histories), std::end(histories),
81  [this](const auto & entry) { return entry.time > histories.back().time - delay; });
82  static_cast<std::ptrdiff_t>(sizeof...(Booleans)) <=
83  std::distance(std::begin(histories), iterator)) {
84  current_value = std::apply(condition, std::tuple{Booleans((--iterator)->result)...});
85  histories.erase(std::begin(histories), iterator);
86  } else {
87  current_value = false;
88  }
89  return asBoolean(current_value);
90  }
91 };
92 
93 auto operator<<(boost::json::object &, const Condition &) -> boost::json::object &;
94 } // namespace syntax
95 } // namespace openscenario_interpreter
96 
97 #endif // OPENSCENARIO_INTERPRETER__SYNTAX__CONDITION_HPP_
decltype(auto) evaluate(Ts &&... xs) const
Definition: pointer.hpp:120
Definition: scope.hpp:154
static auto evaluateSimulationTime(Ts &&... xs) -> double
Definition: simulator_core.hpp:554
auto asBoolean(bool) -> const Object &
Definition: boolean.cpp:62
auto operator<<(std::ostream &, const Boolean &) -> std::ostream &
Definition: boolean.cpp:46
std::string String
Definition: string.hpp:24
Definition: escape_sequence.hpp:22
Pointer< Expression > Object
Definition: object.hpp:26
auto distance(const geometry_msgs::Pose &pose1, const geometry_msgs::Pose &pose2)
Definition: detection_sensor.cpp:34
Definition: condition_edge.hpp:47
Definition: condition.hpp:51
bool current_value
Definition: condition.hpp:58
Condition(const pugi::xml_node &node, Scope &scope)
Definition: condition.cpp:34
auto evaluate() -> Object
Definition: condition.cpp:48
const ConditionEdge condition_edge
Definition: condition.hpp:56
const String name
Definition: condition.hpp:52
const Double delay
Definition: condition.hpp:54