Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ConditionPresetEvaluator.h
Go to the documentation of this file.
1/**
2 * @file ConditionPresetEvaluator.h
3 * @brief Stateless runtime evaluator for ConditionPreset expressions.
4 * @author Olympe Engine
5 * @date 2026-03-17
6 *
7 * @details
8 * ConditionPresetEvaluator is a fully stateless utility class. All methods are
9 * static. It evaluates a ConditionPreset by:
10 *
11 * 1. Resolving the left Operand to a float (via the RuntimeEnvironment).
12 * 2. Resolving the right Operand to a float (via the RuntimeEnvironment).
13 * 3. Applying the ComparisonOp to produce a bool result.
14 *
15 * Operand resolution strategy:
16 * - Variable -> query RuntimeEnvironment::GetBlackboardVariable()
17 * - Const -> use Operand::constValue directly (no environment access)
18 * - Pin -> query RuntimeEnvironment::GetDynamicPinValue()
19 *
20 * All errors are reported through the outErrorMsg parameter; the return value
21 * is always false when an error occurs.
22 *
23 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
24 */
25
26#pragma once
27
28#include <string>
29#include <vector>
30#include "../Editor/ConditionPreset/ConditionPreset.h"
31#include "../Editor/ConditionPreset/NodeConditionRef.h"
32#include "../Editor/ConditionPreset/Operand.h"
33#include "RuntimeEnvironment.h"
34
35namespace Olympe {
36
37// Forward declaration
38class ConditionPresetRegistry;
39
40/**
41 * @class ConditionPresetEvaluator
42 * @brief Evaluates a ConditionPreset against a RuntimeEnvironment.
43 *
44 * @details
45 * Typical usage:
46 * @code
47 * RuntimeEnvironment env;
48 * env.SetBlackboardVariable("mHealth", 1.0f);
49 *
50 * ConditionPreset preset("p1",
51 * Operand::CreateVariable("mHealth"),
52 * ComparisonOp::LessEqual,
53 * Operand::CreateConst(2.0));
54 *
55 * std::string error;
56 * bool result = ConditionPresetEvaluator::Evaluate(preset, env, error);
57 * // result == true, error == ""
58 * @endcode
59 *
60 * For multiple conditions with logical operators:
61 * @code
62 * std::vector<NodeConditionRef> conditions;
63 * // (populated with NodeConditionRef entries referencing presets)
64 *
65 * bool result = ConditionPresetEvaluator::EvaluateConditionChain(
66 * conditions, env, outErrorMsg);
67 * @endcode
68 */
70public:
71
72 /**
73 * @brief Evaluates a single ConditionPreset and returns the boolean result.
74 *
75 * @param preset The condition preset to evaluate.
76 * @param env Runtime environment providing variable and pin values.
77 * @param outErrorMsg Receives a human-readable error description on failure.
78 * Empty on success.
79 * @return true when the condition holds; false on failure or when the
80 * comparison evaluates to false.
81 */
82 static bool Evaluate(
85 std::string& outErrorMsg);
86
87 /**
88 * @brief Evaluates a chain of conditions combined with logical operators (AND/OR).
89 *
90 * @details
91 * Implements short-circuit evaluation:
92 * - For AND chains: stops and returns false at the first false condition
93 * - For OR chains: stops and returns true at the first true condition
94 *
95 * The first condition's LogicalOp is ignored (treated as Start).
96 *
97 * @param conditions Vector of NodeConditionRef objects referencing presets.
98 * Each preset ID is resolved via the provided registry.
99 * @param registry Registry containing the ConditionPreset objects.
100 * @param env Runtime environment providing variable and pin values.
101 * @param outErrorMsg Receives a human-readable error description on failure.
102 * Empty on success.
103 * @return true when the combined condition chain evaluates to true;
104 * false on evaluation failure or when conditions are empty.
105 *
106 * @pre conditions vector must not be empty.
107 * @pre registry must be valid (non-null).
108 */
109 static bool EvaluateConditionChain(
110 const std::vector<NodeConditionRef>& conditions,
113 std::string& outErrorMsg);
114
115private:
116
117 /**
118 * @brief Resolves an Operand to its runtime float value.
119 *
120 * @param operand Operand to resolve.
121 * @param env RuntimeEnvironment used for Variable / Pin lookup.
122 * @param outErrorMsg Receives an error description when resolution fails.
123 * @param outValue Receives the resolved float value on success.
124 * @return true on success; false when the variable / pin is not found.
125 */
126 static bool ResolveOperand(
127 const Operand& operand,
129 std::string& outErrorMsg,
130 float& outValue);
131
132 /**
133 * @brief Applies a ComparisonOp to two float values.
134 *
135 * @param left Left-hand side value.
136 * @param op Operator to apply.
137 * @param right Right-hand side value.
138 * @param outErrorMsg Receives an error description for unknown operators.
139 * @return Result of the comparison, or false on unknown operator.
140 */
141 static bool EvaluateOperator(
142 float left,
143 ComparisonOp op,
144 float right,
145 std::string& outErrorMsg);
146};
147
148} // namespace Olympe
149
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Runtime context supplying Blackboard variables and dynamic pin values.
Evaluates a ConditionPreset against a RuntimeEnvironment.
static bool Evaluate(const ConditionPreset &preset, RuntimeEnvironment &env, std::string &outErrorMsg)
Evaluates a single ConditionPreset and returns the boolean result.
static bool EvaluateOperator(float left, ComparisonOp op, float right, std::string &outErrorMsg)
Applies a ComparisonOp to two float values.
static bool EvaluateConditionChain(const std::vector< NodeConditionRef > &conditions, const ConditionPresetRegistry &registry, RuntimeEnvironment &env, std::string &outErrorMsg)
Evaluates a chain of conditions combined with logical operators (AND/OR).
static bool ResolveOperand(const Operand &operand, RuntimeEnvironment &env, std::string &outErrorMsg, float &outValue)
Resolves an Operand to its runtime float value.
Manages the global pool of ConditionPreset objects.
Provides Blackboard variable values and dynamic pin values at runtime.
< Provides AssetID and INVALID_ASSET_ID
ComparisonOp
The relational operator used in a ConditionPreset.
A globally-stored, reusable condition expression.
One side of a ConditionPreset comparison expression.
Definition Operand.h:45