use std::fmt::Debug;
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::Move;
pub struct PillarChangeMove<S, V> {
entity_indices: Vec<usize>,
descriptor_index: usize,
variable_name: &'static str,
to_value: Option<V>,
getter: fn(&S, usize) -> Option<V>,
setter: fn(&mut S, usize, Option<V>),
}
impl<S, V: Clone> Clone for PillarChangeMove<S, V> {
fn clone(&self) -> Self {
Self {
entity_indices: self.entity_indices.clone(),
descriptor_index: self.descriptor_index,
variable_name: self.variable_name,
to_value: self.to_value.clone(),
getter: self.getter,
setter: self.setter,
}
}
}
impl<S, V: Debug> Debug for PillarChangeMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PillarChangeMove")
.field("entity_indices", &self.entity_indices)
.field("descriptor_index", &self.descriptor_index)
.field("variable_name", &self.variable_name)
.field("to_value", &self.to_value)
.finish()
}
}
impl<S, V> PillarChangeMove<S, V> {
pub fn new(
entity_indices: Vec<usize>,
to_value: Option<V>,
getter: fn(&S, usize) -> Option<V>,
setter: fn(&mut S, usize, Option<V>),
variable_name: &'static str,
descriptor_index: usize,
) -> Self {
Self {
entity_indices,
descriptor_index,
variable_name,
to_value,
getter,
setter,
}
}
pub fn pillar_size(&self) -> usize {
self.entity_indices.len()
}
pub fn to_value(&self) -> Option<&V> {
self.to_value.as_ref()
}
}
impl<S, V> Move<S> for PillarChangeMove<S, V>
where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
{
fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
if self.entity_indices.is_empty() {
return false;
}
let count = score_director.entity_count(self.descriptor_index);
if let Some(&first_idx) = self.entity_indices.first() {
if count.is_none_or(|c| first_idx >= c) {
return false;
}
let current = (self.getter)(score_director.working_solution(), first_idx);
match (¤t, &self.to_value) {
(None, None) => false,
(Some(cur), Some(target)) => cur != target,
_ => true,
}
} else {
false
}
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
let old_values: Vec<(usize, Option<V>)> = self
.entity_indices
.iter()
.map(|&idx| (idx, (self.getter)(score_director.working_solution(), idx)))
.collect();
for &idx in &self.entity_indices {
score_director.before_variable_changed(self.descriptor_index, idx);
}
for &idx in &self.entity_indices {
(self.setter)(
score_director.working_solution_mut(),
idx,
self.to_value.clone(),
);
}
for &idx in &self.entity_indices {
score_director.after_variable_changed(self.descriptor_index, idx);
}
let setter = self.setter;
score_director.register_undo(Box::new(move |s: &mut S| {
for (idx, old_value) in old_values {
setter(s, idx, old_value);
}
}));
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
&self.entity_indices
}
fn variable_name(&self) -> &str {
self.variable_name
}
}