将树表达式解析为 Java 中的表达式列表
Parse Tree Expression to List of expressions in Java
我有 Expressions(odata4j)
的树。我需要将它解析为表达式列表,如下图的 botton 所示:
每个 OrExpression
和 AndExpression
都有像 getRHS(right)
和 getLeft(left)
这样的方法来获取下面的对象。
到目前为止我有以下代码:
private BinaryCommonExpression getLeftRek(BinaryCommonExpression expr, ConditionOperator conditionOperator) {
BinaryCommonExpression lhs = expr;
if (lhs.getLHS() instanceof EntitySimpleProperty == false) {
if(lhs instanceof AndExpression){
conditionOperator = ConditionOperator.AND;
}else if(lhs instanceof OrExpression){
conditionOperator = ConditionOperator.OR;
}
getLeftRek((BinaryCommonExpression)lhs.getLHS(), conditionOperator);
if(lhs.getRHS() instanceof StringLiteral == false && lhs.getRHS() instanceof DateTimeLiteral == false && lhs.getRHS() instanceof IntegralLiteral == false/*lhs.getRHS() instanceof AndExpression || lhs.getRHS() instanceof OrExpression*/){
getLeftRek((BinaryCommonExpression)lhs.getRHS(), null);
}
} else {
Criterion lhsFinish = getLHSFinish(lhs, conditionOperator);
stack.push(lhs+ " "+conditionOperator);
}
return lhs;
}
这是我的列表结果:
[EqExpression(1) OR, EqExpression(2) AND, LtExpression(3) null, LtExpression(4) OR, EqExpression(5) null]
我无法获得 LtExpression(3)
和 EqExpression(5)
的运算符,因为它在树中高 2 级。
有什么想法吗?
我是靠堆砌的。
stack.push(lhs);
while(stack.get(stack.size()-1) instanceof OrExpression || stack.get(stack.size()-1) instanceof AndExpression){
BinaryCommonExpression popValue = (BinaryCommonExpression)stack.pop();
try{
if(stack.get(stack.size()-2) instanceof OrExpression || stack.get(stack.size()-2) instanceof AndExpression){
String operatorAfter = (String)stack.pop();
BinaryCommonExpression popVal2 = (BinaryCommonExpression)stack.pop();
BinaryCommonExpression lhs2 = (BinaryCommonExpression)popVal2.getLHS();
BinaryCommonExpression rhs = (BinaryCommonExpression)popVal2.getRHS();
stack.push(lhs2);
if(popVal2 instanceof OrExpression){
stack.push("OR");
}else if (popVal2 instanceof AndExpression){
stack.push("And");
}
stack.push(rhs);
stack.push(operatorAfter);
}
} catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
BinaryCommonExpression lhs2 = (BinaryCommonExpression)popValue.getLHS();
BinaryCommonExpression rhs = (BinaryCommonExpression)popValue.getRHS();
stack.push(lhs2);
if(popValue instanceof OrExpression){
stack.push("OR");
}else if (popValue instanceof AndExpression){
stack.push("And");
}
stack.push(rhs);
}
这是预期结果:
[EqExpression, OR, GtExpression, And, LtExpression, OR, LtExpression, OR, EqExpression]
我有 Expressions(odata4j)
的树。我需要将它解析为表达式列表,如下图的 botton 所示:
每个 OrExpression
和 AndExpression
都有像 getRHS(right)
和 getLeft(left)
这样的方法来获取下面的对象。
到目前为止我有以下代码:
private BinaryCommonExpression getLeftRek(BinaryCommonExpression expr, ConditionOperator conditionOperator) {
BinaryCommonExpression lhs = expr;
if (lhs.getLHS() instanceof EntitySimpleProperty == false) {
if(lhs instanceof AndExpression){
conditionOperator = ConditionOperator.AND;
}else if(lhs instanceof OrExpression){
conditionOperator = ConditionOperator.OR;
}
getLeftRek((BinaryCommonExpression)lhs.getLHS(), conditionOperator);
if(lhs.getRHS() instanceof StringLiteral == false && lhs.getRHS() instanceof DateTimeLiteral == false && lhs.getRHS() instanceof IntegralLiteral == false/*lhs.getRHS() instanceof AndExpression || lhs.getRHS() instanceof OrExpression*/){
getLeftRek((BinaryCommonExpression)lhs.getRHS(), null);
}
} else {
Criterion lhsFinish = getLHSFinish(lhs, conditionOperator);
stack.push(lhs+ " "+conditionOperator);
}
return lhs;
}
这是我的列表结果:
[EqExpression(1) OR, EqExpression(2) AND, LtExpression(3) null, LtExpression(4) OR, EqExpression(5) null]
我无法获得 LtExpression(3)
和 EqExpression(5)
的运算符,因为它在树中高 2 级。
有什么想法吗?
我是靠堆砌的。
stack.push(lhs);
while(stack.get(stack.size()-1) instanceof OrExpression || stack.get(stack.size()-1) instanceof AndExpression){
BinaryCommonExpression popValue = (BinaryCommonExpression)stack.pop();
try{
if(stack.get(stack.size()-2) instanceof OrExpression || stack.get(stack.size()-2) instanceof AndExpression){
String operatorAfter = (String)stack.pop();
BinaryCommonExpression popVal2 = (BinaryCommonExpression)stack.pop();
BinaryCommonExpression lhs2 = (BinaryCommonExpression)popVal2.getLHS();
BinaryCommonExpression rhs = (BinaryCommonExpression)popVal2.getRHS();
stack.push(lhs2);
if(popVal2 instanceof OrExpression){
stack.push("OR");
}else if (popVal2 instanceof AndExpression){
stack.push("And");
}
stack.push(rhs);
stack.push(operatorAfter);
}
} catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
BinaryCommonExpression lhs2 = (BinaryCommonExpression)popValue.getLHS();
BinaryCommonExpression rhs = (BinaryCommonExpression)popValue.getRHS();
stack.push(lhs2);
if(popValue instanceof OrExpression){
stack.push("OR");
}else if (popValue instanceof AndExpression){
stack.push("And");
}
stack.push(rhs);
}
这是预期结果:
[EqExpression, OR, GtExpression, And, LtExpression, OR, LtExpression, OR, EqExpression]