org.shiwa.fgi.iwir.condition
Class ConditionNode

java.lang.Object
  extended by org.shiwa.fgi.iwir.condition.ConditionNode

public class ConditionNode
extends java.lang.Object

The Class CondirtionNode provides the data structure and methods to create and evaluate a condition AST. The following EBNF-Grammar defines a condition:

 CONDITION := CONJUNCTION {or CONJUNCTION}
 CONJUNCTION := EQUATION {and EQUATION}
 EQUATION := RELATION {EQOP RELATION}
 RELATION := FACTPR {COMPOP FACTOR}
 FACTOR := not FACTOR | (CONDITION) | STRING | PORT | integer | double | bool
 COMPOP := > | >= | < | <=
 EQUOP := != | =
 STRING := "string"
 PORT := string
 
The preference is not >> > | >= | < | <= >> != | = >> and >> or . We use blank and parenthesis as delimiter. For example 3=3 is invalid. (3 < 4)> 3 is valid. A node has a special token type. It is either an operator or a value type. Depending of the node type, values and children are set. Every binary operation has a left and right child. The unary operator not have only a left child. Parenthesis are construct as follows: The parent node contains ConditionToken.LP and child is set as left child. If the type of a node is for example Token.INT, then the attribute num is set and getNum() returns the corresponding integer value. Other value attributes return null.


Constructor Summary
ConditionNode(java.lang.Boolean bool)
          Instantiates a new node with the given Boolean.
ConditionNode(ConditionNode node)
          Returns a copy.
ConditionNode(ConditionToken op)
          Instantiates a new node with the given Operator.
ConditionNode(ConditionToken type, java.lang.String string)
          Instantiates a new node with the given String.
ConditionNode(java.lang.Double dob)
          Instantiates a new node with the given Double.
ConditionNode(java.lang.Integer num)
          Instantiates a new node with the given Integer.
 
Method Summary
static ConditionNode createAST(java.lang.String condition)
          Creates the AST from the given condition string.
static boolean evalNode(ConditionNode n)
          Evaluates an AST and returns a boolean value.
static java.util.List<ConditionNode> flatten(ConditionNode node)
          Flattens the AST and returning a list of nodes.
 java.lang.Boolean getBool()
          Gets the boolean value of the node or null if the value is not set.
static java.lang.String getCondition(ConditionNode n)
          Gets the string representation of a condition AST.
 java.lang.Double getDob()
          Gets the double value of the node or null if the value is not set.
 ConditionNode getLeft()
          Gets the left subtree of the node or null if the value is not set.
 java.lang.Integer getNum()
          Gets the int value of the node or null if the value is not set.
 ConditionToken getOp()
          Gets the current operation or null if the operation is not set.
 java.lang.String getPort()
          Gets the port or null if the value is not set.
 ConditionNode getRight()
          Gets the right subtree of the node or null if the value is not set.
 java.lang.String getString()
          Gets the string value of the node or null if the value is not set.
static boolean isLeaf(ConditionNode n)
          Checks if the node is a leaf node.
static void printCondition(ConditionNode n)
          Prints the condition string from an AST.
static void printInOrder(ConditionNode n)
          Prints the AST in order.
static void printPreOrder(ConditionNode n)
          Prints the AST pre order.
 void setBool(java.lang.Boolean b)
          Sets the boolean value.
 void setDob(java.lang.Double d)
          Sets the double value.
 void setLeft(ConditionNode left)
          Sets the left subtree of a node.
 void setNum(java.lang.Integer i)
          Sets the int value.
 void setPort(java.lang.String p)
          Sets the port value.
 void setRight(ConditionNode right)
          Sets the right subtree of a node.
 void setString(java.lang.String s)
          Sets the string value.
static java.util.List<ConditionNode> split(ConditionNode node, ConditionToken token)
          Split.
 java.lang.String toString()
          Returns the string representation of an AST.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConditionNode

public ConditionNode(ConditionToken op)
Instantiates a new node with the given Operator.

Parameters:
op - the operator of the node

ConditionNode

public ConditionNode(java.lang.Integer num)
Instantiates a new node with the given Integer.

Parameters:
num - the int value of the node

ConditionNode

