Search in sources :

Example 36 with BaseDescr

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

the class DRL5Parser method constraint.

/**
 * constraint := nestedConstraint | conditionalOrExpression
 * @param pattern
 * @throws RecognitionException
 */
private void constraint(PatternDescrBuilder<?> pattern, boolean positional, String prefix) throws RecognitionException {
    if (speculateNestedConstraint()) {
        nestedConstraint(pattern, prefix);
        return;
    }
    if (state.backtracking == 0) {
        helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
    }
    int first = input.index();
    // resetting
    exprParser.getHelper().setHasOperator(false);
    try {
        exprParser.conditionalOrExpression();
    } finally {
        if (state.backtracking == 0) {
            if (input.LA(1) == DRL5Lexer.ID && input.LA(2) == DRL5Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
            } else if (input.LA(1) != DRL5Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
            } else if (!lastTokenWasWhiteSpace()) {
                int location = getCurrentLocation();
                if (location == Location.LOCATION_LHS_INSIDE_CONDITION_END) {
                    helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                } else if (input.get(input.index()).getType() != DRL5Lexer.EOF) {
                    helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
                }
            } else if (getCurrentLocation() == Location.LOCATION_LHS_INSIDE_CONDITION_START && !exprParser.getHelper().getHasOperator() && lastTokenWasWhiteSpace() && input.LA(1) == DRL5Lexer.EOF && input.LA(-1) == DRL5Lexer.ID) {
                helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_OPERATOR);
            }
        }
    }
    if (state.failed)
        return;
    if (state.backtracking == 0 && input.index() > first) {
        // expression consumed something
        int last = input.LT(-1).getTokenIndex();
        String expr = toExpression(prefix, first, last);
        pattern.constraint(expr, positional);
        BaseDescr constrDescr = pattern.getDescr().getDescrs().get(pattern.getDescr().getDescrs().size() - 1);
        constrDescr.setLocation(input.get(first).getLine(), input.get(first).getCharPositionInLine());
        constrDescr.setEndLocation(input.get(last).getLine(), input.get(last).getCharPositionInLine());
        constrDescr.setStartCharacter(((CommonToken) input.get(first)).getStartIndex());
        constrDescr.setEndCharacter(((CommonToken) input.get(last)).getStopIndex());
    }
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr)

Example 37 with BaseDescr

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

Example 38 with BaseDescr

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

the class RuleParserTest method testForall.

@Test
public void testForall() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forall.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final ForallDescr forall = (ForallDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, forall.getDescrs().size());
    final PatternDescr pattern = forall.getBasePattern();
    assertEquals("Person", pattern.getObjectType());
    final List<BaseDescr> remaining = forall.getRemainingPatterns();
    assertEquals(1, remaining.size());
    final PatternDescr cheese = (PatternDescr) remaining.get(0);
    assertEquals("Cheese", cheese.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ForallDescr(org.drools.compiler.lang.descr.ForallDescr) Test(org.junit.Test)

Example 39 with BaseDescr

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

the class RuleParserTest method testOrWithSpecialBind.

@Test
public void testOrWithSpecialBind() throws Exception {
    String source = "rule \"A and (B or C or D)\" \n" + "    when \n" + "        pdo1 : ParametricDataObject( paramID == 101, stringValue == \"1000\" ) and \n" + "        pdo2 :(ParametricDataObject( paramID == 101, stringValue == \"1001\" ) or \n" + "               ParametricDataObject( paramID == 101, stringValue == \"1002\" ) or \n" + "               ParametricDataObject( paramID == 101, stringValue == \"1003\" )) \n" + "    then \n" + "        System.out.println( \"Rule: A and (B or C or D) Fired. pdo1: \" + pdo1 +  \" pdo2: \"+ pdo2); \n" + "end\n";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    AndDescr lhs = rule.getLhs();
    assertEquals(2, lhs.getDescrs().size());
    PatternDescr pdo1 = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("pdo1", pdo1.getIdentifier());
    OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
    assertEquals(3, or.getDescrs().size());
    for (BaseDescr pdo2 : or.getDescrs()) {
        assertEquals("pdo2", ((PatternDescr) pdo2).getIdentifier());
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 40 with BaseDescr

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

the class GroupElementBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final ConditionalElementDescr cedescr = (ConditionalElementDescr) descr;
    final GroupElement ge = this.newGroupElementFor(descr);
    context.getDeclarationResolver().pushOnBuildStack(ge);
    if (prefixPattern != null) {
        ge.addChild(prefixPattern);
    }
    // iterate over child descriptors
    for (final BaseDescr child : cedescr.getDescrs()) {
        // gets child to build
        // child.setResource(..) does not seem to be necessary (since builderImpls have already set the resource for all children)
        // but leaving it in here to be save
        child.setResource(context.getRuleDescr().getResource());
        child.setNamespace(context.getRuleDescr().getNamespace());
        // gets corresponding builder
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(child.getClass());
        if (builder != null) {
            final RuleConditionElement element = builder.build(context, child);
            // builder will return null. Ex: ClassNotFound for the pattern type
            if (element != null) {
                ge.addChild(element);
            }
        } else {
            throw new RuntimeException("BUG: no builder found for descriptor class " + child.getClass());
        }
    }
    context.getDeclarationResolver().popBuildStack();
    return ge;
}
Also used : GroupElement(org.drools.core.rule.GroupElement) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) RuleConditionElement(org.drools.core.rule.RuleConditionElement) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Aggregations

BaseDescr (org.drools.compiler.lang.descr.BaseDescr)109 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)60 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)23 AtomicExprDescr (org.drools.compiler.lang.descr.AtomicExprDescr)17 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)16 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)12 AndDescr (org.drools.compiler.lang.descr.AndDescr)11 RelationalExprDescr (org.drools.compiler.lang.descr.RelationalExprDescr)11 DeclareDescrBuilder (org.drools.compiler.lang.api.DeclareDescrBuilder)9 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)7 ArrayList (java.util.ArrayList)6 AccumulateDescrBuilder (org.drools.compiler.lang.api.AccumulateDescrBuilder)6 AnnotatedDescrBuilder (org.drools.compiler.lang.api.AnnotatedDescrBuilder)6 AnnotationDescrBuilder (org.drools.compiler.lang.api.AnnotationDescrBuilder)6 AttributeDescrBuilder (org.drools.compiler.lang.api.AttributeDescrBuilder)6 BehaviorDescrBuilder (org.drools.compiler.lang.api.BehaviorDescrBuilder)6 CEDescrBuilder (org.drools.compiler.lang.api.CEDescrBuilder)6 CollectDescrBuilder (org.drools.compiler.lang.api.CollectDescrBuilder)6 ConditionalBranchDescrBuilder (org.drools.compiler.lang.api.ConditionalBranchDescrBuilder)6