Skip to main content

ContractRegistry.h File

Contract registry for builder-level validation. More...

Included Headers

#include <memory> #include <string> #include <unordered_map> #include <utility> #include <vector> #include "contracts/Contract.h" #include "contracts/ValidationReport.h"

Namespaces Index

namespacesimaai
namespaceneat

Classes Index

classContractRegistry

Holds a set of Contracts and runs them to produce a ValidationReport. More...

Description

Contract registry for builder-level validation.

ContractRegistry aggregates a deterministic, ordered set of Contracts and runs them all against a NodeGroup to produce a single ValidationReport. It is the entry point used by the Builder/Session at validate()/run() time and by CI tools that want to check a pipeline without going to PLAYING.

See Also

Validators::DefaultRegistry

File Listing

The file content with the documentation metadata removed is:

1// include/contracts/ContractRegistry.h
15#pragma once
16
17#include <memory>
18#include <string>
19#include <unordered_map>
20#include <utility>
21#include <vector>
22
25
26namespace simaai::neat {
27
40class ContractRegistry final {
41public:
43 using ContractPtr = std::shared_ptr<Contract>;
44
46 ContractRegistry() = default;
47
50 if (!c)
51 return *this;
52 const std::string cid = c->id();
53 if (cid.empty())
54 return *this;
55
56 auto it = by_id_.find(cid);
57 if (it == by_id_.end()) {
58 order_.push_back(cid);
59 }
60 by_id_[cid] = std::move(c);
61 return *this;
62 }
63
65 template <class T, class... Args> ContractRegistry& emplace(Args&&... args) {
66 return add(std::make_shared<T>(std::forward<Args>(args)...));
67 }
68
70 bool remove(const std::string& id) {
71 auto it = by_id_.find(id);
72 if (it == by_id_.end())
73 return false;
74 by_id_.erase(it);
75
76 // Keep deterministic order_: erase id if present.
77 for (auto oit = order_.begin(); oit != order_.end(); ++oit) {
78 if (*oit == id) {
79 order_.erase(oit);
80 break;
81 }
82 }
83 return true;
84 }
85
87 void clear() {
88 by_id_.clear();
89 order_.clear();
90 }
91
93 std::size_t size() const noexcept {
94 return by_id_.size();
95 }
97 bool empty() const noexcept {
98 return by_id_.empty();
99 }
100
102 ContractPtr get(const std::string& id) const {
103 auto it = by_id_.find(id);
104 return (it == by_id_.end()) ? nullptr : it->second;
105 }
106
108 std::vector<std::string> ids() const {
109 return order_;
110 }
111
119 ValidationReport validate(const NodeGroup& nodes, const ValidationContext& ctx) const {
120 ValidationReport report;
121 report.set_mode(static_cast<int>(ctx.mode));
122
123 for (const auto& id : order_) {
124 auto it = by_id_.find(id);
125 if (it == by_id_.end() || !it->second)
126 continue;
127
128 report.note_contract_run(id);
129
130 try {
131 it->second->validate(nodes, ctx, report);
132 } catch (const std::exception& e) {
133 report.add_issue({
134 .severity = ValidationSeverity::Error,
135 .contract_id = id,
136 .code = "CONTRACT_THREW",
137 .message = std::string("Contract threw exception: ") + e.what(),
138 .node_index = -1,
139 });
140 } catch (...) {
141 report.add_issue({
142 .severity = ValidationSeverity::Error,
143 .contract_id = id,
144 .code = "CONTRACT_THREW",
145 .message = "Contract threw unknown exception",
146 .node_index = -1,
147 });
148 }
149 }
150
151 return report;
152 }
153
154private:
155 std::unordered_map<std::string, ContractPtr> by_id_;
156 std::vector<std::string> order_;
157};
158
159} // namespace simaai::neat

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.