Skip to main content

Validators.h File

Built-in contract implementations and default registry. More...

Included Headers

#include <memory> #include <string> #include <utility> #include "builder/Node.h" #include "builder/NodeGroup.h" #include "contracts/Contract.h" #include "contracts/ContractRegistry.h" #include "contracts/ValidationReport.h"

Namespaces Index

namespacesimaai
namespaceneat
namespacevalidators

Description

Built-in contract implementations and default registry.

File Listing

The file content with the documentation metadata removed is:

1
6// include/contracts/Validators.h
7#pragma once
8
9#include <memory>
10#include <string>
11#include <utility>
12
13#include "builder/Node.h"
14#include "builder/NodeGroup.h"
18
19namespace simaai::neat {
20namespace validators {
21
22// -----------------------------
23// Built-in Contract factories
24// -----------------------------
25
29inline std::shared_ptr<Contract> NonEmptyPipeline() {
30 class C final : public Contract {
31 public:
32 std::string id() const override {
33 return "NonEmptyPipeline";
34 }
35 std::string description() const override {
36 return "Pipeline must contain at least one node.";
37 }
38
39 void validate(const NodeGroup& nodes, const ValidationContext& ctx,
40 ValidationReport& r) const override {
41 (void)ctx;
42 if (nodes.empty()) {
43 r.add_error(id(), "EMPTY_PIPELINE", "No nodes were added to the pipeline.");
44 }
45 }
46 };
47 return std::make_shared<C>();
48}
49
53inline std::shared_ptr<Contract> NoNullNodes() {
54 class C final : public Contract {
55 public:
56 std::string id() const override {
57 return "NoNullNodes";
58 }
59 std::string description() const override {
60 return "All nodes must be non-null shared_ptr.";
61 }
62
63 void validate(const NodeGroup& nodes, const ValidationContext& ctx,
64 ValidationReport& r) const override {
65 (void)ctx;
66 const auto& v = nodes.nodes();
67 for (int i = 0; i < static_cast<int>(v.size()); ++i) {
68 if (!v[static_cast<std::size_t>(i)]) {
69 r.add_error(id(), "NULL_NODE", "Null node pointer in NodeGroup.", i);
70 }
71 }
72 }
73 };
74 return std::make_shared<C>();
75}
76
82inline std::shared_ptr<Contract> SinkLastForRun(std::string sink_kind = "Output") {
83 class C final : public Contract {
84 public:
85 explicit C(std::string kind) : sink_kind_(std::move(kind)) {}
86 std::string id() const override {
87 return "SinkLastForRun";
88 }
89 std::string description() const override {
90 return "When running, the pipeline must end with the terminal appsink node.";
91 }
92
93 void validate(const NodeGroup& nodes, const ValidationContext& ctx,
94 ValidationReport& r) const override {
96 return;
97 const auto& v = nodes.nodes();
98 if (v.empty())
99 return;
100
101 int last_idx = static_cast<int>(v.size()) - 1;
102 const auto& last = v.back();
103 const std::string last_kind = last ? last->kind() : "";
104
105 // Require sink kind last.
106 if (!last || last_kind != sink_kind_) {
107 r.add_error(id(), "SINK_NOT_LAST", "Last node must be " + sink_kind_ + " for run().",
108 last_idx, last_kind, last ? last->user_label() : "");
109 }
110
111 // Disallow additional sinks earlier (best-effort sanity).
112 for (int i = 0; i < last_idx; ++i) {
113 const auto& n = v[static_cast<std::size_t>(i)];
114 if (n && n->kind() == sink_kind_) {
115 r.add_error(id(), "MULTIPLE_SINKS",
116 "Found " + sink_kind_ + " before the end of the pipeline.", i, n->kind(),
117 n->user_label());
118 }
119 }
120 }
121
122 private:
123 std::string sink_kind_;
124 };
125 return std::make_shared<C>(std::move(sink_kind));
126}
127
133inline std::shared_ptr<Contract> RtspRequiresSource(std::string source_kind = "StillImageInput") {
134 class C final : public Contract {
135 public:
136 explicit C(std::string k) : src_kind_(std::move(k)) {}
137 std::string id() const override {
138 return "RtspRequiresSource";
139 }
140 std::string description() const override {
141 return "RTSP mode requires a server-side source node (e.g., StillImageInput).";
142 }
143
144 void validate(const NodeGroup& nodes, const ValidationContext& ctx,
145 ValidationReport& r) const override {
147 return;
148
149 const auto& v = nodes.nodes();
150 bool found = false;
151 for (int i = 0; i < static_cast<int>(v.size()); ++i) {
152 const auto& n = v[static_cast<std::size_t>(i)];
153 if (n && n->kind() == src_kind_) {
154 found = true;
155 break;
156 }
157 }
158
159 if (!found) {
160 r.add_error(id(), "RTSP_SOURCE_MISSING",
161 "RTSP mode requires a node of kind \"" + src_kind_ + "\".", -1, src_kind_, "");
162 }
163 }
164
165 private:
166 std::string src_kind_;
167 };
168 return std::make_shared<C>(std::move(source_kind));
169}
170
171// -----------------------------
172// Default registry
173// -----------------------------
174
182 reg.add(NonEmptyPipeline());
183 reg.add(NoNullNodes());
184 reg.add(SinkLastForRun());
186 return reg;
187}
188
189} // namespace validators
190} // namespace simaai::neat

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.1.