Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GraphRuntimeInstance.h
Go to the documentation of this file.
1/**
2 * @file GraphRuntimeInstance.h
3 * @brief Single-threaded execution instance for a TaskGraphTemplate.
4 * @author Olympe Engine
5 * @date 2026-03-17
6 *
7 * @details
8 * GraphRuntimeInstance wraps a TaskGraphTemplate and manages the runtime
9 * state needed to execute it step-by-step. It owns a RuntimeEnvironment
10 * (Blackboard + dynamic pin values) and an execution stack.
11 *
12 * Execution model:
13 * - StartExecution() initialises the stack from the graph's entry node.
14 * - StepExecution() advances by one node, evaluating Branch conditions
15 * using ConditionEvaluator.
16 * - IsExecuting() returns false when the stack is empty (done or error).
17 *
18 * Branch node evaluation:
19 * - Each NodeConditionRef is evaluated in order against the environment.
20 * - Multiple conditions are combined using their LogicalOp (And / Or).
21 * - When the combined result is true the node's NextOnSuccess path is taken;
22 * otherwise NextOnFailure.
23 *
24 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
25 */
26
27#pragma once
28
29#include <string>
30#include <vector>
31#include <set>
32#include "../TaskSystem/TaskGraphTemplate.h"
33#include "RuntimeEnvironment.h"
34
35namespace Olympe {
36
37/**
38 * @class GraphRuntimeInstance
39 * @brief Manages single-threaded step-by-step execution of a TaskGraphTemplate.
40 */
42public:
43
44 /**
45 * @brief Constructs an instance bound to the given graph template.
46 *
47 * Call StartExecution() to begin.
48 * @param graph Immutable graph template to execute.
49 */
51
52 // -----------------------------------------------------------------------
53 // Execution control
54 // -----------------------------------------------------------------------
55
56 /**
57 * @brief Initialises the execution stack and marks the instance as running.
58 *
59 * Uses the graph's RootNodeID as the first active node. If RootNodeID is
60 * NODE_INDEX_NONE the call is a no-op and IsExecuting() remains false.
61 */
62 void StartExecution();
63
64 /**
65 * @brief Advances execution by one node.
66 *
67 * Processes the top of the execution stack:
68 * - AtomicTask / other : follows NextOnSuccess (success path).
69 * - Branch : evaluates conditions -> Then or Else path.
70 *
71 * Returns true when execution is still active after this step.
72 * Returns false when the stack becomes empty (execution complete or error).
73 *
74 * @return true while executing; false when done.
75 */
76 bool StepExecution();
77
78 /**
79 * @brief Returns true while there are nodes left to execute.
80 */
81 bool IsExecuting() const;
82
83 // -----------------------------------------------------------------------
84 // Environment
85 // -----------------------------------------------------------------------
86
87 /**
88 * @brief Sets a Blackboard variable in the runtime environment.
89 * @param key Variable identifier.
90 * @param value Float value.
91 */
92 void SetBlackboardVariable(const std::string& key, float value);
93
94 /**
95 * @brief Sets the runtime value of a dynamic data pin.
96 * @param pinID Pin UUID.
97 * @param value Float value delivered by the connected node.
98 */
99 void SetDynamicPinValue(const std::string& pinID, float value);
100
101 // -----------------------------------------------------------------------
102 // Breakpoints
103 // -----------------------------------------------------------------------
104
105 /**
106 * @brief Registers a breakpoint on a node.
107 *
108 * When StepExecution() is about to process a node that has a breakpoint
109 * the execution pauses (StepExecution returns false without consuming the
110 * node) and IsBreakpointHit() returns true.
111 *
112 * @param nodeID Node to break on.
113 */
114 void AddBreakpoint(int32_t nodeID);
115
116 /**
117 * @brief Removes a previously registered breakpoint.
118 * @param nodeID Node whose breakpoint to remove.
119 */
120 void RemoveBreakpoint(int32_t nodeID);
121
122 /**
123 * @brief Returns true when execution is paused at a breakpoint.
124 */
125 bool IsBreakpointHit() const;
126
127 /**
128 * @brief Resumes execution after a breakpoint.
129 *
130 * Clears the breakpoint-hit flag so the next StepExecution() call will
131 * process the current node normally.
132 */
134
135 // -----------------------------------------------------------------------
136 // Inspection
137 // -----------------------------------------------------------------------
138
139 /**
140 * @brief Returns the IDs of all nodes currently on the execution stack.
141 */
142 const std::vector<int32_t>& GetActiveNodeIDs() const;
143
144 /**
145 * @brief Returns the last error message produced during execution.
146 *
147 * Empty when no error has occurred.
148 */
149 const std::string& GetLastError() const;
150
151private:
152
153 /**
154 * @brief Evaluates all NodeConditionRefs on @p node and returns the result.
155 *
156 * Conditions are combined with their LogicalOp (And / Or) in the order
157 * they appear in node.conditionRefs. The LogicalOp of the first entry
158 * (LogicalOp::Start) is ignored.
159 *
160 * @return Combined boolean result; false on evaluation error.
161 */
163
164 const TaskGraphTemplate& m_graph; ///< Bound graph template (not owned)
165 RuntimeEnvironment m_environment; ///< Blackboard + pin values
166 std::vector<int32_t> m_executionStack; ///< Active node IDs (front = next)
167 bool m_isExecuting; ///< True while stack is non-empty
168 bool m_breakpointHit; ///< True when paused at a breakpoint
169 bool m_skipBreakpointOnce; ///< Skip breakpoint check for one step after Resume
170 std::set<int32_t> m_breakpoints; ///< Registered breakpoint node IDs
171 std::string m_lastError; ///< Last error from condition evaluation
172};
173
174} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Runtime context supplying Blackboard variables and dynamic pin values.
Manages single-threaded step-by-step execution of a TaskGraphTemplate.
void SetDynamicPinValue(const std::string &pinID, float value)
Sets the runtime value of a dynamic data pin.
bool IsBreakpointHit() const
Returns true when execution is paused at a breakpoint.
void SetBlackboardVariable(const std::string &key, float value)
Sets a Blackboard variable in the runtime environment.
bool EvaluateBranchConditions(const TaskNodeDefinition &node)
Evaluates all NodeConditionRefs on node and returns the result.
void ResumeFromBreakpoint()
Resumes execution after a breakpoint.
std::string m_lastError
Last error from condition evaluation.
void RemoveBreakpoint(int32_t nodeID)
Removes a previously registered breakpoint.
const TaskGraphTemplate & m_graph
Bound graph template (not owned)
std::vector< int32_t > m_executionStack
Active node IDs (front = next)
const std::string & GetLastError() const
Returns the last error message produced during execution.
RuntimeEnvironment m_environment
Blackboard + pin values.
const std::vector< int32_t > & GetActiveNodeIDs() const
Returns the IDs of all nodes currently on the execution stack.
bool m_isExecuting
True while stack is non-empty.
bool m_skipBreakpointOnce
Skip breakpoint check for one step after Resume.
bool m_breakpointHit
True when paused at a breakpoint.
void AddBreakpoint(int32_t nodeID)
Registers a breakpoint on a node.
bool IsExecuting() const
Returns true while there are nodes left to execute.
void StartExecution()
Initialises the execution stack and marks the instance as running.
bool StepExecution()
Advances execution by one node.
std::set< int32_t > m_breakpoints
Registered breakpoint node IDs.
Provides Blackboard variable values and dynamic pin values at runtime.
Immutable, shareable task graph asset.
< Provides AssetID and INVALID_ASSET_ID
Full description of a single node in the task graph.