Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ConditionEvaluator.h
Go to the documentation of this file.
1/**
2 * @file ConditionEvaluator.h
3 * @brief Evaluates structured Condition expressions for Branch/While nodes.
4 * @author Olympe Engine
5 * @date 2026-03-15
6 *
7 * @details
8 * Supports all 6 source combinations for each side of a condition:
9 * - Variable vs Variable (health < max_health)
10 * - Variable vs Const (health > 50)
11 * - Variable vs Pin (health == Node#42.Out)
12 * - Pin vs Pin (Node#42.Out > Node#43.Out)
13 * - Pin vs Const (Node#42.Out >= threshold)
14 * - Const vs Const (50 < 100)
15 *
16 * Graceful handling:
17 * - Missing Variable: logs WARNING, returns false
18 * - Missing Pin: logs WARNING, returns false
19 * - Const: always available, no error
20 *
21 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
22 */
23
24#pragma once
25
26#include <string>
27#include <unordered_map>
28
29#include "TaskGraphTypes.h"
30#include "LocalBlackboard.h"
31
32namespace Olympe {
33
34/**
35 * @class ConditionEvaluator
36 * @brief Stateless evaluator for structured Condition expressions.
37 *
38 * @details
39 * All methods are static. Call EvaluateAll() with the full conditions list
40 * from a TaskNodeDefinition to compute the implicit-AND result.
41 *
42 * Pin values are looked up in the DataPinCache map (keyed as "nodeID:pinName").
43 * The pin field format is "Node#<id>.<pinName>", e.g. "Node#42.Out".
44 */
46public:
47
48 /**
49 * @brief Evaluates all conditions (implicit AND) on a Branch/While node.
50 *
51 * @param conditions The conditions list from the node definition.
52 * @param localBB The entity's local blackboard (for Variable mode).
53 * @param dataPinCache Frame-local data pin cache (for Pin mode).
54 * @param nodeID Node ID used in log messages.
55 * @return true if all conditions evaluate to true (or list is empty).
56 */
57 static bool EvaluateAll(
58 const std::vector<Condition>& conditions,
60 const std::unordered_map<std::string, TaskValue>& dataPinCache,
61 int32_t nodeID);
62
63 /**
64 * @brief Evaluates a single Condition.
65 *
66 * @param cond The condition to evaluate.
67 * @param localBB The entity's local blackboard.
68 * @param dataPinCache Frame-local data pin cache.
69 * @param nodeID Node ID for log context.
70 * @return true if the condition holds; false on any error or when false.
71 */
72 static bool EvaluateCondition(
73 const Condition& cond,
75 const std::unordered_map<std::string, TaskValue>& dataPinCache,
76 int32_t nodeID);
77
78 /**
79 * @brief Resolves the TaskValue for one side of a condition.
80 *
81 * @param mode "Pin", "Variable", or "Const".
82 * @param pin Pin reference string (mode == "Pin").
83 * @param variable Variable name (mode == "Variable").
84 * @param constValue Literal value (mode == "Const").
85 * @param localBB Local blackboard.
86 * @param dataPinCache Data pin cache.
87 * @param nodeID Node ID for logging.
88 * @param outValue Output value (only valid when return is true).
89 * @return true if the value was successfully resolved.
90 */
91 static bool GetConditionValue(
92 const std::string& mode,
93 const std::string& pin,
94 const std::string& variable,
95 const TaskValue& constValue,
97 const std::unordered_map<std::string, TaskValue>& dataPinCache,
98 int32_t nodeID,
100
101 /**
102 * @brief Compares two TaskValues using the given operator.
103 *
104 * Supported operators: "==", "!=", "<", ">", "<=", ">=".
105 * If types differ, attempts Int->Float promotion.
106 * On unresolvable mismatch logs WARNING and returns false.
107 *
108 * @param left Left-hand value.
109 * @param right Right-hand value.
110 * @param op Operator string.
111 * @param nodeID Node ID for logging.
112 * @return Result of the comparison, or false on error.
113 */
114 static bool CompareValues(const TaskValue& left,
115 const TaskValue& right,
116 const std::string& op,
117 int32_t nodeID);
118
119private:
121
122 /// Parses "Node#42.Out" -> nodeID=42, pinName="Out".
123 /// Returns false if the format is not recognised.
124 static bool ParsePinRef(const std::string& pinRef,
126 std::string& outPinName);
127};
128
129} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Runtime key-value store for task graph variables.
Core enumerations and TaskValue type-safe variant for the Atomic Task System.
Stateless evaluator for structured Condition expressions.
static bool GetConditionValue(const std::string &mode, const std::string &pin, const std::string &variable, const TaskValue &constValue, const LocalBlackboard &localBB, const std::unordered_map< std::string, TaskValue > &dataPinCache, int32_t nodeID, TaskValue &outValue)
Resolves the TaskValue for one side of a condition.
static bool EvaluateAll(const std::vector< Condition > &conditions, const LocalBlackboard &localBB, const std::unordered_map< std::string, TaskValue > &dataPinCache, int32_t nodeID)
Evaluates all conditions (implicit AND) on a Branch/While node.
static bool EvaluateCondition(const Condition &cond, const LocalBlackboard &localBB, const std::unordered_map< std::string, TaskValue > &dataPinCache, int32_t nodeID)
Evaluates a single Condition.
static bool CompareValues(const TaskValue &left, const TaskValue &right, const std::string &op, int32_t nodeID)
Compares two TaskValues using the given operator.
static bool ParsePinRef(const std::string &pinRef, int32_t &outNodeID, std::string &outPinName)
Parses "Node#42.Out" -> nodeID=42, pinName="Out".
Simple map-based blackboard for task graph runtime state.
C++14-compliant type-safe value container for task parameters.
< Provides AssetID and INVALID_ASSET_ID
Describes a single condition expression for Branch/While nodes.