use std::fmt::Debug;
use solverforge_core::score::Score;
use solverforge_core::ConstraintRef;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct EntityRef {
pub type_name: String,
pub display: String,
pub planning_id: Option<String>,
}
impl EntityRef {
pub fn new<T: Debug + ?Sized>(entity: &T) -> Self {
Self {
type_name: std::any::type_name::<T>().to_string(),
display: format!("{entity:?}"),
planning_id: None,
}
}
pub fn with_planning_id(mut self, planning_id: Option<String>) -> Self {
self.planning_id = planning_id;
self
}
pub fn short_type_name(&self) -> &str {
self.type_name
.rsplit("::")
.next()
.unwrap_or(&self.type_name)
}
}
impl Debug for EntityRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EntityRef")
.field("type", &self.short_type_name())
.field("display", &self.display)
.field("planning_id", &self.planning_id)
.finish()
}
}
#[derive(Debug, Clone)]
pub struct ConstraintJustification {
pub entities: Vec<EntityRef>,
pub description: String,
}
impl ConstraintJustification {
pub fn new(entities: Vec<EntityRef>) -> Self {
let description = if entities.is_empty() {
"No entities".to_string()
} else {
entities
.iter()
.map(|e| e.display.as_str())
.collect::<Vec<_>>()
.join(", ")
};
Self {
entities,
description,
}
}
pub fn with_description(entities: Vec<EntityRef>, description: String) -> Self {
Self {
entities,
description,
}
}
}
#[derive(Debug, Clone)]
pub struct DetailedConstraintMatch<'a, Sc: Score> {
pub constraint_ref: &'a ConstraintRef,
pub score: Sc,
pub justification: ConstraintJustification,
}
impl<'a, Sc: Score> DetailedConstraintMatch<'a, Sc> {
pub fn new(
constraint_ref: &'a ConstraintRef,
score: Sc,
justification: ConstraintJustification,
) -> Self {
Self {
constraint_ref,
score,
justification,
}
}
}
#[derive(Debug, Clone)]
pub struct ConstraintAnalysis<'a, Sc: Score> {
pub constraint_ref: &'a ConstraintRef,
pub weight_multiplier: u32,
pub score: Sc,
pub matches: Vec<DetailedConstraintMatch<'a, Sc>>,
pub is_hard: bool,
}
impl<'a, Sc: Score> ConstraintAnalysis<'a, Sc> {
pub fn new(
constraint_ref: &'a ConstraintRef,
weight_multiplier: u32,
score: Sc,
matches: Vec<DetailedConstraintMatch<'a, Sc>>,
is_hard: bool,
) -> Self {
Self {
constraint_ref,
weight_multiplier,
score,
matches,
is_hard,
}
}
pub fn match_count(&self) -> usize {
self.matches.len()
}
pub fn name(&self) -> &str {
&self.constraint_ref.name
}
}