solverforge_solver/scope/
step.rs

1//! Step-level scope.
2
3use solverforge_core::domain::PlanningSolution;
4use solverforge_scoring::ScoreDirector;
5
6use super::PhaseScope;
7
8/// Scope for a single step within a phase.
9///
10/// # Type Parameters
11/// * `'t` - Lifetime of the termination flag
12/// * `'a` - Lifetime of the phase scope reference
13/// * `'b` - Lifetime of the solver scope reference
14/// * `S` - The planning solution type
15/// * `D` - The score director type
16pub struct StepScope<'t, 'a, 'b, S: PlanningSolution, D: ScoreDirector<S>> {
17    /// Reference to the parent phase scope.
18    phase_scope: &'a mut PhaseScope<'t, 'b, S, D>,
19    /// Index of this step within the phase (0-based).
20    step_index: u64,
21    /// Score after this step.
22    step_score: Option<S::Score>,
23}
24
25impl<'t, 'a, 'b, S: PlanningSolution, D: ScoreDirector<S>> StepScope<'t, 'a, 'b, S, D> {
26    /// Creates a new step scope.
27    pub fn new(phase_scope: &'a mut PhaseScope<'t, 'b, S, D>) -> Self {
28        let step_index = phase_scope.step_count();
29        Self {
30            phase_scope,
31            step_index,
32            step_score: None,
33        }
34    }
35
36    /// Returns the step index within the phase.
37    pub fn step_index(&self) -> u64 {
38        self.step_index
39    }
40
41    /// Returns the step score.
42    pub fn step_score(&self) -> Option<&S::Score> {
43        self.step_score.as_ref()
44    }
45
46    /// Sets the step score.
47    pub fn set_step_score(&mut self, score: S::Score) {
48        self.step_score = Some(score);
49    }
50
51    /// Marks this step as complete and increments counters.
52    pub fn complete(&mut self) {
53        self.phase_scope.increment_step_count();
54    }
55
56    /// Returns a reference to the phase scope.
57    pub fn phase_scope(&self) -> &PhaseScope<'t, 'b, S, D> {
58        self.phase_scope
59    }
60
61    /// Returns a mutable reference to the phase scope.
62    pub fn phase_scope_mut(&mut self) -> &mut PhaseScope<'t, 'b, S, D> {
63        self.phase_scope
64    }
65
66    /// Returns a reference to the score director.
67    pub fn score_director(&self) -> &D {
68        self.phase_scope.score_director()
69    }
70
71    /// Returns a mutable reference to the score director.
72    pub fn score_director_mut(&mut self) -> &mut D {
73        self.phase_scope.score_director_mut()
74    }
75
76    /// Calculates the current score.
77    pub fn calculate_score(&mut self) -> S::Score {
78        self.phase_scope.calculate_score()
79    }
80}