solverforge_scoring/api/
analysis.rs1use std::any::Any;
8use std::fmt::Debug;
9use std::hash::{Hash, Hasher};
10use std::sync::Arc;
11
12use solverforge_core::score::Score;
13use solverforge_core::ConstraintRef;
14
15#[derive(Clone)]
19pub struct EntityRef {
20 pub type_name: String,
22 pub display: String,
24 entity: Arc<dyn Any + Send + Sync>,
26}
27
28impl EntityRef {
29 pub fn new<T: Clone + Debug + Send + Sync + 'static>(entity: &T) -> Self {
31 Self {
32 type_name: std::any::type_name::<T>().to_string(),
33 display: format!("{:?}", entity),
34 entity: Arc::new(entity.clone()),
35 }
36 }
37
38 pub fn with_display<T: Clone + Send + Sync + 'static>(entity: &T, display: String) -> Self {
40 Self {
41 type_name: std::any::type_name::<T>().to_string(),
42 display,
43 entity: Arc::new(entity.clone()),
44 }
45 }
46
47 pub fn as_entity<T: 'static>(&self) -> Option<&T> {
49 self.entity.downcast_ref::<T>()
50 }
51
52 pub fn short_type_name(&self) -> &str {
54 self.type_name
55 .rsplit("::")
56 .next()
57 .unwrap_or(&self.type_name)
58 }
59}
60
61impl Debug for EntityRef {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 f.debug_struct("EntityRef")
64 .field("type", &self.short_type_name())
65 .field("display", &self.display)
66 .finish()
67 }
68}
69
70impl PartialEq for EntityRef {
71 fn eq(&self, other: &Self) -> bool {
72 self.type_name == other.type_name && self.display == other.display
73 }
74}
75
76impl Eq for EntityRef {}
77
78impl Hash for EntityRef {
79 fn hash<H: Hasher>(&self, state: &mut H) {
80 self.type_name.hash(state);
81 self.display.hash(state);
82 }
83}
84
85#[derive(Debug, Clone)]
87pub struct ConstraintJustification {
88 pub entities: Vec<EntityRef>,
90 pub description: String,
92}
93
94impl ConstraintJustification {
95 pub fn new(entities: Vec<EntityRef>) -> Self {
97 let description = if entities.is_empty() {
98 "No entities".to_string()
99 } else {
100 entities
101 .iter()
102 .map(|e| e.display.as_str())
103 .collect::<Vec<_>>()
104 .join(", ")
105 };
106 Self {
107 entities,
108 description,
109 }
110 }
111
112 pub fn with_description(entities: Vec<EntityRef>, description: String) -> Self {
114 Self {
115 entities,
116 description,
117 }
118 }
119}
120
121#[derive(Debug, Clone)]
123pub struct DetailedConstraintMatch<'a, Sc: Score> {
124 pub constraint_ref: &'a ConstraintRef,
126 pub score: Sc,
128 pub justification: ConstraintJustification,
130}
131
132impl<'a, Sc: Score> DetailedConstraintMatch<'a, Sc> {
133 pub fn new(
135 constraint_ref: &'a ConstraintRef,
136 score: Sc,
137 justification: ConstraintJustification,
138 ) -> Self {
139 Self {
140 constraint_ref,
141 score,
142 justification,
143 }
144 }
145}
146
147#[derive(Debug, Clone)]
149pub struct ConstraintAnalysis<'a, Sc: Score> {
150 pub constraint_ref: &'a ConstraintRef,
152 pub weight_multiplier: u32,
154 pub score: Sc,
156 pub matches: Vec<DetailedConstraintMatch<'a, Sc>>,
158 pub is_hard: bool,
160}
161
162impl<'a, Sc: Score> ConstraintAnalysis<'a, Sc> {
163 pub fn new(
165 constraint_ref: &'a ConstraintRef,
166 weight_multiplier: u32,
167 score: Sc,
168 matches: Vec<DetailedConstraintMatch<'a, Sc>>,
169 is_hard: bool,
170 ) -> Self {
171 Self {
172 constraint_ref,
173 weight_multiplier,
174 score,
175 matches,
176 is_hard,
177 }
178 }
179
180 pub fn match_count(&self) -> usize {
182 self.matches.len()
183 }
184
185 pub fn name(&self) -> &str {
187 &self.constraint_ref.name
188 }
189}