Search in sources :

Example 1 with BoolOperator

use of io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression.BoolOperator in project vertigo by KleeGroup.

the class DslMultiExpressionRule method handle.

/**
 * {@inheritDoc}
 */
@Override
protected RuleMultiExpression handle(final PegChoice parsing) {
    final List<Object> innerBlock;
    switch(parsing.getChoiceIndex()) {
        case 0:
            final List<?> blockExpression = (List<?>) parsing.getValue();
            innerBlock = (List<Object>) blockExpression.get(2);
            break;
        case 1:
            innerBlock = (List<Object>) parsing.getValue();
            break;
        default:
            throw new IllegalArgumentException("case " + parsing.getChoiceIndex() + " not implemented");
    }
    final List<RuleExpression> expressionDefinitions = new ArrayList<>();
    final List<RuleMultiExpression> multiExpressionDefinitions = new ArrayList<>();
    // first (expression1|multiExpression1)
    final PegChoice firstExpressionChoice = (PegChoice) innerBlock.get(1);
    switch(firstExpressionChoice.getChoiceIndex()) {
        case 0:
            expressionDefinitions.add((RuleExpression) firstExpressionChoice.getValue());
            break;
        case 1:
            multiExpressionDefinitions.add((RuleMultiExpression) firstExpressionChoice.getValue());
            break;
        default:
            throw new IllegalArgumentException("case " + parsing.getChoiceIndex() + " not implemented");
    }
    // manyNextExpressionsRule
    final List<List<Object>> many = (List<List<Object>>) innerBlock.get(3);
    // On récupère le produit de la règle many
    BoolOperator operator = null;
    for (final List<Object> item : many) {
        if (operator != null && operator != item.get(1)) {
            throw new IllegalArgumentException("Can't use different operator in same block, attempt to find " + operator);
        }
        operator = (BoolOperator) item.get(1);
        // next (expression2|multiExpression2)
        final PegChoice nextExpressionChoice = (PegChoice) item.get(3);
        switch(nextExpressionChoice.getChoiceIndex()) {
            case 0:
                expressionDefinitions.add((RuleExpression) nextExpressionChoice.getValue());
                break;
            case 1:
                multiExpressionDefinitions.add((RuleMultiExpression) nextExpressionChoice.getValue());
                break;
            default:
                throw new IllegalArgumentException("case " + nextExpressionChoice.getChoiceIndex() + " not implemented");
        }
    }
    final boolean block = parsing.getChoiceIndex() == 0;
    // ---
    return new RuleMultiExpression(block, operator != null ? operator : BoolOperator.AND, expressionDefinitions, multiExpressionDefinitions);
}
Also used : RuleMultiExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression) ArrayList(java.util.ArrayList) PegChoice(io.vertigo.commons.peg.PegChoice) RuleExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleExpression) List(java.util.List) ArrayList(java.util.ArrayList) BoolOperator(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression.BoolOperator)

Aggregations

RuleExpression (io.vertigo.account.authorization.metamodel.rulemodel.RuleExpression)1 RuleMultiExpression (io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression)1 BoolOperator (io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression.BoolOperator)1 PegChoice (io.vertigo.commons.peg.PegChoice)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1