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