A Simple Sample for Expression Tree
WANG Hailong
I wrote an example of handling expression trees with Visitor Pattern. This paper explains the general method of expression tree treatment, gives a simple example source code and gives a relevant explanation.
The source code is compiled and operated correctly. Java expr.testmain
First, let's see the definition of the class being expressed.
Expression.java Expression class utility interface, all expressions must implement this interface
Package expr;
Public interface express {
Public int value ();
}
Constantexpression.java constant expression
Package expr;
Public class constantExpression imports {
Protected int Data;
Public constantexpression (int thisdata) {
Data = these Data;
}
Public int value () {
Return Data;
}
}
VariableExpression.java variable expression
Package expr;
Import java.util.map;
Public Class VariableExpression Implements Expression {
Protected string myname;
Protected Map VariableTable;
Public variableExpression (String ITSName, Map Thevariabletable) {
MyName = ITSNAME;
VariableTable = ThevariaBletable;
}
// Get the variable name
Public string name () {
Return myname;
}
Public int value () {
// Search in Table
Object valueObj = variabletable.get (MyName);
IF (valueObj == null) {
Return 0;
}
INT DATA = ((Integer) Valueobj) .intValue ();
Return Data;
}
}
AddExpression.java addiction expression
Package expr;
Public class addExpression imports express {
Protected expression leftnode;
Protected Expression RightNode;
Public addExpression (Expression AlexNode, Expression ARightNode) {
LeftNode = aleftnode;
RightNode = arridge;
}
Public int value () {
Return LeftNode.Value () RightNode.Value ();
}
}
SubExpression.java subtraction expression
Package expr;
Public class subs subexpression imports {
Protected expression leftnode;
Protected Expression RightNode;
Public SubExpression (Expression AleftNode, Expression ARightNode) {
LEFTNODE = ALEFTNODE; RightNode = ARightNode;
}
Public int value () {
Rightnode.value () - RightNode.Value ();
}
}
The above is all the code of the expression.
Below is a test program.
Testmain.java First, generate an expression tree, then get the value of this expression
Package expr;
Import java.util.map;
Import java.util.hashtable;
Public class testmain {
Public static void main (String [] args) {
Map variabletable = new hashtable ();
/ / Generate an expression tree x - (6 Y)
VariableExpression X = New VariableExpression ("X", VariableTable);
VariableExpression Y = New VariableExpression ("Y", VariableEtable;
ConstantExpression Six = New constantexpression (6);
AddExpression Six_ADD_Y = New Addexpression (Six, Y);
SubExpression X_SUB_6_Y = New SubExpression (x, Six_ADD_Y);
// set value for Variables
Variabletable.put (x.Name (), new integer (11)); // x = 11
Variabletable.put (Y.Name (), New Integer (3)); // y = 3
// operation
Int results = x_sub_6_y.value ();
System.out.println (Result);
}
}
The output is:
2
The above examples are easy to expand, increase the multiplication operation.