Skip to main content

choco_solver/
solution.rs

1use crate::{
2    CHOCO_BACKEND, CHOCO_LIB,
3    utils::{Handle, HandleT},
4    variables::{BoolVar, IntVar},
5};
6
7/// A solver result holding instantiated variable values.
8#[derive(Debug)]
9pub struct Solution {
10    handle: Handle,
11}
12
13impl Solution {
14    /// # Safety:
15    /// Must be called with a valid raw handle that points to a Solution instance in the backend.
16    pub(crate) unsafe fn new(raw_handle: *mut std::os::raw::c_void) -> Self {
17        Solution {
18            handle: Handle::new(raw_handle),
19        }
20    }
21
22    #[must_use]
23    /// Gets the value of an integer variable in this solution.
24    /// :TODO: presently if the var is not instantiated in solution Java raise an exception
25    /// Need to be added a new function in backend to check if the var is instantiated
26    pub fn get_int_var(&self, int_var: &IntVar<'_>) -> Option<i32> {
27        // Safety:
28        // Safe because Solution instances are created from valid backend handles.
29        CHOCO_BACKEND.with(|backend| unsafe {
30            Some(CHOCO_LIB.Java_org_chocosolver_capi_SolutionApi_getIntVal(
31                backend.thread,
32                self.get_raw_handle(),
33                int_var.get_raw_handle(),
34            ))
35        })
36    }
37
38    /// Gets the value of a boolean variable in this solution.
39    /// :TODO: presently if the var is not instantiated in solution Java raise an exception
40    /// Need to be added a new function in backend to check if the var is instantiated
41    #[must_use]
42    pub fn get_bool_var(&self, bool_var: &BoolVar<'_>) -> Option<bool> {
43        CHOCO_BACKEND.with(|backend|
44        // Safety:
45        // Safe because Solution instances are created from valid backend handles.
46        unsafe {
47            Some(
48                CHOCO_LIB.Java_org_chocosolver_capi_SolutionApi_getIntVal(backend.thread, self.get_raw_handle(), bool_var.get_raw_handle())
49                    != 0,
50            )
51        })
52    }
53}
54
55impl HandleT for Solution {
56    fn get_raw_handle(&self) -> *mut std::os::raw::c_void {
57        self.handle.get_raw_handle()
58    }
59}