将树表达式解析为Java中的表达式列表

时间:2015-04-20 19:47:54

标签: java odata4j

我的树有Expressions(odata4j)。我需要将它解析为表达式列表,如下图中的botton:

http://i58.tinypic.com/x4rsxy.jpg

每个OrExpressionAndExpression都有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级。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我是通过堆栈制作的。

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]