Search in sources :

Example 11 with AccumulateDescr

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

the class AccumulateHelperHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final String expression = ((org.w3c.dom.Text) element.getChildNodes().item(0)).getWholeText();
    final Object parent = parser.getParent();
    final AccumulateDescr accumulate = (AccumulateDescr) parent;
    if (localName.equals("init")) {
        emptyContentCheck(localName, expression, parser);
        accumulate.setInitCode(expression.trim());
    } else if (localName.equals("action")) {
        emptyContentCheck(localName, expression, parser);
        accumulate.setActionCode(expression.trim());
    } else if (localName.equals("result")) {
        emptyContentCheck(localName, expression, parser);
        accumulate.setResultCode(expression.trim());
    } else if (localName.equals("reverse")) {
        emptyContentCheck(localName, expression, parser);
        accumulate.setReverseCode(expression.trim());
    } else if (localName.equals("external-function")) {
        accumulate.addFunction(element.getAttribute("evaluator"), // no support to bindings yet?
        null, false, new String[] { element.getAttribute("expression") });
    }
    return null;
}
Also used : Element(org.w3c.dom.Element) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr)

Example 12 with AccumulateDescr

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

the class AccumulateHandler method start.

public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    final AccumulateDescr accumulateDesrc = new AccumulateDescr();
    return accumulateDesrc;
}
Also used : AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr)

Example 13 with AccumulateDescr

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

the class AccumulateHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final AccumulateDescr accumulateDescr = (AccumulateDescr) parser.getCurrent();
    final Object parent = parser.getParent();
    if (parent.getClass().getName().equals(FromDescr.class.getName())) {
        final PatternDescr result = (PatternDescr) parser.getParent(1);
        result.setSource(accumulateDescr);
    } else if (parent instanceof ConditionalElementDescr) {
        final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parent;
        parentDescr.addDescr(accumulateDescr);
    }
    return accumulateDescr;
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) Element(org.w3c.dom.Element) FromDescr(org.drools.drl.ast.descr.FromDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 14 with AccumulateDescr

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

the class NamedConsequenceVisitor method whenThenDSL.

private MethodCallExpr whenThenDSL(ConditionalBranchDescr desc, PatternDescr patternRelated, Class<?> patternType, String callMethod, MethodCallExpr parentExpression) {
    MethodCallExpr when = parentExpression == null ? createDslTopLevelMethod(callMethod) : new MethodCallExpr(parentExpression, callMethod);
    final String condition = desc.getCondition().toString();
    if (!condition.equals("true")) {
        // Default case
        when.addArgument(new StringLiteralExpr(context.getConditionId(patternType, condition)));
        String identifier = patternRelated.getIdentifier();
        DrlxParseResult parseResult;
        if (identifier == null) {
            // The accumulate pattern doesn't have an identifier. Let's parse the consequence and use the acc functions
            parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(Object.class, "", condition);
            parseResult.accept((DrlxParseSuccess parseSuccess) -> {
                SingleDrlxParseSuccess parseSuccess1 = (SingleDrlxParseSuccess) parseSuccess;
                AccumulateDescr source = (AccumulateDescr) patternRelated.getSource();
                for (String usedDeclaration : parseSuccess1.getUsedDeclarations()) {
                    for (AccumulateDescr.AccumulateFunctionCallDescr functionCallDescr : source.getFunctions()) {
                        if (functionCallDescr.getBind().equals(usedDeclaration)) {
                            addVariable(patternRelated, when, functionCallDescr);
                        }
                    }
                }
                when.addArgument(generateLambdaWithoutParameters(parseSuccess1.getUsedDeclarations(), parseSuccess.getExpr(), true, Optional.empty()));
            });
        } else {
            when.addArgument(context.getVarExpr(identifier));
            parseResult = ConstraintParser.defaultConstraintParser(context, packageModel).drlxParse(patternType, identifier, condition);
            Collection<String> usedDeclarations = ((SingleDrlxParseSuccess) parseResult).getUsedDeclarations();
            if (usedDeclarations.isEmpty()) {
                // _this
                parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(Collections.emptySortedSet(), parseSuccess.getExpr())));
            } else {
                parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(usedDeclarations, parseSuccess.getExpr(), true, Optional.empty())));
            }
        }
    }
    MethodCallExpr then = new MethodCallExpr(when, THEN_CALL);
    MethodCallExpr rhs = onDSL(desc.getConsequence());
    then.addArgument(rhs);
    return then;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 15 with AccumulateDescr

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

the class JavaAccumulateBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final AccumulateDescr accumDescr = (AccumulateDescr) descr;
    if (!accumDescr.hasValidInput()) {
        return null;
    }
    // build source
    BaseDescr input = accumDescr.getInput();
    if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
        input = ((AndDescr) input).getDescrs().get(0);
    }
    final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
    final RuleConditionElement source = builder.build(context, input);
    if (source == null) {
        return null;
    }
    final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
    Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
    if (prefixPattern != null && prefixPattern.getDeclaration() != null) {
        declsInScope.remove(prefixPattern.getDeclaration().getIdentifier());
    }
    Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
    Accumulate accumulate;
    if (accumDescr.isExternalFunction()) {
        // if it uses 1+ external function, build methods for them
        accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
    } else {
        // if it uses inline code, build the class for it
        accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
    }
    return accumulate;
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) RuleConditionElement(org.drools.core.rule.RuleConditionElement) Declaration(org.drools.core.rule.Declaration) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate)

Aggregations

AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)23 PatternDescr (org.drools.drl.ast.descr.PatternDescr)17 PackageDescr (org.drools.drl.ast.descr.PackageDescr)15 RuleDescr (org.drools.drl.ast.descr.RuleDescr)15 Test (org.junit.Test)15 Accumulate (org.drools.core.rule.Accumulate)4 AndDescr (org.drools.drl.ast.descr.AndDescr)3 InputStreamReader (java.io.InputStreamReader)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)2 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)2 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 Declaration (org.drools.core.rule.Declaration)2 MultiAccumulate (org.drools.core.rule.MultiAccumulate)2 RuleConditionElement (org.drools.core.rule.RuleConditionElement)2 SingleAccumulate (org.drools.core.rule.SingleAccumulate)2 BaseDescr (org.drools.drl.ast.descr.BaseDescr)2