Class Dfa<T>
Dfa can be created with NfaBuilder.compileToDfa().
For matching the automaton at the start of an input character source use the high level
methods matchesAtStart(monq.jfa.CharSource), getAtStart(monq.jfa.CharSource), matchLength(monq.jfa.CharSource), match(monq.jfa.CharSource, java.lang.StringBuilder) and
matchWithPartials(monq.jfa.CharSource, monq.jfa.RecordingMatcher<T>). All matching methods remove the matching characters from the
input.
The low level access to traversing the automaton one character one automaton state at a
time is transition(monq.jfa.CharSource) together with getters providing information about the traversal.
These are marked with (*).
Technical Note
In contrast to an Nfa, the compilation step makes sure that there are no epsilon
transitions in the state graph starting at the start state — with one exception: every stop
state (state with a value) has an epsilon transition to a single last state which is not
relevant for matching, but allows to use the automaton again in an NfaBuilder by pushing
it with NfaBuilder.pushCopy(...).
-
Method Summary
Modifier and TypeMethodDescriptionbooleanatStop()(*)Returns the result of theisStoppredicate provided withwithIsStopValue(java.util.function.Predicate<T>)applied togetCurrentValue().voidgetAtStart(String in) CallsgetAtStart(CharSource)with the wrapped string.getAtStart(CharSource in) If this automaton matches at the start of the input, the value stored in the stop state reached by the match is returned, otherwisenull.intgetCh()(*)Get latest character or control code after a call totransition(monq.jfa.CharSource).(*)Provides the value stored in the current state as maintained bytransition(CharSource).int(*)How many characters of the input we over-read bytransition(monq.jfa.CharSource).match(String in, StringBuilder out) Callsmatch(CharSource, StringBuilder)with the wrapped string.match(CharSource in, StringBuilder out) Copies the longest matching prefix of the input to the output.booleanmatchesAtStart(String in) CallsmatchesAtStart(CharSource)with the wrapped string.booleanReturnstrue, if the this automaton matches at the start of the character source.booleanReturns the result of applyingisStopas provided withwithIsStopValue(Predicate)to the start state.intmatchLength(String in) CallsmatchLength(CharSource)with the wrapped string.intIf this automaton matches at the start of the input, the number of matching characters is returned, otherwise -1.matchWithPartials(String text, RecordingMatcher<T> rm) Calls the other one with the input wrapped into aCharSource.matchWithPartials(CharSource in, RecordingMatcher<T> rm) Convenience method to callRecordingMatcher.match(Dfa, String).voidreset()Resets state traversal as follows:voidtoDot(PrintStream out) booleantransition(CharSource in) Reads one character from the input source and performs a transition to the next state of the automaton if possible.withIsStopValue(Predicate<T> isStop) The default behavior for traversing the automaton is to treat states with a non-nullvalue as stop states.
-
Method Details
-
withIsStopValue
The default behavior for traversing the automaton is to treat states with a non-nullvalue as stop states. However when usingmatchWithPartials, states may have values only as a marker to be recorded by that method. In this case a new predicate to exclude markers as stop states can be configured here.- Parameters:
isStop- to decide whether a state's value signals a stop state. The predicate will never be called withnull, as a state with anullis always treated as not being a stop state.- Returns:
- a new automaton with the given predicate.
-
getIsStopValue
-
dotPrint
-
getCh
public int getCh()(*)Get latest character or control code after a call totransition(monq.jfa.CharSource).- Returns:
- -2 if we are positioned on the start state, -1 if we hit EOF on the source, otherwise
the character read by
transition().
-
getCurrentValue
(*)Provides the value stored in the current state as maintained bytransition(CharSource).- Returns:
- the value or
nullif the current state has no value
-
atStop
public boolean atStop()(*)Returns the result of theisStoppredicate provided withwithIsStopValue(java.util.function.Predicate<T>)applied togetCurrentValue(). -
getOverread
public int getOverread()(*)How many characters of the input we over-read bytransition(monq.jfa.CharSource).false.- Returns:
- the excess number of
truereturned bytransition(monq.jfa.CharSource)before noticing that we cannot reach a stop state any more. At this point, the input source is already re-wound by excess characters read.
-
toDot
-
matchesEmpty
public boolean matchesEmpty()Returns the result of applyingisStopas provided withwithIsStopValue(Predicate)to the start state.- Returns:
trueif the start state is a stop state of the automaton
-
matchesAtStart
Returnstrue, if the this automaton matches at the start of the character source.- Throws:
IOException
-
matchesAtStart
CallsmatchesAtStart(CharSource)with the wrapped string.- Throws:
IOException
-
getAtStart
If this automaton matches at the start of the input, the value stored in the stop state reached by the match is returned, otherwisenull.- Throws:
IOException
-
getAtStart
CallsgetAtStart(CharSource)with the wrapped string.- Throws:
IOException
-
matchLength
If this automaton matches at the start of the input, the number of matching characters is returned, otherwise -1. Reminder: 0 is the result for matching the empty string.- Throws:
IOException
-
matchLength
CallsmatchLength(CharSource)with the wrapped string.- Throws:
IOException
-
match
Copies the longest matching prefix of the input to the output.The length of the output may not change for two reasons:
- no match was found or
- a match for the empty string was found.
null.- Returns:
- value associated with the match, or
null, if no match can be found. - Throws:
IOException- only ifin.read()throws.
-
match
Callsmatch(CharSource, StringBuilder)with the wrapped string.- Throws:
IOException
-
matchWithPartials
Convenience method to callRecordingMatcher.match(Dfa, String).- Throws:
IOException
-
matchWithPartials
Calls the other one with the input wrapped into aCharSource.- Throws:
IOException
-
transition
Reads one character from the input source and performs a transition to the next state of the automaton if possible. In this case
trueis returned and the following conditions hold:getCh()returns the character read.getCurrentValue()returns the value stored in the state reached with the character read.getOverread()provides no useful information.
If this methods returns
false, the following conditions hold:- The current automaton state is set to the start state, such that the next call to this method will start over with traversal. Further, all characters read since we last left a stop state are pushed back into the input.
getCh()returns -1 if we hit EOF or -2 if no transition was possible with the character read.getCurrentValue()returns the value of the start state (see above), may benull.getOverread()provides the number N oftruereturns after having left the last seen stop state. This allows the caller to discard information recorded for the last N transitions, as they did not lead to a further stop state.
Hint: This method allows to implement additional matching methods similar to
match(monq.jfa.CharSource, java.lang.StringBuilder)orgetAtStart(monq.jfa.CharSource)which call this method in a loop for the same character source untilfalseis returned. Changing the character source from one call to the next does not make much sense, exceptfalsewas returned orreset()was called in between.- Parameters:
in- to read characters from- Returns:
trueif a character could be read and allowed a state transition.- Throws:
IOException- if the source throws it
-
reset
public void reset()Resets state traversal as follows:
- the current state is set to the start state
getCh()will return -2getOverread()will return 0
This does not need to be called, except in one circumstance: to pre-empt a traversal with
transition(monq.jfa.CharSource), i.e. before it returnsfalse. This may leave the input source used withtransitionin a state where only a partial match was read.
-