载入中...
搜索中...
未找到
RuleEngine.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
9#include "common/Result.h"
11#include "emergence/FactStore.h"
12#include "emergence/RuleTypes.h"
14
15#include <cstdint>
16#include <string>
17#include <string_view>
18#include <unordered_map>
19#include <unordered_set>
20#include <vector>
21
22namespace eve::emergence {
23
28
44public:
46 RuleEngine() = default;
48 ~RuleEngine() = default;
49
50 RuleEngine(const RuleEngine&) = delete;
51 RuleEngine& operator=(const RuleEngine&) = delete;
53 RuleEngine(RuleEngine&&) = default;
56
62 [[nodiscard]] eve::Result<int> replaceCatalogue(std::vector<RuleDefinition> rules);
63
69 [[nodiscard]] eve::Result<int> replaceCatalogueJson(std::string_view json);
70
72 void clear();
73
75 [[nodiscard]] int ruleCount() const noexcept { return static_cast<int>(rules_.size()); }
77 [[nodiscard]] bool contains(std::string_view ruleId) const;
85 [[nodiscard]] const RuleDefinition* find(std::string_view ruleId) const;
86
94 [[nodiscard]] const FactStore& facts() const noexcept { return facts_; }
95
97 [[nodiscard]] eve::Result<bool> setValue(std::string key, eve::Value value);
99 [[nodiscard]] eve::Result<bool> setTag(std::string tag, bool present);
101 [[nodiscard]] eve::Result<bool> setAttribute(std::string key, eve::Value value);
103 [[nodiscard]] eve::Result<bool> setResource(std::string key, eve::Value value);
105 [[nodiscard]] eve::Result<bool> setState(std::string key, eve::Value value);
107 [[nodiscard]] eve::Result<bool> setAuthority(std::string scope, bool granted);
113 [[nodiscard]] eve::Result<bool> setPolicy(std::string name, decision::ConditionResult result);
114
125 [[nodiscard]] eve::Result<int> drain(std::uint64_t tick);
126
128 [[nodiscard]] int activationCount() const noexcept { return static_cast<int>(activations_.size()); }
136 [[nodiscard]] const Activation* activationAt(int index) const;
138 void clearActivations();
139
144 [[nodiscard]] eve::Result<void> resetRule(std::string_view ruleId);
145
147 [[nodiscard]] int dirtyCount() const noexcept { return static_cast<int>(dirty_.size()); }
149 [[nodiscard]] std::uint64_t lastDrainEvaluations() const noexcept { return lastDrainEvaluations_; }
151 [[nodiscard]] std::uint64_t wakeKeyEvents() const noexcept { return wakeKeyEvents_; }
152
154 [[nodiscard]] std::string snapshotJson() const;
159 [[nodiscard]] eve::Result<void> restoreJson(std::string_view json);
160
161private:
162 struct RuleRuntime {
163 bool passed = false;
164 bool fired = false;
165 bool disabled = false;
166 std::uint64_t cooldownUntil = 0;
167 };
168
169 void wakeKey(std::string_view key);
170 [[nodiscard]] eve::Result<void> executeActions(const std::vector<EmergenceAction>& actions, std::uint64_t tick);
171 [[nodiscard]] eve::Result<void> executeOne(const EmergenceAction& action, std::uint64_t tick);
172 [[nodiscard]] static eve::Result<RuleDefinition> validateAndNormalize(RuleDefinition rule);
173
174 FactStore facts_;
175 WatchIndex watchIndex_;
176 std::vector<RuleDefinition> rules_;
177 std::vector<RuleRuntime> runtime_;
178 std::unordered_map<std::string, std::uint32_t> idToIndex_;
179 std::unordered_set<std::uint32_t> dirty_;
180 std::vector<Activation> activations_;
181 std::uint64_t nextSequence_ = 1;
182 std::uint64_t lastDrainEvaluations_ = 0;
183 std::uint64_t wakeKeyEvents_ = 0;
184 bool draining_ = false;
185};
186
187} // namespace eve::emergence
double value
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
Authoritative, rule-visible world facts implementing EvaluationContext.
std::uint32_t key
JobScope scope
std::string name
const std::string * tag
std::string action
Definition PlayHost.cpp:117
std::vector< ActionSpec > actions
Definition PlayHost.cpp:126
Move-only, checked operation results for the common layer.
Immutable emergence rule definitions and deferred activations.
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
SimulationTick tick
uint32_t index
Inverted index from fact keys to interested rule ids.
Move-only operation result carrying either a value or Status.
Definition Result.h:155
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Explain one condition evaluation in a UI- and log-friendly form.
Definition Condition.h:129
Mutable fact bag that doubles as a read-only Condition evaluation context.
Definition FactStore.h:60
Indexed, deferred rule evaluator for tens of thousands of conditions.
Definition RuleEngine.h:43
RuleEngine()=default
Rule engine.
std::uint64_t wakeKeyEvents() const noexcept
Diagnostic: distinct keys that woke at least one rule since construction/clear.
Definition RuleEngine.h:151
~RuleEngine()=default
Rule engine.
const FactStore & facts() const noexcept
Borrow the authoritative fact store for read-only inspection. @ownership Borrowed; this engine remain...
Definition RuleEngine.h:94
RuleEngine & operator=(const RuleEngine &)=delete
std::uint64_t lastDrainEvaluations() const noexcept
Diagnostic: condition evaluations performed during the last drain.
Definition RuleEngine.h:149
int ruleCount() const noexcept
Committed rule count.
Definition RuleEngine.h:75
int dirtyCount() const noexcept
Diagnostic: rules marked dirty since the last successful drain start.
Definition RuleEngine.h:147
RuleEngine(const RuleEngine &)=delete
RuleEngine & operator=(RuleEngine &&)=default
Operator =.
int activationCount() const noexcept
Activations retained from the most recent drain batches since clearActivations.
Definition RuleEngine.h:128
RuleEngine(RuleEngine &&)=default
Rule engine.
One scheduled activation produced by drain().
Definition RuleTypes.h:50
One immutable rule definition owned by the catalogue.
Definition RuleTypes.h:38
Handle domain for module-owned rule engines.
Definition RuleEngine.h:25
Handle plus owner-lifetime epoch for a non-ECS registry object.