xcsp3_rust/constraints/
xslide.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/29 16:45
32 * </p>
33 * <p>@email: zhenlh20@mails.jlu.edu.cn
34 * </p>
35 * <p>@version: 1.0
36 * </p>
37 * <p>@description:
38 * </p>
39 */
40
41pub mod xcsp3_core {
42    use crate::constraints::xconstraint_type::xcsp3_core::XConstraintType;
43    use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
44    use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
45    use crate::utils::utils_functions::xcsp3_utils::list_to_vec_var_val;
46    use crate::variables::xdomain::xcsp3_core::XDomainInteger;
47    use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
48    use std::collections::HashMap;
49    use std::fmt::{Display, Formatter};
50
51    pub struct XSlide<'a> {
52        args: Vec<XVarVal>,
53        map: HashMap<String, &'a XDomainInteger>,
54        set: &'a XVariableSet,
55        template: Box<XConstraintType<'a>>,
56        circular: bool,
57        offset: i32,
58    }
59
60    impl<'a> XSlide<'a> {
61        pub fn get_offset(&self) -> i32 {
62            self.offset
63        }
64        pub fn get_circular(&self) -> bool {
65            self.circular
66        }
67        pub fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
68            for e in &self.args {
69                if let XVarVal::IntVar(s) = e {
70                    if !self.map.contains_key(s) {
71                        if let Ok(vec) = self.set.construct_scope(&[s]) {
72                            for (vs, vv) in vec.into_iter() {
73                                self.map.insert(vs, vv);
74                            }
75                        }
76                    }
77                }
78            }
79            let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
80            for e in self.map.iter() {
81                scope_vec_var.push((e.0, e.1))
82            }
83            scope_vec_var
84        }
85
86        pub fn get_args(&self) -> &Vec<XVarVal> {
87            &self.args
88        }
89
90        pub fn get_template(&self) -> &XConstraintType<'a> {
91            &self.template
92        }
93
94        pub fn from_str(
95            cc: XConstraintType<'a>,
96            arg_str: &str,
97            offset_str: &str,
98            circular_str: &str,
99            set: &'a XVariableSet,
100        ) -> Result<Self, Xcsp3Error> {
101            match list_to_vec_var_val(arg_str) {
102                Ok(scope_vec_str) => {
103                    let offset = if offset_str.is_empty() {
104                        0
105                    } else {
106                        match offset_str.parse::<i32>() {
107                            Ok(o) => o,
108                            Err(_) => {
109                                return Err(Xcsp3Error::get_constraint_slide_error(
110                                    "parse the offset error, ",
111                                ));
112                            }
113                        }
114                    };
115                    let circular = if circular_str.is_empty() {
116                        false
117                    } else {
118                        match circular_str.parse::<bool>() {
119                            Ok(c) => c,
120                            Err(_) => {
121                                return Err(Xcsp3Error::get_constraint_slide_error(
122                                    "parse the circular error, ",
123                                ))
124                            }
125                        }
126                    };
127                    Ok(Self::new(
128                        scope_vec_str,
129                        set,
130                        offset,
131                        circular,
132                        Box::new(cc),
133                    ))
134                }
135                Err(e) => Err(e),
136            }
137
138            // match list_to_vec_var_val(arg_str) {
139            //     Ok(scope_vec_str) => match offset_str.parse::<i32>() {
140            //         Ok(offset) => match circular_str.parse::<bool>() {
141            //             Ok(circular) => Ok(Self::new(
142            //                 scope_vec_str,
143            //                 set,
144            //                 offset,
145            //                 circular,
146            //                 Box::new(cc),
147            //             )),
148            //             Err(_) => Err(Xcsp3Error::get_constraint_slide_error(
149            //                 "parse the circular error, ",
150            //             )),
151            //         },
152            //         Err(_) => Err(Xcsp3Error::get_constraint_slide_error(
153            //             "parse the offset error, ",
154            //         )),
155            //     },
156            //     Err(e) => Err(e),
157            // }
158        }
159
160        pub fn new(
161            args: Vec<XVarVal>,
162            set: &'a XVariableSet,
163            offset: i32,
164            circular: bool,
165            template: Box<XConstraintType<'a>>,
166        ) -> Self {
167            Self {
168                args,
169                map: Default::default(),
170                set,
171                template,
172                circular,
173                offset,
174            }
175        }
176    }
177
178    impl Display for XSlide<'_> {
179        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
180            let mut ret = String::default();
181            for a in self.args.iter() {
182                ret.push_str(a.to_string().as_str());
183                ret.push_str(", ")
184            }
185            write!(
186                f,
187                "XSlide: [constraint = {} [ list =  {}]]",
188                &self.template.to_string(),
189                ret
190            )
191        }
192    }
193}