vec_math
Class Node<E>

java.lang.Object
  extended by vec_math.Node<E>
Direct Known Subclasses:
StringNode

public abstract class Node<E>
extends Object

A node in a parser scheme. The implementation is very basic and does only define principle methods. The concrete parsing of some object into the operator of the node and it sub-nodes must be implemnted by concrete subclasses.


Field Summary
private  boolean isParsed
          True if the node has been successfully parsed.
private  int level
          The level of the node.
private  Node<E> parent
          The parent of the node.
private  E parse
          The object this node can be parsed out.
private  Node<E>[] sub
          The sub-nodes.
private  Operator<E> tie
          The operator of this node.
private  E value
          The value of the node.
 
Constructor Summary
protected Node(E description)
          A basic constructor for a root node.
protected Node(Node<E> superior, E description, int layer)
          A basic constructor for sub nodes.
 
Method Summary
 boolean equals(Node that)
          Compares to nodes for equality.
 int getLevel()
          Returns the level of this node.
 Node<E> getParent()
          Returns the parent of this node.
 E getParseSource()
          Returns the object this node should be parsed from.
 Node<E>[] getSub()
          Returns the sub nodes.
 Node<E> getSubAt(int index)
          Returns a single sub node.
 int getSubNodeNumber()
          Returns the number of sub nodes.
 Operator<E> getTie()
          Returns the node operator.
 E getValue()
          Returns the value of this node.
 Set<E> getVariables()
          Uses the parsed node to dive recursively into all subnodes and piles up a variable list.
 boolean isFinal()
          Returns true if this node is a final node.
 boolean isParsed()
          Returns true if this node has already been parsed.
 boolean isRoot()
          Returns true if this node is the root node.
protected  boolean isVariable(E arg)
          Return true if the value object is a variable.
 void parse()
          Parses this node.
 void parse(E source)
          Parses the object to derive sub nodes and node operator if any.
protected abstract  Operator<E> parseOperator(E src)
          Parses the source for this nodes operator.
protected abstract  Node<E>[] parseSub(E src)
          Parses the source for this nodes sub-nodes.
protected  void setParse(boolean value)
          Sets the parsed flag.
protected  void setParseSource(E source)
          Sets the object this node should be parsed from.
protected  void setSub(Node<E>[] subnodes)
          Sets the sub nodes.
protected  void setTie(Operator<E> op)
          Sets the node operator.
 void setValue(E result)
          Sets the value of this node.
 String toString()
          A String representation of this Node.
protected  void traceVariable(Set<E> accu, Node<E> node)
          Stores the value of this node in the argument set if final.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

parent

private Node<E> parent
The parent of the node.


tie

private Operator<E> tie
The operator of this node.


sub

private Node<E>[] sub
The sub-nodes. The length of the array corresponds to the number of arguments of the operator of this node.


level

private int level
The level of the node. The root node has level 0.


parse

private E parse
The object this node can be parsed out.


value

private E value
The value of the node.


isParsed

private boolean isParsed
True if the node has been successfully parsed.

Constructor Detail

Node

protected Node(E description)
A basic constructor for a root node. Note that parsing must be explicitly initiated via a call to parse.


Node

protected Node(Node<E> superior,
               E description,
               int layer)
A basic constructor for sub nodes. Note that parsing must be explicitly initiated via a call to parse.

Method Detail

getTie

public Operator<E> getTie()
Returns the node operator.


setTie

protected void setTie(Operator<E> op)
Sets the node operator.


setSub

protected void setSub(Node<E>[] subnodes)
Sets the sub nodes.


getSub

public Node<E>[] getSub()
                 throws ParseException
Returns the sub nodes. Throws a ParseException if the node has not successfully parsed prior to the call to this method.

Throws:
ParseException

getSubAt

public Node<E> getSubAt(int index)
                 throws ParseException
Returns a single sub node. Throws a ParseException if the node has not successfully parsed prior to the call to this method.

Throws:
ParseException

getSubNodeNumber

public int getSubNodeNumber()
                     throws ParseException
Returns the number of sub nodes. This number should be identical to the number of operands the operator of this node requires. Throws a ParseException if the node has not successfully parsed prior to the call to this method.

Throws:
ParseException

isRoot

public boolean isRoot()
Returns true if this node is the root node. Simple checks if the node level equals zero.


isFinal

public boolean isFinal()
Returns true if this node is a final node. A final node consists of a single value without any operators. Throws a ParseException if the node has not successfully parsed prior to the call to this method.


getLevel

public int getLevel()
Returns the level of this node. Zero denotes the root node.


getParent

public Node<E> getParent()
Returns the parent of this node. Null is returned if this node is the root node.


getParseSource

public E getParseSource()
Returns the object this node should be parsed from.


setParseSource

protected void setParseSource(E source)
Sets the object this node should be parsed from.


getValue

public E getValue()
Returns the value of this node.


setValue

public void setValue(E result)
Sets the value of this node.


parse

public void parse()
           throws ParseException
Parses this node. Throws a ParseException if the parsable object had not been set prior to the call to this method.

Throws:
ParseException

isParsed

public boolean isParsed()
Returns true if this node has already been parsed.


setParse

protected void setParse(boolean value)
Sets the parsed flag.


parse

public void parse(E source)
           throws ParseException
Parses the object to derive sub nodes and node operator if any. Subclasses can override this class if performance can be increased by simultanously scanning for operator and sub-nodes. In the latter case the subclass should either call this class's parse method or setParse(boolean) to indicate successful parsing.

Throws:
ParseException

getVariables

public Set<E> getVariables()
Uses the parsed node to dive recursively into all subnodes and piles up a variable list.


traceVariable

protected void traceVariable(Set<E> accu,
                             Node<E> node)
Stores the value of this node in the argument set if final. Otherwise dives into subnodes. As for the general contract of a set, the variable is stored in the set only once.


isVariable

protected boolean isVariable(E arg)
Return true if the value object is a variable. The default behaviour is to always return true unless the argument is null. Subclasses should interfere here.


equals

public boolean equals(Node that)
Compares to nodes for equality. The default implementation returns true if the level and the parse object are equal.


toString

public String toString()
A String representation of this Node. Simply calls the toString() method on the parse object.

Overrides:
toString in class Object

parseOperator

protected abstract Operator<E> parseOperator(E src)
                                      throws ParseException
Parses the source for this nodes operator.

Throws:
ParseException

parseSub

protected abstract Node<E>[] parseSub(E src)
                               throws ParseException
Parses the source for this nodes sub-nodes.

Throws:
ParseException