0

我需要将 IConditionTree 表示形式转换为 Criterion。

目前我已经实现了逻辑运算符 AND 和 OR,现在我还需要添加 NOT。但是这种类型的标准不表示为 Junction,需要通过 Restrictions.not(Criterion criteria) 创建。在处理我的条件树期间,我不知道必须作为输入参数插入的以下标准。

//initial call
criteria.add(generateNodeCriteria(conditionTree.getRoot(), conditionTree));

private Criterion generateNodeCriteria(IConditionTreeNode node, IConditionTree conditionTree) throws SomeException {
    // Create criteria for condition
    if (node.getCondition() != null) {
        return createConditionCriterion(node.getCondition());
    }

    // Create criteria for logical operator
    if (node.getLogicalOperator() != null) {
        // What logical operator to use?
        Junction junction = null;
        switch (node.getLogicalOperator()) {
        case AND:
            junction = Restrictions.conjunction();
            break;
        case OR:
            junction = Restrictions.disjunction();
            break;
        }

        // Add all direct children of logical operator into conjunction
        for (IConditionTreeNode childNode : conditionTree.getOneLevelChildren(node)) {
            junction.add(generateNodeCriteria(childNode, conditionTree));
        }
        return junction;
    }
    throw new SomeException();
}

有什么办法,如何将 NOT 逻辑运算符实现到 switch 部分?如果我希望 NOT 逻辑运算符的行为与 AND/OR 运算符相同,我应该改变什么?

4

1 回答 1

0

我想你的树里只有一个孩子,所以这里是:

//initial call
criteria.add(generateNodeCriteria(conditionTree.getRoot(), conditionTree));

private Criterion generateNodeCriteria(IConditionTreeNode node, IConditionTree    conditionTree) throws SomeException {
    // Create criteria for condition
    if (node.getCondition() != null) {
    return createConditionCriterion(node.getCondition());
}

// Create criteria for logical operator
if (node.getLogicalOperator() != null) {

    boolean addChildren = false;

    // What logical operator to use?
    Junction junction = null;
    switch (node.getLogicalOperator()) {
    case AND:
        junction = Restrictions.conjunction();
        addChildren = true;
        break;
    case OR:
        junction = Restrictions.disjunction();
        addChildren = true;
        break;
    case NOT:
        junction = Restrictions.not(generateNodeCriteria(conditionTree.getOneLevelChildren(node).get(0), conditionTree));
    }

    if (addChildren)
      // Create all children first
      for (IConditionTreeNode childNode : conditionTree.getOneLevelChildren(node)) {
          junction.add(generateNodeCriteria(childNode, conditionTree));
      }

    return junction;
}
throw new SomeException();
}
于 2012-06-19T14:26:03.207 回答