Skip to main content

rstm_core/rules/impls/
impl_learned_rule.rs

1/*
2    Appellation: impl_learned_rule <module>
3    Created At: 2025.08.30:18:33:09
4    Contrib: @FL03
5*/
6use crate::rules::{LearnedRule, Rule};
7use crate::{Direction, Head, Tail};
8use rstm_state::RawState;
9
10impl<T, Q, S> LearnedRule<T, Q, S>
11where
12    Q: RawState,
13{
14    /// create a new [`LearnedRule`] using the given head, tail, and confidence
15    pub const fn new(head: Head<Q, S>, tail: Tail<Q, S>, confidence: T) -> Self {
16        Self {
17            confidence,
18            rule: Rule { head, tail },
19        }
20    }
21    /// returns a new instance using the given rule and confidence
22    pub const fn from_rule(rule: Rule<Q, S>, confidence: T) -> Self {
23        Self { confidence, rule }
24    }
25    /// returns a new instance from its constituent parts
26    pub const fn from_parts(
27        state: Q,
28        symbol: S,
29        direction: Direction,
30        next_state: Q,
31        write_symbol: S,
32        confidence: T,
33    ) -> Self {
34        // create a new head
35        let head = Head::new(state, symbol);
36        let tail = Tail::new(direction, next_state, write_symbol);
37        Self {
38            confidence,
39            rule: Rule { head, tail },
40        }
41    }
42    /// returns an immutable reference to the confidence of the rule
43    pub const fn confidence(&self) -> &T {
44        &self.confidence
45    }
46    /// returns a mutable reference to the confidence of the rule
47    pub const fn confidence_mut(&mut self) -> &mut T {
48        &mut self.confidence
49    }
50    /// returns an immutable reference to the learned rule
51    pub const fn rule(&self) -> &Rule<Q, S> {
52        &self.rule
53    }
54    /// returns a mutable reference to the learned rule
55    pub const fn rule_mut(&mut self) -> &mut Rule<Q, S> {
56        &mut self.rule
57    }
58    /// update the confidence level of the rule
59    pub fn set_confidence(&mut self, confidence: T) {
60        self.confidence = confidence;
61    }
62    /// update the rule
63    pub fn set_rule(&mut self, rule: Rule<Q, S>) {
64        self.rule = rule;
65    }
66    /// returns an immutable reference to the head of the rule
67    pub const fn head(&self) -> &Head<Q, S> {
68        self.rule().head()
69    }
70    /// returns a mutable reference to the head of the rule
71    pub const fn head_mut(&mut self) -> &mut Head<Q, S> {
72        self.rule_mut().head_mut()
73    }
74    /// returns an immutable reference to the tail of the rule
75    pub const fn tail(&self) -> &Tail<Q, S> {
76        self.rule().tail()
77    }
78    /// returns a mutable reference to the tail of the rule
79    pub const fn tail_mut(&mut self) -> &mut Tail<Q, S> {
80        self.rule_mut().tail_mut()
81    }
82}
83
84impl<Q, S, T> From<Rule<Q, S>> for LearnedRule<T, Q, S>
85where
86    Q: RawState,
87    T: Default,
88{
89    fn from(rule: Rule<Q, S>) -> Self {
90        Self::new(rule.head, rule.tail, <T>::default())
91    }
92}
93
94impl<Q, A, T> core::ops::Deref for LearnedRule<T, Q, A>
95where
96    Q: RawState,
97{
98    type Target = Rule<Q, A>;
99
100    fn deref(&self) -> &Self::Target {
101        &self.rule
102    }
103}