1
use conjure_cp::essence_expr;
2
use conjure_cp::{
3
    ast::Metadata,
4
    ast::{Atom, Expression as Expr, Moo, SymbolTable},
5
    into_matrix_expr,
6
    rule_engine::{
7
        ApplicationError, ApplicationResult, RuleEffect, register_rule, register_rule_set,
8
    },
9
};
10

            
11
use ApplicationError::RuleNotApplicable;
12

            
13
register_rule_set!("Base", ());
14

            
15
/**
16
 * Turn a Min into a new variable and post a top-level constraint to ensure the new variable is the minimum.
17
 * ```text
18
 * min([a, b]) ~> c ; c <= a & c <= b & (c = a | c = b)
19
 * ```
20
 */
21
#[register_rule("Base", 6000, [Min])]
22
21778
fn min_to_var(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
23
21778
    let Expr::Min(_, inside_min_expr) = expr else {
24
7198
        return Err(RuleNotApplicable);
25
    };
26

            
27
14580
    let Some(exprs) = inside_min_expr.as_ref().clone().into_list() else {
28
14382
        return Err(RuleNotApplicable);
29
    };
30

            
31
198
    let domain = expr.domain_of().ok_or(ApplicationError::DomainError)?;
32
198
    let mut symbols = symbols.clone();
33

            
34
198
    let atom_inner = Atom::new_ref(symbols.gen_find_auxiliary(&domain));
35
198
    let atom_expr = Expr::Atomic(Metadata::new(), atom_inner);
36

            
37
198
    let mut new_top = Vec::new();
38
198
    let mut disjunction = Vec::new();
39
198
    for e in exprs {
40
        // Use the Expr::Atomic version in constraints
41
414
        new_top.push(essence_expr!(&atom_expr <= &e));
42
414
        disjunction.push(essence_expr!(&atom_expr = &e));
43
    }
44
    // TODO: deal with explicit index domains
45
198
    new_top.push(Expr::Or(
46
198
        Metadata::new(),
47
198
        Moo::new(into_matrix_expr![disjunction]),
48
198
    ));
49

            
50
198
    Ok(RuleEffect::new(
51
        // Return the Expr::Atomic
52
198
        essence_expr!(&atom_expr),
53
198
        new_top,
54
198
        symbols.clone(),
55
198
    ))
56
21778
}
57

            
58
/**
59
 * Turn a Max into a new variable and post a top level constraint to ensure the new variable is the maximum.
60
 * ```text
61
 * max([a, b]) ~> c ; c >= a & c >= b & (c = a | c = b)
62
 * ```
63
 */
64
#[register_rule("Base", 6000, [Max])]
65
19906
fn max_to_var(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
66
19906
    let Expr::Max(_, inside_max_expr) = expr else {
67
7198
        return Err(RuleNotApplicable);
68
    };
69

            
70
12708
    let Some(exprs) = inside_max_expr.as_ref().clone().into_list() else {
71
12465
        return Err(RuleNotApplicable);
72
    };
73

            
74
243
    let domain = expr.domain_of().ok_or(ApplicationError::DomainError)?;
75
243
    let mut symbols: SymbolTable = symbols.clone();
76

            
77
243
    let atom_inner = Atom::new_ref(symbols.gen_find_auxiliary(&domain));
78
243
    let atom_expr = Expr::Atomic(Metadata::new(), atom_inner);
79

            
80
243
    let mut new_top = Vec::new(); // the new variable must be more than or equal to all the other variables
81
243
    let mut disjunction = Vec::new(); // the new variable must more than or equal to one of the variables
82
243
    for e in exprs {
83
513
        new_top.push(essence_expr!(&atom_expr >= &e));
84
513
        disjunction.push(essence_expr!(&atom_expr = &e));
85
    }
86
    // FIXME: deal with explicitly given domains
87
243
    new_top.push(Expr::Or(
88
243
        Metadata::new(),
89
243
        Moo::new(into_matrix_expr![disjunction]),
90
243
    ));
91

            
92
243
    Ok(RuleEffect::new(essence_expr!(&atom_expr), new_top, symbols))
93
19906
}