Search in sources :

Example 6 with RuleDescrBuilder

use of org.drools.drl.ast.dsl.RuleDescrBuilder in project drools by kiegroup.

the class DRL6StrictParser method rule.

/* ------------------------------------------------------------------------------------------------
     *                         RULE STATEMENT
     * ------------------------------------------------------------------------------------------------ */
/**
 * rule := annotation* RULE stringId (EXTENDS stringId)? attributes? lhs? rhs END
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public RuleDescr rule(PackageDescrBuilder pkg) throws RecognitionException {
    RuleDescrBuilder rule = null;
    try {
        rule = helper.start(pkg, RuleDescrBuilder.class, null);
        setAnnotationsOn(rule);
        // 'rule'
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.RULE, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        if (helper.validateIdentifierKey(DroolsSoftKeywords.WHEN) || helper.validateIdentifierKey(DroolsSoftKeywords.THEN) || helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
            failMissingTokenException();
            // in case it is backtracking
            return null;
        }
        String name = stringId();
        if (state.failed)
            return null;
        if (state.backtracking == 0) {
            rule.name(name);
            helper.setParaphrasesValue(DroolsParaphraseTypes.RULE, "\"" + name + "\"");
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
        if (helper.validateIdentifierKey(DroolsSoftKeywords.EXTENDS)) {
            // 'extends'
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.EXTENDS, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            String parent = stringId();
            if (state.backtracking == 0)
                rule.extendsRule(parent);
            if (state.failed)
                return null;
        }
        if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
        attributes(rule);
        if (helper.validateIdentifierKey(DroolsSoftKeywords.WHEN)) {
            lhs(rule);
        } else {
            // creates an empty LHS
            rule.lhs();
        }
        rhs(rule);
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.END, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(RuleDescrBuilder.class, rule);
    }
    return (rule != null) ? rule.getDescr() : null;
}
Also used : RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) RecognitionException(org.antlr.runtime.RecognitionException)

Example 7 with RuleDescrBuilder

use of org.drools.drl.ast.dsl.RuleDescrBuilder in project drools by kiegroup.

the class ParserHelper method start.

@SuppressWarnings("unchecked")
public <T extends DescrBuilder<?, ?>> T start(DescrBuilder<?, ?> ctxBuilder, Class<T> clazz, String param) {
    if (state.backtracking == 0) {
        if (PackageDescrBuilder.class.isAssignableFrom(clazz)) {
            pushParaphrases(DroolsParaphraseTypes.PACKAGE);
            beginSentence(DroolsSentenceType.PACKAGE);
            setStart(ctxBuilder);
        } else if (ImportDescrBuilder.class.isAssignableFrom(clazz)) {
            ImportDescrBuilder imp;
            if (validateLT(2, DroolsSoftKeywords.FUNCTION) || validateLT(2, DroolsSoftKeywords.STATIC)) {
                imp = ctxBuilder == null ? DescrFactory.newPackage().newFunctionImport() : ((PackageDescrBuilder) ctxBuilder).newFunctionImport();
            } else {
                imp = ctxBuilder == null ? DescrFactory.newPackage().newImport() : ((PackageDescrBuilder) ctxBuilder).newImport();
            }
            pushParaphrases(DroolsParaphraseTypes.IMPORT);
            beginSentence(DroolsSentenceType.IMPORT_STATEMENT);
            setStart(imp);
            return (T) imp;
        } else if (UnitDescrBuilder.class.isAssignableFrom(clazz)) {
            UnitDescrBuilder imp = ctxBuilder == null ? DescrFactory.newPackage().newUnit() : ((PackageDescrBuilder) ctxBuilder).newUnit();
            pushParaphrases(DroolsParaphraseTypes.UNIT);
            beginSentence(DroolsSentenceType.UNIT);
            setStart(imp);
            return (T) imp;
        } else if (AccumulateImportDescrBuilder.class.isAssignableFrom(clazz)) {
            AccumulateImportDescrBuilder imp = ctxBuilder == null ? DescrFactory.newPackage().newAccumulateImport() : ((PackageDescrBuilder) ctxBuilder).newAccumulateImport();
            pushParaphrases(DroolsParaphraseTypes.ACCUMULATE_IMPORT);
            beginSentence(DroolsSentenceType.ACCUMULATE_IMPORT_STATEMENT);
            setStart(imp);
            return (T) imp;
        } else if (GlobalDescrBuilder.class.isAssignableFrom(clazz)) {
            GlobalDescrBuilder global = ctxBuilder == null ? DescrFactory.newPackage().newGlobal() : ((PackageDescrBuilder) ctxBuilder).newGlobal();
            pushParaphrases(DroolsParaphraseTypes.GLOBAL);
            beginSentence(DroolsSentenceType.GLOBAL);
            setStart(global);
            return (T) global;
        } else if (DeclareDescrBuilder.class.isAssignableFrom(clazz)) {
            DeclareDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare() : ((PackageDescrBuilder) ctxBuilder).newDeclare();
            return (T) declare;
        } else if (TypeDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            TypeDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().type() : ((DeclareDescrBuilder) ctxBuilder).type();
            pushParaphrases(DroolsParaphraseTypes.TYPE_DECLARE);
            beginSentence(DroolsSentenceType.TYPE_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (EnumDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            EnumDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().enumerative() : ((DeclareDescrBuilder) ctxBuilder).enumerative();
            pushParaphrases(DroolsParaphraseTypes.ENUM_DECLARE);
            beginSentence(DroolsSentenceType.ENUM_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (EntryPointDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            EntryPointDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().entryPoint() : ((DeclareDescrBuilder) ctxBuilder).entryPoint();
            pushParaphrases(DroolsParaphraseTypes.ENTRYPOINT_DECLARE);
            beginSentence(DroolsSentenceType.ENTRYPOINT_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (WindowDeclarationDescrBuilder.class.isAssignableFrom(clazz)) {
            WindowDeclarationDescrBuilder declare = ctxBuilder == null ? DescrFactory.newPackage().newDeclare().window() : ((DeclareDescrBuilder) ctxBuilder).window();
            pushParaphrases(DroolsParaphraseTypes.WINDOW_DECLARE);
            beginSentence(DroolsSentenceType.WINDOW_DECLARATION);
            setStart(declare);
            return (T) declare;
        } else if (FieldDescrBuilder.class.isAssignableFrom(clazz)) {
            FieldDescrBuilder field = ((AbstractClassTypeDeclarationBuilder) ctxBuilder).newField(param);
            setStart(field);
            return (T) field;
        } else if (EnumLiteralDescrBuilder.class.isAssignableFrom(clazz)) {
            EnumLiteralDescrBuilder literal = ((EnumDeclarationDescrBuilder) ctxBuilder).newEnumLiteral(param);
            setStart(literal);
            return (T) literal;
        } else if (FunctionDescrBuilder.class.isAssignableFrom(clazz)) {
            FunctionDescrBuilder function;
            if (ctxBuilder == null) {
                function = DescrFactory.newPackage().newFunction();
            } else {
                PackageDescrBuilder pkg = (PackageDescrBuilder) ctxBuilder;
                function = pkg.newFunction().namespace(pkg.getDescr().getName());
                AttributeDescr attribute = pkg.getDescr().getAttribute("dialect");
                if (attribute != null) {
                    function.dialect(attribute.getValue());
                }
            }
            pushParaphrases(DroolsParaphraseTypes.FUNCTION);
            beginSentence(DroolsSentenceType.FUNCTION);
            setStart(function);
            return (T) function;
        } else if (RuleDescrBuilder.class.isAssignableFrom(clazz)) {
            RuleDescrBuilder rule = ctxBuilder == null ? DescrFactory.newPackage().newRule() : ((PackageDescrBuilder) ctxBuilder).newRule();
            pushParaphrases(DroolsParaphraseTypes.RULE);
            beginSentence(DroolsSentenceType.RULE);
            setStart(rule);
            return (T) rule;
        } else if (QueryDescrBuilder.class.isAssignableFrom(clazz)) {
            QueryDescrBuilder query = ctxBuilder == null ? DescrFactory.newPackage().newQuery() : ((PackageDescrBuilder) ctxBuilder).newQuery();
            pushParaphrases(DroolsParaphraseTypes.QUERY);
            beginSentence(DroolsSentenceType.QUERY);
            setStart(query);
            return (T) query;
        } else if (AttributeDescrBuilder.class.isAssignableFrom(clazz)) {
            AttributeDescrBuilder<?> attribute = ((AttributeSupportBuilder<?>) ctxBuilder).attribute(param);
            setStart(attribute);
            return (T) attribute;
        } else if (EvalDescrBuilder.class.isAssignableFrom(clazz)) {
            EvalDescrBuilder<?> eval = ((CEDescrBuilder<?, ?>) ctxBuilder).eval();
            pushParaphrases(DroolsParaphraseTypes.EVAL);
            beginSentence(DroolsSentenceType.EVAL);
            setStart(eval);
            return (T) eval;
        } else if (ForallDescrBuilder.class.isAssignableFrom(clazz)) {
            ForallDescrBuilder<?> forall = ((CEDescrBuilder<?, ?>) ctxBuilder).forall();
            setStart(forall);
            return (T) forall;
        } else if (CEDescrBuilder.class.isAssignableFrom(clazz)) {
            setStart(ctxBuilder);
            return (T) ctxBuilder;
        } else if (PatternDescrBuilder.class.isAssignableFrom(clazz)) {
            PatternDescrBuilder<?> pattern = ((PatternContainerDescrBuilder<?, ?>) ctxBuilder).pattern();
            pushParaphrases(DroolsParaphraseTypes.PATTERN);
            setStart(pattern);
            return (T) pattern;
        } else if (CollectDescrBuilder.class.isAssignableFrom(clazz)) {
            CollectDescrBuilder<?> collect = ((PatternDescrBuilder<?>) ctxBuilder).from().collect();
            setStart(collect);
            return (T) collect;
        } else if (AccumulateDescrBuilder.class.isAssignableFrom(clazz)) {
            AccumulateDescrBuilder<?> accumulate = ((PatternDescrBuilder<?>) ctxBuilder).from().accumulate();
            setStart(accumulate);
            return (T) accumulate;
        } else if (BehaviorDescrBuilder.class.isAssignableFrom(clazz)) {
            BehaviorDescrBuilder<?> behavior = ((PatternDescrBuilder<?>) ctxBuilder).behavior();
            setStart(behavior);
            return (T) behavior;
        } else if (NamedConsequenceDescrBuilder.class.isAssignableFrom(clazz)) {
            NamedConsequenceDescrBuilder<?> namedConsequence = ((CEDescrBuilder<?, ?>) ctxBuilder).namedConsequence();
            setStart(namedConsequence);
            return (T) namedConsequence;
        } else if (ConditionalBranchDescrBuilder.class.isAssignableFrom(clazz)) {
            ConditionalBranchDescrBuilder<?> conditionalBranch = ((CEDescrBuilder<?, ?>) ctxBuilder).conditionalBranch();
            setStart(conditionalBranch);
            return (T) conditionalBranch;
        }
    }
    return null;
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) DeclareDescrBuilder(org.drools.drl.ast.dsl.DeclareDescrBuilder) AttributeDescrBuilder(org.drools.drl.ast.dsl.AttributeDescrBuilder) ForallDescrBuilder(org.drools.drl.ast.dsl.ForallDescrBuilder) NamedConsequenceDescrBuilder(org.drools.drl.ast.dsl.NamedConsequenceDescrBuilder) AccumulateImportDescrBuilder(org.drools.drl.ast.dsl.AccumulateImportDescrBuilder) ImportDescrBuilder(org.drools.drl.ast.dsl.ImportDescrBuilder) FunctionDescrBuilder(org.drools.drl.ast.dsl.FunctionDescrBuilder) CEDescrBuilder(org.drools.drl.ast.dsl.CEDescrBuilder) FieldDescrBuilder(org.drools.drl.ast.dsl.FieldDescrBuilder) QueryDescrBuilder(org.drools.drl.ast.dsl.QueryDescrBuilder) UnitDescrBuilder(org.drools.drl.ast.dsl.UnitDescrBuilder) GlobalDescrBuilder(org.drools.drl.ast.dsl.GlobalDescrBuilder) EntryPointDeclarationDescrBuilder(org.drools.drl.ast.dsl.EntryPointDeclarationDescrBuilder) WindowDeclarationDescrBuilder(org.drools.drl.ast.dsl.WindowDeclarationDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) AccumulateImportDescrBuilder(org.drools.drl.ast.dsl.AccumulateImportDescrBuilder) PatternDescrBuilder(org.drools.drl.ast.dsl.PatternDescrBuilder) AccumulateDescrBuilder(org.drools.drl.ast.dsl.AccumulateDescrBuilder) EnumDeclarationDescrBuilder(org.drools.drl.ast.dsl.EnumDeclarationDescrBuilder) EnumLiteralDescrBuilder(org.drools.drl.ast.dsl.EnumLiteralDescrBuilder) AbstractClassTypeDeclarationBuilder(org.drools.drl.ast.dsl.AbstractClassTypeDeclarationBuilder) TypeDeclarationDescrBuilder(org.drools.drl.ast.dsl.TypeDeclarationDescrBuilder) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr)

Example 8 with RuleDescrBuilder

use of org.drools.drl.ast.dsl.RuleDescrBuilder in project drools by kiegroup.

the class ParserHelper method end.

@SuppressWarnings("unchecked")
public <T extends DescrBuilder<?, ?>> T end(Class<T> clazz, DescrBuilder<?, ?> builder) {
    if (state.backtracking == 0) {
        if (!(FieldDescrBuilder.class.isAssignableFrom(clazz) || AttributeDescrBuilder.class.isAssignableFrom(clazz) || CEDescrBuilder.class.isAssignableFrom(clazz) || CollectDescrBuilder.class.isAssignableFrom(clazz) || AccumulateDescrBuilder.class.isAssignableFrom(clazz) || ForallDescrBuilder.class.isAssignableFrom(clazz) || BehaviorDescrBuilder.class.isAssignableFrom(clazz) || ConditionalBranchDescrBuilder.class.isAssignableFrom(clazz) || NamedConsequenceDescrBuilder.class.isAssignableFrom(clazz))) {
            popParaphrases();
        }
        if (RuleDescrBuilder.class.isAssignableFrom(clazz)) {
            RuleDescrBuilder ruleDescrBuilder = (RuleDescrBuilder) builder;
            ruleDescrBuilder.end().getDescr().afterRuleAdded(ruleDescrBuilder.getDescr());
        }
        setEnd(builder);
        return (T) builder;
    }
    return null;
}
Also used : CEDescrBuilder(org.drools.drl.ast.dsl.CEDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) AccumulateDescrBuilder(org.drools.drl.ast.dsl.AccumulateDescrBuilder) BehaviorDescrBuilder(org.drools.drl.ast.dsl.BehaviorDescrBuilder) NamedConsequenceDescrBuilder(org.drools.drl.ast.dsl.NamedConsequenceDescrBuilder)

Example 9 with RuleDescrBuilder

use of org.drools.drl.ast.dsl.RuleDescrBuilder in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method commonDeclarePatternWithConstraint.

@Test
public void commonDeclarePatternWithConstraint() {
    String patternType = "TEMPERATURE";
    String constraintsString = "value < 35";
    final CEDescrBuilder<CEDescrBuilder<CEDescrBuilder<RuleDescrBuilder, AndDescr>, NotDescr>, ExistsDescr> existsBuilder = lhsBuilder.not().exists();
    KiePMMLDescrLhsFactory.factory(lhsBuilder).commonDeclarePatternWithConstraint(existsBuilder, patternType, constraintsString);
    assertNotNull(existsBuilder.getDescr());
    final List<BaseDescr> descrs = existsBuilder.getDescr().getDescrs();
    assertNotNull(descrs);
    assertEquals(1, descrs.size());
    assertTrue(descrs.get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) descrs.get(0);
    assertEquals(patternType, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    assertEquals(constraintsString, exprConstraintDescr.getExpression());
}
Also used : CEDescrBuilder(org.drools.drl.ast.dsl.CEDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 10 with RuleDescrBuilder

use of org.drools.drl.ast.dsl.RuleDescrBuilder in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method setUp.

@Before
public void setUp() throws Exception {
    PackageDescrBuilder builder = DescrFactory.newPackage().name(PACKAGE_NAME);
    RuleDescrBuilder ruleBuilder = builder.newRule().name(CURRENT_RULE);
    lhsBuilder = ruleBuilder.lhs();
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) Before(org.junit.Before)

Aggregations

RuleDescrBuilder (org.drools.drl.ast.dsl.RuleDescrBuilder)10 RecognitionException (org.antlr.runtime.RecognitionException)3 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)3 AndDescr (org.drools.drl.ast.descr.AndDescr)2 AccumulateDescrBuilder (org.drools.drl.ast.dsl.AccumulateDescrBuilder)2 NamedConsequenceDescrBuilder (org.drools.drl.ast.dsl.NamedConsequenceDescrBuilder)2 PackageDescrBuilder (org.drools.drl.ast.dsl.PackageDescrBuilder)2 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 ExistsDescr (org.drools.drl.ast.descr.ExistsDescr)1 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)1 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1 AbstractClassTypeDeclarationBuilder (org.drools.drl.ast.dsl.AbstractClassTypeDeclarationBuilder)1 AccumulateImportDescrBuilder (org.drools.drl.ast.dsl.AccumulateImportDescrBuilder)1 AttributeDescrBuilder (org.drools.drl.ast.dsl.AttributeDescrBuilder)1 BehaviorDescrBuilder (org.drools.drl.ast.dsl.BehaviorDescrBuilder)1 DeclareDescrBuilder (org.drools.drl.ast.dsl.DeclareDescrBuilder)1 EntryPointDeclarationDescrBuilder (org.drools.drl.ast.dsl.EntryPointDeclarationDescrBuilder)1 EnumDeclarationDescrBuilder (org.drools.drl.ast.dsl.EnumDeclarationDescrBuilder)1 EnumLiteralDescrBuilder (org.drools.drl.ast.dsl.EnumLiteralDescrBuilder)1