载入中...
搜索中...
未找到
SequenceRuntime.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7#include "common/Result.h"
8#include "common/StateValue.h"
9#include "common/Value.h"
12
13#include <cstdint>
14#include <functional>
15#include <optional>
16#include <string>
17#include <string_view>
18#include <unordered_map>
19#include <vector>
20
21namespace eve::dnut {
22
25 bool passed = false;
27 std::string reason;
29 std::string error;
30};
31
42
46 enum class Status : std::uint8_t { Completed, Blocked, Failed };
47
50 std::string error;
51};
52
55
69public:
71 static constexpr std::string_view SaveSchema = "eve.dialogue.runner";
73 static constexpr std::int64_t SaveVersion = 3;
75 static constexpr std::int64_t MinSaveVersion = 2;
76
78 using AssetResolver = std::function<const SequenceAsset*(const std::string&)>;
87
89 enum class EventKind : std::uint8_t { Started, NodeEntered, Blocked, Stepped, Resumed, Ended, Failed };
90
92 struct Event {
93 EventKind kind = EventKind::NodeEntered;
94 std::string assetId;
95 std::string nodeId;
96 std::string detail;
97 };
98
100 using EventSink = std::function<void(const Event&)>;
101
108 static constexpr int kExecutionBudget = 10000;
109
121
127 [[nodiscard]] eve::Result<void> runUntilBlocked();
128
134 [[nodiscard]] eve::Result<void> advance();
135
143 [[nodiscard]] eve::Result<void> select(std::string_view routeLabel);
144
150 [[nodiscard]] eve::Result<void> selectRouteForTransaction(std::string_view routeLabel);
151
159 [[nodiscard]] eve::Result<void> resumeStep(eve::Value result);
160
166 [[nodiscard]] eve::Result<void> resumeCommand(std::string_view requestId, eve::StateValue result);
167
169 [[nodiscard]] eve::Result<void> resumeCommand(std::string_view requestId, eve::Value result);
170
172 void stop();
173
179 [[nodiscard]] eve::Result<void> captureState(eve::Value& out) const;
180
188 [[nodiscard]] eve::Result<void> restoreState(const eve::Value& in);
189
190 void setAssetResolver(AssetResolver resolver) { assetResolver_ = std::move(resolver); }
191 void setConditionEvaluator(ConditionEvaluator evaluator) { conditionEvaluator_ = std::move(evaluator); }
193 void setStepRegistry(const StepKindRegistry* registry) { registry_ = registry; }
195 void registerCommand(std::string name, SequenceCommandHandler handler);
197 void unregisterCommand(std::string_view name);
199 void setCommandDispatcher(SequenceCommandHandler dispatcher) { commandDispatcher_ = std::move(dispatcher); }
201 void clearCommandDispatcher() { commandDispatcher_ = {}; }
209 void setHostContext(void* host) { hostContext_ = host; }
210 void setEventSink(EventSink sink) { eventSink_ = std::move(sink); }
211
213 [[nodiscard]] bool isActive() const { return asset_ != nullptr; }
215 [[nodiscard]] bool isBlocked() const { return blocked_; }
217 [[nodiscard]] bool isWaitingStep() const { return waitingStep_; }
219 [[nodiscard]] bool isWaitingCommand() const { return waitingCommand_; }
221 [[nodiscard]] const SequenceAsset* asset() const { return asset_; }
223 [[nodiscard]] const SequenceNode* currentNode() const;
224 [[nodiscard]] const std::string& currentNodeId() const { return nodeId_; }
225 [[nodiscard]] const eve::Value& bindings() const { return bindings_; }
226 [[nodiscard]] const eve::Value& locals() const { return locals_; }
227 [[nodiscard]] eve::Value& locals() { return locals_; }
229 [[nodiscard]] const eve::Value& lastStepResult() const { return lastStepResult_; }
231 [[nodiscard]] const SequenceCommandRequest* lastCommandRequest() const {
232 return lastCommandRequest_ ? &*lastCommandRequest_ : nullptr;
233 }
235 [[nodiscard]] const std::string& pendingCommandRequestId() const noexcept { return pendingCommandRequestId_; }
241 [[nodiscard]] const SequenceConditionOutcome* lastConditionResult() const {
242 return lastConditionResult_ ? &*lastConditionResult_ : nullptr;
243 }
244
245private:
247 struct LoopFrame {
248 std::string loopNodeId;
249 std::string afterNodeId;
250 std::string mode;
251 int remaining = -1;
252 bool entered = false;
253 };
254
256 struct ParallelLane {
257 std::string nodeId;
258 bool done = false;
259 bool blocked = false;
260 bool waitingStep = false;
261 eve::Value lastStepResult;
262 };
263
265 struct ParallelFrame {
266 std::string parallelNodeId;
267 std::string joinNext;
268 std::string joinNodeId;
269 std::vector<ParallelLane> lanes;
270 std::size_t activeLane = 0;
271 bool hostBlocked = false;
272 };
273
274 struct Frame {
275 const SequenceAsset* asset = nullptr;
276 std::string returnNode;
279 std::vector<LoopFrame> loopStack;
280 std::vector<ParallelFrame> parallelStack;
281 };
282
284 struct ExecutionState {
285 const SequenceAsset* asset = nullptr;
286 std::string nodeId;
289 bool blocked = false;
290 bool waitingStep = false;
291 bool waitingCommand = false;
292 std::vector<Frame> callStack;
293 std::vector<LoopFrame> loopStack;
294 std::vector<ParallelFrame> parallelStack;
295 eve::Value lastStepResult;
296 std::optional<SequenceCommandRequest> lastCommandRequest;
297 std::string pendingCommandRequestId;
298 std::uint64_t commandSequence = 1;
299 std::optional<SequenceConditionOutcome> lastConditionResult;
300 };
301
302 [[nodiscard]] ExecutionState captureExecutionState() const;
303 void restoreExecutionState(ExecutionState state);
304 [[nodiscard]] eve::Result<void> runUntilBlockedImpl();
305 [[nodiscard]] eve::Result<void> enter(const std::string& nodeId);
306 [[nodiscard]] eve::Result<void> fail(eve::DiagnosticCode code, std::string message, std::string path = {});
308 [[nodiscard]] eve::Result<std::string> evaluateRoute(const SequenceNode& node);
309 [[nodiscard]] eve::Result<void> selectImpl(std::string_view routeLabel);
310 [[nodiscard]] eve::Result<void> resumeCommandImpl(std::string_view requestId, eve::Value result);
311 [[nodiscard]] eve::Result<void> handleLoop(const SequenceNode& node);
312 [[nodiscard]] eve::Result<void> handleBreakOrContinue(const SequenceNode& node, bool isBreak);
313 [[nodiscard]] eve::Result<void> handleParallel(const SequenceNode& node);
314 [[nodiscard]] eve::Result<void> handleJoin(const SequenceNode& node);
315 [[nodiscard]] eve::Result<void> driveParallelUntilBlocked();
316 [[nodiscard]] eve::Result<void> stepLaneOnce(ParallelLane& lane);
317 void emit(EventKind kind, const SequenceNode* node = nullptr, const std::string& detail = {}) const;
318
319 const SequenceAsset* asset_ = nullptr;
320 std::string nodeId_;
321 eve::Value bindings_ = eve::Value::Object{};
322 eve::Value locals_ = eve::Value::Object{};
323 bool blocked_ = false;
324 bool waitingStep_ = false;
325 bool waitingCommand_ = false;
326 std::vector<Frame> callStack_;
327 std::vector<LoopFrame> loopStack_;
328 std::vector<ParallelFrame> parallelStack_;
329 AssetResolver assetResolver_;
330 ConditionEvaluator conditionEvaluator_;
331 const StepKindRegistry* registry_ = nullptr;
332 std::unordered_map<std::string, SequenceCommandHandler> commandHandlers_;
333 SequenceCommandHandler commandDispatcher_;
334 void* hostContext_ = nullptr;
335 EventSink eventSink_;
336 eve::Value lastStepResult_;
337 std::optional<SequenceCommandRequest> lastCommandRequest_;
338 std::string pendingCommandRequestId_;
339 std::uint64_t commandSequence_ = 1;
340 std::optional<SequenceConditionOutcome> lastConditionResult_;
342 std::string failureText_;
343};
344
345} // namespace eve::dnut
Duration start
std::string loopNodeId
std::vector< std::vector< CompiledStatement > > lanes
Parallel lane bodies (parallel { lane {…} lane {…} }).
std::string afterNodeId
std::string nodeId
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_PLATFORM
Definition Export.h:107
std::string message
DiagnosticCode code
Poco::JSON::Object::Ptr result
bool done
HSQOBJECT handler
std::vector< ParticleBakeMaterial > bindings
std::string path
Definition PlayHost.cpp:111
const eve::script::BindingContractRegistry & registry
int detail
Move-only, checked operation results for the common layer.
const RoadNode * node
ScriptStructure out
std::string kind
Domain-neutral compiled sequence graph for `.dnut` sources.
Extensible step vocabulary, payload schema and dispatch.
UIHostHandle host
Owning, renderer-independent dynamic values.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:30
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
Caller-owned interpreter over a compiled sequence asset.
void setCommandDispatcher(SequenceCommandHandler dispatcher)
Install the catch-all command dispatcher used after named lookup.
std::function< void(const Event &)> EventSink
Single event outlet; invoked synchronously without locks.
bool isWaitingStep() const
Whether the suspension is a step awaiting resumeStep.
void setEventSink(EventSink sink)
std::function< const SequenceAsset *(const std::string &)> AssetResolver
Resolves a called asset id to a borrowed, externally owned asset.
bool isActive() const
Whether a sequence is currently loaded.
void setHostContext(void *host)
Install the opaque consumer context forwarded to every step handler.
void setConditionEvaluator(ConditionEvaluator evaluator)
bool isBlocked() const
Whether execution is suspended and awaiting host acknowledgement.
void clearCommandDispatcher()
Remove the catch-all command dispatcher.
const std::string & pendingCommandRequestId() const noexcept
Stable id of the blocked command, or an empty string.
const std::string & currentNodeId() const
EventKind
Observable runtime transition.
bool isWaitingCommand() const
Whether the suspension is a command awaiting resumeCommand.
std::function< SequenceConditionOutcome(const eve::Value &)> ConditionEvaluator
Evaluates one authored condition value.
void setStepRegistry(const StepKindRegistry *registry)
Bind the step vocabulary; passing nullptr disables non-core steps.
const SequenceConditionOutcome * lastConditionResult() const
Last evaluated condition outcome.
const SequenceCommandRequest * lastCommandRequest() const
Last emitted command request, or nullptr when none has been emitted.
const eve::Value & locals() const
const SequenceAsset * asset() const
Borrowed active asset, or nullptr. @lifetime Owned by the caller of start.
const eve::Value & lastStepResult() const
Value supplied by the most recent resumeStep.
const eve::Value & bindings() const
void setAssetResolver(AssetResolver resolver)
Canonical step vocabulary owner and runtime handler router.
Result< T > fail(std::string message, DiagnosticCode code=DiagnosticCode::InvalidArgument)
std::function< SequenceCommandResponse(const SequenceCommandRequest &)> SequenceCommandHandler
Synchronous owner-thread handler for one command request.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Immutable, parameterized and versioned compiled sequence.
Immutable request emitted by a generic command node.
eve::Value payload
Complete owning command payload for domain-specific decoding.
Structured response returned by a generic command handler.
Outcome of one authored condition, as seen by the language core.
std::string error
Evaluation failure; non-empty means no route may be selected.
std::string reason
Stable machine-readable reason; empty when passed is true.
One immutable step of a compiled sequence.
One observable transition; every field is owned by the event.