xcsp3_rust/constraints/
xordered.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/15 14:56
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_operator::xcsp3_core::Operator;
45    use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
46    use std::collections::HashMap;
47    use std::fmt::{Display, Formatter};
48
49    use crate::utils::utils_functions::xcsp3_utils::list_to_vec_var_val;
50    use crate::variables::xdomain::xcsp3_core::XDomainInteger;
51    use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
52
53    // #[derive(Clone)]
54    pub struct XOrdered<'a> {
55        scope: Vec<XVarVal>,
56        map: HashMap<String, &'a XDomainInteger>,
57        set: &'a XVariableSet,
58        lengths: Option<Vec<XVarVal>>,
59        operator: Operator,
60    }
61
62    impl Display for XOrdered<'_> {
63        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64            let mut ret = String::default();
65            for e in self.scope.iter() {
66                ret.push('(');
67                ret.push_str(&e.to_string());
68                ret.push_str("), ")
69            }
70            if let Some(vc) = &self.lengths {
71                ret.push_str("lengths = (");
72                for (i, e) in vc.iter().enumerate() {
73                    ret.push_str(&e.to_string());
74                    if i != vc.len() - 1 {
75                        ret.push_str(", ")
76                    }
77                }
78                ret.push_str("), ")
79            }
80            write!(
81                f,
82                "XOrdered: scope =  {} operator = {:?}",
83                ret, self.operator
84            )
85        }
86    }
87
88    impl XConstraintTrait for XOrdered<'_> {
89        fn get_scope_string(&self) -> &Vec<XVarVal> {
90            &self.scope
91        }
92
93        fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
94            for e in &self.scope {
95                if let XVarVal::IntVar(s) = e {
96                    if !self.map.contains_key(s) {
97                        if let Ok(vec) = self.set.construct_scope(&[s]) {
98                            for (vs, vv) in vec.into_iter() {
99                                self.map.insert(vs, vv);
100                            }
101                        }
102                    }
103                }
104            }
105            let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
106            for e in self.map.iter() {
107                scope_vec_var.push((e.0, e.1))
108            }
109            scope_vec_var
110        }
111    }
112
113    impl<'a> XOrdered<'a> {
114        pub fn from_str(
115            list: &str,
116            lengths_str: &str,
117            operator: &str,
118            set: &'a XVariableSet,
119        ) -> Result<Self, Xcsp3Error> {
120            match list_to_vec_var_val(list) {
121                Ok(scope_vec_str) => match list_to_vec_var_val(lengths_str) {
122                    Ok(length_vec_str) => match Operator::get_operator_by_str(operator) {
123                        None => Err(Xcsp3Error::get_constraint_list_of_values_error(
124                            "parse the list of values error. ",
125                        )),
126                        Some(ope) => {
127                            Ok(XOrdered::new(scope_vec_str, set, Some(length_vec_str), ope))
128                        }
129                    },
130                    Err(e) => Err(e),
131                },
132                Err(e) => Err(e),
133            }
134        }
135
136        pub fn from_str_without_lengths(
137            list: &str,
138            operator: &str,
139            set: &'a XVariableSet,
140        ) -> Result<Self, Xcsp3Error> {
141            match list_to_vec_var_val(list) {
142                Ok(scope_vec_str) => match Operator::get_operator_by_str(operator) {
143                    None => Err(Xcsp3Error::get_constraint_list_of_values_error(
144                        "parse the list of values error. ",
145                    )),
146                    Some(ope) => Ok(XOrdered::new(scope_vec_str, set, None, ope)),
147                },
148                Err(e) => Err(e),
149            }
150        }
151
152        pub fn new(
153            scope: Vec<XVarVal>,
154            set: &'a XVariableSet,
155            lengths: Option<Vec<XVarVal>>,
156            operator: Operator,
157        ) -> Self {
158            XOrdered {
159                scope,
160                map: Default::default(),
161                set,
162                lengths,
163                operator,
164            }
165        }
166        pub fn get_lengths(&self) -> &Option<Vec<XVarVal>> {
167            &self.lengths
168        }
169        pub fn get_operator(&self) -> &Operator {
170            &self.operator
171        }
172    }
173}