pub mod xcsp3_core {
use crate::constraints::xconstraint_trait::xcsp3_core::XConstraintTrait;
use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
use crate::data_structs::xrelational_operand::xcsp3_core::Operand;
use crate::data_structs::xrelational_operator::xcsp3_core::Operator;
use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
use crate::utils::utils_functions::xcsp3_utils::list_to_vec_var_val;
use crate::variables::xdomain::xcsp3_core::XDomainInteger;
use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
pub struct XMaxMin<'a> {
scope: Vec<XVarVal>,
map: HashMap<String, &'a XDomainInteger>,
set: &'a XVariableSet,
operator: Operator,
operand: Operand,
is_maximum_or_minimum: bool, }
impl Display for XMaxMin<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut ret: String;
if self.is_maximum() {
ret = "XMaximum: list = ".to_string();
} else {
ret = "XMinimum: list = ".to_string();
}
for e in self.scope.iter() {
ret.push('(');
ret.push_str(&e.to_string());
ret.push_str("), ")
}
ret.push_str(&format!(
" condition = ({:?}, {:?})",
self.operator, self.operand
));
write!(f, "{}", ret)
}
}
impl XConstraintTrait for XMaxMin<'_> {
fn get_scope_string(&self) -> &Vec<XVarVal> {
&self.scope
}
fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
for e in &self.scope {
if let XVarVal::IntVar(s) = e {
if !self.map.contains_key(s) {
if let Ok(vec) = self.set.construct_scope(&[s]) {
for (vs, vv) in vec.into_iter() {
self.map.insert(vs, vv);
}
}
}
}
}
let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
for e in self.map.iter() {
scope_vec_var.push((e.0, e.1))
}
scope_vec_var
}
}
impl<'a> XMaxMin<'a> {
pub fn from_str(
list: &str,
condition: &str,
is_maximum_or_minimum: bool,
set: &'a XVariableSet,
) -> Result<Self, Xcsp3Error> {
match list_to_vec_var_val(list) {
Ok(scope) => {
let condition = condition.replace(['(', ')', ','], " ");
let spilt: Vec<&str> = condition.split_whitespace().collect();
let ope: Operator = match Operator::get_operator_by_str(spilt[0]) {
None => {
return Err(Xcsp3Error::get_constraint_sum_error(
"parse sum constraint error, ",
))
}
Some(o) => o,
};
let rand: Operand = match Operand::get_operand_by_str(&spilt[1..], &ope) {
None => {
return Err(Xcsp3Error::get_constraint_sum_error(
"parse sum constraint error, ",
))
}
Some(r) => r,
};
Ok(Self::new(scope, set, ope, rand, is_maximum_or_minimum))
}
Err(e) => Err(e),
}
}
pub fn new(
scope: Vec<XVarVal>,
set: &'a XVariableSet,
operator: Operator,
operand: Operand,
is_maximum_or_minimum: bool,
) -> Self {
Self {
scope,
map: Default::default(),
set,
operator,
operand,
is_maximum_or_minimum,
}
}
pub fn is_maximum(&self) -> bool {
self.is_maximum_or_minimum
}
pub fn is_minimum(&self) -> bool {
!self.is_maximum_or_minimum
}
pub fn get_operand(&self) -> &Operand {
&self.operand
}
pub fn get_operator(&self) -> &Operator {
&self.operator
}
}
}