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