Class RegularExpression
- java.lang.Object
-
- it.unive.lisa.util.datastructures.regex.RegularExpression
-
- All Implemented Interfaces:
TransitionSymbol<RegularExpression>
,java.lang.Comparable<RegularExpression>
public abstract class RegularExpression extends java.lang.Object implements TransitionSymbol<RegularExpression>
A regular expression that can be recognized by anAutomaton
, or that can be used to represent the language recognized by an automaton.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RegularExpression.PartialSubstring
A class that represents an intermediate result of the computation ofsubstring(int, int)
.
-
Field Summary
-
Fields inherited from interface it.unive.lisa.util.datastructures.automaton.TransitionSymbol
EPSILON, UNKNOWN_SYMBOL
-
-
Constructor Summary
Constructors Constructor Description RegularExpression()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Atom
asAtom()
Comp
asComp()
EmptySet
asEmptySet()
Or
asOr()
Star
asStar()
RegularExpression
comp(RegularExpression other)
Joins together two regular expression, that is, it builds aComp
recognizing this regular expression first, and thanother
.int
compareTo(RegularExpression o)
protected abstract int
compareToAux(RegularExpression other)
AuxiliarycompareTo(RegularExpression)
that can safely assume thatother
is an object of the same class asthis
.abstract boolean
contains(java.lang.String s)
Yieldstrue
if and only if this regular expression always contains the given string.abstract boolean
endsWith(java.lang.String s)
Yieldstrue
if and only if this regular expression always ends with the given string.abstract RegularExpression[]
explode()
Yields a new regular expression that is the exploded version of this one, that is, where all atoms have been broken down to the composition of the characters that compose their inner strings.abstract boolean
is(java.lang.String str)
Yieldstrue
if and only if this regular expression corresponds to the given string.boolean
isAtom()
Yieldstrue
if and only if this regular expression is an instance ofAtom
.boolean
isComp()
Yieldstrue
if and only if this regular expression is an instance ofComp
.abstract boolean
isEmpty()
Yieldstrue
if and only if this regular expression corresponds to the empty string or to no strings at all.boolean
isEmptySet()
Yieldstrue
if and only if this regular expression is an instance ofEmptySet
.boolean
isEpsilon()
Yieldstrue
if and only if this symbol represents the empty string epsilon.boolean
isOr()
Yieldstrue
if and only if this regular expression is an instance ofOr
.boolean
isStar()
Yieldstrue
if and only if this regular expression is an instance ofStar
.abstract boolean
mayContain(java.lang.String s)
Yieldstrue
if and only if this regular expression may contain the given string.abstract boolean
mayEndWith(java.lang.String s)
Yieldstrue
if and only if this regular expression may end with the given string.abstract boolean
mayStartWith(java.lang.String s)
Yieldstrue
if and only if this regular expression may start with the given string.RegularExpression
or(RegularExpression other)
Builds the disjunction (Or
) betweenthis
and the given regular expression.protected abstract boolean
readsWhiteSpaceString()
Checks whether this regular expression recognize a string made just of whitespaces.abstract RegularExpression
repeat(long n)
Yields a new regular expression corresponding to then
-repetition ofthis
.abstract RegularExpression
simplify()
Yields a simplified version of this regular expression.RegularExpression
star()
Builds the Kleene closure of this regular expression (Star
), recognizingthis
zero or more times.abstract boolean
startsWith(java.lang.String s)
Yieldstrue
if and only if this regular expression always starts with the given string.java.util.Set<SymbolicString>
substring(int start, int end)
Returns the set of all possible substrings of this regular expression, starting at the given index (inclusive) and ending at the given index (exclusive).protected abstract java.util.Set<RegularExpression.PartialSubstring>
substringAux(int charsToSkip, int missingChars)
Returns the set of all possible substrings of this regular expression, starting at the given index (inclusive) and ending at the given index (exclusive).abstract <A extends Automaton<A,T>,T extends TransitionSymbol<T>>
AtoAutomaton(AutomataFactory<A,T> factory)
Transforms this regular expression into its equivalent automaton.protected abstract RegularExpression
topAsEmptyString()
Yields a new regular expression where allTopAtom
are assumed to have length zero.protected abstract RegularExpression
topAsSingleChar()
Yields a new regular expression where allTopAtom
are assumed to have length one.abstract RegularExpression
trimLeft()
Yields a new regular expression where leading whitespaces have been removed fromthis
.abstract RegularExpression
trimRight()
Yields a new regular expression where trailing whitespaces have been removed fromthis
.protected abstract RegularExpression
unrollStarToFixedLength(int length)
Yields a new regular expression where allStar
have been unrolled to a sequence of their inner regular expression of lengthlength
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface it.unive.lisa.util.datastructures.automaton.TransitionSymbol
maxLength, minLength, reverse
-
-
-
-
Method Detail
-
compareTo
public final int compareTo(RegularExpression o)
- Specified by:
compareTo
in interfacejava.lang.Comparable<RegularExpression>
-
compareToAux
protected abstract int compareToAux(RegularExpression other)
AuxiliarycompareTo(RegularExpression)
that can safely assume thatother
is an object of the same class asthis
.- Parameters:
other
- the other regular expression- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
-
isEpsilon
public boolean isEpsilon()
Description copied from interface:TransitionSymbol
Yieldstrue
if and only if this symbol represents the empty string epsilon.- Specified by:
isEpsilon
in interfaceTransitionSymbol<RegularExpression>
- Returns:
true
if that condition holds.
-
simplify
public abstract RegularExpression simplify()
Yields a simplified version of this regular expression. Simplification happens through heuristics.- Returns:
- a simplified regular expression equivalent to this
-
toAutomaton
public abstract <A extends Automaton<A,T>,T extends TransitionSymbol<T>> A toAutomaton(AutomataFactory<A,T> factory)
Transforms this regular expression into its equivalent automaton.- Type Parameters:
A
- the concrete type ofAutomaton
that this method yieldsT
- the concrete type ofTransitionSymbol
s that instances ofA
have on their transitions- Parameters:
factory
- the factory that can be used to create the automaton- Returns:
- the automaton
-
asAtom
public final Atom asAtom()
Casts this regular expression to anAtom
if this regular expression is anAtom
. Returnsnull
otherwise.- Returns:
- this regular expression casted to
Atom
, ornull
-
isAtom
public final boolean isAtom()
Yieldstrue
if and only if this regular expression is an instance ofAtom
.- Returns:
true
if that condition holds
-
asComp
public final Comp asComp()
Casts this regular expression to aComp
if this regular expression is aComp
. Returnsnull
otherwise.- Returns:
- this regular expression casted to
Comp
, ornull
-
isComp
public final boolean isComp()
Yieldstrue
if and only if this regular expression is an instance ofComp
.- Returns:
true
if that condition holds
-
asEmptySet
public final EmptySet asEmptySet()
Casts this regular expression to anEmptySet
if this regular expression is anEmptySet
. Returnsnull
otherwise.- Returns:
- this regular expression casted to
EmptySet
, ornull
-
isEmptySet
public final boolean isEmptySet()
Yieldstrue
if and only if this regular expression is an instance ofEmptySet
.- Returns:
true
if that condition holds
-
isOr
public final boolean isOr()
Yieldstrue
if and only if this regular expression is an instance ofOr
.- Returns:
true
if that condition holds
-
asStar
public final Star asStar()
Casts this regular expression to aStar
if this regular expression is aStar
. Returnsnull
otherwise.- Returns:
- this regular expression casted to
Star
, ornull
-
isStar
public final boolean isStar()
Yieldstrue
if and only if this regular expression is an instance ofStar
.- Returns:
true
if that condition holds
-
substring
public final java.util.Set<SymbolicString> substring(int start, int end)
Returns the set of all possible substrings of this regular expression, starting at the given index (inclusive) and ending at the given index (exclusive).- Parameters:
start
- the start indexend
- the end index- Returns:
- the set of all substrings
-
substringAux
protected abstract java.util.Set<RegularExpression.PartialSubstring> substringAux(int charsToSkip, int missingChars)
Returns the set of all possible substrings of this regular expression, starting at the given index (inclusive) and ending at the given index (exclusive). Each substring is decorated with the number of characters that are still missing to reach the beginning of the substring, and the number of characters that still need to be added to the string to reach the desired length.- Parameters:
charsToSkip
- the number of characters to skip before starting to collect the substringmissingChars
- the number of missing characters to complete the substring- Returns:
- the set of partial substrings
-
repeat
public abstract RegularExpression repeat(long n)
Yields a new regular expression corresponding to then
-repetition ofthis
.- Parameters:
n
- number of repetitions- Returns:
- a new regular expression corresponding to the
n
-repetition ofthis
-
trimLeft
public abstract RegularExpression trimLeft()
Yields a new regular expression where leading whitespaces have been removed fromthis
.- Returns:
- a new regular expression where leading whitespaces have been
removed from
this
-
trimRight
public abstract RegularExpression trimRight()
Yields a new regular expression where trailing whitespaces have been removed fromthis
.- Returns:
- a new regular expression where trailing whitespaces have been
removed from
this
-
isEmpty
public abstract boolean isEmpty()
Yieldstrue
if and only if this regular expression corresponds to the empty string or to no strings at all.- Returns:
true
if that condition holds
-
is
public abstract boolean is(java.lang.String str)
Yieldstrue
if and only if this regular expression corresponds to the given string.- Parameters:
str
- the string- Returns:
true
if that condition holds
-
mayContain
public abstract boolean mayContain(java.lang.String s)
Yieldstrue
if and only if this regular expression may contain the given string.- Parameters:
s
- the string- Returns:
true
if that condition holds
-
contains
public abstract boolean contains(java.lang.String s)
Yieldstrue
if and only if this regular expression always contains the given string.- Parameters:
s
- the string- Returns:
true
if that condition holds
-
mayStartWith
public abstract boolean mayStartWith(java.lang.String s)
Yieldstrue
if and only if this regular expression may start with the given string.- Parameters:
s
- the string- Returns:
true
if that condition holds
-
startsWith
public abstract boolean startsWith(java.lang.String s)
Yieldstrue
if and only if this regular expression always starts with the given string.- Parameters:
s
- the string- Returns:
true
if that condition holds
-
mayEndWith
public abstract boolean mayEndWith(java.lang.String s)
Yieldstrue
if and only if this regular expression may end with the given string.- Parameters:
s
- the string- Returns:
true
if that condition holds
-
endsWith
public abstract boolean endsWith(java.lang.String s)
Yieldstrue
if and only if this regular expression always ends with the given string.- Parameters:
s
- the string- Returns:
true
if that condition holds
-
unrollStarToFixedLength
protected abstract RegularExpression unrollStarToFixedLength(int length)
Yields a new regular expression where allStar
have been unrolled to a sequence of their inner regular expression of lengthlength
.- Parameters:
length
- the length- Returns:
- the regular expression with unrolled stars
-
topAsSingleChar
protected abstract RegularExpression topAsSingleChar()
Yields a new regular expression where allTopAtom
are assumed to have length one.- Returns:
- the regular expression with shrinked top regular expressions
-
topAsEmptyString
protected abstract RegularExpression topAsEmptyString()
Yields a new regular expression where allTopAtom
are assumed to have length zero.- Returns:
- the regular expression with empty top regular expressions
-
explode
public abstract RegularExpression[] explode()
Yields a new regular expression that is the exploded version of this one, that is, where all atoms have been broken down to the composition of the characters that compose their inner strings.- Returns:
- the exploded regular expression
-
readsWhiteSpaceString
protected abstract boolean readsWhiteSpaceString()
Checks whether this regular expression recognize a string made just of whitespaces.- Returns:
true
if this regular expression recognize a string made just of whitespaces,false
otherwise.
-
star
public final RegularExpression star()
Builds the Kleene closure of this regular expression (Star
), recognizingthis
zero or more times.- Returns:
- the closure of this regular expression
-
comp
public final RegularExpression comp(RegularExpression other)
Joins together two regular expression, that is, it builds aComp
recognizing this regular expression first, and thanother
.- Parameters:
other
- the other regular expression- Returns:
- the joined regular expression
-
or
public final RegularExpression or(RegularExpression other)
Builds the disjunction (Or
) betweenthis
and the given regular expression.- Parameters:
other
- the other regular expression- Returns:
- the disjunction
-
-