rstm_rules/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 rstm_core::{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            head,
20            tail,
21        }
22    }
23    /// returns a new instance using the given rule and confidence
24    pub fn from_rule(rule: Rule<Q, S>, confidence: T) -> Self {
25        Self::new(rule.head, rule.tail, confidence)
26    }
27    /// returns a new instance from its constituent parts
28    pub const fn from_parts(
29        state: Q,
30        symbol: S,
31        direction: Direction,
32        next_state: Q,
33        write_symbol: S,
34        confidence: T,
35    ) -> Self {
36        // create a new head
37        let head = Head::new(state, symbol);
38        let tail = Tail::new(direction, next_state, write_symbol);
39        Self::new(head, tail, confidence)
40    }
41    /// returns an immutable reference to the confidence of the rule
42    pub const fn confidence(&self) -> &T {
43        &self.confidence
44    }
45    /// returns a reference to the head of the rule
46    pub const fn head(&self) -> Head<&Q, &S> {
47        self.head.view()
48    }
49    /// returns a mutable reference to the head of the rule
50    pub const fn head_mut(&mut self) -> Head<&mut Q, &mut S> {
51        self.head.view_mut()
52    }
53    /// returns a reference to the tail of the rule
54    pub const fn tail(&self) -> Tail<&Q, &S> {
55        self.tail.view()
56    }
57    /// returns a mutable reference to the tail of the rule
58    pub const fn tail_mut(&mut self) -> Tail<&mut Q, &mut S> {
59        self.tail.view_mut()
60    }
61}
62
63impl<Q, S, T> From<Rule<Q, S>> for LearnedRule<T, Q, S>
64where
65    Q: RawState,
66    T: Default,
67{
68    fn from(rule: Rule<Q, S>) -> Self {
69        Self::new(rule.head, rule.tail, <T>::default())
70    }
71}