1
pub use linkme::distributed_slice;
2

            
3
/// This procedural macro registers a decorated function with `conjure_rules`' global registry, and
4
/// adds the rule to one or more `RuleSet`'s.
5
///
6
/// It may be used in any downstream crate.
7
/// For more information on linker magic, see the [`linkme`](https://docs.rs/linkme/latest/linkme/) crate.
8
///
9
/// **IMPORTANT**: Since the resulting rule may not be explicitly referenced, it may be removed by the compiler's dead code elimination.
10
/// To prevent this, you must ensure that either:
11
/// 1. codegen-units is set to 1, i.e. in Cargo.toml:
12
/// ```toml
13
/// [profile.release]
14
/// codegen-units = 1
15
/// ```
16
/// 2. The function is included somewhere else in the code
17
///
18
/// <hr>
19
///
20
/// Functions must have the signature `fn(&Expr) -> ApplicationResult`.
21
/// The created rule will have the same name as the function.
22
///
23
/// Intermediary static variables are created to allow for the decentralized registry, with the prefix `CONJURE_GEN_`.
24
/// Please ensure that other variable names in the same scope do not conflict with these.
25
///
26
/// This macro must decorate a function with the given signature.
27
/// As arguments, it excepts a tuple of 2-tuples in the format:
28
/// `((<RuleSet name>, <Priority in RuleSet>), ...)`
29
///
30
/// <hr>
31
///
32
/// For example:
33
/// ```rust
34
1
/// use conjure_core::ast::Expression;
35
/// use conjure_core::model::Model;
36
/// use conjure_core::rule_engine::{ApplicationError, ApplicationResult, Reduction};
37
/// use conjure_core::rule_engine::register_rule;
38
///
39
/// #[register_rule(("RuleSetName", 10))]
40
/// fn identity(expr: &Expression, mdl: &Model) -> ApplicationResult {
41
///   Ok(Reduction::pure(expr.clone()))
42
/// }
43
/// ```
44
1
pub use conjure_macros::register_rule;
45

            
46
/// This procedural macro registers a rule set with the global registry.
47
/// It may be used in any downstream crate.
48
///
49
/// For more information on linker magic, see the [`linkme`](https://docs.rs/linkme/latest/linkme/) crate.
50
///
51
/// This macro uses the following syntax:
52
///
53
/// ```text
54
/// register_rule_set!(<RuleSet name>, <RuleSet order>, (<DependencyRuleSet1>, <DependencyRuleSet2>, ...));
55
/// ```
56
///
57
/// # Example
58
///
59
/// ```rust
60
1
/// use conjure_core::rule_engine::register_rule_set;
61
///
62
/// register_rule_set!("MyRuleSet", 10, ("DependencyRuleSet", "AnotherRuleSet"));
63
/// ```
64
1
#[doc(inline)]
65
pub use conjure_macros::register_rule_set;
66
pub use resolve_rules::{get_rule_priorities, get_rules_vec, resolve_rule_sets};
67
pub use rewrite::rewrite_model;
68
pub use rewrite_naive::rewrite_naive;
69
pub use rewriter_common::RewriteError;
70
pub use rule::{ApplicationError, ApplicationResult, Reduction, Rule};
71
pub use rule_set::RuleSet;
72

            
73
use crate::solver::SolverFamily;
74

            
75
mod resolve_rules;
76
mod rewrite;
77
mod rewrite_naive;
78
mod rewriter_common;
79
mod rule;
80
mod rule_set;
81

            
82
#[doc(hidden)]
83
#[distributed_slice]
84
pub static RULES_DISTRIBUTED_SLICE: [Rule<'static>];
85

            
86
#[doc(hidden)]
87
#[distributed_slice]
88
pub static RULE_SETS_DISTRIBUTED_SLICE: [RuleSet<'static>];
89

            
90
pub mod _dependencies {
91
    pub use linkme;
92
    pub use linkme::distributed_slice;
93
}
94

            
95
/// Returns a copied `Vec` of all rules registered with the `register_rule` macro.
96
///
97
/// Rules are not guaranteed to be in any particular order.
98
///
99
/// # Example
100
/// ```rust
101
/// # use conjure_core::rule_engine::{ApplicationResult, Reduction, get_rules};
102
/// # use conjure_core::ast::Expression;
103
/// # use conjure_core::model::Model;
104
/// # use conjure_core::rule_engine::register_rule;
105
///
106
/// #[register_rule]
107
/// fn identity(expr: &Expression, mdl: &Model) -> ApplicationResult {
108
///   Ok(Reduction::pure(expr.clone()))
109
/// }
110
///
111
/// fn main() {
112
1
///   println!("Rules: {:?}", get_rules());
113
1
/// }
114
1
/// ```
115
///
116
/// This will print (if no other rules are registered):
117
/// ```text
118
///   Rules: [Rule { name: "identity", application: MEM }]
119
/// ```
120
/// Where `MEM` is the memory address of the `identity` function.
121
544
pub fn get_rules() -> Vec<&'static Rule<'static>> {
122
544
    RULES_DISTRIBUTED_SLICE.iter().collect()
123
544
}
124

            
125
/// Get a rule by name.
126
/// Returns the rule with the given name or None if it doesn't exist.
127
///
128
/// # Example
129
/// ```rust
130
/// use conjure_core::rule_engine::register_rule;
131
/// use conjure_core::rule_engine::{Rule, ApplicationResult, Reduction, get_rule_by_name};
132
/// use conjure_core::ast::Expression;
133
/// use conjure_core::model::Model;
134
///
135
/// #[register_rule]
136
/// fn identity(expr: &Expression, mdl: &Model) -> ApplicationResult {
137
///  Ok(Reduction::pure(expr.clone()))
138
/// }
139
///
140
/// fn main() {
141
1
/// println!("Rule: {:?}", get_rule_by_name("identity"));
142
1
/// }
143
1
/// ```
144
///
145
/// This will print:
146
/// ```text
147
/// Rule: Some(Rule { name: "identity", application: MEM })
148
/// ```
149
289
pub fn get_rule_by_name(name: &str) -> Option<&'static Rule<'static>> {
150
5049
    get_rules().iter().find(|rule| rule.name == name).cloned()
151
289
}
152

            
153
/// Get all rule sets
154
/// Returns a `Vec` of static references to all rule sets registered with the `register_rule_set` macro.
155
/// Rule sets are not guaranteed to be in any particular order.
156
///
157
/// # Example
158
/// ```rust
159
1
/// use conjure_core::rule_engine::register_rule_set;
160
/// use conjure_core::rule_engine::get_rule_sets;
161
///
162
/// register_rule_set!("MyRuleSet", 10, ("AnotherRuleSet"));
163
/// register_rule_set!("AnotherRuleSet", 5, ());
164
///
165
/// println!("Rule sets: {:?}", get_rule_sets());
166
1
/// ```
167
1
///
168
/// This will print (if no other rule sets are registered):
169
/// ```text
170
/// Rule sets: [
171
///   RuleSet { name: "MyRuleSet", order: 10, rules: OnceLock { state: Uninitialized }, dependencies: ["AnotherRuleSet"] },
172
///   RuleSet { name: "AnotherRuleSet", order: 5, rules: OnceLock { state: Uninitialized }, dependencies: [] }
173
/// ]
174
/// ```
175
///
176
5933
pub fn get_rule_sets() -> Vec<&'static RuleSet<'static>> {
177
5933
    RULE_SETS_DISTRIBUTED_SLICE.iter().collect()
178
5933
}
179

            
180
/// Get a rule set by name.
181
/// Returns the rule set with the given name or None if it doesn't exist.
182
///
183
/// # Example
184
/// ```rust
185
1
/// use conjure_core::rule_engine::register_rule_set;
186
/// use conjure_core::rule_engine::get_rule_set_by_name;
187
///
188
/// register_rule_set!("MyRuleSet", 10, ("DependencyRuleSet", "AnotherRuleSet"));
189
///
190
/// println!("Rule set: {:?}", get_rule_set_by_name("MyRuleSet"));
191
1
/// ```
192
1
///
193
/// This will print:
194
/// ```text
195
/// Rule set: Some(RuleSet { name: "MyRuleSet", order: 10, rules: OnceLock { state: Uninitialized }, dependencies: ["DependencyRuleSet", "AnotherRuleSet"] })
196
/// ```
197
4420
pub fn get_rule_set_by_name(name: &str) -> Option<&'static RuleSet<'static>> {
198
4420
    get_rule_sets()
199
4420
        .iter()
200
16167
        .find(|rule_set| rule_set.name == name)
201
4420
        .cloned()
202
4420
}
203

            
204
/// Get all rule sets for a given solver family.
205
/// Returns a `Vec` of static references to all rule sets that are applicable to the given solver family.
206
///
207
/// # Example
208
///
209
/// ```rust
210
1
/// use conjure_core::solver::SolverFamily;
211
/// use conjure_core::rule_engine::get_rule_sets_for_solver_family;
212
///
213
/// let rule_sets = get_rule_sets_for_solver_family(SolverFamily::SAT);
214
1
/// assert_eq!(rule_sets.len(), 1);
215
1
/// assert_eq!(rule_sets[0].name, "CNF");
216
1
/// ```
217
1409
pub fn get_rule_sets_for_solver_family(
218
1496
    solver_family: SolverFamily,
219
1496
) -> Vec<&'static RuleSet<'static>> {
220
1496
    get_rule_sets()
221
1496
        .iter()
222
7480
        .filter(|rule_set| {
223
7480
            rule_set
224
7480
                .solver_families
225
7480
                .iter()
226
7480
                .any(|family| family.eq(&solver_family))
227
7480
        })
228
1496
        .cloned()
229
1496
        .collect()
230
1496
}