| Crates.io | deterministic_automata |
| lib.rs | deterministic_automata |
| version | 0.1.8 |
| created_at | 2025-08-01 20:54:02.475855+00 |
| updated_at | 2025-09-04 03:03:43.332662+00 |
| description | A framework for implementing deterministic and mutation automata with arbitrary state complexity |
| homepage | |
| repository | https://github.com/HaineSensei/deterministic_automata |
| max_upload_size | |
| id | 1777577 |
| size | 147,993 |
A Rust framework for implementing deterministic and mutation automata with arbitrary state complexity.
This crate provides a generic trait-based framework for creating deterministic and mutation automata that can handle state machines more complex than traditional finite state automata. States can carry arbitrary data, allowing recognition of some patterns beyond regular languages, and multiple automata can be composed using product constructions.
Clone type, not limited to simple enumsuse deterministic_automata::{DeterministicAutomatonBlueprint, BasicStateSort};
#[derive(Clone, PartialEq, Debug)]
enum State {
Start,
SawA,
AcceptAB,
}
struct EndsWithAB;
impl DeterministicAutomatonBlueprint for EndsWithAB {
type State = State;
type Alphabet = char;
type StateSort = BasicStateSort;
type ErrorType = String;
fn initial_state(&self) -> Self::State { State::Start }
fn state_sort_map(&self, state: &Self::State) -> Result<Self::StateSort, Self::ErrorType> {
Ok(match state {
State::AcceptAB => BasicStateSort::Accept,
_ => BasicStateSort::Reject,
})
}
fn transition_map(&self, state: &Self::State, character: &Self::Alphabet) -> Result<Self::State, Self::ErrorType> {
Ok(match (state, character) {
(State::Start, 'a') => State::SawA,
(State::Start, _) => State::Start,
(State::SawA, 'b') => State::AcceptAB,
(State::SawA, 'a') => State::SawA,
(State::SawA, _) => State::Start,
(State::AcceptAB, 'a') => State::SawA,
(State::AcceptAB, _) => State::Start,
})
}
}
// Usage
let dfa = EndsWithAB;
assert_eq!(dfa.characterise(&"cab".chars().collect::<Vec<_>>()).unwrap(), BasicStateSort::Accept);
use deterministic_automata::counter_automaton_example::CounterAutomatonBlueprint;
let blueprint = CounterAutomatonBlueprint::new('a', 'b');
assert_eq!(blueprint.characterise(&"aabb".chars().collect()).unwrap(), BasicStateSort::Accept);
use deterministic_automata::{BasicStateSort, MutationAutomatonBlueprint};
struct Counter;
impl MutationAutomatonBlueprint for Counter {
type State = i32;
type Alphabet = char;
type StateSort = BasicStateSort;
type ErrorType = String;
fn initial_mutation_state(&self) -> Self::State { 0 }
fn mutation_state_sort_map(&self, state: &Self::State) -> Result<Self::StateSort, Self::ErrorType> {
Ok(if *state >= 0 { BasicStateSort::Accept } else { BasicStateSort::Reject })
}
fn mutation_transition_map(&self, state: &mut Self::State, character: &Self::Alphabet) -> Result<(), Self::ErrorType> {
match character {
'+' => *state += 1,
'-' => *state -= 1,
_ => return Err("Invalid character".to_string()),
}
Ok(())
}
}
let counter = Counter;
assert_eq!(counter.mutation_characterise(&['+', '+', '-']).unwrap(), BasicStateSort::Accept);
use deterministic_automata::{DynamicAutomatonBlueprint, counter_automaton_example::CounterAutomatonBlueprint};
// Use the EndsWithAB automaton from the first example above
let pattern_automaton = EndsWithAB;
let counter_automaton = CounterAutomatonBlueprint::new('a', 'b');
// Store automata with different state types in the same collection
let automata: Vec<&DynamicAutomatonBlueprint<char, BasicStateSort, String>> = vec![
&pattern_automaton, // Uses enum state
&counter_automaton, // Uses counter state
];
// Use them polymorphically despite different internal state representations
for automaton in automata {
let result = automaton.characterise(&"aab".chars().collect::<Vec<_>>());
println!("Result: {:?}", result.unwrap());
}
DeterministicAutomatonBlueprint: Functional automaton behavior with immutable state transitionsMutationAutomatonBlueprint: In-place automaton behavior with mutable state updatescounter_automaton_example: Recognizes the context-free language a^n b^n using counter-based statesproduct_automaton: Product constructions including union and intersection operations for both paradigmseither_automaton: Runtime choice between different automaton types with deterministic/mutation submodulesmutation_automaton: Core mutation automaton types and blanket interoperability implementationdynamic_automaton: Dyn-compatible traits for runtime polymorphism over heterogeneous state typesDeterministicAutomaton: Runtime instance for functional step-by-step input processingMutationAutomaton: Runtime instance for mutation-based step-by-step input processingBasicStateSort: Simple Accept/Reject state classificationThe crate includes comprehensive integration tests covering:
Run tests with:
cargo test
Generate and view the full documentation:
cargo doc --open
See CHANGELOG.md for details about changes in each release.
This project is licensed under the MIT License - see the LICENSE file for details.
Documentation and comprehensive test suite contributions by Claude (Anthropic). All core automata logic and framework design by the original author.