ProgramNode (JEvolution)
Date: 2013-03-21

evSOLve.JEvolution.gp

## Class ProgramNode

- java.lang.Object
- - evSOLve.JEvolution.gp.ProgramNode

- All Implemented Interfaces:
    java.lang.Cloneable, javax.swing.tree.MutableTreeNode, javax.swing.tree.TreeNode
  Direct Known Subclasses:
    AddDouble, AndBoolean, ConstBoolean, ConstDouble, ConstInteger, DivDouble, IfInteger, LessEqualBoolean, MultDouble, NotBoolean, OrBoolean, SubDouble, VarDouble

      public abstract class ProgramNode
      extends java.lang.Object
      implements javax.swing.tree.MutableTreeNode, java.lang.Cloneable

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected ProgramNode[]` | `children` The children of this node.
    `protected ProgramNode` | `parent` The parent of this node.
    `static int` | `TYPE_BOOLEAN`
    `static int` | `TYPE_DOUBLE`
    `static int` | `TYPE_INT`
    `static int` | `TYPE_UNDEFINED` The node result/operand types.
    `protected static java.util.ArrayList` | `values` Container for the variable values passed to the program.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ProgramNode()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `addValue(java.lang.Object value)` Adds a value to the variable container.
    `java.util.Enumeration` | `children()` Returns an Enumeration of the children.
    `static void` | `clearValues()` Removes all values from the container.
    `ProgramNode` | `clone()` A deep clone of this node and all the nodes below, if the node is in a valid tree structure.
    `static ProgramNode` | `create(Element element)` Creates the ProgramNode designated by an XML element by calling the XML constructor.
    `boolean` | `equals(java.lang.Object otherNode)` Checks for equality of this node to another by using the nodes' string representation.
    `abstract java.lang.Object` | `eval()` Evaluates the node.
    `boolean` | `getAllowsChildren()` Returns if the node allows children.
    `javax.swing.tree.MutableTreeNode` | `getChildAt(int childIndex)` Returns the child at the given index.
    `int` | `getChildCount()` Returns the number of children of this node.
    `int` | `getIndex(javax.swing.tree.TreeNode node)` Returns the index of the given node in children.
    `int` | `getLevel()` Returns the node's level in a tree.
    `int` | `getOperandType(int index)` Returns the operand type of the program node.
    `ProgramNode` | `getParent()` Returns the parent of this node.
    `abstract int` | `getResultType()` Returns the result type of the program node.
    `abstract java.lang.String` | `getSignature()` Returns the string signature of the node.
    `int` | `hashCode()` Returns a hash code for the node by using the hash code for its string representation.
    `void` | `insert(javax.swing.tree.MutableTreeNode node, int index)` Sets the given node at index in children.
    `boolean` | `isLeaf()` Returns if the node is a leaf node.
    `void` | `mutate()` Mutates attributes of a program node, if necessary.
    `void` | `randomize()` Randomizes attributes of a program node, if necessary.
    `void` | `remove(int index)` Sets the node at the given child index to null.
    `void` | `remove(javax.swing.tree.MutableTreeNode node)` Removes the given child node.
    `void` | `removeFromParent()` Removes this node from parent.
    `void` | `setParent(javax.swing.tree.MutableTreeNode parent)` Sets the parent of this node.
    `void` | `setUserObject(java.lang.Object o)` Empty implementation.
    `abstract java.lang.String` | `toString()` Returns the string representation of the node.
    `Element` | `toXml(Element parent)` Saves the node to XML.

    - ### Methods inherited from class java.lang.Object

      `finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### TYPE_UNDEFINED

          public static final int TYPE_UNDEFINED

      The node result/operand types.
      See Also:
        Constant Field Values

    - #### TYPE_INT

          public static final int TYPE_INT

      See Also:
        Constant Field Values

    - #### TYPE_DOUBLE

          public static final int TYPE_DOUBLE

      See Also:
        Constant Field Values

    - #### TYPE_BOOLEAN

          public static final int TYPE_BOOLEAN

      See Also:
        Constant Field Values

    - #### values

          protected static java.util.ArrayList values

      Container for the variable values passed to the program.

    - #### parent

          protected ProgramNode parent

      The parent of this node.

    - #### children

          protected ProgramNode[] children

      The children of this node. For a terminal node this must be null. If children is not null, entries may be null in the final tree.

  - ### Constructor Detail

    - #### ProgramNode

          public ProgramNode()

  - ### Method Detail

    - #### clone

          public ProgramNode clone()

      A deep clone of this node and all the nodes below, if the node is in a valid tree structure.
      Overrides:
        `clone` in class `java.lang.Object`
      Returns:
        a deep clone

    - #### equals

          public boolean equals(java.lang.Object otherNode)

      Checks for equality of this node to another by using the nodes' string representation.
      Overrides:
        `equals` in class `java.lang.Object`
      Parameters:
        `otherNode` - another node
      Returns:
        true, if the nodes' toString() method returns equal strings

    - #### hashCode

          public int hashCode()

      Returns a hash code for the node by using the hash code for its string representation. Overwrite this method, if you prefer a different hash code method.
      Overrides:
        `hashCode` in class `java.lang.Object`
      Returns:
        a hash code

    - #### getParent

          public ProgramNode getParent()

      Returns the parent of this node.
      Specified by:
        `getParent` in interface `javax.swing.tree.TreeNode`
      Returns:
        parent the parent node

    - #### setParent

          public void setParent(javax.swing.tree.MutableTreeNode parent)

      Sets the parent of this node.
      Specified by:
        `setParent` in interface `javax.swing.tree.MutableTreeNode`
      Parameters:
        `parent` - the parent node

    - #### getChildAt

          public javax.swing.tree.MutableTreeNode getChildAt(int childIndex)

      Returns the child at the given index.
      Specified by:
        `getChildAt` in interface `javax.swing.tree.TreeNode`
      Parameters:
        `childIndex` - the index of the child
      Returns:
        the child node

    - #### getChildCount

          public int getChildCount()

      Returns the number of children of this node.
      Specified by:
        `getChildCount` in interface `javax.swing.tree.TreeNode`
      Returns:
        the number of children

    - #### getIndex

          public int getIndex(javax.swing.tree.TreeNode node)

      Returns the index of the given node in children.
      Specified by:
        `getIndex` in interface `javax.swing.tree.TreeNode`
      Returns:
        the index, -1, if not found

    - #### insert

          public void insert(javax.swing.tree.MutableTreeNode node,
                    int index)

      Sets the given node at index in children. The old entry is replaced.
      Specified by:
        `insert` in interface `javax.swing.tree.MutableTreeNode`
      Parameters:
        `node` - a ProgramNode
        `index` - the index

    - #### remove

          public void remove(int index)

      Sets the node at the given child index to null.
      Specified by:
        `remove` in interface `javax.swing.tree.MutableTreeNode`
      Parameters:
        `index` - the index

    - #### remove

          public void remove(javax.swing.tree.MutableTreeNode node)

      Removes the given child node.
      Specified by:
        `remove` in interface `javax.swing.tree.MutableTreeNode`
      Parameters:
        `node` - a child node

    - #### removeFromParent

          public void removeFromParent()

      Removes this node from parent.
      Specified by:
        `removeFromParent` in interface `javax.swing.tree.MutableTreeNode`

    - #### getAllowsChildren

          public boolean getAllowsChildren()

      Returns if the node allows children. For correct operations a terminal node must set 'children' to null. A function node must NOT set 'children' to null.
      Specified by:
        `getAllowsChildren` in interface `javax.swing.tree.TreeNode`
      Returns:
        true if function, otherwise terminal

    - #### isLeaf

          public boolean isLeaf()

      Returns if the node is a leaf node. In the program tree context, a leaf must always be a terminal, so we know all the time that a terminal is a leaf node and a function node is not.
      Specified by:
        `isLeaf` in interface `javax.swing.tree.TreeNode`
      Returns:
        true, if leaf

    - #### children

          public java.util.Enumeration children()

      Returns an Enumeration of the children.
      Specified by:
        `children` in interface `javax.swing.tree.TreeNode`
      Returns:
        the Enumeration

    - #### setUserObject

          public void setUserObject(java.lang.Object o)

      Empty implementation.
      Specified by:
        `setUserObject` in interface `javax.swing.tree.MutableTreeNode`
      Parameters:
        `o` - a user object

    - #### getLevel

          public int getLevel()

      Returns the node's level in a tree. Levels are positive. The root has a level of 0.
      Returns:
        the node's level

    - #### eval

          public abstract java.lang.Object eval()

      Evaluates the node.
      Returns:
        the evaluation result, which can be of any type

    - #### getSignature

          public abstract java.lang.String getSignature()

      Returns the string signature of the node. The signature is a string describing the node's basic function. Note that the signature should be unique to prevent confusion with nodes implemented in the future. However, for a single application it is only necessary that all program nodes used in that application have different signatures.
      Returns:
        the node's signature

    - #### toString

          public abstract java.lang.String toString()

      Returns the string representation of the node. It is included here explicitly to enforce implementation. It is basically the signature with potential parameters given in square brackets, e.g. "const[2.7]" could be a constant with value 2.7 with "const" being the signature.
      Overrides:
        `toString` in class `java.lang.Object`
      Returns:
        the node's string representation

    - #### getResultType

          public abstract int getResultType()

      Returns the result type of the program node.
      Returns:
        the result type

    - #### getOperandType

          public int getOperandType(int index)

      Returns the operand type of the program node. Terminal nodes have no operand, so their default implementation is given here. Function nodes have to override this method.
      Parameters:
        `index` - a child index
      Returns:
        the operand type

    - #### randomize

          public void randomize()

      Randomizes attributes of a program node, if necessary. The given default implementation does nothing.

    - #### mutate

          public void mutate()

      Mutates attributes of a program node, if necessary. The given default implementation does nothing.

    - #### addValue

          public static void addValue(java.lang.Object value)

      Adds a value to the variable container.
      Parameters:
        `value` - a variable value of arbitrary type

    - #### clearValues

          public static void clearValues()

      Removes all values from the container.

    - #### create

          public static ProgramNode create(Element element)

      Creates the ProgramNode designated by an XML element by calling the XML constructor.
      Parameters:
        `element` - an XML JDOM element
      Returns:
        a new ProgramNode (null, if problems)

    - #### toXml

          public Element toXml(Element parent)

      Saves the node to XML.
      Parameters:
        `parent` - the JDOM parent element
      Returns:
        the Node element