diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/action.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/action.hpp index 6d394ac56ae..cbe7c76134c 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/action.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/action.hpp @@ -42,6 +42,8 @@ struct Action : public Scope, public ComplexType, public StoryboardElement { bool overridden = false; + std::string type_name; + explicit Action(const pugi::xml_node &, Scope &); using StoryboardElement::evaluate; diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/condition.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/condition.hpp index a6c78b7af44..839e93048b7 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/condition.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/condition.hpp @@ -57,6 +57,8 @@ struct Condition : public ComplexType, private SimulatorCore::ConditionEvaluatio bool current_value; + std::string type_name; + private: struct History { diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/simulation_time_condition.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/simulation_time_condition.hpp index 11a919fe657..424bb910722 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/simulation_time_condition.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/simulation_time_condition.hpp @@ -43,6 +43,8 @@ struct SimulationTimeCondition : private SimulatorCore::ConditionEvaluation Double result; + std::string description_condition_part; + auto description() const -> String; auto evaluate() -> Object; diff --git a/openscenario/openscenario_interpreter/src/openscenario_interpreter.cpp b/openscenario/openscenario_interpreter/src/openscenario_interpreter.cpp index 5c0db9409aa..dec8645870c 100644 --- a/openscenario/openscenario_interpreter/src/openscenario_interpreter.cpp +++ b/openscenario/openscenario_interpreter/src/openscenario_interpreter.cpp @@ -286,6 +286,7 @@ auto Interpreter::on_shutdown(const rclcpp_lifecycle::State &) -> Result auto Interpreter::publishCurrentContext() const -> void { auto start = std::chrono::steady_clock::now(); + static size_t string_size = 0; Context context; { boost::json::monotonic_resource mr; @@ -294,7 +295,15 @@ auto Interpreter::publishCurrentContext() const -> void if (publish_empty_context) { context.data = ""; } else { - context.data = boost::json::serialize(json << *script); + json << *script; + + std::ostringstream os(context.data); + + os << json; + context.data = os.str(); + + string_size = context.data.size(); + auto end = std::chrono::steady_clock::now(); RCLCPP_INFO_STREAM( get_logger(), diff --git a/openscenario/openscenario_interpreter/src/syntax/action.cpp b/openscenario/openscenario_interpreter/src/syntax/action.cpp index 8b5c21794d4..ba67113e616 100644 --- a/openscenario/openscenario_interpreter/src/syntax/action.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/action.cpp @@ -30,6 +30,7 @@ Action::Action(const pugi::xml_node & node, Scope & scope) std::make_pair( "PrivateAction", [this](auto && node) { return make< PrivateAction>(node, local()); }))) // clang-format on { + type_name = apply([](auto && action) { return makeTypename(action.type()); }, *this); } auto Action::accomplished() const -> bool { return ComplexType::accomplished(); } @@ -64,8 +65,7 @@ auto operator<<(boost::json::object & json, const Action & datum) -> boost::json json["currentState"] = boost::lexical_cast(datum.state()); - json["type"] = - apply([](auto && action) { return makeTypename(action.type()); }, datum); + json["type"] = datum.type_name; return json; } diff --git a/openscenario/openscenario_interpreter/src/syntax/condition.cpp b/openscenario/openscenario_interpreter/src/syntax/condition.cpp index e70d6443a76..9acb4b2c9ce 100644 --- a/openscenario/openscenario_interpreter/src/syntax/condition.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/condition.cpp @@ -43,6 +43,17 @@ Condition::Condition(const pugi::xml_node & node, Scope & scope) current_value(false) // clang-format on { + // clang-format off + static const std::unordered_map< + std::type_index, + std::function> table + { + { typeid(ByEntityCondition), [&](const Condition & condition) { return makeTypename(condition.as().type()); } }, + { typeid( ByValueCondition), [&](const Condition & condition) { return makeTypename(condition.as< ByValueCondition>().type()); } }, + }; + // clang-format on + + type_name = table.at(type())(*this); } auto Condition::evaluate() -> Object @@ -76,17 +87,7 @@ auto operator<<(boost::json::object & json, const Condition & datum) -> boost::j json["name"] = datum.name; - // clang-format off - static const std::unordered_map< - std::type_index, - std::function> table - { - { typeid(ByEntityCondition), [&](const Condition & condition) { return makeTypename(condition.as().type()); } }, - { typeid( ByValueCondition), [&](const Condition & condition) { return makeTypename(condition.as< ByValueCondition>().type()); } }, - }; - // clang-format on - - json["type"] = table.at(datum.type())(datum); + json["type"] = datum.type_name; return json; } diff --git a/openscenario/openscenario_interpreter/src/syntax/simulation_time_condition.cpp b/openscenario/openscenario_interpreter/src/syntax/simulation_time_condition.cpp index 865c8197ced..a8ec06ccd10 100644 --- a/openscenario/openscenario_interpreter/src/syntax/simulation_time_condition.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/simulation_time_condition.cpp @@ -12,6 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#define FMT_HEADER_ONLY + +#include + #include #include #include @@ -24,16 +28,16 @@ SimulationTimeCondition::SimulationTimeCondition(const pugi::xml_node & node, Sc : value(readAttribute("value", node, scope)), compare(readAttribute("rule", node, scope)) { + std::stringstream os; + os << "is " << compare << " " << value << "?"; + description_condition_part = os.str(); } auto SimulationTimeCondition::description() const -> String { - std::stringstream description; - - description << "Is the simulation time (= " << std::fixed << std::setprecision(6) << result - << ") is " << compare << " " << value << "?"; - - return description.str(); + return fmt::format( + "Is the simulation time (= {:.30f}) {}", static_cast(result), + description_condition_part); } auto SimulationTimeCondition::evaluate() -> Object diff --git a/openscenario/openscenario_interpreter/src/syntax/trigger.cpp b/openscenario/openscenario_interpreter/src/syntax/trigger.cpp index 51ea2dd41b6..2e64c4f58cd 100644 --- a/openscenario/openscenario_interpreter/src/syntax/trigger.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/trigger.cpp @@ -65,7 +65,7 @@ auto Trigger::activeConditionGroupDescription() const auto operator<<(boost::json::object & json, const Trigger & datum) -> boost::json::object & { - json["currentValue"] = boost::lexical_cast(Boolean(datum.current_value)); + json.emplace("currentValue", boost::lexical_cast(Boolean(datum.current_value))); auto & condition_groups = json["ConditionGroup"].emplace_array();