conjure_cp_core/representation/
uniform.rs1use super::{ReprInitError, ReprRulePtr};
7use crate::ast::{DomainPtr, ReturnType, Typeable};
8use crate::settings::{Channelling, channelling};
9use std::cell::RefCell;
10use std::collections::HashMap;
11use std::mem::{Discriminant, discriminant};
12
13pub(crate) type Choices = HashMap<Discriminant<ReturnType>, ReprRulePtr>;
14thread_local! {
15 static CHOICES: RefCell<Choices> = RefCell::new(HashMap::new());
16}
17
18fn family(dom: &DomainPtr, rule: ReprRulePtr) -> Discriminant<ReturnType> {
19 discriminant(&if rule.is_integer_encoding() {
20 ReturnType::Int
21 } else {
22 dom.return_type()
23 })
24}
25
26pub fn selected(dom: &DomainPtr, integer_encoding: bool) -> Option<ReprRulePtr> {
28 if channelling() != Channelling::Uniform {
29 return None;
30 }
31 let key = discriminant(&if integer_encoding {
32 ReturnType::Int
33 } else {
34 dom.return_type()
35 });
36 CHOICES.with(|choices| choices.borrow().get(&key).copied())
37}
38
39pub(crate) fn check_choice(dom: &DomainPtr, rule: ReprRulePtr) -> Result<(), ReprInitError> {
40 if channelling() == Channelling::Uniform
41 && let Some(existing) =
42 CHOICES.with(|choices| choices.borrow().get(&family(dom, rule)).copied())
43 && existing.id() != rule.id()
44 {
45 return Err(ReprInitError::UniformConflict {
46 domain: dom.clone(),
47 existing: existing.name(),
48 requested: rule.name(),
49 });
50 }
51 Ok(())
52}
53
54pub(crate) fn reset() {
55 CHOICES.with(|choices| choices.borrow_mut().clear());
56}
57
58pub(crate) fn record_choice(dom: &DomainPtr, rule: ReprRulePtr) {
59 if channelling() == Channelling::Uniform {
60 CHOICES.with(|choices| {
61 choices.borrow_mut().insert(family(dom, rule), rule);
62 });
63 }
64}
65
66pub(crate) struct Scope(Choices);
68impl Scope {
69 pub(crate) fn new() -> Self {
70 Self(CHOICES.with(|choices| std::mem::take(&mut *choices.borrow_mut())))
71 }
72
73 pub(crate) fn save() -> Self {
74 Self(CHOICES.with(|choices| choices.borrow().clone()))
75 }
76}
77impl Drop for Scope {
78 fn drop(&mut self) {
79 CHOICES.with(|choices| *choices.borrow_mut() = std::mem::take(&mut self.0));
80 }
81}
82
83pub(crate) fn probe<T>(f: impl FnOnce() -> T) -> T {
85 if channelling() != Channelling::Uniform {
86 return f();
87 }
88 let _restore = Scope::save();
89 f()
90}
91
92pub(crate) fn attempt(
94 f: impl FnOnce() -> crate::rule_engine::ApplicationResult,
95) -> crate::rule_engine::ApplicationResult {
96 if channelling() != Channelling::Uniform {
97 return f();
98 }
99 let previous = CHOICES.with(|choices| choices.borrow().clone());
100 let restore = Scope(previous.clone());
101 let result = f().map(|mut effect| {
102 effect.uniform_choices = CHOICES.with(|choices| {
103 choices
104 .borrow()
105 .iter()
106 .filter(|(key, _)| !previous.contains_key(*key))
107 .map(|(key, rule)| (*key, *rule))
108 .collect()
109 });
110 effect
111 });
112 drop(restore);
113 result
114}
115
116pub(crate) fn commit(choices: Choices) {
117 if channelling() == Channelling::Uniform {
118 CHOICES.with(|current| current.borrow_mut().extend(choices));
119 }
120}