Package dk.brics.automaton
Class Automaton
java.lang.Object
dk.brics.automaton.Automaton
- All Implemented Interfaces:
Serializable,Cloneable
Finite-state automaton with regular expression operations.
Class invariants:
- An automaton is either represented explicitly (with
StateandTransitionobjects) 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()
and setDeterministic(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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMinimize using Brzozowski's O(2n) algorithm.static final intMinimize using Hopcroft's O(n log n) algorithm.static final intMinimize using Huffman's O(n2) algorithm.static final intMinimize using Valmari's O(n + m log m) algorithm. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEpsilons(Collection<StatePair> pairs) clone()Returns a clone of this automaton.static Automatonconcatenate(List<Automaton> l) voidbooleanReturns true if the language of this automaton is equal to the language of the given automaton.voidExpands singleton representation to normal representation.Returns the set of reachable accept states.getFiniteStrings(int limit) getInfo()Returns extra information associated with this automaton.Gets initial state.Returns the set of live states.intReturns the number of states in this automaton.intReturns the number of transitions in this automaton.getShortestExample(boolean accepted) Returns the singleton string for this automaton.Returns the set of states that are reachable from the initial state.getStrings(int length) inthashCode()Returns hash code for this automaton.static Automatonhomomorph(char[] source, char[] dest) booleanReturns deterministic flag for this automaton.booleanisEmpty()booleanbooleanisFinite()booleanisTotal()static Automatonload(InputStream stream) Retrieves a serializedAutomatonfrom a stream.static AutomatonRetrieves a serializedAutomatonlocated by a URL.static Automatonstatic Automatonstatic AutomatonmakeChar(char c) static AutomatonmakeCharRange(char min, char max) static AutomatonmakeCharSet(String set) static AutomatonmakeDecimalValue(String value) static Automatonstatic Automatonstatic AutomatonmakeFractionDigits(int i) static AutomatonmakeIntegerValue(String value) static AutomatonmakeInterval(int min, int max, int digits) static Automatonstatic Automatonstatic AutomatonmakeString(String s) static Automatonstatic AutomatonmakeStringUnion(CharSequence... strings) static AutomatonmakeTotalDigits(int i) voidminimize()static Automatonoptional()voidprojectChars(Set<Character> chars) voidreduce()Reduces this automaton.voidRemoves transitions to dead states and callsreduce()andclearHashCode().repeat()repeat(int min) repeat(int min, int max) static AutomatonvoidRestores representation invariant.booleanstatic booleansetAllowMutate(boolean flag) Sets or resets allow mutate flag.voidsetDeterministic(boolean deterministic) Sets deterministic flag for this automaton.voidAssociates extra information with this automaton.voidSets initial state.static voidsetMinimization(int algorithm) Selects minimization algorithm (default:MINIMIZE_HOPCROFT).static voidsetMinimizeAlways(boolean flag) Sets or resets minimize always flag.static StringshuffleSubsetOf(Collection<Automaton> ca, Automaton a, Character suspend_shuffle, Character resume_shuffle) voidstore(OutputStream stream) Writes thisAutomatonto the given stream.booleantoDot()Returns Graphviz Dot representation of this automaton.toString()Returns a string representation of this automaton.static Automatonunion(Collection<Automaton> l)
-
Field Details
-
MINIMIZE_HUFFMAN
public static final int MINIMIZE_HUFFMANMinimize using Huffman's O(n2) algorithm. This is the standard text-book algorithm.- See Also:
-
MINIMIZE_BRZOZOWSKI
public static final int MINIMIZE_BRZOZOWSKIMinimize 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:
-
MINIMIZE_HOPCROFT
public static final int MINIMIZE_HOPCROFTMinimize using Hopcroft's O(n log n) algorithm. This is regarded as one of the most generally efficient algorithms that exist.- See Also:
-
MINIMIZE_VALMARI
public static final int MINIMIZE_VALMARIMinimize using Valmari's O(n + m log m) algorithm.- See Also:
-
-
Constructor Details
-
Automaton
public Automaton()Constructs a new automaton that accepts the empty language. Using this constructor, automata can be constructed manually fromStateandTransitionobjects.- See Also:
-
-
Method Details
-
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
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
Sets initial state.- Parameters:
s- 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
Associates extra information with this automaton.- Parameters:
info- extra information
-
getInfo
Returns extra information associated with this automaton.- Returns:
- extra information
- See Also:
-
getStates
Returns the set of states that are reachable from the initial state.- Returns:
- set of
Stateobjects
-
getAcceptStates
Returns the set of reachable accept states.- Returns:
- set of
Stateobjects
-
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:
-
reduce
public void reduce()Reduces this automaton. An automaton is "reduced" by combining overlapping and adjacent edge intervals with same destination. -
getLiveStates
Returns the set of live states. A state is "live" if an accept state is reachable from it.- Returns:
- set of
Stateobjects
-
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
Returns true if the language of this automaton is equal to the language of the given automaton. Implemented usinghashCodeandsubsetOf. -
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
Returns a string representation of this automaton. -
toDot
Returns Graphviz Dot representation of this automaton. -
clone
Returns a clone of this automaton. -
load
public static Automaton load(URL url) throws IOException, ClassCastException, ClassNotFoundException Retrieves a serializedAutomatonlocated by a URL.- Parameters:
url- URL of serialized automaton- Throws:
IOException- if input/output related exception occursClassCastException- if the data is not a serializedAutomatonClassNotFoundException- if the class of the serialized object cannot be found
-
load
public static Automaton load(InputStream stream) throws IOException, ClassCastException, ClassNotFoundException Retrieves a serializedAutomatonfrom a stream.- Parameters:
stream- input stream with serialized automaton- Throws:
IOException- if input/output related exception occursClassCastException- if the data is not a serializedAutomatonClassNotFoundException- if the class of the serialized object cannot be found
-
store
Writes thisAutomatonto the given stream.- Parameters:
stream- output stream for serialized automaton- Throws:
IOException- if input/output related exception occurs
-
makeEmpty
-
makeEmptyString
-
makeAnyString
-
makeAnyChar
-
makeChar
-
makeCharRange
-
makeCharSet
-
makeInterval
- Throws:
IllegalArgumentException
-
makeString
-
makeStringUnion
-
makeMaxInteger
-
makeMinInteger
-
makeTotalDigits
-
makeFractionDigits
-
makeIntegerValue
-
makeDecimalValue
-
makeStringMatcher
-
concatenate
-
concatenate
-
optional
-
repeat
-
repeat
-
repeat
-
complement
-
minus
-
intersection
-
subsetOf
-
union
-
union
-
determinize
public void determinize() -
addEpsilons
-
isEmptyString
public boolean isEmptyString() -
isEmpty
public boolean isEmpty() -
isTotal
public boolean isTotal() -
getShortestExample
-
run
-
minimize
public void minimize() -
minimize
SeeMinimizationOperations.minimize(Automaton). Returns the automaton being given as argument. -
overlap
-
singleChars
-
trim
-
compress
-
subst
-
subst
-
homomorph
-
projectChars
-
isFinite
public boolean isFinite() -
getStrings
-
getFiniteStrings
-
getFiniteStrings
-
getCommonPrefix
-
prefixClose
public void prefixClose() -
hexCases
-
replaceWhitespace
-
shuffleSubsetOf
-
shuffle
-