Skip to main content

conjure_oxide/
test_solve.rs

1use crate::exit;
2use std::path::PathBuf;
3use std::sync::Arc;
4
5use crate::cli::GlobalArgs;
6use crate::solve;
7use clap::ValueHint;
8use conjure_cp::instantiate::{instantiate_model, validate_instantiation_conditions};
9use conjure_cp::pipeline::Pipeline;
10use conjure_cp::solutions::SolutionOptions;
11use conjure_cp_cli::utils::conjure::{get_solutions_from_conjure, solutions_to_json};
12use conjure_cp_cli::utils::testing::normalize_solutions_for_comparison;
13
14#[derive(Clone, Debug, clap::Args)]
15pub struct Args {
16    /// The input Essence problem file
17    #[arg(value_name = "INPUT_ESSENCE",value_hint=ValueHint::FilePath)]
18    pub input_file: PathBuf,
19
20    /// The input Essence parameter file
21    #[arg(value_name = "PARAM_ESSENCE", value_hint=ValueHint::FilePath)]
22    pub param_file: Option<PathBuf>,
23}
24
25pub fn run_test_solve_command(global_args: GlobalArgs, local_args: Args) -> anyhow::Result<()> {
26    let pipeline = Pipeline::new(global_args.library_config())?;
27    pipeline.with_settings(|| run_test_solve(&pipeline, global_args, local_args))
28}
29
30fn run_test_solve(
31    pipeline: &Pipeline,
32    global_args: GlobalArgs,
33    local_args: Args,
34) -> anyhow::Result<()> {
35    let input_file = local_args.input_file;
36    let param_file = local_args.param_file;
37    let context = pipeline.context();
38    {
39        let mut context = context.write().unwrap();
40        context.essence_file_name = Some(input_file.to_string_lossy().into_owned());
41        context.param_file_name = param_file
42            .as_ref()
43            .map(|path| path.to_string_lossy().into_owned());
44    }
45    let unified_model = pipeline.with_settings(|| -> anyhow::Result<_> {
46        let problem_model = pipeline.parse_file(&input_file)?;
47        match param_file.as_ref() {
48            Some(param_file) => {
49                let param_model = solve::parse_param(pipeline, param_file, &problem_model)?;
50                Ok(instantiate_model(problem_model, param_model)?)
51            }
52            None => {
53                let mut problem_model = problem_model;
54                validate_instantiation_conditions(&mut problem_model)?;
55                Ok(problem_model)
56            }
57        }
58    })?;
59    let rewritten_model = pipeline.rewrite(unified_model)?;
60    let our_solutions = pipeline.solve(
61        rewritten_model,
62        &SolutionOptions {
63            number_of_solutions: 0,
64            solver_input_file: global_args.save_solver_input_file.clone(),
65            rule_trace_cdp: global_args.rule_trace_cdp,
66            ..Default::default()
67        },
68    )?;
69
70    let conjure_solutions = pipeline.with_settings(|| {
71        get_solutions_from_conjure(
72            input_file.to_str().unwrap(),
73            param_file.as_deref().map(|f| f.to_str().unwrap()),
74            Arc::clone(&context),
75        )
76    })?;
77
78    let our_solutions = normalize_solutions_for_comparison(&our_solutions);
79    let conjure_solutions = normalize_solutions_for_comparison(&conjure_solutions);
80
81    let mut our_solutions_json = solutions_to_json(&our_solutions);
82    let mut conjure_solutions_json = solutions_to_json(&conjure_solutions);
83
84    our_solutions_json.sort_all_objects();
85    conjure_solutions_json.sort_all_objects();
86
87    if our_solutions_json == conjure_solutions_json {
88        eprintln!("Success: solutions match!");
89        exit(0);
90    } else {
91        eprintln!("=== our solutions:");
92        eprintln!("{our_solutions_json}");
93        eprintln!("=== conjure's solutions:");
94        eprintln!("{conjure_solutions_json}");
95        eprintln!("Failure: solutions do not match!");
96        exit(1);
97    }
98}