1
//! Functions to pretty print a model as a [Minion
2
//! file](https://minion-solver.readthedocs.io/en/latest/usage/input.html).
3

            
4
use std::io::Write;
5

            
6
use crate::ast::{Constant, Constraint, Model, Var, VarName};
7

            
8
/// Writes a complete Minion file for this model to `writer`.
9
pub fn write_minion_file(writer: &mut impl Write, model: &Model) -> Result<(), std::io::Error> {
10
    writeln!(writer, "# Autogenerated by minion-sys")?;
11
    writeln!(writer, "MINION 3")?;
12

            
13
    write_variables_section(writer, model)?;
14
    write_search_section(writer, model)?;
15
    write_constraints_section(writer, model)?;
16
    writeln!(writer, "**EOF**")
17
}
18

            
19
/// Writes the `VARIABLES` section of the Minion file to `writer`.
20
pub fn write_variables_section(
21
    writer: &mut impl Write,
22
    model: &Model,
23
) -> Result<(), std::io::Error> {
24
    writeln!(writer, "**VARIABLES**")?;
25

            
26
    let symtab = &model.named_variables;
27

            
28
    // print variables in declaration order
29
    for name in symtab.get_variable_order() {
30
        write_variable_declaration(writer, model, name)?;
31
    }
32
    Ok(())
33
}
34

            
35
/// Writes the `SEARCH` section of the Minion file to `writer`.
36
pub fn write_search_section(writer: &mut impl Write, model: &Model) -> Result<(), std::io::Error> {
37
    // TODO: print maximising and minimising once we get it
38

            
39
    let symtab = &model.named_variables;
40

            
41
    writeln!(writer, "**SEARCH**")?;
42

            
43
    // no aux vars
44
    let varorder = symtab.get_search_variable_order();
45

            
46
    writeln!(writer, "VARORDER STATIC [{}]", varorder.join(","))
47
}
48

            
49
/// Writes the `CONSTRAINTS` section of the Minion file to `writer`.
50
pub fn write_constraints_section(
51
    writer: &mut impl Write,
52
    model: &Model,
53
) -> Result<(), std::io::Error> {
54
    let constraints = &model.constraints;
55
    writeln!(writer, "**CONSTRAINTS**")?;
56

            
57
    for constraint in constraints {
58
        writeln!(writer, "{constraint}")?;
59
    }
60

            
61
    Ok(())
62
}
63

            
64
/// Writes the variable declaration of `name` to `writer`.
65
///
66
/// # Panics
67
///
68
/// If `name` does not exist.
69
pub fn write_variable_declaration(
70
    writer: &mut impl Write,
71
    model: &Model,
72
    name: VarName,
73
) -> Result<(), std::io::Error> {
74
    let symtab = &model.named_variables;
75

            
76
    #[allow(clippy::expect_used)]
77
    match symtab.get_vartype(name.clone()).expect("name should exist") {
78
        crate::ast::VarDomain::Bound(i, j) => writeln!(writer, "BOUND {name} {{{i}..{j}}}")?,
79
        crate::ast::VarDomain::Discrete(i, j) => writeln!(writer, "DISCRETE {name}, {{{i}..{j}}}")?,
80
        crate::ast::VarDomain::Bool => writeln!(writer, "BOOL {name}")?,
81
    };
82

            
83
    Ok(())
84
}
85

            
86
pub(crate) fn print_const_array(array: &[Constant]) -> String {
87
    let string_array: Vec<String> = array.iter().map(|x| format!("{x}")).collect();
88
    let string = string_array.join(",");
89
    format!("[{string}]")
90
}
91

            
92
pub(crate) fn print_var_array(array: &[Var]) -> String {
93
    let string_array: Vec<String> = array.iter().map(|x| format!("{x}")).collect();
94
    let string = string_array.join(",");
95
    format!("[{string}]")
96
}
97

            
98
pub(crate) fn print_constraint_array(array: &[Constraint]) -> String {
99
    let string_array: Vec<String> = array.iter().map(|x| format!("{x}")).collect();
100
    let string = string_array.join(",");
101
    format!("[{string}]")
102
}