-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automaton.java
61 lines (54 loc) · 1.85 KB
/
Automaton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright 2020 daspoet
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons
* to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*/
package dev.daspoet.automaton;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import java.util.List;
import java.util.stream.Collectors;
/**
* A deterministic state machine.
*/
@Getter
@AllArgsConstructor
public class Automaton {
@NonNull private final State initialState;
/**
* Walks the state tree rooted at {@link #initialState},
* calling walkFunction on each {@link State} in the tree,
* including the initial state.
*
* @param walkFunction the function to execute on each state
*/
public void walk(DoubleInputFunction<State, Transition, Boolean> walkFunction) {
this.initialState.walk(walkFunction);
}
/**
* Checks if a given word is accepted by the automaton
* <p>
* All successive states of the automaton's initial state
* are traversed recursively to check whether any of the
* emerging chains accepts the given word.
*
* @param word the word to check
* @return whether the automaton accepts the word
*/
public boolean accepts(String word) {
List<Character> chars = word.chars().mapToObj(it -> (char) it).collect(Collectors.toList());
return this.initialState.traverse(chars);
}
}