Search in sources :

Example 16 with ConditionalElementDescr

use of org.drools.drl.ast.descr.ConditionalElementDescr in project drools by kiegroup.

the class DRL6Parser method lhsOr.

/**
 * lhsOr := LEFT_PAREN OR lhsAnd+ RIGHT_PAREN
 *        | lhsAnd (OR lhsAnd)*
 *
 * @param ce
 * @param allowOr
 * @throws org.antlr.runtime.RecognitionException
 */
private BaseDescr lhsOr(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
    BaseDescr result = null;
    if (allowOr && input.LA(1) == DRL6Lexer.LEFT_PAREN && helper.validateLT(2, DroolsSoftKeywords.OR)) {
        // prefixed OR
        CEDescrBuilder<?, OrDescr> or = null;
        if (state.backtracking == 0) {
            or = ce.or();
            result = or.getDescr();
            helper.start(or, CEDescrBuilder.class, null);
        }
        try {
            match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            while (input.LA(1) == DRL6Lexer.AT) {
                // annotation*
                annotation(or);
                if (state.failed)
                    return null;
            }
            if (state.backtracking == 0) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
            }
            while (input.LA(1) != DRL6Lexer.RIGHT_PAREN) {
                lhsAnd(or, allowOr);
                if (state.failed)
                    return null;
            }
            match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
        } finally {
            if (state.backtracking == 0) {
                helper.end(CEDescrBuilder.class, or);
            }
        }
    } else {
        // infix OR
        // create an OR anyway, as if it is not an OR we remove it later
        CEDescrBuilder<?, OrDescr> or = null;
        if (state.backtracking == 0) {
            or = ce.or();
            result = or.getDescr();
            helper.start(or, CEDescrBuilder.class, null);
        }
        try {
            lhsAnd(or, allowOr);
            if (state.failed)
                return null;
            if (allowOr && (helper.validateIdentifierKey(DroolsSoftKeywords.OR) || input.LA(1) == DRL6Lexer.DOUBLE_PIPE)) {
                while (helper.validateIdentifierKey(DroolsSoftKeywords.OR) || input.LA(1) == DRL6Lexer.DOUBLE_PIPE) {
                    if (input.LA(1) == DRL6Lexer.DOUBLE_PIPE) {
                        match(input, DRL6Lexer.DOUBLE_PIPE, null, null, DroolsEditorType.SYMBOL);
                    } else {
                        match(input, DRL6Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
                    }
                    if (state.failed)
                        return null;
                    while (input.LA(1) == DRL6Lexer.AT) {
                        // annotation*
                        annotation(or);
                        if (state.failed)
                            return null;
                    }
                    if (state.backtracking == 0) {
                        helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
                    }
                    lhsAnd(or, allowOr);
                    if (state.failed)
                        return null;
                }
            } else if (allowOr) {
                if (state.backtracking == 0) {
                    // if no OR present, then remove it and add children to parent
                    ((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(or.getDescr());
                    for (BaseDescr base : or.getDescr().getDescrs()) {
                        ((ConditionalElementDescr) ce.getDescr()).addDescr(base);
                    }
                    result = ce.getDescr();
                }
            }
        } finally {
            if (state.backtracking == 0) {
                helper.end(CEDescrBuilder.class, or);
            }
        }
    }
    return result;
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 17 with ConditionalElementDescr

use of org.drools.drl.ast.descr.ConditionalElementDescr in project drools by kiegroup.

the class DRL6Parser method lhsExpression.

/**
 * lhsExpression := lhsOr*
 *
 * @param lhs
 * @throws org.antlr.runtime.RecognitionException
 */
private void lhsExpression(CEDescrBuilder<?, AndDescr> lhs) throws RecognitionException {
    helper.start(lhs, CEDescrBuilder.class, null);
    if (state.backtracking == 0) {
        helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
    }
    try {
        while (input.LA(1) != DRL6Lexer.EOF && !helper.validateIdentifierKey(DroolsSoftKeywords.THEN) && !helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
            if (state.backtracking == 0) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
            }
            lhsOr(lhs, true);
            if (lhs.getDescr() != null && lhs.getDescr() instanceof ConditionalElementDescr) {
                ConditionalElementDescr root = (ConditionalElementDescr) lhs.getDescr();
                BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
                root.getDescrs().clear();
                for (int i = 0; i < descrs.length; i++) {
                    root.addOrMerge(descrs[i]);
                }
            }
            if (state.failed)
                return;
        }
    } finally {
        helper.end(CEDescrBuilder.class, lhs);
    }
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 18 with ConditionalElementDescr

use of org.drools.drl.ast.descr.ConditionalElementDescr in project drools by kiegroup.

the class DRL5Parser method fromAccumulate.

/**
 * fromAccumulate := ACCUMULATE LEFT_PAREN lhsAnd COMMA
 *                   ( INIT chunk_(_) COMMA ACTION chunk_(_) COMMA
 *                     ( REVERSE chunk_(_) COMMA)? RESULT chunk_(_)
 *                   | accumulateFunction
 *                   ) RIGHT_PAREN
 *
 * @param pattern
 * @throws RecognitionException
 */
private void fromAccumulate(PatternDescrBuilder<?> pattern) throws RecognitionException {
    AccumulateDescrBuilder<?> accumulate = helper.start(pattern, AccumulateDescrBuilder.class, null);
    try {
        match(input, DRL5Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return;
        if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
            helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
        }
        match(input, DRL5Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
        if (state.failed)
            return;
        CEDescrBuilder<?, AndDescr> source = accumulate.source();
        try {
            helper.start(source, CEDescrBuilder.class, null);
            lhsAnd(source, false);
            if (state.failed)
                return;
            if (source.getDescr() != null && source.getDescr() instanceof ConditionalElementDescr) {
                ConditionalElementDescr root = (ConditionalElementDescr) source.getDescr();
                BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
                root.getDescrs().clear();
                for (int i = 0; i < descrs.length; i++) {
                    root.addOrMerge(descrs[i]);
                }
            }
        } finally {
            helper.end(CEDescrBuilder.class, source);
        }
        if (input.LA(1) == DRL5Lexer.COMMA) {
            match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return;
        }
        if (helper.validateIdentifierKey(DroolsSoftKeywords.INIT)) {
            // custom code, inline accumulate
            // initBlock
            match(input, DRL5Lexer.ID, DroolsSoftKeywords.INIT, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return;
            if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_INIT);
            }
            String init = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_INIT_INSIDE);
            if (state.failed)
                return;
            if (state.backtracking == 0)
                accumulate.init(init);
            if (input.LA(1) == DRL5Lexer.COMMA) {
                match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return;
            }
            // actionBlock
            match(input, DRL5Lexer.ID, DroolsSoftKeywords.ACTION, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return;
            if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION);
            }
            String action = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION_INSIDE);
            if (state.failed)
                return;
            if (state.backtracking == 0)
                accumulate.action(action);
            if (input.LA(1) == DRL5Lexer.COMMA) {
                match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return;
            }
            // reverseBlock
            if (helper.validateIdentifierKey(DroolsSoftKeywords.REVERSE)) {
                match(input, DRL5Lexer.ID, DroolsSoftKeywords.REVERSE, null, DroolsEditorType.KEYWORD);
                if (state.failed)
                    return;
                if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
                    helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE);
                }
                String reverse = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE_INSIDE);
                if (state.failed)
                    return;
                if (state.backtracking == 0)
                    accumulate.reverse(reverse);
                if (input.LA(1) == DRL5Lexer.COMMA) {
                    match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                    if (state.failed)
                        return;
                }
            }
            // resultBlock
            match(input, DRL5Lexer.ID, DroolsSoftKeywords.RESULT, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return;
            if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_RESULT);
            }
            String result = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_RESULT_INSIDE);
            if (state.failed)
                return;
            if (state.backtracking == 0)
                accumulate.result(result);
        } else {
            // accumulate functions
            accumulateFunction(accumulate, false, null);
            if (state.failed)
                return;
        }
        match(input, DRL5Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
        if (state.failed)
            return;
    } finally {
        helper.end(AccumulateDescrBuilder.class, accumulate);
        if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
            helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
        }
    }
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 19 with ConditionalElementDescr

