Package org.moeaframework.util.tree
Class Node
java.lang.Object
org.moeaframework.util.tree.Node
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
Abs
,Acos
,Acosh
,Add
,And
,Asin
,Asinh
,Atan
,Atanh
,Call
,Ceil
,Constant
,Cos
,Cosh
,Define
,Divide
,Equals
,Exp
,Floor
,For
,Get
,GreaterThan
,GreaterThanOrEqual
,IfElse
,Lambda
,LessThan
,LessThanOrEqual
,Log
,Log10
,Max
,Min
,Modulus
,Multiply
,NOP
,Not
,Or
,Power
,Program
,Round
,Sequence
,Set
,Sign
,Sin
,Sinh
,Square
,SquareRoot
,Subtract
,Tan
,Tanh
,Truncate
,While
A node in an expression tree. Expression trees are strongly typed, meaning nodes have defined return types and
argument types. The return type of all nodes must match the argument type from its parent node.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract Node
copyNode()
Returns a copy of this node, but without any children or parents assigned.copyTree()
Returns a copy of this node including copies of all its arguments (child nodes).abstract Object
evaluate
(Environment environment) Evaluates this node in the context of the specified environment.getArgument
(int index) Returns the argument (child node) at the specified index.Class<?>
getArgumentType
(int index) Returns the type of the argument (child node) at the specified index.int
getDepth()
Returns the depth of this node, which is the number of branches between this node and the root.getFunctionAt
(int index) Returns the function (non-terminal) node at the specified index in the subtree rooted at this node.getFunctionAt
(Class<?> type, int index) Returns the function (non-terminal) node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type.int
Returns the number of branches between this node and the furthest leaf.int
Returns the number of branches between this node and the nearest leaf.getNodeAt
(int index) Returns the node at the specified index in the subtree rooted at this node.protected Node
Returns the node at the specified index in the subtree rooted at this node.Returns the node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type.int
Returns the number of arguments (child nodes) of this node.int
Returns the number of function (non-terminal) nodes contained in the subtree rooted at this node.int
getNumberOfFunctions
(Class<?> type) Returns the number of function (non-terminal) nodes contained in the subtree rooted at this node that match the given return type.int
Returns the number of nodes contained in the subtree rooted at this node.int
getNumberOfNodes
(Class<?> type) Returns the number of nodes contained in the subtree rooted at this node that match the given return type.protected int
getNumberOfNodes
(Class<?> type, boolean includeFunctions, boolean includeTerminals) Returns the number of nodes contained in the subtree rooted at this node.int
Returns the number of terminal nodes contained in the subtree rooted at this node.int
getNumberOfTerminals
(Class<?> type) Returns the number of terminal nodes contained in the subtree rooted at this node that match the given return type.Returns the parent of this node; ornull
if this node has no parent.Class<?>
Returns the return type of this node.getTerminalAt
(int index) Returns the terminal node at the specified index in the subtree rooted at this node.getTerminalAt
(Class<?> type, int index) Returns the terminal node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type.boolean
isFixed()
Returnstrue
if this node should not be modified, altered, or replaced;false
otherwise.boolean
Returnstrue
if this is a terminal node (has no arguments);false
otherwise.boolean
isValid()
Returnstrue
if this node and its arguments are valid;false
otherwise.setArgument
(int index, Node expression) Sets the argument (child node) at the specified index.void
setFixed
(boolean fixed) Set totrue
if this node should not be modified, altered, or replaced;false
otherwise.void
setFixedTree
(boolean fixed) CallssetFixed(boolean)
on all nodes in the subtree rooted at this node.int
size()
Returns the number of nodes contained in the tree rooted at this node.toString()
-
Constructor Details
-
Node
public Node()Constructs a new node. -
Node
Constructs a new node with the given return type and argument types.- Parameters:
returnType
- the return type of the nodeargumentTypes
- the type of the arguments, if any, for the node
-
-
Method Details
-
setFixedTree
public void setFixedTree(boolean fixed) CallssetFixed(boolean)
on all nodes in the subtree rooted at this node.- Parameters:
fixed
-true
if all nodes in the subtree rooted at this node should not be modified, altered, or replaced;false
otherwise
-
setFixed
public void setFixed(boolean fixed) Set totrue
if this node should not be modified, altered, or replaced;false
otherwise.- Parameters:
fixed
-true
if this node should not be modified, altered, or replaced;false
otherwise
-
isFixed
public boolean isFixed()Returnstrue
if this node should not be modified, altered, or replaced;false
otherwise.- Returns:
true
if this node should not be modified, altered, or replaced;false
otherwise
-
getNumberOfArguments
public int getNumberOfArguments()Returns the number of arguments (child nodes) of this node.- Returns:
- the number of arguments (child nodes) of this node
-
getArgument
Returns the argument (child node) at the specified index.- Parameters:
index
- the index of the argument to return- Returns:
- the argument (child node) at the specified index
-
getParent
Returns the parent of this node; ornull
if this node has no parent.- Returns:
- the parent of this node; or
null
if this node has no parent
-
getReturnType
Returns the return type of this node.- Returns:
- the return type of this node
-
getArgumentType
Returns the type of the argument (child node) at the specified index.- Parameters:
index
- the index of the argument- Returns:
- the type of the argument (child node) at the specified index
-
setArgument
Sets the argument (child node) at the specified index.- Parameters:
index
- the index of the new argumentexpression
- the expression defining the argument- Returns:
- a reference to this node, allowing multiple calls to be chained together
-
size
public int size()Returns the number of nodes contained in the tree rooted at this node.- Returns:
- the number of nodes contained in the tree rooted at this node
-
getDepth
public int getDepth()Returns the depth of this node, which is the number of branches between this node and the root.- Returns:
- the depth of this node
-
getMinimumHeight
public int getMinimumHeight()Returns the number of branches between this node and the nearest leaf.- Returns:
- the number of branches between this node and the nearest leaf
-
getMaximumHeight
public int getMaximumHeight()Returns the number of branches between this node and the furthest leaf.- Returns:
- the number of branches between this node and the furthest leaf
-
copyNode
Returns a copy of this node, but without any children or parents assigned.- Returns:
- a copy of this node, but without any children or parents assigned
-
copyTree
Returns a copy of this node including copies of all its arguments (child nodes). All attributes of the node are also retained, such asisFixed()
.- Returns:
- a copy of this node including copies of all its arguments (child nodes)
-
evaluate
Evaluates this node in the context of the specified environment.- Parameters:
environment
- the execution environment- Returns:
- the result of evaluating this node
-
isValid
public boolean isValid()Returnstrue
if this node and its arguments are valid;false
otherwise. A valid node has all arguments defined, all arguments are valid, and all arguments are the appropriate type.- Returns:
true
if this node and its arguments are valid;false
otherwise
-
toString
-
isTerminal
public boolean isTerminal()Returnstrue
if this is a terminal node (has no arguments);false
otherwise.- Returns:
true
if this is a terminal node (has no arguments);false
otherwise
-
getNumberOfFunctions
Returns the number of function (non-terminal) nodes contained in the subtree rooted at this node that match the given return type.- Parameters:
type
- the return type of the node- Returns:
- the number of function (non-terminal) nodes contained in the subtree rooted at this node that match the given return type
-
getFunctionAt
Returns the function (non-terminal) node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type. A depth-first search (DFS) walk of the tree is used to find the appropriate node.- Parameters:
type
- the return type of the nodeindex
- the index of the node to return- Returns:
- the function (non-terminal) node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type
- Throws:
IndexOutOfBoundsException
- if the index refers to a node not within the subtree rooted at this node
-
getNumberOfFunctions
public int getNumberOfFunctions()Returns the number of function (non-terminal) nodes contained in the subtree rooted at this node.- Returns:
- the number of function (non-terminal) nodes contained in the subtree rooted at this node
-
getFunctionAt
Returns the function (non-terminal) node at the specified index in the subtree rooted at this node. A depth-first search (DFS) walk of the tree is used to find the appropriate node.- Parameters:
index
- the index of the node to return- Returns:
- the function (non-terminal) node at the specified index in the subtree rooted at this node
- Throws:
IndexOutOfBoundsException
- if the index refers to a node not within the subtree rooted at this node
-
getNumberOfTerminals
Returns the number of terminal nodes contained in the subtree rooted at this node that match the given return type.- Parameters:
type
- the return type of the node- Returns:
- the number of terminal nodes contained in the subtree rooted at this node that match the given return type
-
getTerminalAt
Returns the terminal node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type. A depth-first search (DFS) walk of the tree is used to find the appropriate node.- Parameters:
type
- the return type of the nodeindex
- the index of the node to return- Returns:
- the terminal node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type
- Throws:
IndexOutOfBoundsException
- if the index refers to a node not within the subtree rooted at this node
-
getNumberOfTerminals
public int getNumberOfTerminals()Returns the number of terminal nodes contained in the subtree rooted at this node.- Returns:
- the number of terminal nodes contained in the subtree rooted at this node
-
getTerminalAt
Returns the terminal node at the specified index in the subtree rooted at this node. A depth-first search (DFS) walk of the tree is used to find the appropriate node.- Parameters:
index
- the index of the node to return- Returns:
- the terminal node at the specified index in the subtree rooted at this node
- Throws:
IndexOutOfBoundsException
- if the index refers to a node not within the subtree rooted at this node
-
getNumberOfNodes
Returns the number of nodes contained in the subtree rooted at this node that match the given return type.- Parameters:
type
- the return type of the node- Returns:
- the number of nodes contained in the subtree rooted at this node that match the given return type
-
getNodeAt
Returns the node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type. A depth-first search (DFS) walk of the tree is used to find the appropriate node.- Parameters:
type
- the return type of the nodeindex
- the index of the node to return- Returns:
- the node at the specified index in the subtree rooted at this node, counting only nodes that match the given return type
- Throws:
IndexOutOfBoundsException
- if the index refers to a node not within the subtree rooted at this node
-
getNumberOfNodes
public int getNumberOfNodes()Returns the number of nodes contained in the subtree rooted at this node.- Returns:
- the number of nodes contained in the subtree rooted at this node
-
getNodeAt
Returns the node at the specified index in the subtree rooted at this node. A depth-first search (DFS) walk of the tree is used to find the appropriate node.- Parameters:
index
- the index of the node to return- Returns:
- the node at the specified index in the subtree rooted at this node
- Throws:
IndexOutOfBoundsException
- if the index refers to a node not within the subtree rooted at this node
-
getNumberOfNodes
Returns the number of nodes contained in the subtree rooted at this node.- Parameters:
type
- the return type of the nodeincludeFunctions
-true
if functions (non-terminals) are counted;false
otherwiseincludeTerminals
-true
if terminals are counted;false
otherwise- Returns:
- the number of nodes contained in the subtree rooted at this node
-
getNodeAt
protected Node getNodeAt(Class<?> type, boolean includeFunctions, boolean includeTerminals, int index) Returns the node at the specified index in the subtree rooted at this node. A depth-first search (DFS) walk of the tree is used to find the appropriate node.- Parameters:
type
- the return type of the nodeincludeFunctions
-true
if functions (non-terminals) are counted;false
otherwiseincludeTerminals
-true
if terminals are counted;false
otherwiseindex
- the index of the node to return- Returns:
- the node at the specified index in the subtree rooted at this node
- Throws:
IndexOutOfBoundsException
- if the index refers to a node not within the subtree rooted at this node
-