Package org.moeaframework.util.tree
Class Node
java.lang.Object
org.moeaframework.util.tree.Node
- All Implemented Interfaces:
Serializable,Named
- 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,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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract NodecopyNode()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 Objectevaluate(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.intgetDepth()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.intReturns the number of branches between this node and the furthest leaf.intReturns the number of branches between this node and the nearest leaf.getName()Returns the name of this object.getNodeAt(int index) Returns the node at the specified index in the subtree rooted at this node.protected NodeReturns 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.intReturns the number of arguments (child nodes) of this node.intReturns the number of function (non-terminal) nodes contained in the subtree rooted at this node.intgetNumberOfFunctions(Class<?> type) Returns the number of function (non-terminal) nodes contained in the subtree rooted at this node that match the given return type.intReturns the number of nodes contained in the subtree rooted at this node.intgetNumberOfNodes(Class<?> type) Returns the number of nodes contained in the subtree rooted at this node that match the given return type.protected intgetNumberOfNodes(Class<?> type, boolean includeFunctions, boolean includeTerminals) Returns the number of nodes contained in the subtree rooted at this node.intReturns the number of terminal nodes contained in the subtree rooted at this node.intgetNumberOfTerminals(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; ornullif 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.booleanisFixed()Returnstrueif this node should not be modified, altered, or replaced;falseotherwise.booleanReturnstrueif this is a terminal node (has no arguments);falseotherwise.booleanisValid()Returnstrueif this node and its arguments are valid;falseotherwise.setArgument(int index, Node expression) Sets the argument (child node) at the specified index.voidsetFixed(boolean fixed) Set totrueif this node should not be modified, altered, or replaced;falseotherwise.voidsetFixedTree(boolean fixed) CallssetFixed(boolean)on all nodes in the subtree rooted at this node.intsize()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
-
getName
Description copied from interface:NamedReturns the name of this object. The format for the name depends on the specific implementation, but in general we recommend:- Prefer using alphanumeric characters including
'_'and'-'. - Avoid whitespace and other control characters.
nullcan be returned, if allowed by the implementation, to indicate the object is anonymous or has no assigned name.
- Prefer using alphanumeric characters including
-
setFixedTree
public void setFixedTree(boolean fixed) CallssetFixed(boolean)on all nodes in the subtree rooted at this node.- Parameters:
fixed-trueif all nodes in the subtree rooted at this node should not be modified, altered, or replaced;falseotherwise
-
setFixed
public void setFixed(boolean fixed) Set totrueif this node should not be modified, altered, or replaced;falseotherwise.- Parameters:
fixed-trueif this node should not be modified, altered, or replaced;falseotherwise
-
isFixed
public boolean isFixed()Returnstrueif this node should not be modified, altered, or replaced;falseotherwise.- Returns:
trueif this node should not be modified, altered, or replaced;falseotherwise
-
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; ornullif this node has no parent.- Returns:
- the parent of this node; or
nullif 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()Returnstrueif this node and its arguments are valid;falseotherwise. A valid node has all arguments defined, all arguments are valid, and all arguments are the appropriate type.- Returns:
trueif this node and its arguments are valid;falseotherwise
-
toString
-
isTerminal
public boolean isTerminal()Returnstrueif this is a terminal node (has no arguments);falseotherwise.- Returns:
trueif this is a terminal node (has no arguments);falseotherwise
-
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-trueif functions (non-terminals) are counted;falseotherwiseincludeTerminals-trueif terminals are counted;falseotherwise- 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-trueif functions (non-terminals) are counted;falseotherwiseincludeTerminals-trueif terminals are counted;falseotherwiseindex- 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
-