Package eu.bandm.tools.lexic
Class Behavior<L,T>
java.lang.Object
eu.bandm.tools.lexic.Behavior<L,T>
- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton
- All Implemented Interfaces:
FormatClient
- Direct Known Subclasses:
ZAutomaton
Behavior of an automaton in a particular state.
The purpose of a behavior object is implemented by the methods
getLabel()
and getTransition(int)
:
getLabel()
returns the label value associated with the underlying state, without regard to any input.getTransition(int)
specifies the reaction to one input code point:- If the input code point is recognized in the underlying state, then the transition to perform is returned.
- If the input code point is not recognized in the underlying
state, then
null
is returned.
All other methods newly declared by this class are factories
that construct new behaviors. These are to be used by application
instead of the protected
constructor.
-
Field Summary
Modifier and TypeFieldDescriptionprotected final L
The label of this behavior.protected final CodePointMap<T>
The map of transitions of this behavior. -
Constructor Summary
ModifierConstructorDescriptionprotected
Behavior
(L label, CodePointMap<T> transitions) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionboolean
anyTransition
(Predicate<? super T> pred) Checks whether the given predicate holds for any transition.static <L,
T> Behavior<L, T> constant
(L label, T transition) Returns a behavior that recognizes all input code points.static <L,
T> Behavior<L, T> consume
(L label, int codePoint, T transition) Returns a behavior that recognizes only a particular input code point.static <L,
T> Behavior<L, T> consumeAnyOf
(L label, int[] codePoints, T transition) Returns a behavior that recognizes only some particular input code points.static <L,
T> Behavior<L, T> consumeExcept
(L label, int[] codePoints, T transition) Returns a behavior that recognizes all except some particular input code points.void
forEachKey
(IntConsumer action) Performs an action for all explicitly recognized code points.void
forEachTransition
(Consumer<? super T> action) Performs an action for all transitions.<F> F
format
(FormatServer<F> server) Returns a pretty-printable representation of this behavior.(package private) <F> F
format
(FormatServer<F> server, Function<? super L, ? extends F> onLabel, Function<? super T, ? extends F> onTransition) Returns a pretty-printable representation of this behavior.getLabel()
Returns the label value associated with the current state.getTransition
(int codePoint) Returns the transition performed by the automaton when consuming a given input code point.static <L,
T, U, V> Behavior<L, V> intersect
(Behavior<L, T> one, Behavior<L, U> other, BinaryOperator<L> intersectLabels, BiFunction<T, U, V> intersectTransitions) Returns a behavior that intersects both transitions and labels of two behaviors.<M,
U> Behavior<M, U> Returns a behavior that differs from this behavior by having applied the given functions to the label and all transitions, respectively.mapTransitions
(Function<? super T, ? extends U> onTransition) Returns a behavior that differs from this behavior by having applied the given function to all transitions.static <L,
T> Behavior<L, T> merge
(Behavior<L, T> one, Behavior<L, T> other, BinaryOperator<L> mergeLabels, BinaryOperator<T> mergeTransitions) Returns a behavior that merges both labels and transitions of two behaviors.static <L,
T> Behavior<L, T> of
(L label) Returns a behavior that recognizes no input code points.simplify()
Returns a behavior that differs from this behavior at most by simplification of the transition map.toString()
Returns a human-readable representation of this behavior.Returns a behavior that differs from this behavior by recognizing all previously unrecognized code points.
-
Field Details
-
label
The label of this behavior. -
transitions
The map of transitions of this behavior.
-
-
Constructor Details
-
Behavior
Creates a new instance.- Parameters:
label
- the label for the behaviortransitions
- the map of transitions for the behavior
-
-
Method Details
-
getLabel
Returns the label value associated with the current state.- Returns:
- the label value associated with the current state
-
getTransition
Returns the transition performed by the automaton when consuming a given input code point.- Parameters:
codePoint
- the input code point to consume- Returns:
- the performed transition if the input is recognized; or
null
otherwise - See Also:
-
of
Returns a behavior that recognizes no input code points.- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton- Parameters:
label
- the label to associate with the underlying state- Returns:
- a behavior that recognizes no input code points
-
merge
public static <L,T> Behavior<L,T> merge(Behavior<L, T> one, Behavior<L, T> other, BinaryOperator<L> mergeLabels, BinaryOperator<T> mergeTransitions) Returns a behavior that merges both labels and transitions of two behaviors.- Transitions that occur only in one of the two behaviors, i.e., where the other does not recognize the same input code point, are copied directly.
- Coinciding transitions, i.e., where both behaviors recognize the same input code point, are merged according to a given operation.
- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton- Parameters:
one
- one behavior to mergeother
- the other behavior to mergemergeLabels
- the operation to merge the labels of the behaviorsmergeTransitions
- the operation to merge coinciding transitions of the two behaviors- Returns:
- a behavior that merges both labels and transitions of the given behaviors
- See Also:
-
intersect
public static <L,T, Behavior<L,U, V> V> intersect(Behavior<L, T> one, Behavior<L, U> other, BinaryOperator<L> intersectLabels, BiFunction<T, U, V> intersectTransitions) Returns a behavior that intersects both transitions and labels of two behaviors.- Transitions that occur only in one of the two behaviors, i.e., where the other does not recognize the same input code point, are dropped.
- Coinciding transitions, i.e., where both behaviors recognize the same input code point, are intersected according to a given operation.
- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton of the first behaviorU
- the transition type of the automaton of the second behaviorV
- the transition type of the automaton of the intersected behavior- Parameters:
one
- one behavior to intersectother
- the other behavior to intersectintersectLabels
- the operation to intersect the labels of the behaviorsintersectTransitions
- the operation to intersect coinciding transitions of the two behaviors- Returns:
- a behavior that intersect both labels and transitions of the given behaviors
- See Also:
-
mapTransitions
Returns a behavior that differs from this behavior by having applied the given function to all transitions.- Type Parameters:
U
- the result transition type- Parameters:
onTransition
- the function to apply to each transition- Returns:
- a behavior that differs from this behavior by having applied the given function to all transitions
- See Also:
-
map
public <M,U> Behavior<M,U> map(Function<? super L, ? extends M> onLabel, Function<? super T, ? extends U> onTransition) Returns a behavior that differs from this behavior by having applied the given functions to the label and all transitions, respectively.- Type Parameters:
M
- the result label typeU
- the result transition type- Parameters:
onLabel
- the function to apply to the labelonTransition
- the function to apply to each transition- Returns:
- a behavior that differs from this behavior by having applied the given functions to the label and all transitions, respectively
- See Also:
-
consume
Returns a behavior that recognizes only a particular input code point. No successful transition is performed for any other code point.- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton- Parameters:
label
- the label to associate with the underlying statecodePoint
- the code point to recognizetransition
- the transition to perform when the code point is recognized- Returns:
- a behavior that recognizes only the given input code point
- See Also:
-
consumeAnyOf
Returns a behavior that recognizes only some particular input code points. No successful transition is performed for any other code point.- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton- Parameters:
label
- the label to associate with the underlying statecodePoints
- the code points to recognizetransition
- the transition to perform when any of the code points is recognized- Returns:
- a behavior that recognizes only the given input code points
- See Also:
-
consumeExcept
Returns a behavior that recognizes all except some particular input code points. No successful transition is performed for any of the specified code points.- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton- Parameters:
label
- the label to associate with the underlying statecodePoints
- the code points not to recognizetransition
- the transition to perform when any other code point is recognized- Returns:
- a behavior that recognizes all except the given input code points, performing the given transition
- See Also:
-
constant
Returns a behavior that recognizes all input code points.- Type Parameters:
L
- the label type of the automatonT
- the transition type of the automaton- Parameters:
label
- the label to associate with the underlying statetransition
- the transition to perform when any code point is recognized- Returns:
- a behavior that recognizes all input code points, performing the given transition
-
totalize
Returns a behavior that differs from this behavior by recognizing all previously unrecognized code points.- Parameters:
otherwise
- the transition to perform for all input code points unrecognized by this behavior- Returns:
- a behavior that differs from this behavior by recognizing all previously unrecognized code points
- See Also:
-
toString
Returns a human-readable representation of this behavior. -
forEachTransition
Performs an action for all transitions.- Parameters:
action
- the action to perform
-
forEachKey
Performs an action for all explicitly recognized code points.- Parameters:
action
- the action to perform
-
format
Returns a pretty-printable representation of this behavior.- Specified by:
format
in interfaceFormatClient
- Type Parameters:
F
- the type of format objects to produce- Parameters:
server
- a factory object that can produce format objects- Returns:
- a format object produced by the server
-
format
<F> F format(FormatServer<F> server, Function<? super L, ? extends F> onLabel, Function<? super T, ? extends F> onTransition) Returns a pretty-printable representation of this behavior.- Type Parameters:
F
- the type of format objects to produce- Parameters:
server
- a factory object that can produce format objectsonLabel
- the operation to compute a representation of the labelonTransition
- the operation to compute a representation of each transition- Returns:
- a pretty-printable representation of this behavior
-
anyTransition
Checks whether the given predicate holds for any transition.- Parameters:
pred
- the predicate to apply- Returns:
true
if there is a recognized input code point that performs a transition for which the given predicate returnstrue
;false
otherwise
-
simplify
Returns a behavior that differs from this behavior at most by simplification of the transition map.- Returns:
- a behavior that differs from this behavior at most by simplification of the transition map
- See Also:
-