载入中...
搜索中...
未找到
SequenceAsset.cpp
浏览该文件的文档.
2
3#include <string>
4#include <unordered_set>
5#include <utility>
6
7namespace eve::dnut {
8
9namespace {
10
11} // namespace
12
13bool isCoreSequenceNodeType(const std::string& type) noexcept {
14 return type == "branch" || type == "choice" || type == "call" || type == "command" || type == "wait" ||
15 type == "end" || type == "label" || type == "jump" || type == "loop" || type == "break" ||
16 type == "continue" || type == "parallel" || type == "join";
17}
18
20 switch (type) {
21 case SequenceParameterType::Any: return "any";
22 case SequenceParameterType::String: return "string";
23 case SequenceParameterType::Integer: return "integer";
24 case SequenceParameterType::Number: return "number";
25 case SequenceParameterType::Boolean: return "boolean";
26 }
27 return "unknown";
28}
29
30const SequenceNode* SequenceAsset::findNode(const std::string& nodeId) const noexcept {
31 for (const auto& node : nodes) {
32 if (node.id == nodeId) return &node;
33 }
34 return nullptr;
35}
36
38 if (id.empty())
40 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence asset has no id", "id", {}, "dnut.sequence"));
41 if (entry.empty())
43 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence asset has no entry node", "entry", {}, "dnut.sequence"));
44 if (version < 1)
46 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence asset version must be positive", "version", {}, "dnut.sequence"));
47
48 std::unordered_set<std::string> parameterNames;
49 for (const auto& parameter : parameters) {
50 if (parameter.name.empty())
52 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence parameter has an empty name", "parameters", {}, "dnut.sequence"));
53 if (!parameterNames.insert(parameter.name).second)
55 eve::Diagnostic::error(eve::DiagnosticCode::AlreadyExists, "duplicate sequence parameter '" + parameter.name + "'", "parameters." + parameter.name, {}, "dnut.sequence"));
56 if (parameter.required && !parameter.defaultValue.isNull())
58 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "required sequence parameter '" + parameter.name + "' cannot have a default", "parameters." + parameter.name, {}, "dnut.sequence"));
59 }
60
61 std::unordered_set<std::string> ids;
62 for (const auto& node : nodes) {
63 if (node.id.empty())
65 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence node has an empty id", "nodes", {}, "dnut.sequence"));
66 if (!ids.insert(node.id).second)
68 eve::Diagnostic::error(eve::DiagnosticCode::AlreadyExists, "duplicate sequence node id '" + node.id + "'", "nodes." + node.id, {}, "dnut.sequence"));
69 if (node.type.empty())
71 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence node '" + node.id + "' has no type", "nodes." + node.id + ".type", {}, "dnut.sequence"));
72 if (!node.payload.isObject())
74 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence node '" + node.id + "' payload must be an object", "nodes." + node.id + ".payload", {}, "dnut.sequence"));
75 for (const auto& route : node.routes) {
76 if (!route.payload.isObject())
78 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "sequence route payload must be an object", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
79 }
80 }
81
82 if (!findNode(entry))
84 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "entry node '" + entry + "' does not exist", "entry", {}, "dnut.sequence"));
85
86 const auto checkReference = [&](const std::string& owner, const std::string& field,
87 const std::string& reference) -> eve::Result<void> {
90 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "sequence node '" + owner + "' references missing node '" + reference + "'", "nodes." + owner + "." + field, {}, "dnut.sequence"));
91 };
92
93 for (const auto& node : nodes) {
94 auto nextResult = checkReference(node.id, "next", node.next);
95 if (!nextResult.ok()) return nextResult;
96
97 if (node.type == "call") {
98 const eve::Value* target = node.payload.find("target");
99 if (!target || !target->isString() || target->asString().empty())
101 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "call node '" + node.id + "' requires a target asset id", "nodes." + node.id + ".payload.target", {}, "dnut.sequence"));
102 const eve::Value* returnNode = node.payload.find("return");
103 if (returnNode) {
104 if (!returnNode->isString())
106 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "call node '" + node.id + "' return must be a node id", "nodes." + node.id + ".payload.return", {}, "dnut.sequence"));
107 auto returnResult = checkReference(node.id, "payload.return", returnNode->asString());
108 if (!returnResult.ok()) return returnResult;
109 }
110 }
111 if (node.type == "choice") {
112 if (node.routes.empty())
114 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "choice node '" + node.id + "' requires at least one route", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
115 std::unordered_set<std::string> labels;
116 for (const auto& route : node.routes) {
117 if (route.label.empty())
119 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "choice node '" + node.id + "' has a route without a label", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
120 if (!labels.insert(route.label).second)
122 eve::Diagnostic::error(eve::DiagnosticCode::AlreadyExists, "choice node '" + node.id + "' has duplicate route label '" + route.label + "'", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
123 }
124 }
125 if (node.type == "branch") {
126 if (node.routes.empty())
128 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "branch node '" + node.id + "' requires at least one route", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
129 }
130 if (node.type == "label") {
131 const eve::Value* name = node.payload.find("name");
132 if (!name || !name->isString() || name->asString().empty())
134 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "label node '" + node.id + "' requires a name", "nodes." + node.id + ".payload.name", {}, "dnut.sequence"));
135 }
136 if (node.type == "jump") {
137 const eve::Value* target = node.payload.find("target");
138 if (!target || !target->isString() || target->asString().empty())
140 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "jump node '" + node.id + "' requires a resolved target", "nodes." + node.id + ".payload.target", {}, "dnut.sequence"));
141 auto jumpTarget = checkReference(node.id, "payload.target", target->asString());
142 if (!jumpTarget.ok()) return jumpTarget;
143 }
144 if (node.type == "loop") {
145 const eve::Value* mode = node.payload.find("mode");
146 if (!mode || !mode->isString())
148 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "loop node '" + node.id + "' requires mode", "nodes." + node.id + ".payload.mode", {}, "dnut.sequence"));
149 const std::string& modeText = mode->asString();
150 if (modeText != "forever" && modeText != "while" && modeText != "times")
152 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "loop node '" + node.id + "' has unknown mode '" + modeText + "'", "nodes." + node.id + ".payload.mode", {}, "dnut.sequence"));
153 if (node.routes.empty())
155 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "loop node '" + node.id + "' requires a body route", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
156 if (modeText == "times") {
157 const eve::Value* count = node.payload.find("count");
158 if (!count || !count->isNumeric())
160 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "loop times node '" + node.id + "' requires count", "nodes." + node.id + ".payload.count", {}, "dnut.sequence"));
161 }
162 if (modeText == "while" && node.routes.front().condition.isNull())
164 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "loop while node '" + node.id + "' requires a condition", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
165 }
166 if (node.type == "parallel") {
167 if (node.routes.size() < 2)
169 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "parallel node '" + node.id + "' requires at least two lanes", "nodes." + node.id + ".routes", {}, "dnut.sequence"));
170 const eve::Value* join = node.payload.find("join");
171 if (!join || !join->isString() || join->asString().empty())
173 eve::Diagnostic::error(eve::DiagnosticCode::InvariantViolation, "parallel node '" + node.id + "' requires join", "nodes." + node.id + ".payload.join", {}, "dnut.sequence"));
174 auto joinResult = checkReference(node.id, "payload.join", join->asString());
175 if (!joinResult.ok()) return joinResult;
176 }
177 for (const auto& route : node.routes) {
178 auto routeResult = checkReference(node.id, "routes", route.target);
179 if (!routeResult.ok()) return routeResult;
180 }
181 }
182
183 std::unordered_set<std::string> labelNames;
184 for (const auto& node : nodes) {
185 if (node.type != "label") continue;
186 const eve::Value* name = node.payload.find("name");
187 if (!name || !name->isString()) continue;
188 if (!labelNames.insert(name->asString()).second)
190 eve::Diagnostic::error(eve::DiagnosticCode::AlreadyExists, "duplicate label '" + name->asString() + "'", "nodes." + node.id + ".payload.name", {}, "dnut.sequence"));
191 }
193}
194
195} // namespace eve::dnut
LogicalId target
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
std::string nodeId
std::string name
Definition ECS.cpp:154
std::map< std::string, Node > nodes
std::shared_ptr< OceanGpuSpectrum > owner
std::string parameter
const RoadNode * node
Domain-neutral compiled sequence graph for `.dnut` sources.
int count
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
Definition Value.cpp:207
bool isString() const noexcept
Return true when this value is a string.
Definition Value.h:106
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
bool isCoreSequenceNodeType(const std::string &type) noexcept
Return whether type is a control-flow type interpreted by the runtime.
const char * sequenceParameterTypeName(SequenceParameterType type) noexcept
Return the stable lowercase spelling of a parameter type.
SequenceParameterType
Declared value kind for one sequence invocation parameter.
eve::Result< void > validate() const
Validate stable identifiers, the entry point and every graph reference.
const SequenceNode * findNode(const std::string &nodeId) const noexcept
Find one node by its stable identifier.
std::vector< SequenceNode > nodes
std::vector< SequenceParameter > parameters
One immutable step of a compiled sequence.