Class Variable<A>
- Type Parameters:
A- the target type of objects to match against
- All Implemented Interfaces:
Extractor<A>,Pattern<A>,Serializable,Cloneable
A pattern variable is deterministic, matches any object and binds itself.
- See Also:
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks whether a variable is bound by this pattern.voidclear(boolean recursively) Disposes of all temporary internal state related to results and nondeterminism.fork()Makes a copy of this pattern with independent internal state.getName()Returns the name of this variable.getValue()Returns the current value bound to this variable.booleanChecks whether this pattern is deterministic.Returns a motif that has the occurrences of this variable in the given body pattern as the hole.booleanAttempts to match this pattern against a given object.Returns a pattern that is equivalent to one or more iterations of the given root pattern nested via this variable.booleanChecks whether a variable is preserved by this pattern unconditionally.booleanChecks whether a variable is preserved by this pattern, conditionally on success or failure.Returns a pattern that is equivalent to zero or more iterations of the given root pattern nested via this variable.toString()Methods inherited from class eu.bandm.tools.paisley.AbstractPattern
clone, docloneMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface eu.bandm.tools.paisley.Extractor
bind, eagerBindings, lazyBindings, stream
-
Constructor Details
-
Variable
public Variable()Creates a new anonymous variable. -
Variable
Creates a new named variable. Variable names are for user-level annotation purposes only, they have no effect on matching behaviour.- Parameters:
name- the variable name.
-
-
Method Details
-
getName
Returns the name of this variable.- Returns:
- the name of this variable, or
nullif this variable is anonymous.
-
getValue
Returns the current value bound to this variable. The value is initiallynull. It is set to a meaningful value by a successful match with a pattern that guarantees to bind the variable. It may be set to an unspecified value by a match with a pattern that does not guarantee to preserve the variable. -
clear
public void clear(boolean recursively) Disposes of all temporary internal state related to results and nondeterminism.The default implementation just invokes
cut(false). Subclasses that override this method should either invoke this overridden implementation, or ensure by other means that the effect ofcut(false)is implied. This implementation resets the variable value tonull. -
match
Attempts to match this pattern against a given object.If the class of this pattern imposes a specific order on matches, the first one is chosen.
The
nulltarget is generally allowed. This implementation sets the variable value totarget. -
binds
Checks whether a variable is bound by this pattern.The default implementation returns
false. -
preserves
Checks whether a variable is preserved by this pattern, conditionally on success or failure.The default implementation returns
false. -
preserves
Checks whether a variable is preserved by this pattern unconditionally.The default implementation returns
false. -
isDeterministic
public boolean isDeterministic()Checks whether this pattern is deterministic.The default implementation returns
false.- Specified by:
isDeterministicin interfacePattern<A>- Returns:
true- See Also:
-
toString
-
fork
Makes a copy of this pattern with independent internal state.Subclasses must override this method if necessary to honour the following rules for all transitively reachable patterns:
- Subpatterns that are instances of
Variablemust be shared. This implies thatfork()must returnthis. - Fields that do not refer to subpatterns, but to matching
state (changed by invocations of
Pattern.match(A),Pattern.matchAgain(),Pattern.cut()orPattern.clear()) must be duplicated. If matching state changes are effected by modification of other objects, these must be duplicated. In summary, matching operations on this pattern and its copy must not interfere, with the exception of variable bindings. - Fields neither referring to subpatterns nor to matching state may be either shared or duplicated. The usage of such fields in a modifiable way (such that the distinction is relevant) is strongly deprecated.
- Patterns
whose fields need not be duplicated need not be duplicated themselves, but
may return
this.
Subpatterns that are instances of
Variablemust be shared. This implies thatfork()must returnthis. - Subpatterns that are instances of
-
lambda
Returns a motif that has the occurrences of this variable in the given body pattern as the hole.- Type Parameters:
B- the target type of the body pattern- Parameters:
body- the body pattern- Returns:
- a motif that has the occurrences of
thisinbodybody pattern as the hole - Throws:
NullPointerException- ifbodyis null.
-
plus
Returns a pattern that is equivalent to one or more iterations of the given root pattern nested via this variable.Conceptually,
x.plus(p)is equivalent to the pattern arising fromp.fork()by substitutingx.star(p)forx. Note that this cannot be used naively for implementation, since the ensuing recursive substitution would not terminate. The actual implementation guarantees to produce only as many new subpatterns as dynamically necessary.- Parameters:
root- the root pattern, which should bind this variable- Returns:
- a pattern that is equivalent to the infinite disjunction of
root,bind(root, root),bind(bind(root, root), root), etc. - See Also:
-
star
Returns a pattern that is equivalent to zero or more iterations of the given root pattern nested via this variable.Conceptually,
x.star(p)is equivalent toeither(x, x.plus(p)).- Parameters:
root- the root pattern, which should bind this variable- Returns:
- a pattern that is equivalent to the infinite disjunction of
this,root,bind(root, root),bind(bind(root, root), root), etc. - See Also:
-