solverforge_scoring/api/
analysis.rs1use std::fmt::Debug;
7
8use solverforge_core::score::Score;
9use solverforge_core::ConstraintRef;
10
11#[derive(Clone, PartialEq, Eq, Hash)]
16pub struct EntityRef {
17 pub type_name: String,
19 pub display: String,
21}
22
23impl EntityRef {
24 pub fn new<T: Debug + ?Sized>(entity: &T) -> Self {
26 Self {
27 type_name: std::any::type_name::<T>().to_string(),
28 display: format!("{entity:?}"),
29 }
30 }
31
32 pub fn short_type_name(&self) -> &str {
34 self.type_name
35 .rsplit("::")
36 .next()
37 .unwrap_or(&self.type_name)
38 }
39}
40
41impl Debug for EntityRef {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 f.debug_struct("EntityRef")
44 .field("type", &self.short_type_name())
45 .field("display", &self.display)
46 .finish()
47 }
48}
49
50#[derive(Debug, Clone)]
52pub struct ConstraintJustification {
53 pub entities: Vec<EntityRef>,
55 pub description: String,
57}
58
59impl ConstraintJustification {
60 pub fn new(entities: Vec<EntityRef>) -> Self {
62 let description = if entities.is_empty() {
63 "No entities".to_string()
64 } else {
65 entities
66 .iter()
67 .map(|e| e.display.as_str())
68 .collect::<Vec<_>>()
69 .join(", ")
70 };
71 Self {
72 entities,
73 description,
74 }
75 }
76
77 pub fn with_description(entities: Vec<EntityRef>, description: String) -> Self {
79 Self {
80 entities,
81 description,
82 }
83 }
84}
85
86#[derive(Debug, Clone)]
88pub struct DetailedConstraintMatch<'a, Sc: Score> {
89 pub constraint_ref: &'a ConstraintRef,
91 pub score: Sc,
93 pub justification: ConstraintJustification,
95}
96
97impl<'a, Sc: Score> DetailedConstraintMatch<'a, Sc> {
98 pub fn new(
100 constraint_ref: &'a ConstraintRef,
101 score: Sc,
102 justification: ConstraintJustification,
103 ) -> Self {
104 Self {
105 constraint_ref,
106 score,
107 justification,
108 }
109 }
110}
111
112#[derive(Debug, Clone)]
114pub struct ConstraintAnalysis<'a, Sc: Score> {
115 pub constraint_ref: &'a ConstraintRef,
117 pub weight_multiplier: u32,
119 pub score: Sc,
121 pub matches: Vec<DetailedConstraintMatch<'a, Sc>>,
123 pub is_hard: bool,
125}
126
127impl<'a, Sc: Score> ConstraintAnalysis<'a, Sc> {
128 pub fn new(
130 constraint_ref: &'a ConstraintRef,
131 weight_multiplier: u32,
132 score: Sc,
133 matches: Vec<DetailedConstraintMatch<'a, Sc>>,
134 is_hard: bool,
135 ) -> Self {
136 Self {
137 constraint_ref,
138 weight_multiplier,
139 score,
140 matches,
141 is_hard,
142 }
143 }
144
145 pub fn match_count(&self) -> usize {
147 self.matches.len()
148 }
149
150 pub fn name(&self) -> &str {
152 &self.constraint_ref.name
153 }
154}