Package dk.brics.automaton
Class Automaton
- java.lang.Object
-
- dk.brics.automaton.Automaton
-
- All Implemented Interfaces:
Serializable
,Cloneable
public class Automaton extends Object implements Serializable, Cloneable
Finite-state automaton with regular expression operations.Class invariants:
- An automaton is either represented explicitly (with
State
andTransition
objects) or with a singleton string (seegetSingleton()
andexpandSingleton()
) in case the automaton is known to accept exactly one string. (Implicitly, all states and transitions of an automaton are reachable from its initial state.) - Automata are always reduced (see
reduce()
) and have no transitions to dead states (seeremoveDeadTransitions()
). - If an automaton is nondeterministic, then
isDeterministic()
returns false (but the converse is not required). - Automata provided as input to operations are generally assumed to be disjoint.
If the states or transitions are manipulated manually, the
restoreInvariant()
andsetDeterministic(boolean)
methods should be used afterwards to restore representation invariants that are assumed by the built-in automata operations.- Author:
- Anders Møller <amoeller@cs.au.dk>
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
MINIMIZE_BRZOZOWSKI
Minimize using Brzozowski's O(2n) algorithm.static int
MINIMIZE_HOPCROFT
Minimize using Hopcroft's O(n log n) algorithm.static int
MINIMIZE_HUFFMAN
Minimize using Huffman's O(n2) algorithm.static int
MINIMIZE_VALMARI
Minimize using Valmari's O(n + m log m) algorithm.
-
Constructor Summary
Constructors Constructor Description Automaton()
Constructs a new automaton that accepts the empty language.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEpsilons(Collection<StatePair> pairs)
Automaton
clone()
Returns a clone of this automaton.Automaton
complement()
Automaton
compress(String set, char c)
Automaton
concatenate(Automaton a)
static Automaton
concatenate(List<Automaton> l)
void
determinize()
boolean
equals(Object obj)
Returns true if the language of this automaton is equal to the language of the given automaton.void
expandSingleton()
Expands singleton representation to normal representation.Set<State>
getAcceptStates()
Returns the set of reachable accept states.String
getCommonPrefix()
Set<String>
getFiniteStrings()
Set<String>
getFiniteStrings(int limit)
Object
getInfo()
Returns extra information associated with this automaton.State
getInitialState()
Gets initial state.Set<State>
getLiveStates()
Returns the set of live states.int
getNumberOfStates()
Returns the number of states in this automaton.int
getNumberOfTransitions()
Returns the number of transitions in this automaton.String
getShortestExample(boolean accepted)
String
getSingleton()
Returns the singleton string for this automaton.Set<State>
getStates()
Returns the set of states that are reachable from the initial state.Set<String>
getStrings(int length)
int
hashCode()
Returns hash code for this automaton.static Automaton
hexCases(Automaton a)
Automaton
homomorph(char[] source, char[] dest)
Automaton
intersection(Automaton a)
boolean
isDeterministic()
Returns deterministic flag for this automaton.boolean
isEmpty()
boolean
isEmptyString()
boolean
isFinite()
boolean
isTotal()
static Automaton
load(InputStream stream)
Retrieves a serializedAutomaton
from a stream.static Automaton
load(URL url)
Retrieves a serializedAutomaton
located by a URL.static Automaton
makeAnyChar()
static Automaton
makeAnyString()
static Automaton
makeChar(char c)
static Automaton
makeCharRange(char min, char max)
static Automaton
makeCharSet(String set)
static Automaton
makeDecimalValue(String value)
static Automaton
makeEmpty()
static Automaton
makeEmptyString()
static Automaton
makeFractionDigits(int i)
static Automaton
makeIntegerValue(String value)
static Automaton
makeInterval(int min, int max, int digits)
static Automaton
makeMaxInteger(String n)
static Automaton
makeMinInteger(String n)
static Automaton
makeString(String s)
static Automaton
makeStringMatcher(String s)
static Automaton
makeStringUnion(CharSequence... strings)
static Automaton
makeTotalDigits(int i)
void
minimize()
static Automaton
minimize(Automaton a)
Automaton
minus(Automaton a)
Automaton
optional()
Automaton
overlap(Automaton a)
void
prefixClose()
Automaton
projectChars(Set<Character> chars)
void
reduce()
Reduces this automaton.void
removeDeadTransitions()
Removes transitions to dead states and callsreduce()
andclearHashCode()
.Automaton
repeat()
Automaton
repeat(int min)
Automaton
repeat(int min, int max)
static Automaton
replaceWhitespace(Automaton a)
void
restoreInvariant()
Restores representation invariant.boolean
run(String s)
static boolean
setAllowMutate(boolean flag)
Sets or resets allow mutate flag.void
setDeterministic(boolean deterministic)
Sets deterministic flag for this automaton.void
setInfo(Object info)
Associates extra information with this automaton.void
setInitialState(State s)
Sets initial state.static void
setMinimization(int algorithm)
Selects minimization algorithm (default:MINIMIZE_HOPCROFT
).static void
setMinimizeAlways(boolean flag)
Sets or resets minimize always flag.Automaton
shuffle(Automaton a)
static String
shuffleSubsetOf(Collection<Automaton> ca, Automaton a, Character suspend_shuffle, Character resume_shuffle)
Automaton
singleChars()
void
store(OutputStream stream)
Writes thisAutomaton
to the given stream.boolean
subsetOf(Automaton a)
Automaton
subst(char c, String s)
Automaton
subst(Map<Character,Set<Character>> map)
String
toDot()
Returns Graphviz Dot representation of this automaton.String
toString()
Returns a string representation of this automaton.Automaton
trim(String set, char c)
Automaton
union(Automaton a)
static Automaton
union(Collection<Automaton> l)
-
-
-
Field Detail
-
MINIMIZE_HUFFMAN
public static final int MINIMIZE_HUFFMAN
Minimize using Huffman's O(n2) algorithm. This is the standard text-book algorithm.- See Also:
setMinimization(int)
, Constant Field Values
-
MINIMIZE_BRZOZOWSKI
public static final int MINIMIZE_BRZOZOWSKI
Minimize using Brzozowski's O(2n) algorithm. This algorithm uses the reverse-determinize-reverse-determinize trick, which has a bad worst-case behavior but often works very well in practice (even better than Hopcroft's!).- See Also:
setMinimization(int)
, Constant Field Values
-
MINIMIZE_HOPCROFT
public static final int MINIMIZE_HOPCROFT
Minimize using Hopcroft's O(n log n) algorithm. This is regarded as one of the most generally efficient algorithms that exist.- See Also:
setMinimization(int)
, Constant Field Values
-
MINIMIZE_VALMARI
public static final int MINIMIZE_VALMARI
Minimize using Valmari's O(n + m log m) algorithm.- See Also:
setMinimization(int)
, Constant Field Values
-
-
Constructor Detail
-
Automaton
public Automaton()
Constructs a new automaton that accepts the empty language. Using this constructor, automata can be constructed manually fromState
andTransition
objects.- See Also:
setInitialState(State)
,State
,Transition
-
-
Method Detail
-
setMinimization
public static void setMinimization(int algorithm)
Selects minimization algorithm (default:MINIMIZE_HOPCROFT
).- Parameters:
algorithm
- minimization algorithm
-
setMinimizeAlways
public static void setMinimizeAlways(boolean flag)
Sets or resets minimize always flag. If this flag is set, thenminimize()
will automatically be invoked after all operations that otherwise may produce non-minimal automata. By default, the flag is not set.- Parameters:
flag
- if true, the flag is set
-
setAllowMutate
public static boolean setAllowMutate(boolean flag)
Sets or resets allow mutate flag. If this flag is set, then all automata operations may modify automata given as input; otherwise, operations will always leave input automata languages unmodified. By default, the flag is not set.- Parameters:
flag
- if true, the flag is set- Returns:
- previous value of the flag
-
getSingleton
public String getSingleton()
Returns the singleton string for this automaton. An automaton that accepts exactly one string may be represented in singleton mode. In that case, this method may be used to obtain the string.- Returns:
- string, null if this automaton is not in singleton mode.
-
setInitialState
public void setInitialState(State s)
Sets initial state.- Parameters:
s
- state
-
getInitialState
public State getInitialState()
Gets initial state.- Returns:
- state
-
isDeterministic
public boolean isDeterministic()
Returns deterministic flag for this automaton.- Returns:
- true if the automaton is definitely deterministic, false if the automaton may be nondeterministic
-
setDeterministic
public void setDeterministic(boolean deterministic)
Sets deterministic flag for this automaton. This method should (only) be used if automata are constructed manually.- Parameters:
deterministic
- true if the automaton is definitely deterministic, false if the automaton may be nondeterministic
-
setInfo
public void setInfo(Object info)
Associates extra information with this automaton.- Parameters:
info
- extra information
-
getInfo
public Object getInfo()
Returns extra information associated with this automaton.- Returns:
- extra information
- See Also:
setInfo(Object)
-
getStates
public Set<State> getStates()
Returns the set of states that are reachable from the initial state.- Returns:
- set of
State
objects
-
getAcceptStates
public Set<State> getAcceptStates()
Returns the set of reachable accept states.- Returns:
- set of
State
objects
-
restoreInvariant
public void restoreInvariant()
Restores representation invariant. This method must be invoked before any built-in automata operation is performed if automaton states or transitions are manipulated manually.- See Also:
setDeterministic(boolean)
-
reduce
public void reduce()
Reduces this automaton. An automaton is "reduced" by combining overlapping and adjacent edge intervals with same destination.
-
getLiveStates
public Set<State> getLiveStates()
Returns the set of live states. A state is "live" if an accept state is reachable from it.- Returns:
- set of
State
objects
-
removeDeadTransitions
public void removeDeadTransitions()
Removes transitions to dead states and callsreduce()
andclearHashCode()
. (A state is "dead" if no accept state is reachable from it.)
-
expandSingleton
public void expandSingleton()
Expands singleton representation to normal representation. Does nothing if not in singleton representation.
-
getNumberOfStates
public int getNumberOfStates()
Returns the number of states in this automaton.
-
getNumberOfTransitions
public int getNumberOfTransitions()
Returns the number of transitions in this automaton. This number is counted as the total number of edges, where one edge may be a character interval.
-
equals
public boolean equals(Object obj)
Returns true if the language of this automaton is equal to the language of the given automaton. Implemented usinghashCode
andsubsetOf
.
-
hashCode
public int hashCode()
Returns hash code for this automaton. The hash code is based on the number of states and transitions in the minimized automaton. Invoking this method may involve minimizing the automaton.
-
toString
public String toString()
Returns a string representation of this automaton.
-
toDot
public String toDot()
Returns Graphviz Dot representation of this automaton.
-
load
public static Automaton load(URL url) throws IOException, ClassCastException, ClassNotFoundException
Retrieves a serializedAutomaton
located by a URL.- Parameters:
url
- URL of serialized automaton- Throws:
IOException
- if input/output related exception occursClassCastException
- if the data is not a serializedAutomaton
ClassNotFoundException
- if the class of the serialized object cannot be found
-
load
public static Automaton load(InputStream stream) throws IOException, ClassCastException, ClassNotFoundException
Retrieves a serializedAutomaton
from a stream.- Parameters:
stream
- input stream with serialized automaton- Throws:
IOException
- if input/output related exception occursClassCastException
- if the data is not a serializedAutomaton
ClassNotFoundException
- if the class of the serialized object cannot be found
-
store
public void store(OutputStream stream) throws IOException
Writes thisAutomaton
to the given stream.- Parameters:
stream
- output stream for serialized automaton- Throws:
IOException
- if input/output related exception occurs
-
makeEmpty
public static Automaton makeEmpty()
-
makeEmptyString
public static Automaton makeEmptyString()
-
makeAnyString
public static Automaton makeAnyString()
-
makeAnyChar
public static Automaton makeAnyChar()
-
makeChar
public static Automaton makeChar(char c)
-
makeCharRange
public static Automaton makeCharRange(char min, char max)
-
makeInterval
public static Automaton makeInterval(int min, int max, int digits) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
makeStringUnion
public static Automaton makeStringUnion(CharSequence... strings)
-
makeTotalDigits
public static Automaton makeTotalDigits(int i)
-
makeFractionDigits
public static Automaton makeFractionDigits(int i)
-
optional
public Automaton optional()
-
repeat
public Automaton repeat()
-
repeat
public Automaton repeat(int min)
-
repeat
public Automaton repeat(int min, int max)
-
complement
public Automaton complement()
-
subsetOf
public boolean subsetOf(Automaton a)
-
union
public static Automaton union(Collection<Automaton> l)
-
determinize
public void determinize()
-
addEpsilons
public void addEpsilons(Collection<StatePair> pairs)
-
isEmptyString
public boolean isEmptyString()
-
isEmpty
public boolean isEmpty()
-
isTotal
public boolean isTotal()
-
getShortestExample
public String getShortestExample(boolean accepted)
-
run
public boolean run(String s)
-
minimize
public void minimize()
-
minimize
public static Automaton minimize(Automaton a)
SeeMinimizationOperations.minimize(Automaton)
. Returns the automaton being given as argument.
-
singleChars
public Automaton singleChars()
-
homomorph
public Automaton homomorph(char[] source, char[] dest)
-
isFinite
public boolean isFinite()
-
getCommonPrefix
public String getCommonPrefix()
-
prefixClose
public void prefixClose()
-
shuffleSubsetOf
public static String shuffleSubsetOf(Collection<Automaton> ca, Automaton a, Character suspend_shuffle, Character resume_shuffle)
-
-