Class Automaton<A extends Automaton<A,T>,T extends TransitionSymbol<T>>
- java.lang.Object
-
- it.unive.lisa.util.datastructures.automaton.Automaton<A,T>
-
- Type Parameters:
A
- the concrete type of this automatonT
- the concrete type ofTransitionSymbol
s that instances of this class have on their transitions
- All Implemented Interfaces:
AutomataFactory<A,T>
public abstract class Automaton<A extends Automaton<A,T>,T extends TransitionSymbol<T>> extends java.lang.Object implements AutomataFactory<A,T>
A class that describes a generic automaton(dfa, nfa, epsilon nfa). Transitions recognize instances ofTransitionSymbol
.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Optional<java.lang.Boolean>
deterministic
Flag that tracks if this automaton is deterministic.protected java.util.Optional<java.lang.Boolean>
minimized
Flag that tracks if this automaton has been minimized.protected java.util.SortedSet<State>
states
The states of this automaton.protected java.util.SortedSet<Transition<T>>
transitions
The transitions of this automaton.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Automaton()
Builds an empty automaton.protected
Automaton(java.util.SortedSet<State> states, java.util.SortedSet<Transition<T>> transitions)
Builds a new automaton with givenstates
andtransitions
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptsEmptyLanguage()
Checks if the Automatonthis
accepts the empty language.void
addState(State s)
Adds a new state to this automaton.void
addTransition(State from, State to, T input)
Builds a new transition going fromfrom
toto
and recognizinginput
and adds it to the set of transitions of this automaton.void
addTransition(Transition<T> t)
Adds the given transition to the set of transitions of this automaton.boolean
areMutuallyReachable(State s1, State s2)
Yieldstrue
if and only if the two given states are mutually reachable, that is, if there exist a path going froms1
tos2
and one going froms2
tos1
.java.util.SortedSet<T>
commonAlphabet(A other)
Yields the common alphabet of the automatathis
andother
.A
complement(java.util.Set<T> sigma)
Return a new Automaton that accept a language that is the complementary language ofthis
.A
concat(A other)
Create a new automaton representing the concatenation ofthis
andother
.abstract T
concat(T first, T second)
Yields aTransitionSymbol
that is the concatenation of the two given ones.A
copy()
Deep-copies this automaton to a new one.A
determinize()
Yields a deterministic automaton equivalent to this one.abstract T
epsilon()
Yields the symbol representing the empty string.java.util.SortedSet<State>
epsilonClosure(State s)
Computes the epsilon closure of this automaton starting froms
, namely the set of states that are reachable froms
just with epsilon transitions.java.util.SortedSet<State>
epsilonClosure(java.util.Set<State> set)
Computes the epsilon closure of this automaton starting fromset
, namely the set of states that are reachable from each state inset
just with epsilon transitions.boolean
equals(java.lang.Object obj)
A
extractLongestString()
Yields the sub-automaton contained in this one that recognizes only the longest string in the language ofthis
.A
factors()
Yields the automaton that recognizes all possible substrings of the strings recognized by this automaton.A
factorsChangingInitialState(State s)
Yields a copy of this automaton, but where all states are final and only the given state is initial.java.util.Set<java.util.List<State>>
getAllPaths()
Yields all possible paths going from an initial state to a final state in the target automaton.java.util.SortedSet<Transition<T>>
getAllTransitionsConnecting(State s1, State s2)
Yields the set of transitions going froms1
tos2
.java.util.SortedSet<T>
getAlphabet()
Yields the set of symbols that represents the alphabet of the symbols recognized by this automaton (that is, as subset of the alphabet over which the automata is defined).java.util.SortedSet<State>
getFinalStates()
Yields the set of final states of this automaton.java.util.SortedSet<Transition<T>>
getIngoingTransitionsFrom(State s)
Yields the set of all ingoing transitions to the given state.State
getInitialState()
Yields one of the initial states of this automaton.java.util.SortedSet<State>
getInitialStates()
Yields the set of initial states of this automaton.java.util.SortedSet<java.lang.String>
getLanguage()
Returns the concretized language accepted bythis
.java.util.SortedSet<State>
getNextStates(State node)
Yields the set of possible successors of the given node.java.util.SortedSet<T>
getNextSymbols(State s, int n)
Yields the set of symbols of length at mostn
that can be recognized starting from the given state.java.util.SortedSet<Transition<T>>
getOutgoingTransitionsFrom(State s)
Yields the set of all outgoing transitions from the given state.java.util.SortedSet<T>
getReadableSymbolsFromState(State state)
Yields the set of symbols that can be read (accepted) by the given state, excluding epsilon.java.util.SortedSet<T>
getReadableSymbolsFromStates(java.util.Set<State> states)
Yields the set of symbols that can be read (accepted) by the given set of states.protected State
getStateFromPair(java.util.Map<State,org.apache.commons.lang3.tuple.Pair<State,State>> mapping, org.apache.commons.lang3.tuple.Pair<State,State> pair)
Given a pair of states, yields the state associated to the pair in the given state mapping.java.util.SortedSet<State>
getStates()
Yields the set of states of this automaton.java.util.SortedSet<Transition<T>>
getTransitions()
Yields the set of transitions contained in this automaton.boolean
hasCycle()
Checks if the Automatonthis
has any cycle.int
hashCode()
boolean
hasOnlyOnePath()
Yieldstrue
if and only if this automaton has only one path.A
intersection(A other)
Returns the Automaton that accepts the language that is the intersection between the language ofthis
and another Automaton.boolean
isContained(A other)
Yieldstrue
if and only ifthis
is contained intoother
, that is, if the language recognized by the intersection betweenthis
and the complement ofother
is empty.boolean
isDeterministic()
Yields true if and only if this automaton is deterministic, that is, if the transition relation is a function.boolean
isEqualTo(A other)
Checks if the automatonthis
accepts the same language asother
, implemented as:
language(A).equals(language(B))
if both automata are loop free,A.contains(B) && B.contains(A)
otherwise.int
lenghtOfLongestString()
Yields the length of the longest string recognized by this automaton, withInteger.MAX_VALUE
representing infinity.A
makeAcyclic()
Yields an acyclic version of this automaton, where all SCCs have been removed.java.util.List<State>
maximumPath(State src, State target)
Finds the maximum path between the two given states using the Dijkstra algorithm.A
minimize()
Yields a minimal automaton equivalent to this one through Brzozowski's minimization algorithm.java.util.List<State>
minimumPath(State src, State target)
Finds the minimum path between the two given states using the Dijkstra algorithm.java.util.SortedSet<State>
nextStatesNFA(java.util.Set<State> set, T sym)
Yields that states that can be reached from at least one state inset
by reading symbolsym
.A
prefix()
Yields the automaton that recognizes all possible prefixes of the strings recognized by this automaton.java.lang.String
prettyPrint()
Yields a textual representation of the adjacency matrix of this automaton.boolean
recognizesExactlyOneString()
Yieldstrue
if this automaton recognizes exactly one string, that is, if it has no loops and all states have at most one outgoing transition.void
removeStates(java.util.Set<State> ts)
Removes every state in the given set from the ones of this automaton.void
removeTransitions(java.util.Set<Transition<T>> ts)
Removes every transition in the given set from the ones of this automaton.A
removeUnreachableStates()
Remove all the unreachable states from the current automaton.A
reverse()
Creates an automaton that accept the reverse language.A
star()
Creates a new automaton composed of a loop over this one.A
suffix()
Yields the automaton that recognizes all possible suffixes of the strings recognized by this automaton.abstract RegularExpression
symbolToRegex(T symbol)
Yields aRegularExpression
representing the given symbol.RegularExpression
toRegex()
Creates and return the regex that represent the accepted language by the automatonthis
, using Brzozowski algebraic method (see here).A
toSingleInitalState()
If this automaton has a single initial state, this method returnsthis
.java.lang.String
toString()
A
totalize(java.util.Set<T> sigma)
Performs the totalization of this automaton w.r.t the given alphabet.A
union(A other)
Yields the automaton recognizing the language that is the union of the languages recognized bythis
andother
.A
widening(int n)
Creates a new automaton that represent the widening operator applied on the automatonthis
.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface it.unive.lisa.util.datastructures.automaton.AutomataFactory
emptyLanguage, emptyString, from, singleString, unknownString
-
-
-
-
Field Detail
-
states
protected final java.util.SortedSet<State> states
The states of this automaton.
-
transitions
protected final java.util.SortedSet<Transition<T extends TransitionSymbol<T>>> transitions
The transitions of this automaton.
-
deterministic
protected java.util.Optional<java.lang.Boolean> deterministic
Flag that tracks if this automaton is deterministic. IfOptional.isPresent()
returnsfalse
, then it is unknown if this automaton is deterministic or not.
-
minimized
protected java.util.Optional<java.lang.Boolean> minimized
Flag that tracks if this automaton has been minimized. IfOptional.isPresent()
returnsfalse
, then it is unknown if this automaton is minimum or not.
-
-
Constructor Detail
-
Automaton
protected Automaton()
Builds an empty automaton.
-
Automaton
protected Automaton(java.util.SortedSet<State> states, java.util.SortedSet<Transition<T>> transitions)
Builds a new automaton with givenstates
andtransitions
.- Parameters:
states
- the set of states of the new automatontransitions
- the set of the transitions of the new automaton- Throws:
java.lang.IllegalArgumentException
- if the set of states contains multiple states with the same ids
-
-
Method Detail
-
getStates
public java.util.SortedSet<State> getStates()
Yields the set of states of this automaton.- Returns:
- the set of states
-
getTransitions
public java.util.SortedSet<Transition<T>> getTransitions()
Yields the set of transitions contained in this automaton.- Returns:
- the set of transitions
-
addState
public void addState(State s)
Adds a new state to this automaton.- Parameters:
s
- the state to add- Throws:
java.lang.IllegalArgumentException
- a state with the given id is already part of this automaton
-
addTransition
public void addTransition(State from, State to, T input)
Builds a new transition going fromfrom
toto
and recognizinginput
and adds it to the set of transitions of this automaton.- Parameters:
from
- the source nodeto
- the destination nodeinput
- the input to be recognized by the transition
-
addTransition
public void addTransition(Transition<T> t)
Adds the given transition to the set of transitions of this automaton.- Parameters:
t
- the transition to add
-
removeTransitions
public void removeTransitions(java.util.Set<Transition<T>> ts)
Removes every transition in the given set from the ones of this automaton.- Parameters:
ts
- the set of transitions to remove
-
removeStates
public void removeStates(java.util.Set<State> ts)
Removes every state in the given set from the ones of this automaton.- Parameters:
ts
- the set of states to remove
-
minimize
public A minimize()
Yields a minimal automaton equivalent to this one through Brzozowski's minimization algorithm.
This automaton is never modified.- Returns:
- a minimal automaton equivalent to this one
-
getOutgoingTransitionsFrom
public java.util.SortedSet<Transition<T>> getOutgoingTransitionsFrom(State s)
Yields the set of all outgoing transitions from the given state.- Parameters:
s
- the state- Returns:
- the set of outgoing transitions
-
getIngoingTransitionsFrom
public java.util.SortedSet<Transition<T>> getIngoingTransitionsFrom(State s)
Yields the set of all ingoing transitions to the given state.- Parameters:
s
- the state- Returns:
- the set of ingoing transitions
-
getFinalStates
public java.util.SortedSet<State> getFinalStates()
Yields the set of final states of this automaton.- Returns:
- the set of final states
-
getInitialState
public State getInitialState()
Yields one of the initial states of this automaton. If this automaton does not have an initial state, this method returnsnull
.- Returns:
- one of the initial states, or
null
-
getInitialStates
public java.util.SortedSet<State> getInitialStates()
Yields the set of initial states of this automaton.- Returns:
- the set of initial states
-
removeUnreachableStates
public A removeUnreachableStates()
Remove all the unreachable states from the current automaton.- Returns:
- a copy of this automaton without unreachable states
-
reverse
public A reverse()
Creates an automaton that accept the reverse language. The returned automaton might have more than one initial state.- Returns:
- a newly created automaton that accepts the reverse language of
this
.
-
epsilonClosure
public java.util.SortedSet<State> epsilonClosure(State s)
Computes the epsilon closure of this automaton starting froms
, namely the set of states that are reachable froms
just with epsilon transitions.- Parameters:
s
- the state from which the method starts to compute the epsilon closure- Returns:
- the set of states that are reachable from
s
just with epsilon transitions
-
epsilonClosure
public java.util.SortedSet<State> epsilonClosure(java.util.Set<State> set)
Computes the epsilon closure of this automaton starting fromset
, namely the set of states that are reachable from each state inset
just with epsilon transitions.- Parameters:
set
- the set of states from which the epsilon closure is computed.- Returns:
- the set of states that are reachable from
set
just with epsilon transitions
-
nextStatesNFA
public java.util.SortedSet<State> nextStatesNFA(java.util.Set<State> set, T sym)
Yields that states that can be reached from at least one state inset
by reading symbolsym
.- Parameters:
set
- the set of states to start fromsym
- the symbol to read- Returns:
- the set of states that can be reached
-
getReadableSymbolsFromStates
public java.util.SortedSet<T> getReadableSymbolsFromStates(java.util.Set<State> states)
Yields the set of symbols that can be read (accepted) by the given set of states.- Parameters:
states
- the set of states- Returns:
- the set of symbols that can be read
-
getReadableSymbolsFromState
public java.util.SortedSet<T> getReadableSymbolsFromState(State state)
Yields the set of symbols that can be read (accepted) by the given state, excluding epsilon.- Parameters:
state
- the state- Returns:
- the set of symbols that can be read
-
isDeterministic
public boolean isDeterministic()
Yields true if and only if this automaton is deterministic, that is, if the transition relation is a function. This is computed by detecting if none of the states of this automaton has two outgoing transitions recognizing the same symbol but going to different states.- Returns:
true
if and only if that condition holds
-
determinize
public A determinize()
Yields a deterministic automaton equivalent to this one. It this automaton is already deterministic, it is immediately returned instead.
This automaton is never modified by this method.- Returns:
- a deterministic automaton equivalent to this one.
-
union
public A union(A other)
Yields the automaton recognizing the language that is the union of the languages recognized bythis
andother
.- Parameters:
other
- the other automaton- Returns:
- Yields the automaton recognizing the language that is the union
of the languages recognized by
this
andother
-
hasCycle
public boolean hasCycle()
Checks if the Automatonthis
has any cycle.- Returns:
- a boolean value that tells if
this
has any cycle.
-
getNextStates
public java.util.SortedSet<State> getNextStates(State node)
Yields the set of possible successors of the given node.- Parameters:
node
- the node- Returns:
- the set of possible successors
-
getLanguage
public java.util.SortedSet<java.lang.String> getLanguage() throws CyclicAutomatonException
Returns the concretized language accepted bythis
.Transition
s' symbols representing unknown characters or strings will be encoded usingTransitionSymbol.UNKNOWN_SYMBOL
, while empty strings will be concretized (that is, they will not appear asTransitionSymbol.EPSILON
).- Returns:
- a set representing the language accepted by
this
. - Throws:
CyclicAutomatonException
- thrown if the automaton is cyclic.
-
totalize
public A totalize(java.util.Set<T> sigma)
Performs the totalization of this automaton w.r.t the given alphabet.- Parameters:
sigma
- the alphabet to use during totalization- Returns:
- the totalized automaton
-
complement
public A complement(java.util.Set<T> sigma)
Return a new Automaton that accept a language that is the complementary language ofthis
.- Parameters:
sigma
- the alphabet used for automaton completion.- Returns:
- the complement Automaton of
this
.
-
intersection
public A intersection(A other)
Returns the Automaton that accepts the language that is the intersection between the language ofthis
and another Automaton.- Parameters:
other
- the Automaton used for intersection with this.- Returns:
- a new Automaton accepting the language which is the intersection
between the language of
this
andother
's langauge.
-
getStateFromPair
protected State getStateFromPair(java.util.Map<State,org.apache.commons.lang3.tuple.Pair<State,State>> mapping, org.apache.commons.lang3.tuple.Pair<State,State> pair)
Given a pair of states, yields the state associated to the pair in the given state mapping.- Parameters:
mapping
- the mappingpair
- the pair of states- Returns:
- the state associated to the pair in the given state mapping
-
acceptsEmptyLanguage
public boolean acceptsEmptyLanguage()
Checks if the Automatonthis
accepts the empty language.- Returns:
- a boolean value that points out if
this
accepts the empty language
-
isContained
public boolean isContained(A other)
Yieldstrue
if and only ifthis
is contained intoother
, that is, if the language recognized by the intersection betweenthis
and the complement ofother
is empty.- Parameters:
other
- the other automaton- Returns:
true
if that condition holds
-
isEqualTo
public boolean isEqualTo(A other)
Checks if the automatonthis
accepts the same language asother
, implemented as:
language(A).equals(language(B))
if both automata are loop free,A.contains(B) && B.contains(A)
otherwise.- Parameters:
other
- the other automaton- Returns:
- a boolean value that points out if the automata are equivalent
-
copy
public A copy()
Deep-copies this automaton to a new one.- Returns:
- the copied automaton.
-
commonAlphabet
public java.util.SortedSet<T> commonAlphabet(A other)
Yields the common alphabet of the automatathis
andother
.- Parameters:
other
- the other automaton- Returns:
- a set of strings representing the common alphabet.
-
getAlphabet
public java.util.SortedSet<T> getAlphabet()
Yields the set of symbols that represents the alphabet of the symbols recognized by this automaton (that is, as subset of the alphabet over which the automata is defined).- Returns:
- the alphabet recognized by this automaton
-
getNextSymbols
public java.util.SortedSet<T> getNextSymbols(State s, int n)
Yields the set of symbols of length at mostn
that can be recognized starting from the given state. In this context, the length of a symbol is the number of sub-symbols it is composed of, regardless of the length of the concrete strings it represents.
For instance, if symbols are regular expressions, givenr1 = aab
andr2 = c*
,r = r1r2
has length 2.- Parameters:
s
- the state where to start the inspectionn
- the maximum length of the symbols- Returns:
- the computed set of symbols
-
widening
public A widening(int n)
Creates a new automaton that represent the widening operator applied on the automatonthis
.- Parameters:
n
- the parameter of the widening operator.- Returns:
- a newly created automaton representing the widening automaton.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
prettyPrint
public java.lang.String prettyPrint()
Yields a textual representation of the adjacency matrix of this automaton.- Returns:
- a string containing a textual representation of the automaton
-
concat
public A concat(A other)
Create a new automaton representing the concatenation ofthis
andother
.- Parameters:
other
- the other automaton.- Returns:
- a newly created automaton representing the concatenation of the given automata.
-
toSingleInitalState
public A toSingleInitalState()
If this automaton has a single initial state, this method returnsthis
. Otherwise, it yields a new one where a new unique initial state has been introduced, connected to the original initial states through epsilon-transitions.- Returns:
- an automaton with a single initial state
-
toRegex
public RegularExpression toRegex()
Creates and return the regex that represent the accepted language by the automatonthis
, using Brzozowski algebraic method (see here). Note that the returned regular expression is not simplified.- Returns:
- a String representing that is the regex that represent the accepted language.
-
getAllTransitionsConnecting
public java.util.SortedSet<Transition<T>> getAllTransitionsConnecting(State s1, State s2)
Yields the set of transitions going froms1
tos2
.- Parameters:
s1
- the source states2
- the destination state- Returns:
- the set of transitions connecting the two states
-
star
public A star()
Creates a new automaton composed of a loop over this one.- Returns:
- the star automaton
-
areMutuallyReachable
public boolean areMutuallyReachable(State s1, State s2)
Yieldstrue
if and only if the two given states are mutually reachable, that is, if there exist a path going froms1
tos2
and one going froms2
tos1
. Paths are searched using the Dijkstra algorithm.- Parameters:
s1
- the first states2
- the second state- Returns:
true
if and only ifs1
ands2
are mutually reachable
-
getAllPaths
public java.util.Set<java.util.List<State>> getAllPaths()
Yields all possible paths going from an initial state to a final state in the target automaton. Note that each node of an SCC of the automaton will appear at most once in each path.- Returns:
- the set of all possible paths
-
minimumPath
public java.util.List<State> minimumPath(State src, State target)
Finds the minimum path between the two given states using the Dijkstra algorithm.- Parameters:
src
- the source nodetarget
- the destination node- Returns:
- the minimum path
-
maximumPath
public java.util.List<State> maximumPath(State src, State target)
Finds the maximum path between the two given states using the Dijkstra algorithm.- Parameters:
src
- the source nodetarget
- the destination node- Returns:
- the maximum path
-
hasOnlyOnePath
public boolean hasOnlyOnePath() throws CyclicAutomatonException
Yieldstrue
if and only if this automaton has only one path. This means that, after minimization, if the automaton has only one path then the each state is the starting point of a transition at most once, and it is the destination node of a transition at most once.- Returns:
true
if that condition holds- Throws:
CyclicAutomatonException
- if the minimized automaton contain loops
-
extractLongestString
public A extractLongestString()
Yields the sub-automaton contained in this one that recognizes only the longest string in the language ofthis
. Note that this method assumes that the given automaton is loop-free and that it is a single-path automaton, that is, that all the strings of the language it recognizes are prefixes of the longest one.- Returns:
- the sub-automaton
-
makeAcyclic
public A makeAcyclic()
Yields an acyclic version of this automaton, where all SCCs have been removed.- Returns:
- the acyclic automaton
-
factors
public A factors()
Yields the automaton that recognizes all possible substrings of the strings recognized by this automaton.- Returns:
- the factors automaton
-
prefix
public A prefix()
Yields the automaton that recognizes all possible prefixes of the strings recognized by this automaton.- Returns:
- the prefix automaton
-
suffix
public A suffix()
Yields the automaton that recognizes all possible suffixes of the strings recognized by this automaton.- Returns:
- the suffix automaton
-
lenghtOfLongestString
public int lenghtOfLongestString()
Yields the length of the longest string recognized by this automaton, withInteger.MAX_VALUE
representing infinity.- Returns:
- the length of the longest string
-
recognizesExactlyOneString
public boolean recognizesExactlyOneString()
Yieldstrue
if this automaton recognizes exactly one string, that is, if it has no loops and all states have at most one outgoing transition.- Returns:
true
if that condition holds
-
factorsChangingInitialState
public A factorsChangingInitialState(State s)
Yields a copy of this automaton, but where all states are final and only the given state is initial.- Parameters:
s
- the state to make the initial state- Returns:
- the modified copy of this automaton
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
epsilon
public abstract T epsilon()
Yields the symbol representing the empty string.- Returns:
- the symbol for epsilon
-
concat
public abstract T concat(T first, T second)
Yields aTransitionSymbol
that is the concatenation of the two given ones.- Parameters:
first
- the first symbol to concatenatesecond
- the second symbol to concatenate- Returns:
- the result of the concatenation
-
symbolToRegex
public abstract RegularExpression symbolToRegex(T symbol)
Yields aRegularExpression
representing the given symbol.- Parameters:
symbol
- the symbol to represent- Returns:
- the equivalent regular expression
-
-