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