rstm_core/rule/
impl_learned_rule.rs1use 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 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 pub const fn from_rule(rule: Rule<Q, S>, confidence: T) -> Self {
24 Self { confidence, rule }
25 }
26 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 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 pub const fn confidence(&self) -> &T {
45 &self.confidence
46 }
47 pub const fn confidence_mut(&mut self) -> &mut T {
49 &mut self.confidence
50 }
51 pub const fn rule(&self) -> &Rule<Q, S> {
53 &self.rule
54 }
55 pub const fn rule_mut(&mut self) -> &mut Rule<Q, S> {
57 &mut self.rule
58 }
59 pub fn set_confidence(&mut self, confidence: T) {
61 self.confidence = confidence;
62 }
63 pub fn set_rule(&mut self, rule: Rule<Q, S>) {
65 self.rule = rule;
66 }
67 pub const fn head(&self) -> &Head<Q, S> {
69 self.rule().head()
70 }
71 pub const fn head_mut(&mut self) -> &mut Head<Q, S> {
73 self.rule_mut().head_mut()
74 }
75 pub const fn tail(&self) -> &Tail<Q, S> {
77 self.rule().tail()
78 }
79 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}