1
use crate::exit;
2
use std::path::PathBuf;
3
use std::sync::Arc;
4

            
5
use crate::cli::GlobalArgs;
6
use crate::solve;
7
use clap::ValueHint;
8
use conjure_cp::instantiate::{instantiate_model, validate_instantiation_conditions};
9
use conjure_cp::pipeline::Pipeline;
10
use conjure_cp::solutions::SolutionOptions;
11
use conjure_cp_cli::utils::conjure::{get_solutions_from_conjure, solutions_to_json};
12
use conjure_cp_cli::utils::testing::normalize_solutions_for_comparison;
13

            
14
#[derive(Clone, Debug, clap::Args)]
15
pub 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

            
25
8
pub fn run_test_solve_command(global_args: GlobalArgs, local_args: Args) -> anyhow::Result<()> {
26
8
    let pipeline = Pipeline::new(global_args.library_config())?;
27
8
    pipeline.with_settings(|| run_test_solve(&pipeline, global_args, local_args))
28
8
}
29

            
30
8
fn run_test_solve(
31
8
    pipeline: &Pipeline,
32
8
    global_args: GlobalArgs,
33
8
    local_args: Args,
34
8
) -> anyhow::Result<()> {
35
8
    let input_file = local_args.input_file;
36
8
    let param_file = local_args.param_file;
37
8
    let context = pipeline.context();
38
    {
39
8
        let mut context = context.write().unwrap();
40
8
        context.essence_file_name = Some(input_file.to_string_lossy().into_owned());
41
8
        context.param_file_name = param_file
42
8
            .as_ref()
43
4
            .map(|path| path.to_string_lossy().into_owned());
44
    }
45
8
    let unified_model = pipeline.with_settings(|| -> anyhow::Result<_> {
46
8
        let problem_model = pipeline.parse_file(&input_file)?;
47
8
        match param_file.as_ref() {
48
            Some(param_file) => {
49
4
                let param_model = solve::parse_param(pipeline, param_file, &problem_model)?;
50
4
                Ok(instantiate_model(problem_model, param_model)?)
51
            }
52
            None => {
53
4
                let mut problem_model = problem_model;
54
4
                validate_instantiation_conditions(&mut problem_model)?;
55
4
                Ok(problem_model)
56
            }
57
        }
58
8
    })?;
59
6
    let rewritten_model = pipeline.rewrite(unified_model)?;
60
6
    let our_solutions = pipeline.solve(
61
6
        rewritten_model,
62
6
        &SolutionOptions {
63
6
            number_of_solutions: 0,
64
6
            solver_input_file: global_args.save_solver_input_file.clone(),
65
6
            rule_trace_cdp: global_args.rule_trace_cdp,
66
6
            ..Default::default()
67
6
        },
68
6
    )?;
69

            
70
6
    let conjure_solutions = pipeline.with_settings(|| {
71
6
        get_solutions_from_conjure(
72
6
            input_file.to_str().unwrap(),
73
6
            param_file.as_deref().map(|f| f.to_str().unwrap()),
74
6
            Arc::clone(&context),
75
        )
76
6
    })?;
77

            
78
6
    let our_solutions = normalize_solutions_for_comparison(&our_solutions);
79
6
    let conjure_solutions = normalize_solutions_for_comparison(&conjure_solutions);
80

            
81
6
    let mut our_solutions_json = solutions_to_json(&our_solutions);
82
6
    let mut conjure_solutions_json = solutions_to_json(&conjure_solutions);
83

            
84
6
    our_solutions_json.sort_all_objects();
85
6
    conjure_solutions_json.sort_all_objects();
86

            
87
6
    if our_solutions_json == conjure_solutions_json {
88
6
        eprintln!("Success: solutions match!");
89
6
        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
2
}