xcsp3_rust/constraints/
xmax_min.rs

1/*=============================================================================
2* parser for CSP instances represented in XCSP3 Format
3*
4* Copyright (c) 2023 xcsp.org (contact @ xcsp.org)
5*
6* Permission is hereby granted, free of charge, to any person obtaining a copy
7* of this software and associated documentation files (the "Software"), to deal
8* in the Software without restriction, including without limitation the rights
9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10* copies of the Software, and to permit persons to whom the Software is
11* furnished to do so, subject to the following conditions:
12*
13* The above copyright notice and this permission notice shall be included in
14* all copies or substantial portions of the Software.
15*
16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22* THE SOFTWARE.
23*=============================================================================
24*/
25
26/*
27* <p>@project_name: xcsp3-rust
28* </p>
29* <p>@author: luhan zhen
30* </p>
31* <p>@date:  2023/7/21 10:42
32* </p>
33* <p>@email: zhenlh20@mails.jlu.edu.cn
34* </p>
35* <p>@version: 1.0
36* </p>
37 * <p>@description: 1.0
38* </p>
39 */
40
41pub 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    // #[derive(Clone)]
54    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, // true if maximum, false if minimum
61    }
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}