xcsp3_rust/constraints/
xelement.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/23 00:59
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::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    // #[derive(Clone)]
52    pub struct XElement<'a> {
53        scope: Vec<XVarVal>,
54        map: HashMap<String, &'a XDomainInteger>,
55        set: &'a XVariableSet,
56        value: XVarVal,
57        index: XVarVal,
58        start_index: Option<i32>,
59    }
60
61    impl<'a> XElement<'a> {
62        pub fn from_str(
63            list: &str,
64            value_str: &str,
65            index_str: &str,
66            start_index_str: &str,
67            set: &'a XVariableSet,
68        ) -> Result<Self, Xcsp3Error> {
69            // println!("{start_index_str}");
70            match list_to_vec_var_val(list) {
71                Ok(scope_vec_str) => {
72                    let value = match XVarVal::from_string(value_str) {
73                        None => {
74                            return Err(Xcsp3Error::get_constraint_sum_error(
75                                "parse element constraint value error, ",
76                            ));
77                        }
78                        Some(v) => v,
79                    };
80                    let index = if !index_str.is_empty() {
81                        match XVarVal::from_string(index_str) {
82                            None => {
83                                return Err(Xcsp3Error::get_constraint_sum_error(
84                                    "parse element constraint index error, ",
85                                ));
86                            }
87                            Some(i) => i,
88                        }
89                    } else {
90                        XVarVal::IntNone
91                    };
92                    let start_index = if !start_index_str.is_empty() {
93                        match start_index_str.parse::<i32>() {
94                            Ok(n) => Some(n),
95                            Err(_) => {
96                                return Err(Xcsp3Error::get_constraint_sum_error(
97                                    "parse element constraint start_index error, ",
98                                ));
99                            }
100                        }
101                    } else {
102                        None
103                    };
104
105                    Ok(XElement::new(scope_vec_str, set, value, index, start_index))
106                }
107
108                Err(e) => Err(e),
109            }
110        }
111
112        pub fn new(
113            scope: Vec<XVarVal>,
114            set: &'a XVariableSet,
115            value: XVarVal,
116            index: XVarVal,
117            start_index: Option<i32>,
118        ) -> Self {
119            Self {
120                scope,
121                map: Default::default(),
122                set,
123                value,
124                index,
125                start_index,
126            }
127        }
128        pub fn get_start_index(&self) -> &Option<i32> {
129            &self.start_index
130        }
131    }
132    impl Display for XElement<'_> {
133        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
134            let mut ret = String::default();
135            for e in self.scope.iter() {
136                ret.push('(');
137                ret.push_str(&e.to_string());
138                ret.push_str("), ")
139            }
140            if let Some(n) = &self.start_index {
141                ret.push_str(&format!(" start_index = {} ", n))
142            }
143            if let XVarVal::IntNone = self.index {
144                ret.push_str(&format!(" index = {} ", self.index))
145            }
146            write!(f, "XElement: list   =  {}, value = {}", ret, self.value,)
147        }
148    }
149
150    impl XConstraintTrait for XElement<'_> {
151        fn get_scope_string(&self) -> &Vec<XVarVal> {
152            &self.scope
153        }
154
155        fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
156            for e in &self.scope {
157                if let XVarVal::IntVar(s) = e {
158                    if !self.map.contains_key(s) {
159                        if let Ok(vec) = self.set.construct_scope(&[s]) {
160                            for (vs, vv) in vec.into_iter() {
161                                self.map.insert(vs, vv);
162                            }
163                        }
164                    }
165                }
166            }
167            let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
168            for e in self.map.iter() {
169                scope_vec_var.push((e.0, e.1))
170            }
171            scope_vec_var
172        }
173    }
174}