vec_math
Class StringNode

java.lang.Object
  extended by vec_math.Node<String>
      extended by vec_math.StringNode
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
BooleanNode, ExpressionNode

public class StringNode
extends Node<String>
implements Serializable

A node that is parseable from a string. Parentheses are used for changing the normal precedence. The valid operators in a regime must be set explicately with the setValidOperator(java.util.List>) method. For String parsing the following rules must be met.

See Also:
Serialized Form

Field Summary
private  int operatorIndex
          Position in parse string of node operator.
private static List<Operator<String>> valid
          Valid operators.
 
Constructor Summary
StringNode(String definition)
          Constructs a root string node.
StringNode(StringNode parent, String definition, int level)
          Defines a fully qualified String node.
 
Method Summary
private static int checkParenthesis(String test)
          Checks if all parenthesis opened are closed again.
private static String getStripped(String def)
          Strips the String from leading and trailing whitespaces.
 Set<String> getVariables()
          It is difficult to assure proper replacement of variables.
protected  boolean isVariable(String sarg)
          Only strings are considered to be variables.
protected  Operator<String> parseOperator(String def)
          Parses the input string for the root-level operator with the highest precedence.
protected  StringNode[] parseSub(String def)
          Parses the String for subnodes.
static String replaceVariable(String target, String variable, Object map)
          Returns a string where the occurence of a given variable's name is replaced with the actual value of the variable.
static String replaceVariable(String target, String variable, Object map, boolean quote)
          Returns a string where the occurence of a given variable's name is replaced with the actual value of the variable.
static void setValidOperator(List<Operator<String>> op)
          Sets the allowed operators.
 
Methods inherited from class vec_math.Node
equals, getLevel, getParent, getParseSource, getSub, getSubAt, getSubNodeNumber, getTie, getValue, isFinal, isParsed, isRoot, parse, parse, setParse, setParseSource, setSub, setTie, setValue, toString, traceVariable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

valid

private static List<Operator<String>> valid
Valid operators. Must be set prior to parsing.


operatorIndex

private int operatorIndex
Position in parse string of node operator.

Constructor Detail

StringNode

public StringNode(String definition)
Constructs a root string node.


StringNode

public StringNode(StringNode parent,
                  String definition,
                  int level)
Defines a fully qualified String node. Any subclass of string node must provide a constructor with this format.

See Also:
parseSub(java.lang.String)
Method Detail

setValidOperator

public static void setValidOperator(List<Operator<String>> op)
Sets the allowed operators. This method must be called prior to any call to the parsing methods.


replaceVariable

public static String replaceVariable(String target,
                                     String variable,
                                     Object map)
                              throws ParseException
Returns a string where the occurence of a given variable's name is replaced with the actual value of the variable. This method is used for converting a string like
             sin(height) to sin(0.342956765).
       
Whenever the replacement object is not a string, the object's string representation is used as the replacement. For string objects, however, this string is put within double quotes.

Parameters:
target - The entire string to parse.
variable - The name of the variable as found in the target.
map - The replacement for the variable.
Returns:
The target string with the variable replaced with its value.
Throws:
ParseException - If the variable cannot be found in the target.

replaceVariable

public static String replaceVariable(String target,
                                     String variable,
                                     Object map,
                                     boolean quote)
                              throws ParseException
Returns a string where the occurence of a given variable's name is replaced with the actual value of the variable. This method is used for converting a string like
             sin(height) to sin(0.342956765).
       
Whenever the replacement object is not a string, the object's string representation is used as the replacement. For string objects, however, this string is put within double quotes if the qutoe flag is set.

Parameters:
target - The entire string to parse.
variable - The name of the variable as found in the target.
map - The replacement for the variable.
quote - If true, strings are wrapped in double quotes.
Returns:
The target string with the variable replaced with its value.
Throws:
ParseException - If the variable cannot be found in the target.

isVariable

protected boolean isVariable(String sarg)
Only strings are considered to be variables.

Overrides:
isVariable in class Node<String>

getStripped

private static String getStripped(String def)
Strips the String from leading and trailing whitespaces. Removes root-level parenthesis by re-entrant method.


checkParenthesis

private static int checkParenthesis(String test)
Checks if all parenthesis opened are closed again. Returns zero if successful, otherwise the number of open parenthesis (which can be negative) is returned.


getVariables

public Set<String> getVariables()
It is difficult to assure proper replacement of variables. We do a trick here. First, we collect all variables from the parental class, then we reshovel this in a sorted set, where the longest variable names are first. If we then traverse the set of variables and replace them with some number constant, this should ensure that longest variable names are replaced first.

Overrides:
getVariables in class Node<String>

parseOperator

protected Operator<String> parseOperator(String def)
                                  throws ParseException
Parses the input string for the root-level operator with the highest precedence. If called prior to setValidOperator(java.util.List>) a ParseException is thrown.

Specified by:
parseOperator in class Node<String>
Throws:
ParseException

parseSub

protected StringNode[] parseSub(String def)
                         throws ParseException
Parses the String for subnodes. This method relies that parseOperator(java.lang.String) has been called beforehand which should always be the case in the normal line of execution, guaranteed by the order of method calls in the superclass.

Note that this class is able to produce subnodes of the same class type as itself if the constructor requirement is met.

Specified by:
parseSub in class Node<String>
Throws:
ParseException