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 /// Category - should be quantified or decision variable
44 pub(super) category: Category,
45}
46
47impl DecisionVariable {
48 pub fn new(domain: DomainPtr, category: Category) -> DecisionVariable {
49 assert!(
50 category >= Category::Quantified,
51 "category of a DecisionVariable should be quantified or decision"
52 );
53 DecisionVariable {
54 domain,
55 representations: vec![],
56 category,
57 }
58 }
59}
60
61impl CategoryOf for DecisionVariable {
62 fn category_of(&self) -> Category {
63 self.category
64 }
65}
66
67impl HasDomain for DecisionVariable {
68 fn domain_of(&self) -> DomainPtr {
69 self.domain.clone()
70 }
71}
72
73impl Display for DecisionVariable {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 self.domain.fmt(f)
76 }
77}