I have tree with Expressions(odata4j)
. I need to parse it to list of Expressions like in botton on photo below:
Every OrExpression
and AndExpression
has methods like getRHS(right)
and getLeft(left)
to get objects below.
Till now I have the following code:
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;
}
Here is result of my list:
[EqExpression(1) OR, EqExpression(2) AND, LtExpression(3) null, LtExpression(4) OR, EqExpression(5) null]
I can't get operator to LtExpression(3)
and EqExpression(5)
because its 2 levels higher in tree.
Any ideas?
I made it by stack.
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);
}
Here is expected result:
[EqExpression, OR, GtExpression, And, LtExpression, OR, LtExpression, OR, EqExpression]