54 [[nodiscard]] std::optional<int32_t>
GetPhaseValue(
const std::string &name)
const {
114 auto initial_value = config_.
GetPhaseValue(config_.initial_phase);
115 if (!initial_value.has_value()) {
116 throw ConfigError(
"PhaseManager",
"initial_phase '" + config_.initial_phase +
117 "' not found in definitions");
119 current_phase_ = *initial_value;
120 previous_phase_ = current_phase_;
124 compiled_conditions_.clear();
125 compiled_conditions_.reserve(config_.transitions.size());
127 for (
size_t i = 0; i < config_.transitions.size(); ++i) {
128 const auto &trans = config_.transitions[i];
130 compiled_conditions_.push_back(parser.
Parse(trans.condition));
132 throw ConfigError(
"PhaseManager",
"invalid condition in transition " +
133 std::to_string(i) +
": " + e.what());
154 previous_phase_ = current_phase_;
155 phase_changed_ =
false;
157 for (
size_t i = 0; i < config_.transitions.size(); ++i) {
158 const auto &trans = config_.transitions[i];
161 if (trans.from_phase != -1 && trans.from_phase != current_phase_) {
167 if (compiled_conditions_[i].Evaluate(registry)) {
169 current_phase_ = trans.to_phase;
170 phase_changed_ =
true;
189 return config_.GetPhaseName(current_phase_);
213 if (config_.entity_prefix.empty()) {
216 return config_.entity_prefix +
".phase";
229 auto initial_value = config_.GetPhaseValue(config_.initial_phase);
230 if (initial_value.has_value()) {
231 current_phase_ = *initial_value;
232 previous_phase_ = current_phase_;
233 phase_changed_ =
false;
245 auto value = config_.GetPhaseValue(phase_name);
246 if (!value.has_value()) {
247 throw ConfigError(
"PhaseManager",
"phase '" + phase_name +
"' not found");
249 previous_phase_ = current_phase_;
250 current_phase_ = *value;
251 phase_changed_ = (current_phase_ != previous_phase_);
258 previous_phase_ = current_phase_;
259 current_phase_ = value;
260 phase_changed_ = (current_phase_ != previous_phase_);
265 std::vector<CompiledCondition<Scalar>> compiled_conditions_;
267 int32_t current_phase_ = 0;
268 int32_t previous_phase_ = 0;
269 bool phase_changed_ =
false;
270 bool configured_ =
false;
Signal-based condition expression parser and evaluator.
Consolidated error handling for Icarus.
Signal Registry (Backplane) for Icarus.
Condition parsing and evaluation errors.
Definition Error.hpp:377
Parser for condition expressions.
Definition ConditionParser.hpp:411
CompiledCondition< Scalar > Parse(const std::string &condition)
Parse a condition string.
Definition ConditionParser.hpp:420
Configuration/parsing errors with optional file context.
Definition Error.hpp:185
std::string GetPhaseSignalPath() const
Get phase signal path for this entity.
Definition PhaseManager.hpp:212
void Reset()
Reset to initial phase.
Definition PhaseManager.hpp:227
std::string CurrentPhaseName() const
Get current phase name.
Definition PhaseManager.hpp:188
void SetPhaseValue(int32_t value)
Force set phase by value (for testing or initialization).
Definition PhaseManager.hpp:257
int32_t CurrentPhase() const
Get current phase value.
Definition PhaseManager.hpp:183
void SetPhase(const std::string &phase_name)
Force set phase (for testing or initialization).
Definition PhaseManager.hpp:244
int32_t PreviousPhase() const
Get previous phase value (before last EvaluateTransitions).
Definition PhaseManager.hpp:195
void EvaluateTransitions(const SignalRegistry< Scalar > ®istry)
Evaluate transition conditions and potentially change phase.
Definition PhaseManager.hpp:149
const PhaseConfig & GetConfig() const
Get the phase configuration.
Definition PhaseManager.hpp:222
bool PhaseChangedThisStep() const
Check if phase changed in last EvaluateTransitions call.
Definition PhaseManager.hpp:200
void Configure(const PhaseConfig &config)
Configure phase manager with phase definitions and transitions.
Definition PhaseManager.hpp:110
bool IsConfigured() const
Check if phase manager is configured.
Definition PhaseManager.hpp:205
Central registry for all simulation signals.
Definition Registry.hpp:37
Definition AggregationTypes.hpp:13
Configuration for phase management.
Definition PhaseManager.hpp:40
std::vector< PhaseTransition > transitions
Transition rules.
Definition PhaseManager.hpp:48
std::optional< int32_t > GetPhaseValue(const std::string &name) const
Get phase integer from name, or nullopt if not found.
Definition PhaseManager.hpp:54
std::map< std::string, int32_t > definitions
Phase name to integer mapping (e.g., {"GROUND": 0, "BOOST": 1, ...}).
Definition PhaseManager.hpp:42
std::string entity_prefix
Entity prefix for phase signal (e.g., "Vehicle" -> "Vehicle.phase").
Definition PhaseManager.hpp:51
std::string GetPhaseName(int32_t value) const
Get phase name from integer, or empty string if not found.
Definition PhaseManager.hpp:63
std::string initial_phase
Initial phase name.
Definition PhaseManager.hpp:45
std::string condition
Condition expression (e.g., "fuel_mass < 0.01").
Definition PhaseManager.hpp:30
PhaseTransition()=default
int32_t from_phase
Source phase (-1 = any phase).
Definition PhaseManager.hpp:28
int32_t to_phase
Destination phase.
Definition PhaseManager.hpp:29
PhaseTransition(int32_t from, int32_t to, std::string cond)
Definition PhaseManager.hpp:33