0

IConditionTree表現をCriterionに変換する必要があります。

現在、論理演算子ANDとORを実装していますが、NOTも追加する必要があります。ただし、このタイプの基準はジャンクションとして表されないため、Restrictions.not(基準基準)を使用して作成する必要があります。条件ツリーの処理中に、入力パラメーターとして挿入する必要がある次の基準がわかりません。

//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論理演算子をスイッチ部分に実装する方法はありますか?AND / OR演算子と同じNOT論理演算子の動作が必要な場合はどうすればよいですか?

4

1 に答える 1

0

私はあなたのツリーに子供が1人しかいないと思うので、ここに行きます:

//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 に答える