From 9e8720c2cac0ce03c6b2f51796f4dc6af461ccec Mon Sep 17 00:00:00 2001 From: Niklas Dewally Date: Wed, 29 Nov 2023 10:52:05 +0000 Subject: [PATCH] minion_rs: better callback example --- solvers/minion/src/lib.rs | 45 ++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/solvers/minion/src/lib.rs b/solvers/minion/src/lib.rs index 48889019a..b6f407e35 100644 --- a/solvers/minion/src/lib.rs +++ b/solvers/minion/src/lib.rs @@ -31,30 +31,14 @@ //! use std::collections::HashMap; //! //! // Get solutions out of Minion. -//! // In this example, we just check that the solutions are correct. -//! // See the documentation for Callback for more examples on how to use this. -//! -//! fn callback(solutions: HashMap) -> bool { -//! let x = match solutions.get("x").unwrap() { -//! Constant::Integer(n) => n, -//! _ => panic!("x should be a integer"), -//! }; -//! -//! let y = match solutions.get("y").unwrap() { -//! Constant::Integer(n) => n, -//! _ => panic!("y should be a integer"), -//! }; -//! -//! let z = match solutions.get("z").unwrap() { -//! Constant::Integer(n) => n, -//! _ => panic!("z should be a integer"), -//! }; -//! -//! assert_eq!(*x, 1); -//! assert_eq!(*y, 2); -//! assert_eq!(*z, 1); -//! -//! return true; +//! // See the documentation for Callback for details. +//! +//! static ALL_SOLUTIONS: Mutex>> = Mutex::new(vec![]); +//! +//! fn callback(solutions: HashMap) -> bool { +//! let mut guard = ALL_SOLUTIONS.lock().unwrap(); +//! guard.push(solutions); +//! true //! } //! //! // Build and run the model. @@ -99,6 +83,19 @@ //! //! let res = run_minion(model, callback); //! res.expect("Error occurred"); +//! +//! // Get solutions +//! let guard = ALL_SOLUTIONS.lock().unwrap(); +//! let solution_set_1 = &(guard.get(0).unwrap()); +//! +//! let x1 = solution_set_1.get("x").unwrap(); +//! let y1 = solution_set_1.get("y").unwrap(); +//! let z1 = solution_set_1.get("z").unwrap(); +//! +//! assert_eq!(guard.len(),1); +//! assert_eq!(*x1,Constant::Integer(1)); +//! assert_eq!(*y1,Constant::Integer(2)); +//! assert_eq!(*z1,Constant::Integer(1)); //! ``` //! //! ## `PRINT` and `VARORDER`