Search in sources :

Example 61 with AndDescr

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

the class DRL6Parser method fromAccumulate.

/**
 * fromAccumulate := ACCUMULATE LEFT_PAREN lhsAnd (COMMA|SEMICOLON)
 *                   ( INIT chunk_(_) COMMA ACTION chunk_(_) COMMA
 *                     ( REVERSE chunk_(_) COMMA)? RESULT chunk_(_)
 *                   | accumulateFunction
 *                   ) RIGHT_PAREN
 *
 * @param pattern
 * @throws org.antlr.runtime.RecognitionException
 */
private void fromAccumulate(PatternDescrBuilder<?> pattern) throws RecognitionException {
    AccumulateDescrBuilder<?> accumulate = helper.start(pattern, AccumulateDescrBuilder.class, null);
    try {
        if (helper.validateIdentifierKey(DroolsSoftKeywords.ACCUMULATE)) {
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
        } else {
            // might be using the short mnemonic
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACC, null, DroolsEditorType.KEYWORD);
        }
        if (state.failed)
            return;
        if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
            helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
        }
        match(input, DRL6Lexer.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) == DRL6Lexer.COMMA) {
            match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return;
        } else if (input.LA(-1) != DRL6Lexer.SEMICOLON) {
            match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return;
        }
        if (helper.validateIdentifierKey(DroolsSoftKeywords.INIT)) {
            // custom code, inline accumulate
            // initBlock
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.INIT, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return;
            if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_INIT);
            }
            String init = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_INIT_INSIDE);
            if (state.failed)
                return;
            if (state.backtracking == 0)
                accumulate.init(init);
            if (input.LA(1) == DRL6Lexer.COMMA) {
                match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return;
            }
            // actionBlock
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACTION, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return;
            if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION);
            }
            String action = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION_INSIDE);
            if (state.failed)
                return;
            if (state.backtracking == 0)
                accumulate.action(action);
            if (input.LA(1) == DRL6Lexer.COMMA) {
                match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return;
            }
            // reverseBlock
            if (helper.validateIdentifierKey(DroolsSoftKeywords.REVERSE)) {
                match(input, DRL6Lexer.ID, DroolsSoftKeywords.REVERSE, null, DroolsEditorType.KEYWORD);
                if (state.failed)
                    return;
                if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
                    helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE);
                }
                String reverse = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE_INSIDE);
                if (state.failed)
                    return;
                if (state.backtracking == 0)
                    accumulate.reverse(reverse);
                if (input.LA(1) == DRL6Lexer.COMMA) {
                    match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                    if (state.failed)
                        return;
                }
            }
            // resultBlock
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.RESULT, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return;
            if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_RESULT);
            }
            String result = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.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, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
        if (state.failed)
            return;
    } finally {
        helper.end(AccumulateDescrBuilder.class, accumulate);
        if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.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 62 with AndDescr

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

the class DRL6Parser method lhsAccumulate.

/**
 * lhsAccumulate := (ACCUMULATE|ACC) LEFT_PAREN lhsAnd (COMMA|SEMICOLON)
 *                      accumulateFunctionBinding (COMMA accumulateFunctionBinding)*
 *                      (SEMICOLON constraints)?
 *                  RIGHT_PAREN SEMICOLON?
 *
 * @param ce
 * @return
 * @throws org.antlr.runtime.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 {
            if (helper.validateIdentifierKey(DroolsSoftKeywords.ACCUMULATE)) {
                match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
            } else {
                // might be using the short mnemonic
                match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACC, null, DroolsEditorType.KEYWORD);
            }
            if (state.failed)
                return null;
            if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
            }
            match(input, DRL6Lexer.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, true);
                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) == DRL6Lexer.COMMA) {
                match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
            } else if (input.LA(-1) != DRL6Lexer.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, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
            }
            // accumulate functions
            accumulateFunctionBinding(accumulate);
            if (state.failed)
                return null;
            while (input.LA(1) == DRL6Lexer.COMMA) {
                match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
                accumulateFunctionBinding(accumulate);
                if (state.failed)
                    return null;
            }
            if (input.LA(1) == DRL6Lexer.SEMICOLON) {
                match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
                if (state.failed)
                    return null;
                constraints(pattern);
            }
            match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
        } finally {
            helper.end(AccumulateDescrBuilder.class, accumulate);
            if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
            }
        }
    } finally {
        helper.end(PatternDescrBuilder.class, pattern);
    }
    if (input.LA(1) == DRL6Lexer.SEMICOLON) {
        match(input, DRL6Lexer.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) AccumulateImportDescrBuilder(org.drools.drl.ast.dsl.AccumulateImportDescrBuilder) 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) UnitDescrBuilder(org.drools.drl.ast.dsl.UnitDescrBuilder) 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 63 with AndDescr

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

the class RuleHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final RuleDescr ruleDescr = (RuleDescr) parser.getCurrent();
    final AndDescr lhs = ruleDescr.getLhs();
    if (lhs == null) {
        throw new SAXParseException("<rule> requires a LHS", parser.getLocator());
    }
    NodeList list = element.getElementsByTagName("rhs");
    if (list.getLength() == 0) {
        throw new SAXParseException("<rule> requires a <rh> child element", parser.getLocator());
    }
    ruleDescr.setConsequence(((org.w3c.dom.Text) list.item(0).getChildNodes().item(0)).getWholeText());
    NodeList attributes = element.getElementsByTagName("rule-attribute");
    for (int i = 0, length = attributes.getLength(); i < length; i++) {
        final String name = ((Element) attributes.item(i)).getAttribute("name");
        emptyAttributeCheck("rule-attribute", "name", name, parser);
        final String value = ((Element) attributes.item(i)).getAttribute("value");
        ruleDescr.addAttribute(new AttributeDescr(name, value));
    }
    ((PackageDescr) parser.getData()).addRule(ruleDescr);
    return ruleDescr;
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr)

Example 64 with AndDescr

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

the class AndHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final AndDescr andDescr = (AndDescr) parser.getCurrent();
    final Object parent = parser.getParent();
    if (!andDescr.getDescrs().isEmpty()) {
        if (parent instanceof RuleDescr || parent instanceof QueryDescr) {
            final RuleDescr ruleDescr = (RuleDescr) parent;
            ruleDescr.setLhs(andDescr);
        } else if (parent instanceof MultiPatternDestinationDescr) {
            final MultiPatternDestinationDescr mpDescr = (MultiPatternDestinationDescr) parent;
            mpDescr.setInput(andDescr);
        } else if (parent instanceof ConditionalElementDescr) {
            final ConditionalElementDescr ceDescr = (ConditionalElementDescr) parent;
            ceDescr.addDescr(andDescr);
        }
    }
    return andDescr;
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) MultiPatternDestinationDescr(org.drools.drl.ast.descr.MultiPatternDestinationDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) Element(org.w3c.dom.Element) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 65 with AndDescr

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

the class AccumulateVisitor method validateAccFunctionTypeAgainstPatternType.

private void validateAccFunctionTypeAgainstPatternType(RuleContext context, PatternDescr basePattern, AccumulateFunction accumulateFunction) {
    final String expectedResultTypeString = basePattern.getObjectType();
    final Class<?> expectedResultType = DrlxParseUtil.getClassFromType(context.getTypeResolver(), DrlxParseUtil.toClassOrInterfaceType(expectedResultTypeString));
    final Class actualResultType = accumulateFunction.getResultType();
    final boolean isJavaDialect = context.getRuleDialect().equals(RuleContext.RuleDialect.JAVA);
    final boolean isQuery = context.isQuery();
    final boolean isCollectFunction = isCollectFunction(accumulateFunction);
    final boolean isInsideAccumulate = ((AccumulateDescr) basePattern.getSource()).getInput() instanceof AndDescr;
    final boolean checkCollect = !isCollectFunction || isInsideAccumulate;
    if (!isQuery && checkCollect && isJavaDialect && !Pattern.isCompatibleWithAccumulateReturnType(expectedResultType, actualResultType)) {
        context.addCompilationError(new InvalidExpressionErrorResult(String.format("Pattern of type: '[ClassObjectType class=%s]' " + "on rule '%s' " + "is not compatible with type %s returned by accumulate function.", expectedResultType.getCanonicalName(), context.getRuleName(), actualResultType.getCanonicalName()), Optional.of(context.getRuleDescr())));
    }
}
Also used : InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) AndDescr(org.drools.drl.ast.descr.AndDescr)

Aggregations

AndDescr (org.drools.drl.ast.descr.AndDescr)88 Test (org.junit.Test)61 PatternDescr (org.drools.drl.ast.descr.PatternDescr)60 RuleDescr (org.drools.drl.ast.descr.RuleDescr)54 PackageDescr (org.drools.drl.ast.descr.PackageDescr)39 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)34 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 BaseDescr (org.drools.drl.ast.descr.BaseDescr)18 Cheese (org.drools.mvel.compiler.Cheese)13 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)11 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)10 OrDescr (org.drools.drl.ast.descr.OrDescr)10 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)9 BindingDescr (org.drools.drl.ast.descr.BindingDescr)8 NotDescr (org.drools.drl.ast.descr.NotDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 QueryDescr (org.drools.drl.ast.descr.QueryDescr)6 RuleDescrBuilder (org.drools.drl.ast.dsl.RuleDescrBuilder)5 FieldConstraintDescr (org.drools.drl.ast.descr.FieldConstraintDescr)4 LiteralRestrictionDescr (org.drools.drl.ast.descr.LiteralRestrictionDescr)4