Class Program<T>

  • Type Parameters:
    T - the argument type of the operation
    All Implemented Interfaces:
    Op<T>, Serializable, Function<T[],​T>, Supplier<Op<T>>

    public class Program<T>
    extends Object
    implements Op<T>, Serializable
    This class composes a given operation tree to a new operation. which can serve as a sub program in an other operation tree.
    Since:
    3.9
    Version:
    4.1
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Program​(String name, Tree<? extends Op<T>,​?> tree)
      Create a new program with the given name and the given operation tree.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T apply​(T[] args)  
      int arity()
      Return the arity of the operation function.
      static void check​(Tree<? extends Op<?>,​?> program)
      Validates the given program tree.
      boolean equals​(Object obj)  
      static <T> T eval​(Tree<? extends Op<T>,​?> tree, T... variables)
      Evaluates the given operation tree with the given variables.
      T eval​(T... args)
      Convenient method, which lets you apply the program function without explicitly create a wrapper array.
      int hashCode()  
      String name()
      Return the name of the operation.
      static <A> TreeNode<Op<A>> of​(int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals)
      Create a new, random program tree from the given (non) terminal operations with the desired depth.
      static <A> TreeNode<Op<A>> of​(int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals, Random random)
      Create a new, random program tree from the given (non) terminal operations with the desired depth.
      static <A> Program<A> of​(String name, int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals)
      Create a new, random program from the given (non) terminal operations with the desired depth.
      static <A> Program<A> of​(String name, int depth, ISeq<? extends Op<A>> operations, ISeq<? extends Op<A>> terminals, Random random)
      Create a new, random program from the given (non) terminal operations with the desired depth.
      String toString()  
      static <A> TreeNode<Op<A>> toTree​(ISeq<? extends FlatTree<? extends Op<A>,​?>> nodes, ISeq<? extends Op<A>> terminals)
      Creates a valid program tree from the given flattened sequence of op nodes.
      Tree<Op<T>,​?> tree()
      Return the underlying expression tree.
    • Constructor Detail

      • Program

        public Program​(String name,
                       Tree<? extends Op<T>,​?> tree)
        Create a new program with the given name and the given operation tree. The arity of the program is calculated from the given operation tree and set to the maximal arity of the operations of the tree.
        Parameters:
        name - the program name
        tree - the operation tree
        Throws:
        NullPointerException - if one of the given arguments is null
        IllegalArgumentException - if the given operation tree is invalid, which means there is at least one node where the operation arity and the node child count differ.
    • Method Detail

      • name

        public String name()
        Description copied from interface: Op
        Return the name of the operation.
        Specified by:
        name in interface Op<T>
        Returns:
        the name of the operation
      • arity

        public int arity()
        Description copied from interface: Op
        Return the arity of the operation function. If the arity is zero, the operation is terminal operation.
        Specified by:
        arity in interface Op<T>
        Returns:
        the arity of the operation
      • tree

        public Tree<Op<T>,​?> tree()
        Return the underlying expression tree.
        Returns:
        the underlying expression tree
        Since:
        4.1
      • eval

        @SafeVarargs
        public final T eval​(T... args)
        Convenient method, which lets you apply the program function without explicitly create a wrapper array.
        Parameters:
        args - the function arguments
        Returns:
        the evaluated value
        Throws:
        NullPointerException - if the given variable array is null
        IllegalArgumentException - if the length of the arguments array is smaller than the program arity
        See Also:
        apply(Object[])
      • eval

        @SafeVarargs
        public static <T> T eval​(Tree<? extends Op<T>,​?> tree,
                                 T... variables)
        Evaluates the given operation tree with the given variables.
        Type Parameters:
        T - the argument type
        Parameters:
        tree - the operation tree
        variables - the input variables
        Returns:
        the result of the operation tree evaluation
        Throws:
        NullPointerException - if one of the arguments is null
        IllegalArgumentException - if the length of the variable array is smaller than the program arity
      • check

        public static void check​(Tree<? extends Op<?>,​?> program)
        Validates the given program tree.
        Parameters:
        program - the program to validate
        Throws:
        NullPointerException - if the given program is null
        IllegalArgumentException - if the given operation tree is invalid, which means there is at least one node where the operation arity and the node child count differ.
      • of

        public static <A> Program<A> of​(String name,
                                        int depth,
                                        ISeq<? extends Op<A>> operations,
                                        ISeq<? extends Op<A>> terminals)
        Create a new, random program from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
        Type Parameters:
        A - the operational type
        Parameters:
        name - the program name
        depth - the desired depth of the program tree
        operations - the list of non-terminal operations
        terminals - the list of terminal operations
        Returns:
        a new program
        Throws:
        NullPointerException - if one of the given operations is null
        IllegalArgumentException - if the given tree depth is smaller than zero
        Since:
        4.1
      • of

        public static <A> Program<A> of​(String name,
                                        int depth,
                                        ISeq<? extends Op<A>> operations,
                                        ISeq<? extends Op<A>> terminals,
                                        Random random)
        Create a new, random program from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
        Type Parameters:
        A - the operational type
        Parameters:
        name - the program name
        depth - the desired depth of the program tree
        operations - the list of non-terminal operations
        terminals - the list of terminal operations
        random - the random engine used for creating the program
        Returns:
        a new program
        Throws:
        NullPointerException - if one of the given operations is null
        IllegalArgumentException - if the given tree depth is smaller than zero
        Since:
        4.1
      • of

        public static <A> TreeNode<Op<A>> of​(int depth,
                                             ISeq<? extends Op<A>> operations,
                                             ISeq<? extends Op<A>> terminals)
        Create a new, random program tree from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
        Type Parameters:
        A - the operational type
        Parameters:
        depth - the desired depth of the program tree
        operations - the list of non-terminal operations
        terminals - the list of terminal operations
        Returns:
        a new program tree
        Throws:
        NullPointerException - if one of the given operations is null
        IllegalArgumentException - if the given tree depth is smaller than zero
      • of

        public static <A> TreeNode<Op<A>> of​(int depth,
                                             ISeq<? extends Op<A>> operations,
                                             ISeq<? extends Op<A>> terminals,
                                             Random random)
        Create a new, random program tree from the given (non) terminal operations with the desired depth. The created program tree is a full tree.
        Type Parameters:
        A - the operational type
        Parameters:
        depth - the desired depth of the program tree
        operations - the list of non-terminal operations
        terminals - the list of terminal operations
        random - the random engine used for creating the program
        Returns:
        a new program tree
        Throws:
        NullPointerException - if one of the given operations is null
        IllegalArgumentException - if the given tree depth is smaller than zero
        Since:
        4.1
      • toTree

        public static <A> TreeNode<Op<A>> toTree​(ISeq<? extends FlatTree<? extends Op<A>,​?>> nodes,
                                                 ISeq<? extends Op<A>> terminals)
        Creates a valid program tree from the given flattened sequence of op nodes. The given operations and termination nodes are used for repairing the program tree, if necessary.
        Type Parameters:
        A - the operation argument type
        Parameters:
        nodes - the flattened, possible corrupt, program tree
        terminals - the usable non-terminal operation nodes to use for reparation
        Returns:
        a new valid program tree build from the flattened program tree
        Throws:
        NullPointerException - if one of the arguments is null
        IllegalArgumentException - if the nodes sequence is empty