Skip to main content

ContractRegistry.h File

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

Included Headers

#include <memory> #include <span> #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 an ordered node list to produce a single ValidationReport. It is the entry point used by the Builder/Graph 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 <span>
19#include <string>
20#include <unordered_map>
21#include <utility>
22#include <vector>
23
26
27namespace simaai::neat {
28
41class ContractRegistry final {
42public:
44 using ContractPtr = std::shared_ptr<Contract>;
45
47 ContractRegistry() = default;
48
51 if (!c)
52 return *this;
53 const std::string cid = c->id();
54 if (cid.empty())
55 return *this;
56
57 auto it = by_id_.find(cid);
58 if (it == by_id_.end()) {
59 order_.push_back(cid);
60 }
61 by_id_[cid] = std::move(c);
62 return *this;
63 }
64
66 template <class T, class... Args> ContractRegistry& emplace(Args&&... args) {
67 return add(std::make_shared<T>(std::forward<Args>(args)...));
68 }
69
71 bool remove(const std::string& id) {
72 auto it = by_id_.find(id);
73 if (it == by_id_.end())
74 return false;
75 by_id_.erase(it);
76
77 // Keep deterministic order_: erase id if present.
78 for (auto oit = order_.begin(); oit != order_.end(); ++oit) {
79 if (*oit == id) {
80 order_.erase(oit);
81 break;
82 }
83 }
84 return true;
85 }
86
88 void clear() {
89 by_id_.clear();
90 order_.clear();
91 }
92
94 std::size_t size() const noexcept {
95 return by_id_.size();
96 }
98 bool empty() const noexcept {
99 return by_id_.empty();
100 }
101
103 ContractPtr get(const std::string& id) const {
104 auto it = by_id_.find(id);
105 return (it == by_id_.end()) ? nullptr : it->second;
106 }
107
109 std::vector<std::string> ids() const {
110 return order_;
111 }
112
120 ValidationReport validate(std::span<const std::shared_ptr<Node>> nodes,
121 const ValidationContext& ctx) const {
122 ValidationReport report;
123 report.set_mode(static_cast<int>(ctx.mode));
124
125 for (const auto& id : order_) {
126 auto it = by_id_.find(id);
127 if (it == by_id_.end() || !it->second)
128 continue;
129
130 report.note_contract_run(id);
131
132 try {
133 it->second->validate(nodes, ctx, report);
134 } catch (const std::exception& e) {
135 report.add_issue({
136 .severity = ValidationSeverity::Error,
137 .contract_id = id,
138 .code = "CONTRACT_THREW",
139 .message = std::string("Contract threw exception: ") + e.what(),
140 .node_index = -1,
141 });
142 } catch (...) {
143 report.add_issue({
144 .severity = ValidationSeverity::Error,
145 .contract_id = id,
146 .code = "CONTRACT_THREW",
147 .message = "Contract threw unknown exception",
148 .node_index = -1,
149 });
150 }
151 }
152
153 return report;
154 }
155
156private:
157 std::unordered_map<std::string, ContractPtr> by_id_;
158 std::vector<std::string> order_;
159};
160
161} // namespace simaai::neat

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.