TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
detect.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Category detection and role binding (design doc 15 §3): each footprint is scored against every category's
4// detectors (integer weights, confidence = min(100, sum)); a category instance is the anchor footprint plus the
5// parts, pads and nets that play each role. Pure function of the board, the catalogue and the user override file;
6// output in catalogue order, then natural reference order (doc 15 §2.4).
7#include <string>
8#include <string_view>
9#include <vector>
10
11#include <nlohmann/json.hpp>
12
13#include "crules/catalogue.hpp"
14#include "crules/overrides.hpp"
15#include "crules/topology.hpp"
16#include "model/board.hpp"
17
18namespace tmk::crules {
19
20struct Role {
21 std::string name;
22 std::vector<int> parts; // footprint indices
23 std::vector<int> pads; // pad indices (pins playing the role, e.g. an IC's XTAL pins)
24 std::vector<model::NetId> nets;
25 bool empty() const { return parts.empty() && pads.empty() && nets.empty(); }
26};
27
28// What the user override file (doc 15 §6.3) changed for one rule of one instance.
30 std::string rule; // rule id
31 bool disabled = false;
32 bool has_params = false;
33 nlohmann::ordered_json params; // the rule's parameters after `set` entries (valid when has_params)
34 std::vector<std::string> notes; // "disabled (disable XTAL-04@Y1)", "max_mm = 5 (catalogue 2; set XTAL-02 ...)"
35};
36
37struct Instance {
38 int category = -1; // index into Catalogue::categories
39 int anchor = -1; // footprint index
40 int confidence = 0; // 0..100
41 bool capped = false; // confidence capped because the board has no pin names (doc 15 §3.6)
42 std::vector<std::string> evidence; // matched detectors, e.g. "lib_id +60"
43 std::vector<Role> roles; // bound roles, binding order
44 std::string superseded_by; // set when a conflicting category won the same anchor (doc 15 §3.4)
45 bool asserted = false; // forced by the user override file (`assert`)
46 std::vector<RuleOverride> overrides; // from `disable` and `set` entries, one per rule id
47 const Role* role(std::string_view name) const;
48 const RuleOverride* override_for(std::string_view rule) const;
49 bool disabled(std::string_view rule) const;
50};
51
52struct Detection {
53 std::vector<Instance> instances; // confidence >= suggest threshold, not superseded
54 std::vector<Instance> possible; // below the suggest threshold (or superseded): reported only
55 bool board_has_pin_names = false;
56 // User override file: its name, its entries (as text) and the entries that matched no instance on this board.
57 std::string override_source;
58 std::vector<std::string> override_entries, override_unused;
59};
60
61// The parameters of rule `r` for instance `in`: the catalogue's, or the user's after `set` entries (doc 15 §6.3).
62const nlohmann::ordered_json& rule_params(const Instance& in, const RuleSpec& r);
63
64// Confidence cap when the board carries no pad pin names (KiCad 5 files): doc 15 §3.6.
65inline constexpr int kNoPinNameCap = 60;
66// Instances below this confidence are not even listed as "possible" (they would list every J* and U*).
67inline constexpr int kPossibleMin = 20;
68
69// With `ov` (the user override file, doc 15 §3.5 level 1): references are checked against the board (throws on
70// unknown ones); `deny` removes a category from a part (listed as possible, so a competing category can win);
71// `assert` forces one (confidence 100, wins conflicts; bound by the category's binder, or to the anchor alone when
72// the binder rejects the part); `disable` and `set` are attached to the instances: entries for one reference
73// (@REF) take precedence over global ones, and among equals the later entry wins.
74Detection detect(const model::Board& b, const Catalogue& cat, const Overrides* ov = nullptr);
75
76} // namespace tmk::crules
The board model (design doc 02 §3): every object TraceMaker reads from a .kicad_pcb,...
Component-rule catalogue (design doc 15 §6.1): categories of components with their detectors,...
constexpr int kPossibleMin
Instances below this confidence are not even listed as "possible" (they would list every J* and U*).
Definition detect.hpp:67
constexpr int kNoPinNameCap
Confidence cap when the board carries no pad pin names (KiCad 5 files): doc 15 §3....
Definition detect.hpp:65
Detection detect(const model::Board &b, const Catalogue &cat, const Overrides *ov=nullptr)
With ov (the user override file, doc 15 §3.5 level 1): references are checked against the board (thro...
const nlohmann::ordered_json & rule_params(const Instance &in, const RuleSpec &r)
The parameters of rule r for instance in: the catalogue's, or the user's after set entries (doc 15 §6...
The user override file (design doc 15 §3.5 level 1, §6.3): the user disables rules,...
std::vector< std::string > override_entries
Definition detect.hpp:58
std::string override_source
User override file: its name, its entries (as text) and the entries that matched no instance on this ...
Definition detect.hpp:57
std::vector< Instance > possible
below the suggest threshold (or superseded): reported only
Definition detect.hpp:54
std::vector< std::string > override_unused
Definition detect.hpp:58
std::vector< Instance > instances
confidence >= suggest threshold, not superseded
Definition detect.hpp:53
int confidence
0..100
Definition detect.hpp:40
int category
index into Catalogue::categories
Definition detect.hpp:38
const RuleOverride * override_for(std::string_view rule) const
std::vector< RuleOverride > overrides
from disable and set entries, one per rule id
Definition detect.hpp:46
std::string superseded_by
set when a conflicting category won the same anchor (doc 15 §3.4)
Definition detect.hpp:44
const Role * role(std::string_view name) const
bool disabled(std::string_view rule) const
int anchor
footprint index
Definition detect.hpp:39
bool asserted
forced by the user override file (assert)
Definition detect.hpp:45
bool capped
confidence capped because the board has no pin names (doc 15 §3.6)
Definition detect.hpp:41
std::vector< std::string > evidence
matched detectors, e.g. "lib_id +60"
Definition detect.hpp:42
std::vector< Role > roles
bound roles, binding order
Definition detect.hpp:43
bool empty() const
Definition detect.hpp:25
std::vector< int > pads
pad indices (pins playing the role, e.g. an IC's XTAL pins)
Definition detect.hpp:23
std::vector< int > parts
footprint indices
Definition detect.hpp:22
std::string name
Definition detect.hpp:21
std::vector< model::NetId > nets
Definition detect.hpp:24
What the user override file (doc 15 §6.3) changed for one rule of one instance.
Definition detect.hpp:29
std::vector< std::string > notes
"disabled (disable XTAL-04\@Y1)", "max_mm = 5 (catalogue 2; set XTAL-02 ...)"
Definition detect.hpp:34
nlohmann::ordered_json params
the rule's parameters after set entries (valid when has_params)
Definition detect.hpp:33
std::string rule
rule id
Definition detect.hpp:30
Netlist views of a board for component recognition (doc 15 §3.1 "Topology", §8.0 topology detectors):...