1
//! Modifying a model during search.
2
//!
3
//! Incremental solving can be triggered for a solverthrough the
4
//! [`Solver::solve_mut`] method.
5
//!
6
//! This gives access to a [`ModelModifier`] in the solution retrieval callback.
7

            
8
use crate::ast::{Domain, Expression, Name};
9

            
10
use super::private;
11
use super::Solver;
12

            
13
/// A ModelModifier provides an interface to modify a model during solving.
14
///
15
/// Modifications are defined in terms of Conjure AST nodes, so must be translated to a solver
16
/// specfic form before use.
17
///
18
/// It is implementation defined whether these constraints can be given at high level and passed
19
/// through the rewriter, or only low-level solver constraints are supported.
20
///
21
/// See also: [`Solver::solve_mut`].
22
pub trait ModelModifier: private::Sealed {
23
    fn add_constraint(&self, constraint: Expression) -> Result<(), ModificationFailure> {
24
        Err(ModificationFailure::OpNotSupported)
25
    }
26

            
27
    fn add_variable(&self, name: Name, domain: Domain) -> Result<(), ModificationFailure> {
28
        Err(ModificationFailure::OpNotSupported)
29
    }
30
}
31

            
32
/// A [`ModelModifier`] for a solver that does not support incremental solving. Returns
33
/// [`OperationNotSupported`](`ModificationFailure::OperationNotSupported`) for all operations.
34
pub struct NotModifiable;
35

            
36
impl private::Sealed for NotModifiable {}
37
impl ModelModifier for NotModifiable {}
38

            
39
/// The requested modification to the model has failed.
40
#[non_exhaustive]
41
pub enum ModificationFailure {
42
    /// The desired operation is not supported for this solver adaptor.
43
    OpNotSupported,
44

            
45
    /// The desired operation is supported by this solver adaptor, but has not been
46
    /// implemented yet.
47
    OpNotImplemented,
48

            
49
    // The arguments given to the operation are invalid.
50
    ArgsInvalid(anyhow::Error),
51

            
52
    /// An unspecified error has occurred.
53
    Error(anyhow::Error),
54
}