Skip to main content

conjure_cp_core/ast/
variables.rs

1use std::fmt::Display;
2
3use super::categories::{Category, CategoryOf};
4use crate::representation::Representation;
5use conjure_cp_core::ast::DomainPtr;
6use conjure_cp_core::ast::domains::HasDomain;
7use derivative::Derivative;
8use serde::{Deserialize, Serialize};
9
10/// Represents a decision variable within a computational model.
11///
12/// A `DecisionVariable` has a domain that defines the set of values it can take. The domain could be:
13/// - A boolean domain, meaning the variable can only be `true` or `false`.
14/// - An integer domain, meaning the variable can only take specific integer values or a range of integers.
15///
16/// # Fields
17/// - `domain`:
18///   - Type: `Domain`
19///   - Represents the set of possible values that this decision variable can assume. The domain can be a range of integers
20///     (IntDomain) or a boolean domain (BoolDomain).
21///
22/// # Example
23///
24/// use crate::ast::domains::{DecisionVariable, Domain, Range};
25///
26/// let bool_var = DecisionVariable::new(Domain::BoolDomain);
27/// let int_var = DecisionVariable::new(Domain::IntDomain(vec![Range::Bounded(1, 10)]));
28///
29/// println!("Boolean Variable: {}", bool_var);
30/// println!("Integer Variable: {}", int_var);
31
32#[derive(Clone, Debug, Serialize, Deserialize, Derivative)]
33#[derivative(Hash, PartialEq, Eq)]
34pub struct DecisionVariable {
35    pub domain: DomainPtr,
36
37    // use this through [`Declaration`] - in the future, this probably will be stored in
38    // declaration / domain, not here.
39    #[serde(skip)]
40    #[derivative(Hash = "ignore", PartialEq = "ignore")]
41    pub(super) representations: Vec<Vec<Box<dyn Representation>>>,
42}
43
44impl DecisionVariable {
45    pub fn new(domain: DomainPtr) -> DecisionVariable {
46        DecisionVariable {
47            domain,
48            representations: vec![],
49        }
50    }
51}
52
53impl CategoryOf for DecisionVariable {
54    fn category_of(&self) -> Category {
55        Category::Decision
56    }
57}
58
59impl HasDomain for DecisionVariable {
60    fn domain_of(&self) -> DomainPtr {
61        self.domain.clone()
62    }
63}
64
65impl Display for DecisionVariable {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        self.domain.fmt(f)
68    }
69}