Class Behavior<L,T>

java.lang.Object
eu.bandm.tools.lexic.Behavior<L,T>
Type Parameters:
L - the label type of the automaton
T - the transition type of the automaton
All Implemented Interfaces:
FormatClient
Direct Known Subclasses:
ZAutomaton

public class Behavior<L,T> extends Object implements FormatClient
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

    Fields
    Modifier and Type
    Field
    Description
    protected final L
    The label of this behavior.
    protected final CodePointMap<T>
    The map of transitions of this behavior.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Behavior(L label, CodePointMap<T> transitions)
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    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
    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.
    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>
    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.
    <U> Behavior<L,U>
    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.
    Returns a behavior that differs from this behavior at most by simplification of the transition map.
    Returns a human-readable representation of this behavior.
    totalize(T otherwise)
    Returns a behavior that differs from this behavior by recognizing all previously unrecognized code points.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • label

      protected final L label
      The label of this behavior.
    • transitions

      protected final CodePointMap<T> transitions
      The map of transitions of this behavior.
  • Constructor Details

    • Behavior

      protected Behavior(L label, CodePointMap<T> transitions)
      Creates a new instance.
      Parameters:
      label - the label for the behavior
      transitions - the map of transitions for the behavior
  • Method Details

    • getLabel

      public L getLabel()
      Returns the label value associated with the current state.
      Returns:
      the label value associated with the current state
    • getTransition

      @Opt public T getTransition(int codePoint)
      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

      public static <L, T> Behavior<L,T> of(L label)
      Returns a behavior that recognizes no input code points.
      Type Parameters:
      L - the label type of the automaton
      T - 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 automaton
      T - the transition type of the automaton
      Parameters:
      one - one behavior to merge
      other - the other behavior to merge
      mergeLabels - the operation to merge the labels of the behaviors
      mergeTransitions - 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, 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.
      • 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 automaton
      T - the transition type of the automaton of the first behavior
      U - the transition type of the automaton of the second behavior
      V - the transition type of the automaton of the intersected behavior
      Parameters:
      one - one behavior to intersect
      other - the other behavior to intersect
      intersectLabels - the operation to intersect the labels of the behaviors
      intersectTransitions - 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

      public <U> Behavior<L,U> mapTransitions(Function<? super T,? extends U> onTransition)
      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 type
      U - the result transition type
      Parameters:
      onLabel - the function to apply to the label
      onTransition - 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

      public static <L, T> Behavior<L,T> consume(L label, int codePoint, T transition)
      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 automaton
      T - the transition type of the automaton
      Parameters:
      label - the label to associate with the underlying state
      codePoint - the code point to recognize
      transition - the transition to perform when the code point is recognized
      Returns:
      a behavior that recognizes only the given input code point
      See Also:
    • consumeAnyOf

      public static <L, T> Behavior<L,T> consumeAnyOf(L label, int[] codePoints, T transition)
      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 automaton
      T - the transition type of the automaton
      Parameters:
      label - the label to associate with the underlying state
      codePoints - the code points to recognize
      transition - 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

      public 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. No successful transition is performed for any of the specified code points.
      Type Parameters:
      L - the label type of the automaton
      T - the transition type of the automaton
      Parameters:
      label - the label to associate with the underlying state
      codePoints - the code points not to recognize
      transition - 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

      public static <L, T> Behavior<L,T> constant(L label, T transition)
      Returns a behavior that recognizes all input code points.
      Type Parameters:
      L - the label type of the automaton
      T - the transition type of the automaton
      Parameters:
      label - the label to associate with the underlying state
      transition - the transition to perform when any code point is recognized
      Returns:
      a behavior that recognizes all input code points, performing the given transition
    • totalize

      public Behavior<L,T> totalize(T otherwise)
      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

      public String toString()
      Returns a human-readable representation of this behavior.
      Overrides:
      toString in class Object
    • forEachTransition

      public void forEachTransition(Consumer<? super T> action)
      Performs an action for all transitions.
      Parameters:
      action - the action to perform
    • forEachKey

      public void forEachKey(IntConsumer action)
      Performs an action for all explicitly recognized code points.
      Parameters:
      action - the action to perform
    • format

      public <F> F format(FormatServer<F> server)
      Returns a pretty-printable representation of this behavior.
      Specified by:
      format in interface FormatClient
      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 objects
      onLabel - the operation to compute a representation of the label
      onTransition - the operation to compute a representation of each transition
      Returns:
      a pretty-printable representation of this behavior
    • anyTransition

      public boolean anyTransition(Predicate<? super T> pred)
      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 returns true; false otherwise
    • simplify

      public Behavior<L,T> 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: