-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classprivate static final classprivate static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum effort thatdeterminize(org.apache.lucene.util.automaton.Automaton, int)should spend before giving up and throwingTooComplexToDeterminizeException.static final intMaximum level of recursion allowed in recursive operations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Automatoncomplement(Automaton a, int determinizeWorkLimit) Returns a (deterministic) automaton that accepts the complement of the language of the given automaton.static Automatonconcatenate(List<Automaton> l) Returns an automaton that accepts the concatenation of the languages of the given automata.static Automatonconcatenate(Automaton a1, Automaton a2) Returns an automaton that accepts the concatenation of the languages of the given automata.static Automatondeterminize(Automaton a, int workLimit) Determinizes the given automaton.static StringReturns the longest string that is a prefix of all accepted strings and visits each state at most once.static BytesRefReturns the longest BytesRef that is a prefix of all accepted strings and visits each state at most once.static BytesRefReturns the longest BytesRef that is a suffix of all accepted strings.private static BitSetReturns the set of live states.private static BitSetReturns bitset marking states reachable from the initial state.private static BitSetReturns bitset marking states that can reach an accept state.static IntsRefIf this automaton accepts a single input, return it.static booleanReturns true if this automaton has any states that cannot be reached from the initial state or cannot reach an accept state.static booleanReturns true if there are dead states reachable from an initial state.static booleanReturns true if there are dead states that reach an accept state.static Automatonintersection(Automaton a1, Automaton a2) Returns an automaton that accepts the intersection of the languages of the given automata.static booleanReturns true if the given automaton accepts no strings.static booleanReturns true if the language of this automaton is finite.private static booleanisFinite(Transition scratch, Automaton a, int state, BitSet path, BitSet visited, int level) Checks whether there is a loop containing state.static booleanReturns true if the given automaton accepts all strings.static booleanReturns true if the given automaton accepts all strings for the specified min/max range of the alphabet.static AutomatonReturns a (deterministic) automaton that accepts the intersection of the language ofa1and the complement of the language ofa2.static AutomatonReturns an automaton that accepts the union of the empty string and the language of the given automaton.static AutomatonRemoves transitions to dead states (a state is "dead" if it is not reachable from the initial state or no accept state is reachable from it.)static AutomatonReturns an automaton that accepts the Kleene star (zero or more concatenated repetitions) of the language of the given automaton.static AutomatonReturns an automaton that acceptsminor more concatenated repetitions of the language of the given automaton.static AutomatonReturns an automaton that accepts betweenminandmax(including both) concatenated repetitions of the language of the given automaton.static AutomatonReturns an automaton accepting the reverse language.static AutomatonReverses the automaton, returning the new initial states.private static voidreverseBytes(BytesRef ref) static booleanReturns true if the given string is accepted by the automaton.static booleanReturns true if the given string (expressed as unicode codepoints) is accepted by the automaton.static booleansameLanguage(Automaton a1, Automaton a2) Returns true if these two automata accept exactly the same language.static booleanReturns true if the language ofa1is a subset of the language ofa2.static int[]Returns the topological sort of all states reachable from the initial state.private static inttopoSortStates(Automaton a, int[] states) Performs a topological sort on the states of the given Automaton.private static IntHashSet(package private) static AutomatonReturns a new automaton accepting the same language with added transitions to a dead state so that from every state and every label there is a transition.static Automatonunion(Collection<Automaton> l) Returns an automaton that accepts the union of the languages of the given automata.static AutomatonReturns an automaton that accepts the union of the languages of the given automata.
-
Field Details
-
DEFAULT_DETERMINIZE_WORK_LIMIT
public static final int DEFAULT_DETERMINIZE_WORK_LIMITDefault maximum effort thatdeterminize(org.apache.lucene.util.automaton.Automaton, int)should spend before giving up and throwingTooComplexToDeterminizeException.- See Also:
-
MAX_RECURSION_LEVEL
public static final int MAX_RECURSION_LEVELMaximum level of recursion allowed in recursive operations.- See Also:
-
-
Constructor Details
-
Operations
private Operations()
-
-
Method Details
-
concatenate
Returns an automaton that accepts the concatenation of the languages of the given automata.Complexity: linear in total number of states.
-
concatenate
Returns an automaton that accepts the concatenation of the languages of the given automata.Complexity: linear in total number of states.
-
optional
Returns an automaton that accepts the union of the empty string and the language of the given automaton. This may create a dead state.Complexity: linear in number of states.
-
repeat
Returns an automaton that accepts the Kleene star (zero or more concatenated repetitions) of the language of the given automaton. Never modifies the input automaton language.Complexity: linear in number of states.
-
repeat
Returns an automaton that acceptsminor more concatenated repetitions of the language of the given automaton.Complexity: linear in number of states and in
min. -
repeat
Returns an automaton that accepts betweenminandmax(including both) concatenated repetitions of the language of the given automaton.Complexity: linear in number of states and in
minandmax. -
toSet
-
complement
Returns a (deterministic) automaton that accepts the complement of the language of the given automaton.Complexity: linear in number of states if already deterministic and exponential otherwise.
- Parameters:
determinizeWorkLimit- maximum effort to spend determinizing the automaton. Set higher to allow more complex queries and lower to prevent memory exhaustion.DEFAULT_DETERMINIZE_WORK_LIMITis a good starting default.
-
minus
Returns a (deterministic) automaton that accepts the intersection of the language ofa1and the complement of the language ofa2. As a side-effect, the automata may be determinized, if not already deterministic.Complexity: quadratic in number of states if a2 already deterministic and exponential in number of a2's states otherwise.
- Parameters:
a1- the initial automatona2- the automaton to subtractdeterminizeWorkLimit- maximum effort to spend determinizing the automaton. Set higher to allow more complex queries and lower to prevent memory exhaustion.DEFAULT_DETERMINIZE_WORK_LIMITis a good starting default.
-
intersection
Returns an automaton that accepts the intersection of the languages of the given automata. Never modifies the input automata languages.Complexity: quadratic in number of states.
-
sameLanguage
Returns true if these two automata accept exactly the same language. This is a costly computation! Both automata must be determinized and have no dead states! -
hasDeadStates
Returns true if this automaton has any states that cannot be reached from the initial state or cannot reach an accept state. Cost is O(numTransitions+numStates). -
hasDeadStatesFromInitial
Returns true if there are dead states reachable from an initial state. -
hasDeadStatesToAccept
Returns true if there are dead states that reach an accept state. -
subsetOf
Returns true if the language ofa1is a subset of the language ofa2. Both automata must be determinized and must have no dead states.Complexity: quadratic in number of states.
-
union
Returns an automaton that accepts the union of the languages of the given automata.Complexity: linear in number of states.
-
union
Returns an automaton that accepts the union of the languages of the given automata.Complexity: linear in number of states.
-
determinize
Determinizes the given automaton.Worst case complexity: exponential in number of states.
- Parameters:
workLimit- Maximum amount of "work" that the powerset construction will spend before throwingTooComplexToDeterminizeException. Higher numbers allow this operation to consume more memory and CPU but allow more complex automatons. UseDEFAULT_DETERMINIZE_WORK_LIMITas a decent default if you don't otherwise know what to specify.- Throws:
TooComplexToDeterminizeException- if determinizing requires more thanworkLimit"effort"
-
isEmpty
Returns true if the given automaton accepts no strings. -
isTotal
Returns true if the given automaton accepts all strings. The automaton must be minimized. -
isTotal
Returns true if the given automaton accepts all strings for the specified min/max range of the alphabet. The automaton must be minimized. -
run
Returns true if the given string is accepted by the automaton. The input must be deterministic.Complexity: linear in the length of the string.
Note: for full performance, use the
RunAutomatonclass. -
run
Returns true if the given string (expressed as unicode codepoints) is accepted by the automaton. The input must be deterministic.Complexity: linear in the length of the string.
Note: for full performance, use the
RunAutomatonclass. -
getLiveStates
Returns the set of live states. A state is "live" if an accept state is reachable from it and if it is reachable from the initial state. -
getLiveStatesFromInitial
Returns bitset marking states reachable from the initial state. -
getLiveStatesToAccept
Returns bitset marking states that can reach an accept state. -
removeDeadStates
Removes transitions to dead states (a state is "dead" if it is not reachable from the initial state or no accept state is reachable from it.) -
isFinite
Returns true if the language of this automaton is finite. The automaton must not have any dead states. -
isFinite
private static boolean isFinite(Transition scratch, Automaton a, int state, BitSet path, BitSet visited, int level) Checks whether there is a loop containing state. (This is sufficient since there are never transitions to dead states.) -
getCommonPrefix
Returns the longest string that is a prefix of all accepted strings and visits each state at most once. The automaton must not have dead states. If this automaton has already been converted to UTF-8 (e.g. usingUTF32ToUTF8) then you should usegetCommonPrefixBytesRef(org.apache.lucene.util.automaton.Automaton)instead.- Returns:
- common prefix, which can be an empty (length 0) String (never null)
- Throws:
IllegalArgumentException- if the automaton has dead states reachable from the initial state.
-
getCommonPrefixBytesRef
Returns the longest BytesRef that is a prefix of all accepted strings and visits each state at most once.- Returns:
- common prefix, which can be an empty (length 0) BytesRef (never null), and might possibly include a UTF-8 fragment of a full Unicode character
-
getSingleton
If this automaton accepts a single input, return it. Else, return null. The automaton must be deterministic. -
getCommonSuffixBytesRef
Returns the longest BytesRef that is a suffix of all accepted strings. Worst case complexity: quadratic with number of states+transitions.- Returns:
- common suffix, which can be an empty (length 0) BytesRef (never null)
-
reverseBytes
-
reverse
Returns an automaton accepting the reverse language. -
reverse
Reverses the automaton, returning the new initial states. -
totalize
Returns a new automaton accepting the same language with added transitions to a dead state so that from every state and every label there is a transition. -
topoSortStates
Returns the topological sort of all states reachable from the initial state. This method assumes that the automaton does not contain cycles, and will throw an IllegalArgumentException if a cycle is detected. The CPU cost is O(numTransitions), and the implementation is non-recursive, so it will not exhaust the java stack for automaton matching long strings. If there are dead states in the automaton, they will be removed from the returned array.Note: This method uses a deque to iterative the states, which could potentially consume a lot of heap space for some automatons. Specifically, automatons with a deep level of states (i.e., a large number of transitions from the initial state to the final state) may particularly contribute to high memory usage. The memory consumption of this method can be considered as O(N), where N is the depth of the automaton (the maximum number of transitions from the initial state to any state). However, as this method detects cycles, it will never attempt to use infinite RAM.
- Parameters:
a- the Automaton to be sorted- Returns:
- the topologically sorted array of state ids
-
topoSortStates
Performs a topological sort on the states of the given Automaton.- Parameters:
a- The automaton whose states are to be topologically sorted.states- An int array which stores the states.- Returns:
- the number of states in the final sorted list.
- Throws:
IllegalArgumentException- if the input automaton has a cycle.
-