xcsp3_rust/constraints/xextension.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/14 18:54
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::utils::utils_functions::xcsp3_utils::{list_to_vec_var_val, tuple_to_vector};
44 use crate::variables::xdomain::xcsp3_core::XDomainInteger;
45 use std::collections::HashMap;
46 use std::fmt::{Display, Formatter};
47
48 use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
49 use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
50 use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
51 use std::slice::Iter;
52
53 // #[derive(Clone)]
54 pub struct XExtension<'a> {
55 scope: Vec<XVarVal>,
56 map: HashMap<String, &'a XDomainInteger>,
57 set: &'a XVariableSet,
58 ///if the value in tuples is i32::MAX, then it is the star
59 tuples: Vec<Vec<i32>>,
60 is_support: bool,
61 }
62
63 impl Display for XExtension<'_> {
64 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
65 let mut ret = String::default();
66 for e in self.scope.iter() {
67 ret.push('(');
68 ret.push_str(&e.to_string());
69 ret.push_str("), ")
70 }
71 if self.is_support {
72 ret.push_str("supports = ")
73 } else {
74 ret.push_str("conflicts = ")
75 }
76 // ret.push_str(&format!(
77 // "tuples = {:?}, is_support = {}",
78 // self.tuples, self.is_support
79 // ));
80 write!(f, "XExtension: list = {}{:?}", ret, self.tuples)
81 }
82 }
83
84 impl XConstraintTrait for XExtension<'_> {
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> XExtension<'a> {
110 /// construct the constraint from two strings and a bool
111 pub fn from_str(
112 list: &str,
113 tuple: &str,
114 is_support: bool,
115 set: &'a XVariableSet,
116 ) -> Result<Self, Xcsp3Error> {
117 // let tt= TimeInterval::new();
118 let a = match list_to_vec_var_val(list) {
119 Ok(scope_vec_str) => match tuple_to_vector(tuple, !tuple.contains('(')) {
120 Ok(tuples) => {
121 // println!("{:?}", &tuples);
122 Ok(XExtension::new(scope_vec_str, set, tuples, is_support))
123 }
124 Err(e) => Err(e),
125 },
126 Err(e) => Err(e),
127 };
128 // println!("{:?}",tt.get());
129 a
130 }
131
132 pub fn new(
133 scope: Vec<XVarVal>,
134 set: &'a XVariableSet,
135 tuples: Vec<Vec<i32>>,
136 is_support: bool,
137 ) -> Self {
138 XExtension {
139 scope,
140 map: Default::default(),
141 set,
142 tuples,
143 is_support,
144 }
145 }
146 ///return the iter of the supports tuples, if the value is i32::MAX, then it is the star
147 pub fn supports_iter(&self) -> Option<Iter<'_, Vec<i32>>> {
148 if self.is_support {
149 Some(self.tuples.iter())
150 } else {
151 None
152 }
153 }
154
155 ///return the iter of the conflict tuples, if the value is i32::MAX, then it is the star
156 pub fn conflicts_iter(&self) -> Option<Iter<'_, Vec<i32>>> {
157 if !self.is_support {
158 Some(self.tuples.iter())
159 } else {
160 None
161 }
162 }
163 }
164}