rstm_rules/
rule.rs

1/*
2    Appellation: rule <module>
3    Created At: 2025.08.30:01:04:10
4    Contrib: @FL03
5*/
6mod impl_learned_rule;
7
8mod impl_rule;
9mod impl_rule_builder;
10mod impl_rule_ext;
11
12use rstm_core::{Direction, Head, Tail};
13use rstm_state::{RawState, State};
14
15/// The [`Rule`] implementation is a concrete representation of a single instruction, or rule,
16/// within a given Turing machine program. It encapsulates the necessary components to define
17/// the behavior of the Turing machine when it encounters a specific state and symbol.
18///
19/// **Note**: The inner fields are flattened for serialization purposes when using `serde`;
20/// this means that the fields of the `Head` and `Tail` structs will be serialized as if they
21/// were direct fields of the `Rule` struct itself.
22#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
23#[cfg_attr(
24    feature = "serde",
25    derive(serde::Deserialize, serde::Serialize),
26    serde(default, rename_all = "camelCase")
27)]
28pub struct Rule<Q = String, A = char>
29where
30    Q: RawState,
31{
32    #[cfg_attr(feature = "serde", serde(flatten))]
33    pub head: Head<Q, A>,
34    #[cfg_attr(feature = "serde", serde(flatten))]
35    pub tail: Tail<Q, A>,
36}
37
38#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
39#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
40pub struct LearnedRule<C = f32, Q = usize, S = usize>
41where
42    Q: RawState,
43{
44    pub confidence: C,
45    pub head: Head<Q, S>,
46    pub tail: Tail<Q, S>,
47}
48
49#[derive(Default)]
50pub struct RuleBuilder<Q, S> {
51    direction: Direction,
52    state: Option<State<Q>>,
53    symbol: Option<S>,
54    next_state: Option<State<Q>>,
55    write_symbol: Option<S>,
56}