use crate::{Direction, Head, RawState, Rule, State, Tail};
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize)
)]
pub struct LearnedRule<C = f32, Q = usize, S = usize>
where
Q: RawState,
{
pub confidence: C,
pub head: Head<Q, S>,
pub tail: Tail<Q, S>,
}
impl<T, Q, S> LearnedRule<T, Q, S>
where
Q: RawState,
{
pub const fn new(head: Head<Q, S>, tail: Tail<Q, S>, confidence: T) -> Self {
Self {
confidence,
head,
tail,
}
}
pub const fn from_parts(
state: State<Q>,
symbol: S,
direction: Direction,
next_state: State<Q>,
next_symbol: S,
confidence: T,
) -> Self {
let head = Head::new(state, symbol);
let tail = Tail::new(direction, next_state, next_symbol);
Self::new(head, tail, confidence)
}
pub const fn confidence(&self) -> &T {
&self.confidence
}
pub fn head(&self) -> Head<&Q, &S> {
self.head.view()
}
pub fn head_mut(&mut self) -> Head<&mut Q, &mut S> {
self.head.view_mut()
}
pub fn tail(&self) -> Tail<&Q, &S> {
self.tail.view()
}
pub fn tail_mut(&mut self) -> Tail<&mut Q, &mut S> {
self.tail.view_mut()
}
}
impl<Q, S, T> From<Rule<Q, S>> for LearnedRule<T, Q, S>
where
Q: RawState,
T: Default,
{
fn from(rule: Rule<Q, S>) -> Self {
Self::new(rule.head, rule.tail, <T>::default())
}
}