public ConditionNode(java.lang.Double dob)
Instantiates a new node with the given Double.

Parameters:
dob - the double value of the node

ConditionNode

public ConditionNode(java.lang.Boolean bool)
Instantiates a new node with the given Boolean.

Parameters:
bool - the boolean value of the node

ConditionNode

public ConditionNode(ConditionToken type,
                     java.lang.String string)
Instantiates a new node with the given String.

Parameters:
type - the type
string - the string value of the node

ConditionNode

public ConditionNode(ConditionNode node)
Returns a copy.

Parameters:
node - the node
Method Detail

createAST

public static ConditionNode createAST(java.lang.String condition)
Creates the AST from the given condition string.

Parameters:
condition - the condition to parse
Returns:
the root node of the AST

flatten

public static java.util.List<ConditionNode> flatten(ConditionNode node)
Flattens the AST and returning a list of nodes. Note that each node in the list loses the information of its left and right child.

Parameters:
node - the node
Returns:
the list

split

public static java.util.List<ConditionNode> split(ConditionNode node,
                                                  ConditionToken token)
Split.

Parameters:
node - the node
token - the token
Returns:
the list

printInOrder

public static void printInOrder(ConditionNode n)
Prints the AST in order.

Parameters:
n - the root node of the AST

printPreOrder

public static void printPreOrder(ConditionNode n)
Prints the AST pre order.

Parameters:
n - the root node of the AST

getCondition

public static java.lang.String getCondition(ConditionNode n)
Gets the string representation of a condition AST.

Parameters:
n - the root node
Returns:
the condition string

printCondition

public static void printCondition(ConditionNode n)
Prints the condition string from an AST.

Parameters:
n - the root node of the AST

toString

public java.lang.String toString()
Returns the string representation of an AST. Same as

Overrides:
toString in class java.lang.Object
Returns:
the string getCondition(ConditionNode)

getOp

public ConditionToken getOp()
Gets the current operation or null if the operation is not set.

Returns:
the op

getNum

public java.lang.Integer getNum()
Gets the int value of the node or null if the value is not set.

Returns:
the num

setNum

public void setNum(java.lang.Integer i)
Sets the int value. Throws an IllegalStateException if the node has not an appropriate type.

Parameters:
i - the new int value

getDob

public java.lang.Double getDob()
Gets the double value of the node or null if the value is not set.

Returns:
the dob

setDob

public void setDob(java.lang.Double d)
Sets the double value. Throws an IllegalStateException if the node has not an appropriate type.

Parameters:
d - the new double value

getBool

public java.lang.Boolean getBool()
Gets the boolean value of the node or null if the value is not set.

Returns:
the bool

setBool

public void setBool(java.lang.Boolean b)
Sets the boolean value. Throws an IllegalStateException if the node has not an appropriate type.

Parameters:
b - the new boolean value

getString

public java.lang.String getString()
Gets the string value of the node or null if the value is not set.

Returns:
the string

setString

public void setString(java.lang.String s)
Sets the string value. Throws an IllegalStateException if the node has not an appropriate type.

Parameters:
s - the new string value

getLeft

public ConditionNode getLeft()
Gets the left subtree of the node or null if the value is not set.

Returns:
the left

getRight

public ConditionNode getRight()
Gets the right subtree of the node or null if the value is not set.

Returns:
the right

getPort

public java.lang.String getPort()
Gets the port or null if the value is not set.

Returns:
the port

setPort

public void setPort(java.lang.String p)
Sets the port value. Throws an IllegalStateException if the node has not an appropriate type.

Parameters:
p - the new string value

isLeaf

public static boolean isLeaf(ConditionNode n)
Checks if the node is a leaf node. i.e left and right children is null.

Parameters:
n - the node to check
Returns:
true, if node is a leaf

setLeft

public void setLeft(ConditionNode left)
Sets the left subtree of a node.

Parameters:
left - the new left subtree

setRight

public void setRight(ConditionNode right)
Sets the right subtree of a node.

Parameters:
right - the new right subtree

evalNode

public static boolean evalNode(ConditionNode n)
Evaluates an AST and returns a boolean value.

Parameters:
n - the root node
Returns:
the boolean result