Package dk.brics.automaton
Class RunAutomaton
- java.lang.Object
-
- dk.brics.automaton.RunAutomaton
-
- All Implemented Interfaces:
java.io.Serializable
public class RunAutomaton extends java.lang.Object implements java.io.SerializableFinite-state automaton with fast run operation.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description (package private) boolean[]accept(package private) int[]classmap(package private) intinitial(package private) char[]points(package private) static longserialVersionUID(package private) intsize(package private) int[]transitions
-
Constructor Summary
Constructors Modifier Constructor Description privateRunAutomaton()RunAutomaton(Automaton a)Constructs a newRunAutomatonfrom a deterministicAutomaton.RunAutomaton(Automaton a, boolean tableize)Constructs a newRunAutomatonfrom a deterministicAutomaton.protectedRunAutomaton(Automaton a, boolean tableize, java.lang.Integer overrideAcceptedTransition)Constructs a newRunAutomatonfrom a deterministicAutomaton.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) intgetCharClass(char c)Gets character class of given char.char[]getCharIntervals()Returns array of character class interval start points.intgetInitialState()Returns initial state.intgetSize()Returns number of states in automaton.booleanisAccept(int state)Returns acceptance status for given state.static RunAutomatonload(java.io.InputStream stream)Retrieves a serializedRunAutomatonfrom a stream.static RunAutomatonload(java.net.URL url)Retrieves a serializedRunAutomatonlocated by a URL.AutomatonMatchernewMatcher(java.lang.CharSequence s)Creates a new automaton matcher for the given input.AutomatonMatchernewMatcher(java.lang.CharSequence s, int startOffset, int endOffset)Creates a new automaton matcher for the given input.booleanrun(java.lang.String s)Returns true if the given string is accepted by this automaton.intrun(java.lang.String s, int offset)Returns the length of the longest accepted run of the given string starting at the given offset.(package private) voidsetAlphabet()Sets alphabet table for optimal run performance.intstep(int state, char c)Returns the state obtained by reading the given char from the given state.voidstore(java.io.OutputStream stream)Writes thisRunAutomatonto the given stream.java.lang.StringtoString()Returns a string representation of this automaton.private inttransition(State q, boolean overrideTransitionWithState, int override)For normal operation, simply return the transitioned to state.
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
size
int size
-
accept
boolean[] accept
-
initial
int initial
-
transitions
int[] transitions
-
points
char[] points
-
classmap
int[] classmap
-
-
Constructor Detail
-
RunAutomaton
private RunAutomaton()
-
RunAutomaton
public RunAutomaton(Automaton a)
Constructs a newRunAutomatonfrom a deterministicAutomaton. Same asRunAutomaton(a, true).- Parameters:
a- an automaton
-
RunAutomaton
public RunAutomaton(Automaton a, boolean tableize)
Constructs a newRunAutomatonfrom a deterministicAutomaton. If the given automaton is not deterministic, it is determinized first.- Parameters:
a- an automatontableize- if true, a transition table is created which makes therunmethod faster in return of a higher memory usage
-
RunAutomaton
protected RunAutomaton(Automaton a, boolean tableize, java.lang.Integer overrideAcceptedTransition)
Constructs a newRunAutomatonfrom a deterministicAutomaton. If the given automaton is not deterministic, it is determinized first.- Parameters:
a- an automatontableize- if true, a transition table is created which makes therunmethod faster in return of a higher memory usageoverrideAcceptedTransition- whether to set a specific integer value for an accepted state or not.nullindicates to simply treat a state that is accepted as normal, a non-null value indicates to set all states which could be accepted to a specific int value.
-
-
Method Detail
-
setAlphabet
void setAlphabet()
Sets alphabet table for optimal run performance.
-
toString
public java.lang.String toString()
Returns a string representation of this automaton.- Overrides:
toStringin classjava.lang.Object
-
getSize
public int getSize()
Returns number of states in automaton.
-
isAccept
public boolean isAccept(int state)
Returns acceptance status for given state.
-
getInitialState
public int getInitialState()
Returns initial state.
-
getCharIntervals
public char[] getCharIntervals()
Returns array of character class interval start points. The array should not be modified by the caller.
-
getCharClass
int getCharClass(char c)
Gets character class of given char.
-
load
public static RunAutomaton load(java.net.URL url) throws java.io.IOException, java.lang.ClassCastException, java.lang.ClassNotFoundException
Retrieves a serializedRunAutomatonlocated by a URL.- Parameters:
url- URL of serialized automaton- Throws:
java.io.IOException- if input/output related exception occursjava.lang.ClassCastException- if the data is not a serializedRunAutomatonjava.lang.ClassNotFoundException- if the class of the serialized object cannot be found
-
load
public static RunAutomaton load(java.io.InputStream stream) throws java.io.IOException, java.lang.ClassCastException, java.lang.ClassNotFoundException
Retrieves a serializedRunAutomatonfrom a stream.- Parameters:
stream- input stream with serialized automaton- Throws:
java.io.IOException- if input/output related exception occursjava.lang.ClassCastException- if the data is not a serializedRunAutomatonjava.lang.ClassNotFoundException- if the class of the serialized object cannot be found
-
store
public void store(java.io.OutputStream stream) throws java.io.IOExceptionWrites thisRunAutomatonto the given stream.- Parameters:
stream- output stream for serialized automaton- Throws:
java.io.IOException- if input/output related exception occurs
-
transition
private int transition(State q, boolean overrideTransitionWithState, int override)
For normal operation, simply return the transitioned to state. If the transition is to an accepted state and an override value is provided, then return the override.This allows us to avoid looking up the accepted boolean array as we know we will have reached an accepted state from the transition via the returned state.
-
step
public int step(int state, char c)Returns the state obtained by reading the given char from the given state. Returns -1 if not obtaining any such state. (If the originalAutomatonhad no dead states, -1 is returned here if and only if a dead state is entered in an equivalent automaton with a total transition function.)
-
run
public boolean run(java.lang.String s)
Returns true if the given string is accepted by this automaton.
-
run
public int run(java.lang.String s, int offset)Returns the length of the longest accepted run of the given string starting at the given offset.- Parameters:
s- the stringoffset- offset intoswhere the run starts- Returns:
- length of the longest accepted run, -1 if no run is accepted
-
newMatcher
public AutomatonMatcher newMatcher(java.lang.CharSequence s)
Creates a new automaton matcher for the given input.- Parameters:
s- the CharSequence to search- Returns:
- A new automaton matcher for the given input
-
newMatcher
public AutomatonMatcher newMatcher(java.lang.CharSequence s, int startOffset, int endOffset)
Creates a new automaton matcher for the given input.- Parameters:
s- the CharSequence to searchstartOffset- the starting offset of the given character sequenceendOffset- the ending offset of the given character sequence- Returns:
- A new automaton matcher for the given input
-
-