rstm_core/rules/impls/
impl_learned_rule.rs1use 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 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 pub const fn from_rule(rule: Rule<Q, S>, confidence: T) -> Self {
23 Self { confidence, rule }
24 }
25 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 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 pub const fn confidence(&self) -> &T {
44 &self.confidence
45 }
46 pub const fn confidence_mut(&mut self) -> &mut T {
48 &mut self.confidence
49 }
50 pub const fn rule(&self) -> &Rule<Q, S> {
52 &self.rule
53 }
54 pub const fn rule_mut(&mut self) -> &mut Rule<Q, S> {
56 &mut self.rule
57 }
58 pub fn set_confidence(&mut self, confidence: T) {
60 self.confidence = confidence;
61 }
62 pub fn set_rule(&mut self, rule: Rule<Q, S>) {
64 self.rule = rule;
65 }
66 pub const fn head(&self) -> &Head<Q, S> {
68 self.rule().head()
69 }
70 pub const fn head_mut(&mut self) -> &mut Head<Q, S> {
72 self.rule_mut().head_mut()
73 }
74 pub const fn tail(&self) -> &Tail<Q, S> {
76 self.rule().tail()
77 }
78 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}