Search in sources :

Example 61 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class DRL5Parser method lhsAnd.

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

Example 62 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr 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.compiler.lang.descr.AndDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 63 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class RuleDescrBuilderImpl method lhs.

public CEDescrBuilder<RuleDescrBuilder, AndDescr> lhs() {
    CEDescrBuilder<RuleDescrBuilder, AndDescr> ce = new CEDescrBuilderImpl<RuleDescrBuilder, AndDescr>(this, new AndDescr());
    descr.setLhs(ce.getDescr());
    return ce;
}
Also used : RuleDescrBuilder(org.drools.compiler.lang.api.RuleDescrBuilder) AndDescr(org.drools.compiler.lang.descr.AndDescr)

Example 64 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class QueryDescrBuilderImpl method lhs.

public CEDescrBuilder<QueryDescrBuilder, AndDescr> lhs() {
    CEDescrBuilder<QueryDescrBuilder, AndDescr> ce = new CEDescrBuilderImpl<QueryDescrBuilder, AndDescr>(this, new AndDescr());
    descr.setLhs(ce.getDescr());
    return ce;
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) QueryDescrBuilder(org.drools.compiler.lang.api.QueryDescrBuilder)

Example 65 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class RuleBuilderTest method testBuildBigIntegerLiteralConstraint.

@Test
public void testBuildBigIntegerLiteralConstraint() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    AndDescr andDescr = new AndDescr();
    PatternDescr patDescr = new PatternDescr("java.math.BigInteger", "$bd");
    ExprConstraintDescr fcd = new ExprConstraintDescr("this==10");
    patDescr.addConstraint(fcd);
    andDescr.addDescr(patDescr);
    ruleDescr.setLhs(andDescr);
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackages()[0].getRule("Test Rule");
    final GroupElement and = rule.getLhs();
    final Pattern pat = (Pattern) and.getChildren().get(0);
    if (pat.getConstraints().get(0) instanceof MvelConstraint) {
        final MvelConstraint fc = (MvelConstraint) pat.getConstraints().get(0);
        assertTrue("Wrong class. Expected java.math.BigInteger. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigInteger);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) BigInteger(java.math.BigInteger) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

AndDescr (org.drools.compiler.lang.descr.AndDescr)79 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)54 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)52 Test (org.junit.Test)52 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)39 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)27 Cheese (org.drools.compiler.Cheese)13 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)12 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)12 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)8 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)7 NotDescr (org.drools.compiler.lang.descr.NotDescr)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 List (java.util.List)4 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)4 QueryDescr (org.drools.compiler.lang.descr.QueryDescr)4