载入中...
搜索中...
未找到
Condition.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
10#include "common/Value.h"
11
12#include <cstdint>
13#include <functional>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace eve::decision {
20
22enum class ConditionKind : std::uint8_t {
23 All,
24 Any,
25 Not,
26 Compare,
27 HasTag,
33};
34
36enum class CompareOperator : std::uint8_t {
37 Equal,
39 Less,
41 Greater,
43};
44
56
72
74enum class ConditionReasonCode : std::uint32_t {
75 Passed = 0,
76 ChildFailed = 1,
77 NoChildPassed = 2,
78 Negated = 3,
79 MissingValue = 4,
80 ValueMismatch = 5,
81 TagMissing = 6,
85 StateMissing = 10,
86 StateMismatch = 11,
87 AuthorityDenied = 12,
89 PolicyRejected = 14,
92};
93
102
110[[nodiscard]] EVENGINE_API_FOUNDATION const char* conditionKindName(ConditionKind kind) noexcept;
111
119[[nodiscard]] EVENGINE_API_FOUNDATION const char* compareOperatorName(CompareOperator op) noexcept;
120
129 [[nodiscard("ConditionResult must be inspected before the decision is used")]] ConditionResult {
130public:
132 static ConditionResult success(Value evidence = {}, Value details = Value::Object{});
134 static ConditionResult failed(ConditionReasonCode reason, Value evidence = {}, Value details = Value::Object{});
135
137 bool passed() const noexcept { return passed_; }
139 ConditionReasonCode reasonCode() const noexcept { return reason_; }
141 const Value& evidence() const noexcept { return evidence_; }
143 const Value& details() const noexcept { return details_; }
144
145private:
147 ConditionResult(bool passed, ConditionReasonCode reason, Value evidence, Value details)
149 : passed_(passed), reason_(reason), evidence_(std::move(evidence)), details_(std::move(details)) {}
150
151 bool passed_ = false;
152 ConditionReasonCode reason_ = ConditionReasonCode::InvalidCondition;
153 Value evidence_;
154 Value details_;
155};
156
167public:
169 virtual ~EvaluationContext() = default;
170
172 [[nodiscard]] virtual std::optional<Value> value(std::string_view key) const = 0;
174 [[nodiscard]] virtual std::optional<bool> hasTag(std::string_view tag) const = 0;
176 [[nodiscard]] virtual std::optional<Value> attribute(std::string_view key) const = 0;
178 [[nodiscard]] virtual std::optional<Value> resource(std::string_view key) const = 0;
180 [[nodiscard]] virtual std::optional<Value> state(std::string_view key) const = 0;
182 [[nodiscard]] virtual std::optional<bool> authority(std::string_view scope) const = 0;
184 [[nodiscard]] virtual std::optional<ConditionResult> policy(std::string_view name,
185 const Value& arguments) const = 0;
186};
187
196public:
198 Condition() = default;
199
201 static Condition all(std::vector<Condition> children);
203 static Condition any(std::vector<Condition> children);
205 static Condition not_(Condition child);
207 static Condition compare(std::string key, CompareOperator op, Value expected);
209 static Condition hasTag(std::string tag);
211 static Condition hasAttribute(std::string attribute);
213 static Condition hasResource(std::string resource);
215 static Condition stateEquals(std::string key, Value expected);
217 static Condition authorityCheck(std::string scope);
225 static Condition policyCall(std::string name, Value arguments = Value::Object{},
226 std::optional<ScriptConditionDeclaration> scriptDeclaration = std::nullopt);
227
229 ConditionKind kind() const noexcept { return kind_; }
231 const std::vector<Condition>& children() const noexcept { return children_; }
233 const std::string& key() const noexcept { return key_; }
235 CompareOperator compareOperator() const noexcept { return compare_; }
237 const Value& expected() const noexcept { return expected_; }
239 const Value& arguments() const noexcept { return arguments_; }
241 const std::optional<ScriptConditionDeclaration>& scriptDeclaration() const noexcept { return scriptDeclaration_; }
243 [[nodiscard]] bool isValid() const noexcept;
245 [[nodiscard]] ConditionResult evaluate(const EvaluationContext& context) const;
246
247private:
248 explicit Condition(ConditionKind kind) : kind_(kind) {}
249
250 ConditionKind kind_ = ConditionKind::All;
251 std::vector<Condition> children_;
252 std::string key_;
253 CompareOperator compare_ = CompareOperator::Equal;
254 Value expected_;
255 Value arguments_ = Value::Object{};
256 std::optional<ScriptConditionDeclaration> scriptDeclaration_;
257};
258
259} // namespace eve::decision
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
DiagnosticCode code
std::uint32_t key
JobScope scope
TokenKind kind
std::string name
const std::string * tag
std::string resource
int children
Definition TreeMesh.cpp:295
Owning, renderer-independent dynamic values.
const VegetationPresetContext & context
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
Explain one condition evaluation in a UI- and log-friendly form.
Definition Condition.h:129
bool passed() const noexcept
Whether the condition passed.
Definition Condition.h:137
ConditionReasonCode reasonCode() const noexcept
Stable machine-readable reason code.
Definition Condition.h:139
const Value & evidence() const noexcept
Observed value or child evidence owned by this result.
Definition Condition.h:141
const Value & details() const noexcept
Structured details suitable for UI explanation.
Definition Condition.h:143
Immutable condition AST node.
Definition Condition.h:195
const std::optional< ScriptConditionDeclaration > & scriptDeclaration() const noexcept
Return an optional script dependency/determinism declaration.
Definition Condition.h:241
const std::vector< Condition > & children() const noexcept
Return owned children; empty for a leaf node.
Definition Condition.h:231
Condition()=default
Construct an empty All node, which passes vacuously.
const std::string & key() const noexcept
Return the key/tag/scope/name used by a leaf, if applicable.
Definition Condition.h:233
ConditionKind kind() const noexcept
Return this node's kind.
Definition Condition.h:229
const Value & expected() const noexcept
Return the expected value owned by Compare/StateEquals.
Definition Condition.h:237
const Value & arguments() const noexcept
Return owned PolicyCall arguments.
Definition Condition.h:239
CompareOperator compareOperator() const noexcept
Return the comparison operator used by Compare.
Definition Condition.h:235
Read-only state boundary used by every condition evaluation.
Definition Condition.h:166
virtual std::optional< bool > hasTag(std::string_view tag) const =0
Query tag membership; nullopt means the tag source is unavailable.
virtual std::optional< Value > attribute(std::string_view key) const =0
Query an attribute value; nullopt means the attribute is absent.
virtual std::optional< bool > authority(std::string_view scope) const =0
Query authority without changing grants, leases, or state.
virtual std::optional< Value > state(std::string_view key) const =0
Query a state value for StateEquals nodes.
virtual ~EvaluationContext()=default
Releases EvaluationContext resources.
virtual std::optional< Value > resource(std::string_view key) const =0
Query a resource amount or resource marker.
virtual std::optional< ConditionResult > policy(std::string_view name, const Value &arguments) const =0
Evaluate a named read-only policy with owned arguments.
virtual std::optional< Value > value(std::string_view key) const =0
Query a named scalar or structured value for Compare nodes.
CompareOperator
Scalar comparison used by a Compare node.
Definition Condition.h:36
const char * conditionKindName(ConditionKind kind) noexcept
Return the stable lowercase spelling of a condition node kind. @ownership Borrowed process-static tex...
ConditionKind
Node kind in the side-effect-free condition AST.
Definition Condition.h:22
DeterminismLevel
Determinism contract declared by a script-backed condition policy.
Definition Condition.h:46
@ ToleranceBounded
Results may vary within a documented numeric tolerance.
@ TickDeterministic
Deterministic when evaluated at the injected simulation tick.
@ BitExact
Same inputs produce the same bits without an external clock.
@ ExplicitlyNondeterministic
Result is intentionally not suitable for replay or lockstep.
const char * conditionReasonCodeName(ConditionReasonCode code) noexcept
Return the stable lowercase spelling of a condition reason code. @ownership Borrowed process-static t...
Definition Condition.cpp:82
const char * compareOperatorName(CompareOperator op) noexcept
Return the stable lowercase spelling of a comparison operator. @ownership Borrowed process-static tex...
ConditionReasonCode
Stable machine-readable explanation code for a condition outcome.
Definition Condition.h:74
Metadata a script policy must declare before it is used by a condition.
Definition Condition.h:64
std::string name
Stable script policy name.
Definition Condition.h:66
std::vector< std::string > dependencies
Logical state/query names read by the policy.
Definition Condition.h:68
DeterminismLevel determinism
Replay/network determinism contract.
Definition Condition.h:70