-1

我有树Expressions(odata4j)。我需要将其解析为下面照片中底部的表达式列表:

每个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 个级别。

有任何想法吗?

4

1 回答 1

-1

我是用堆栈做的。

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]
于 2015-04-22T09:49:42.053 回答