xcsp3_rust/objectives/xobjective_expression.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 15:36
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::data_structs::expression_tree::xcsp3_utils::ExpressionTree;
43 use crate::data_structs::xint_val_var::xcsp3_core::XVarVal;
44 use crate::errors::xcsp3error::xcsp3_core::Xcsp3Error;
45 use crate::variables::xdomain::xcsp3_core::XDomainInteger;
46 use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
47 use std::collections::HashMap;
48 use std::fmt::{Display, Formatter};
49
50 #[derive(Clone)]
51 pub struct XObjectiveExpression<'a> {
52 expression: ExpressionTree,
53 scope: Vec<XVarVal>,
54 map: HashMap<String, &'a XDomainInteger>,
55 set: &'a XVariableSet,
56 }
57
58 impl Display for XObjectiveExpression<'_> {
59 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60 let mut ret = String::default();
61 for e in self.scope.iter() {
62 ret.push('(');
63 ret.push_str(&e.to_string());
64 ret.push_str("), ")
65 }
66 write!(
67 f,
68 "list = {}, expression = {:?}",
69 ret,
70 self.expression.to_string()
71 )
72 }
73 }
74
75 impl<'a> XObjectiveExpression<'a> {
76 pub fn get_scope_string(&self) -> &Vec<XVarVal> {
77 &self.scope
78 }
79 pub fn get_expression(&self) -> &ExpressionTree {
80 &self.expression
81 }
82
83 pub fn get_scope(&mut self) -> Vec<(&String, &XDomainInteger)> {
84 for e in &self.scope {
85 if let XVarVal::IntVar(s) = e {
86 if !self.map.contains_key(s) {
87 if let Ok(vec) = self.set.construct_scope(&[s]) {
88 for (vs, vv) in vec.into_iter() {
89 self.map.insert(vs, vv);
90 }
91 }
92 }
93 }
94 }
95 let mut scope_vec_var: Vec<(&String, &XDomainInteger)> = vec![];
96 for e in self.map.iter() {
97 scope_vec_var.push((e.0, e.1))
98 }
99 scope_vec_var
100 }
101 pub fn from_expr(expr: &str, set: &'a XVariableSet) -> Result<Self, Xcsp3Error> {
102 match ExpressionTree::from_string(expr) {
103 Ok(tree) => {
104 // let mut scope: Vec<XVarVal> = vec![];
105 let scope: Vec<XVarVal> = tree.get(set);
106 // for e in tree.get() {
107 // match set.find_variable(&e) {
108 // Ok(_) => {
109 // // println!("{}", r);
110 // scope.push(XVarVal::IntVar(e))
111 // }
112 // Err(err) => {
113 // return Err(err);
114 // }
115 // }
116 // }
117 // Ok(Self::new(scope, set, tree))
118 Ok(Self::new(tree, scope, set))
119 }
120
121 Err(e) => Err(e),
122 }
123 }
124 pub fn new(expression: ExpressionTree, scope: Vec<XVarVal>, set: &'a XVariableSet) -> Self {
125 Self {
126 expression,
127 scope,
128 map: Default::default(),
129 set,
130 }
131 }
132 }
133}