xcsp3_rust/constraints/
xmax_min.rs1pub mod xcsp3_core {
42 use crate::constraints::xconstraint_trait::xcsp3_core::XConstraintTrait;
43 use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
44 use crate::data_structs::xrelational_operand::xcsp3_core::Operand;
45 use crate::data_structs::xrelational_operator::xcsp3_core::Operator;
46 use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
47 use crate::utils::utils_functions::xcsp3_utils::list_to_vec_var_val;
48 use crate::variables::xdomain::xcsp3_core::XDomainInteger;
49 use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
50 use std::collections::HashMap;
51 use std::fmt::{Display, Formatter};
52
53 pub struct XMaxMin<'a> {
55 scope: Vec<XVarVal>,
56 map: HashMap<String, &'a XDomainInteger>,
57 set: &'a XVariableSet,
58 operator: Operator,
59 operand: Operand,
60 is_maximum_or_minimum: bool, }
62
63 impl Display for XMaxMin<'_> {
64 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
65 let mut ret: String;
66 if self.is_maximum() {
67 ret = "XMaximum: list = ".to_string();
68 } else {
69 ret = "XMinimum: list = ".to_string();
70 }
71 for e in self.scope.iter() {
72 ret.push('(');
73 ret.push_str(&e.to_string());
74 ret.push_str("), ")
75 }
76 ret.push_str(&format!(
77 " condition = ({:?}, {:?})",
78 self.operator, self.operand
79 ));
80 write!(f, "{}", ret)
81 }
82 }
83
84 impl XConstraintTrait for XMaxMin<'_> {
85 fn get_scope_string(&self) -> &Vec<XVarVal> {
86 &self.scope
87 }
88
89 fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
90 for e in &self.scope {
91 if let XVarVal::IntVar(s) = e {
92 if !self.map.contains_key(s) {
93 if let Ok(vec) = self.set.construct_scope(&[s]) {
94 for (vs, vv) in vec.into_iter() {
95 self.map.insert(vs, vv);
96 }
97 }
98 }
99 }
100 }
101 let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
102 for e in self.map.iter() {
103 scope_vec_var.push((e.0, e.1))
104 }
105 scope_vec_var
106 }
107 }
108
109 impl<'a> XMaxMin<'a> {
110 pub fn from_str(
111 list: &str,
112 condition: &str,
113 is_maximum_or_minimum: bool,
114 set: &'a XVariableSet,
115 ) -> Result<Self, Xcsp3Error> {
116 match list_to_vec_var_val(list) {
117 Ok(scope) => {
118 let condition = condition.replace(['(', ')', ','], " ");
119 let spilt: Vec<&str> = condition.split_whitespace().collect();
120 let ope: Operator = match Operator::get_operator_by_str(spilt[0]) {
121 None => {
122 return Err(Xcsp3Error::get_constraint_sum_error(
123 "parse sum constraint error, ",
124 ))
125 }
126 Some(o) => o,
127 };
128 let rand: Operand = match Operand::get_operand_by_str(&spilt[1..], &ope) {
129 None => {
130 return Err(Xcsp3Error::get_constraint_sum_error(
131 "parse sum constraint error, ",
132 ))
133 }
134 Some(r) => r,
135 };
136 Ok(Self::new(scope, set, ope, rand, is_maximum_or_minimum))
137 }
138 Err(e) => Err(e),
139 }
140 }
141 pub fn new(
142 scope: Vec<XVarVal>,
143 set: &'a XVariableSet,
144 operator: Operator,
145 operand: Operand,
146 is_maximum_or_minimum: bool,
147 ) -> Self {
148 Self {
149 scope,
150 map: Default::default(),
151 set,
152 operator,
153 operand,
154 is_maximum_or_minimum,
155 }
156 }
157 pub fn is_maximum(&self) -> bool {
158 self.is_maximum_or_minimum
159 }
160
161 pub fn is_minimum(&self) -> bool {
162 !self.is_maximum_or_minimum
163 }
164
165 pub fn get_operand(&self) -> &Operand {
166 &self.operand
167 }
168
169 pub fn get_operator(&self) -> &Operator {
170 &self.operator
171 }
172 }
173}