TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
catalogue.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Component-rule catalogue (design doc 15 §6.1): categories of components with their detectors, roles and
4// layout rules. The source is docs/component_rules.yaml; scripts/crules_catalogue.py converts it to
5// src/crules/catalogue.json, which the build embeds (the engine has no YAML parser), and a ctest keeps the
6// two identical.
7#include <cstdint>
8#include <regex>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
14#include <nlohmann/json.hpp>
15
16namespace tmk::crules {
17
18// What a detector looks at (doc 15 §3.1).
19enum class Signal : std::uint8_t { RefPrefix, LibId, Value, Keywords, PinName, NetName, Topology };
20enum class Severity : std::uint8_t { Hard, Soft, Advisory };
21
22const char* signal_name(Signal s);
23const char* severity_name(Severity s);
24
25struct Detector {
27 std::string pattern; // ECMAScript regex (case-insensitive), or a topology name for Signal::Topology
28 int weight = 0; // 0..100; confidence = min(100, sum of matching weights)
29 std::regex re; // compiled `pattern` (unused for topology)
30};
31
32struct RuleSpec {
33 std::string id, kind, text, evidence, note;
34 std::vector<std::string> applies_to, enforce, sources;
35 nlohmann::ordered_json params;
37 bool enforced_in(std::string_view stage) const;
38};
39
40struct Category {
41 std::string id, name, prefix;
42 std::vector<Detector> detect;
43 std::vector<std::pair<std::string, std::string>> roles; // name -> description, catalogue order
44 std::vector<RuleSpec> rules;
45 bool has_signal(Signal s) const;
46};
47
48struct Catalogue {
49 int version = 0;
50 int apply = 70, suggest = 40; // confidence thresholds (doc 15 §3.2)
51 // Skew ps -> mm without a stackup (doc 15 §4, §5.3): conservative FR-4 delays, outer / inner layers.
53 // General high-speed guidance (defaults.general_high_speed): a stitching via within this distance of a layer change.
54 double stitch_via_within_mm = 5.08;
55 std::vector<Category> categories;
56 int index_of(std::string_view id) const; // -1 if absent
57 std::size_t rule_count() const;
58};
59
60// Parses the catalogue JSON. Throws std::runtime_error naming the offending entry on schema errors (an
61// unknown signal, a weight outside 0..100, a bad regex, a missing rule id/kind/severity).
62Catalogue parse_catalogue(std::string_view json_text);
63Catalogue load_catalogue_file(const std::string& path);
64// The catalogue embedded at build time (src/crules/catalogue.json), parsed once.
66std::string_view builtin_catalogue_json();
67
68} // namespace tmk::crules
const char * severity_name(Severity s)
const Catalogue & builtin_catalogue()
The catalogue embedded at build time (src/crules/catalogue.json), parsed once.
Catalogue parse_catalogue(std::string_view json_text)
Parses the catalogue JSON.
Signal
What a detector looks at (doc 15 §3.1).
Definition catalogue.hpp:19
Catalogue load_catalogue_file(const std::string &path)
const char * signal_name(Signal s)
std::string_view builtin_catalogue_json()
std::size_t rule_count() const
std::vector< Category > categories
Definition catalogue.hpp:55
int suggest
confidence thresholds (doc 15 §3.2)
Definition catalogue.hpp:50
double prop_delay_outer
Skew ps -> mm without a stackup (doc 15 §4, §5.3): conservative FR-4 delays, outer / inner layers.
Definition catalogue.hpp:52
int index_of(std::string_view id) const
-1 if absent
double stitch_via_within_mm
General high-speed guidance (defaults.general_high_speed): a stitching via within this distance of a ...
Definition catalogue.hpp:54
std::vector< RuleSpec > rules
Definition catalogue.hpp:44
std::vector< std::pair< std::string, std::string > > roles
name -> description, catalogue order
Definition catalogue.hpp:43
bool has_signal(Signal s) const
std::vector< Detector > detect
Definition catalogue.hpp:42
int weight
0..100; confidence = min(100, sum of matching weights)
Definition catalogue.hpp:28
std::string pattern
ECMAScript regex (case-insensitive), or a topology name for Signal::Topology.
Definition catalogue.hpp:27
std::regex re
compiled pattern (unused for topology)
Definition catalogue.hpp:29
nlohmann::ordered_json params
Definition catalogue.hpp:35
std::vector< std::string > sources
Definition catalogue.hpp:34
std::vector< std::string > applies_to
Definition catalogue.hpp:34
std::vector< std::string > enforce
Definition catalogue.hpp:34
bool enforced_in(std::string_view stage) const