From 04cb6db4277c442760176734ea9d15dd0c6f6fb1 Mon Sep 17 00:00:00 2001 From: Kotaro Yoshimoto Date: Tue, 21 Jan 2025 20:04:21 +0900 Subject: [PATCH] refactor: introduce StochasticParameterDistributionBase class --- .../parameter_distribution.hpp | 27 +++++++++++++++++++ .../syntax/histogram.hpp | 5 ++-- .../syntax/normal_distribution.hpp | 7 +++-- .../syntax/poisson_distribution.hpp | 7 +++-- .../syntax/probability_distribution_set.hpp | 7 +++-- .../syntax/uniform_distribution.hpp | 7 +++-- .../src/syntax/histogram.cpp | 2 +- .../src/syntax/normal_distribution.cpp | 2 +- .../src/syntax/poisson_distribution.cpp | 2 +- .../syntax/probability_distribution_set.cpp | 2 +- .../src/syntax/uniform_distribution.cpp | 2 +- 11 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 openscenario/openscenario_interpreter/include/openscenario_interpreter/parameter_distribution.hpp diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/parameter_distribution.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/parameter_distribution.hpp new file mode 100644 index 00000000000..2a6222e833f --- /dev/null +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/parameter_distribution.hpp @@ -0,0 +1,27 @@ +// Copyright 2015 TIER IV, Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef OPENSCENARIO_INTERPRETER__PARAMETER_DISTRIBUTION_HPP_ +#define OPENSCENARIO_INTERPRETER__PARAMETER_DISTRIBUTION_HPP_ + +#include + +namespace openscenario_interpreter +{ +struct StochasticParameterDistributionBase +{ + virtual auto derive() -> Object = 0; +}; +} // namespace openscenario_interpreter +#endif // OPENSCENARIO_INTERPRETER__PARAMETER_DISTRIBUTION_HPP_ diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/histogram.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/histogram.hpp index ad11ee676dc..bb0d8ec666c 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/histogram.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/histogram.hpp @@ -15,6 +15,7 @@ #ifndef OPENSCENARIO_INTERPRETER__SYNTAX__HISTOGRAM_HPP_ #define OPENSCENARIO_INTERPRETER__SYNTAX__HISTOGRAM_HPP_ +#include #include #include #include @@ -34,7 +35,7 @@ inline namespace syntax */ -struct Histogram : public ComplexType, private Scope +struct Histogram : public ComplexType, private Scope, public StochasticParameterDistributionBase { /** * Note: HistogramBin must be stored in continuous range and ascending order, to `bins` @@ -45,7 +46,7 @@ struct Histogram : public ComplexType, private Scope explicit Histogram(const pugi::xml_node &, Scope & scope); - auto evaluate() -> Object; + auto derive() -> Object override; }; } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/normal_distribution.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/normal_distribution.hpp index 34717375214..2b01eaf47dc 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/normal_distribution.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/normal_distribution.hpp @@ -15,6 +15,7 @@ #ifndef OPENSCENARIO_INTERPRETER__SYNTAX__NORMAL_DISTRIBUTION_HPP_ #define OPENSCENARIO_INTERPRETER__SYNTAX__NORMAL_DISTRIBUTION_HPP_ +#include #include #include #include @@ -38,7 +39,9 @@ inline namespace syntax */ -struct NormalDistribution : public ComplexType, private Scope +struct NormalDistribution : public ComplexType, + private Scope, + public StochasticParameterDistributionBase { const Range range; @@ -50,7 +53,7 @@ struct NormalDistribution : public ComplexType, private Scope explicit NormalDistribution(const pugi::xml_node &, Scope & scope); - auto evaluate() -> Object; + auto derive() -> Object override; }; } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/poisson_distribution.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/poisson_distribution.hpp index a94e395f9ff..a044efd45a0 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/poisson_distribution.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/poisson_distribution.hpp @@ -15,6 +15,7 @@ #ifndef OPENSCENARIO_INTERPRETER__SYNTAX__POISSON_DISTRIBUTION_HPP_ #define OPENSCENARIO_INTERPRETER__SYNTAX__POISSON_DISTRIBUTION_HPP_ +#include #include #include #include @@ -36,7 +37,9 @@ inline namespace syntax */ -struct PoissonDistribution : public ComplexType, private Scope +struct PoissonDistribution : public ComplexType, + private Scope, + public StochasticParameterDistributionBase { const Range range; @@ -46,7 +49,7 @@ struct PoissonDistribution : public ComplexType, private Scope explicit PoissonDistribution(const pugi::xml_node &, Scope & scope); - auto evaluate() -> Object; + auto derive() -> Object override; }; } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/probability_distribution_set.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/probability_distribution_set.hpp index f61858e51ff..60262cca11b 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/probability_distribution_set.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/probability_distribution_set.hpp @@ -15,6 +15,7 @@ #ifndef OPENSCENARIO_INTERPRETER__SYNTAX__PROBABILITY_DISTRIBUTION_SET_HPP_ #define OPENSCENARIO_INTERPRETER__SYNTAX__PROBABILITY_DISTRIBUTION_SET_HPP_ +#include #include #include @@ -33,7 +34,9 @@ inline namespace syntax */ -struct ProbabilityDistributionSet : public ComplexType, private Scope +struct ProbabilityDistributionSet : public ComplexType, + private Scope, + public StochasticParameterDistributionBase { const std::vector elements; @@ -41,7 +44,7 @@ struct ProbabilityDistributionSet : public ComplexType, private Scope explicit ProbabilityDistributionSet(const pugi::xml_node &, Scope & scope); - auto evaluate() -> Object; + auto derive() -> Object override; }; } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/uniform_distribution.hpp b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/uniform_distribution.hpp index e29e196c6a1..a643030f58b 100644 --- a/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/uniform_distribution.hpp +++ b/openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/uniform_distribution.hpp @@ -15,6 +15,7 @@ #ifndef OPENSCENARIO_INTERPRETER__SYNTAX__UNIFORM_DISTRIBUTION_HPP_ #define OPENSCENARIO_INTERPRETER__SYNTAX__UNIFORM_DISTRIBUTION_HPP_ +#include #include #include #include @@ -34,7 +35,9 @@ inline namespace syntax */ -struct UniformDistribution : public ComplexType, private Scope +struct UniformDistribution : public ComplexType, + private Scope, + public StochasticParameterDistributionBase { const Range range; @@ -42,7 +45,7 @@ struct UniformDistribution : public ComplexType, private Scope explicit UniformDistribution(const pugi::xml_node &, Scope & scope); - auto evaluate() -> Object; + auto derive() -> Object override; }; } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/src/syntax/histogram.cpp b/openscenario/openscenario_interpreter/src/syntax/histogram.cpp index 03047da210a..3a65b6c9be9 100644 --- a/openscenario/openscenario_interpreter/src/syntax/histogram.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/histogram.cpp @@ -33,6 +33,6 @@ Histogram::Histogram(const pugi::xml_node & node, openscenario_interpreter::Scop { } -auto Histogram::evaluate() -> Object { return make(distribute(random_engine)); } +auto Histogram::derive() -> Object { return make(distribute(random_engine)); } } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/src/syntax/normal_distribution.cpp b/openscenario/openscenario_interpreter/src/syntax/normal_distribution.cpp index 7e26bf0f8e3..690402daa28 100644 --- a/openscenario/openscenario_interpreter/src/syntax/normal_distribution.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/normal_distribution.cpp @@ -29,6 +29,6 @@ NormalDistribution::NormalDistribution( { } -auto NormalDistribution::evaluate() -> Object { return make(distribute(random_engine)); } +auto NormalDistribution::derive() -> Object { return make(distribute(random_engine)); } } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/src/syntax/poisson_distribution.cpp b/openscenario/openscenario_interpreter/src/syntax/poisson_distribution.cpp index 4b1425c634e..1b280b4aedd 100644 --- a/openscenario/openscenario_interpreter/src/syntax/poisson_distribution.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/poisson_distribution.cpp @@ -28,6 +28,6 @@ PoissonDistribution::PoissonDistribution( { } -auto PoissonDistribution::evaluate() -> Object { return make(distribute(random_engine)); } +auto PoissonDistribution::derive() -> Object { return make(distribute(random_engine)); } } // namespace syntax } // namespace openscenario_interpreter diff --git a/openscenario/openscenario_interpreter/src/syntax/probability_distribution_set.cpp b/openscenario/openscenario_interpreter/src/syntax/probability_distribution_set.cpp index 3e18a8b4d6f..b62a22412be 100644 --- a/openscenario/openscenario_interpreter/src/syntax/probability_distribution_set.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/probability_distribution_set.cpp @@ -35,7 +35,7 @@ ProbabilityDistributionSet::ProbabilityDistributionSet( { } -auto ProbabilityDistributionSet::evaluate() -> Object +auto ProbabilityDistributionSet::derive() -> Object { std::size_t index = distribute(random_engine); return elements.at(index); diff --git a/openscenario/openscenario_interpreter/src/syntax/uniform_distribution.cpp b/openscenario/openscenario_interpreter/src/syntax/uniform_distribution.cpp index f50164c89c4..223be5f217e 100644 --- a/openscenario/openscenario_interpreter/src/syntax/uniform_distribution.cpp +++ b/openscenario/openscenario_interpreter/src/syntax/uniform_distribution.cpp @@ -27,6 +27,6 @@ UniformDistribution::UniformDistribution( { } -auto UniformDistribution::evaluate() -> Object { return make(distribute(random_engine)); } +auto UniformDistribution::derive() -> Object { return make(distribute(random_engine)); } } // namespace syntax } // namespace openscenario_interpreter