use of org.drools.drl.ast.descr.ConditionalElementDescr in project drools by kiegroup.

the class DRL5Parser method lhsAccumulate.

/**
 * lhsAccumulate := ACCUMULATE LEFT_PAREN lhsAnd (COMMA|SEMICOLON)
 *                      accumulateFunctionBinding (COMMA accumulateFunctionBinding)*
 *                      (SEMICOLON constraints)?
 *                  RIGHT_PAREN SEMICOLON?
 *
 * @param ce
 * @return
 * @throws RecognitionException
 */
private BaseDescr lhsAccumulate(PatternContainerDescrBuilder<?, ?> ce) throws RecognitionException {
    PatternDescrBuilder<?> pattern = null;
    BaseDescr result = null;
    pattern = helper.start((DescrBuilder<?, ?>) ce, PatternDescrBuilder.class, null);
    if (pattern != null) {
        result = pattern.getDescr();
    }
    try {
        if (state.backtracking == 0) {
            pattern.type("Object[]");
            pattern.isQuery(false);
        // might have to add the implicit bindings as well
        }
        AccumulateDescrBuilder<?> accumulate = helper.start(pattern, AccumulateDescrBuilder.class, null);
        try {
            match(input, DRL5Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
            }
            match(input, DRL5Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
            CEDescrBuilder<?, AndDescr> source = accumulate.source();
            try {
                helper.start(source, CEDescrBuilder.class, null);
                lhsAnd(source, false);
                if (state.failed)
                    return null;
                if (source.getDescr() != null && source.getDescr() instanceof ConditionalElementDescr) {
                    ConditionalElementDescr root = (ConditionalElementDescr) source.getDescr();
                    BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
                    root.getDescrs().clear();
                    for (int i = 0; i < descrs.length; i++) {
                        root.addOrMerge(descrs[i]);
                    }
                }
            } finally {
                helper.end(CEDescrBuilder.class, source);
            }
            if (input.LA(1) == DRL5Lexer.COMMA) {
                match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
            } else if (input.LA(-1) != DRL5Lexer.SEMICOLON) {
                // lhsUnary will consume an optional SEMICOLON, so we need to check if it was consumed already
                // or if we must fail consuming it now
                match(input, DRL5Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
            }
            // accumulate functions
            accumulateFunctionBinding(accumulate);
            if (state.failed)
                return null;
            while (input.LA(1) == DRL5Lexer.COMMA) {
                match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
                accumulateFunctionBinding(accumulate);
                if (state.failed)
                    return null;
            }
            if (input.LA(1) == DRL5Lexer.SEMICOLON) {
                match(input, DRL5Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
                constraints(pattern);
            }
            match(input, DRL5Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
        } finally {
            helper.end(AccumulateDescrBuilder.class, accumulate);
            if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
            }
        }
    } finally {
        helper.end(PatternDescrBuilder.class, pattern);
    }
    if (input.LA(1) == DRL5Lexer.SEMICOLON) {
        match(input, DRL5Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
        if (state.failed)
            return null;
    }
    return result;
}
Also used : PatternDescrBuilder(org.drools.drl.ast.dsl.PatternDescrBuilder) AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) FunctionDescrBuilder(org.drools.drl.ast.dsl.FunctionDescrBuilder) DescrBuilder(org.drools.drl.ast.dsl.DescrBuilder) ForallDescrBuilder(org.drools.drl.ast.dsl.ForallDescrBuilder) NamedConsequenceDescrBuilder(org.drools.drl.ast.dsl.NamedConsequenceDescrBuilder) DeclareDescrBuilder(org.drools.drl.ast.dsl.DeclareDescrBuilder) EvalDescrBuilder(org.drools.drl.ast.dsl.EvalDescrBuilder) AnnotationDescrBuilder(org.drools.drl.ast.dsl.AnnotationDescrBuilder) AccumulateDescrBuilder(org.drools.drl.ast.dsl.AccumulateDescrBuilder) WindowDeclarationDescrBuilder(org.drools.drl.ast.dsl.WindowDeclarationDescrBuilder) PatternDescrBuilder(org.drools.drl.ast.dsl.PatternDescrBuilder) FieldDescrBuilder(org.drools.drl.ast.dsl.FieldDescrBuilder) EntryPointDeclarationDescrBuilder(org.drools.drl.ast.dsl.EntryPointDeclarationDescrBuilder) EnumDeclarationDescrBuilder(org.drools.drl.ast.dsl.EnumDeclarationDescrBuilder) QueryDescrBuilder(org.drools.drl.ast.dsl.QueryDescrBuilder) CollectDescrBuilder(org.drools.drl.ast.dsl.CollectDescrBuilder) ImportDescrBuilder(org.drools.drl.ast.dsl.ImportDescrBuilder) AnnotatedDescrBuilder(org.drools.drl.ast.dsl.AnnotatedDescrBuilder) BehaviorDescrBuilder(org.drools.drl.ast.dsl.BehaviorDescrBuilder) EnumLiteralDescrBuilder(org.drools.drl.ast.dsl.EnumLiteralDescrBuilder) GlobalDescrBuilder(org.drools.drl.ast.dsl.GlobalDescrBuilder) PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) AttributeDescrBuilder(org.drools.drl.ast.dsl.AttributeDescrBuilder) CEDescrBuilder(org.drools.drl.ast.dsl.CEDescrBuilder) PatternContainerDescrBuilder(org.drools.drl.ast.dsl.PatternContainerDescrBuilder) ConditionalBranchDescrBuilder(org.drools.drl.ast.dsl.ConditionalBranchDescrBuilder) TypeDeclarationDescrBuilder(org.drools.drl.ast.dsl.TypeDeclarationDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 20 with ConditionalElementDescr

use of org.drools.drl.ast.descr.ConditionalElementDescr in project drools by kiegroup.

the class DRL5Parser method lhsExpression.

/**
 * lhsExpression := lhsOr*
 *
 * @param lhs
 * @throws RecognitionException
 */
private void lhsExpression(CEDescrBuilder<?, AndDescr> lhs) throws RecognitionException {
    helper.start(lhs, CEDescrBuilder.class, null);
    if (state.backtracking == 0) {
        helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
    }
    try {
        while (input.LA(1) != DRL5Lexer.EOF && !helper.validateIdentifierKey(DroolsSoftKeywords.THEN) && !helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
            if (state.backtracking == 0) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
            }
            lhsOr(lhs, true);
            if (lhs.getDescr() != null && lhs.getDescr() instanceof ConditionalElementDescr) {
                ConditionalElementDescr root = (ConditionalElementDescr) lhs.getDescr();
                BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
                root.getDescrs().clear();
                for (int i = 0; i < descrs.length; i++) {
                    root.addOrMerge(descrs[i]);
                }
            }
            if (state.failed)
                return;
        }
    } finally {
        helper.end(CEDescrBuilder.class, lhs);
    }
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Aggregations

ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)30 BaseDescr (org.drools.drl.ast.descr.BaseDescr)16 AndDescr (org.drools.drl.ast.descr.AndDescr)11 Element (org.w3c.dom.Element)10 AnnotatedBaseDescr (org.drools.drl.ast.descr.AnnotatedBaseDescr)5 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)5 OrDescr (org.drools.drl.ast.descr.OrDescr)4 FromDescr (org.drools.drl.ast.descr.FromDescr)3 PatternDescr (org.drools.drl.ast.descr.PatternDescr)3 AccumulateDescrBuilder (org.drools.drl.ast.dsl.AccumulateDescrBuilder)3 AnnotatedDescrBuilder (org.drools.drl.ast.dsl.AnnotatedDescrBuilder)3 AnnotationDescrBuilder (org.drools.drl.ast.dsl.AnnotationDescrBuilder)3 AttributeDescrBuilder (org.drools.drl.ast.dsl.AttributeDescrBuilder)3 BehaviorDescrBuilder (org.drools.drl.ast.dsl.BehaviorDescrBuilder)3 CollectDescrBuilder (org.drools.drl.ast.dsl.CollectDescrBuilder)3 ConditionalBranchDescrBuilder (org.drools.drl.ast.dsl.ConditionalBranchDescrBuilder)3 DeclareDescrBuilder (org.drools.drl.ast.dsl.DeclareDescrBuilder)3 DescrBuilder (org.drools.drl.ast.dsl.DescrBuilder)3 EntryPointDeclarationDescrBuilder (org.drools.drl.ast.dsl.EntryPointDeclarationDescrBuilder)3 EnumDeclarationDescrBuilder (org.drools.drl.ast.dsl.EnumDeclarationDescrBuilder)3