Search in sources :

Example 11 with BaseDescr

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

the class PatternDSL method findAllConstraint.

private List<PatternConstraintParseResult> findAllConstraint(PatternDescr pattern, List<? extends BaseDescr> constraintDescrs, Class<?> patternType) {
    ConstraintParser constraintParser = ConstraintParser.defaultConstraintParser(context, packageModel);
    List<PatternConstraintParseResult> patternConstraintParseResults = new ArrayList<>();
    for (BaseDescr constraint : constraintDescrs) {
        List<PatternConstraintParseResult> patternConstraintParseResultsPerConstraintDescr = new ArrayList<>();
        String patternIdentifier = pattern.getIdentifier();
        boolean isPositional = isPositional(constraint);
        ConstraintExpression constraintExpression = ConstraintExpression.createConstraintExpression(context, patternType, constraint, isPositional);
        if (constraintExpression == null) {
            continue;
        }
        DrlxParseResult drlxParseResult;
        try {
            context.setCurrentConstraintDescr(Optional.of(constraint));
            drlxParseResult = constraintParser.drlxParse(patternType, patternIdentifier, constraintExpression, isPositional);
        } finally {
            context.resetCurrentConstraintDescr();
        }
        String expression = constraintExpression.getExpression();
        if (drlxParseResult.isSuccess() && ((DrlxParseSuccess) drlxParseResult).isRequiresSplit() && ((DrlxParseSuccess) drlxParseResult).getExpr().isBinaryExpr()) {
            BinaryExpr expr = ((DrlxParseSuccess) drlxParseResult).getExpr().asBinaryExpr();
            String leftExpression = printNode(((SingleDrlxParseSuccess) drlxParseResult).getLeft().getExpression());
            DrlxParseResult leftExpressionReparsed = constraintParser.drlxParse(patternType, patternIdentifier, leftExpression, isPositional);
            patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(leftExpression, patternIdentifier, leftExpressionReparsed));
            String rightExpression = printNode(((SingleDrlxParseSuccess) drlxParseResult).getRight().getExpression());
            DrlxParseResult rightExpressionReparsed = constraintParser.drlxParse(patternType, patternIdentifier, rightExpression, isPositional);
            DrlxParseResult normalizedParseResult = ConstraintUtil.normalizeConstraint(rightExpressionReparsed);
            patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(rightExpression, patternIdentifier, normalizedParseResult));
        } else {
            DrlxParseResult normalizedParseResult = ConstraintUtil.normalizeConstraint(drlxParseResult);
            patternConstraintParseResultsPerConstraintDescr.add(new PatternConstraintParseResult(expression, patternIdentifier, normalizedParseResult));
        }
        // Cast-check should be placed earlier than Null-check (calling the add method later means pushing the constraint earlier)
        addNullSafeExpr(constraintParser, pattern.getIdentifier(), patternConstraintParseResultsPerConstraintDescr);
        addImplicitCastExpr(constraintParser, pattern.getIdentifier(), patternConstraintParseResultsPerConstraintDescr);
        patternConstraintParseResults.addAll(patternConstraintParseResultsPerConstraintDescr);
    }
    return patternConstraintParseResults;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ArrayList(java.util.ArrayList) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) BaseDescr(org.drools.drl.ast.descr.BaseDescr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)

Example 12 with BaseDescr

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

the class AccumulateVisitor method visit.

public void visit(AccumulateDescr descr, PatternDescr basePattern) {
    final MethodCallExpr accumulateDSL = createDslTopLevelMethod(ACCUMULATE_CALL);
    context.addExpression(accumulateDSL);
    final MethodCallExpr accumulateExprs = createDslTopLevelMethod(AND_CALL);
    accumulateDSL.addArgument(accumulateExprs);
    this.context.pushScope(descr);
    pushAccumulateContext(accumulateExprs);
    try {
        Set<String> externalDeclrs = new HashSet<>(context.getAvailableBindings());
        BaseDescr input = descr.getInputPattern() == null ? descr.getInput() : descr.getInputPattern();
        input.accept(modelGeneratorVisitor);
        if (accumulateExprs.getArguments().isEmpty()) {
            accumulateDSL.remove(accumulateExprs);
        } else if (accumulateExprs.getArguments().size() == 1) {
            accumulateDSL.setArgument(0, accumulateExprs.getArguments().get(0));
        }
        if (!descr.getFunctions().isEmpty()) {
            if (validateBindings(descr)) {
                return;
            }
            processAccumulateFunctions(descr, basePattern, input, accumulateDSL);
        } else if (descr.getFunctions().isEmpty() && descr.getInitCode() != null) {
            new AccumulateInlineVisitor(context, packageModel).inlineAccumulate(descr, basePattern, accumulateDSL, externalDeclrs, input);
        } else {
            throw new UnsupportedOperationException("Unknown type of Accumulate.");
        }
    } finally {
        context.popExprPointer();
        this.context.popScope();
    }
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) HashSet(java.util.HashSet)

Example 13 with BaseDescr

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

the class FromCollectVisitor method trasformFromCollectToCollectList.

public void trasformFromCollectToCollectList(PatternDescr pattern, CollectDescr collectDescr) {
    // The inner pattern of the "from collect" needs to be processed to have the binding
    final PatternDescr collectDescrInputPattern = collectDescr.getInputPattern();
    if (!parentVisitor.initPattern(collectDescrInputPattern)) {
        return;
    }
    final AccumulateDescr accumulateDescr = new AccumulateDescr();
    accumulateDescr.setInputPattern(collectDescrInputPattern);
    accumulateDescr.addFunction("collectList", null, false, new String[] { collectDescrInputPattern.getIdentifier() });
    final PatternDescr transformedPatternDescr = new PatternDescr(pattern.getObjectType(), pattern.getIdentifier());
    for (BaseDescr o : pattern.getConstraint().getDescrs()) {
        transformedPatternDescr.addConstraint(o);
    }
    transformedPatternDescr.setSource(accumulateDescr);
    transformedPatternDescr.accept(parentVisitor);
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr)

Example 14 with BaseDescr

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

the class OrVisitor method visit.

public void visit(ConditionalElementDescr descr) {
    final MethodCallExpr ceDSL = createDslTopLevelMethod(OR_CALL);
    context.addExpression(ceDSL);
    for (BaseDescr subDescr : descr.getDescrs()) {
        final MethodCallExpr andDSL = createDslTopLevelMethod(AND_CALL);
        context.setNestedInsideOr(true);
        context.pushExprPointer(andDSL::addArgument);
        subDescr.accept(modelGeneratorVisitor);
        context.popExprPointer();
        ceDSL.addArgument(andDSL);
        context.setNestedInsideOr(false);
    }
    for (String k : context.getBindingOr()) {
        if (context.getBindingOr().sizeFor(k) != descr.getDescrs().size()) {
            context.getUnusableOrBinding().add(k);
        }
    }
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 15 with BaseDescr

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

the class MVELDialect method analyzeExpression.

public static AnalysisResult analyzeExpression(final PackageBuildContext context, final BaseDescr descr, final Object content, final BoundIdentifiers availableIdentifiers, final Map<String, Class<?>> localTypes) {
    AnalysisResult result = null;
    // the following is required for proper error handling
    BaseDescr temp = context.getParentDescr();
    context.setParentDescr(descr);
    try {
        result = MVELExprAnalyzer.analyzeExpression(context, (String) content, availableIdentifiers, localTypes, "drools", KnowledgeHelper.class);
    } catch (final Exception e) {
        AsmUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to determine the used declarations.\n" + e.getMessage()));
    } finally {
        // setting it back to original parent descr
        context.setParentDescr(temp);
    }
    return result;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) BaseDescr(org.drools.drl.ast.descr.BaseDescr) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) IOException(java.io.IOException)

Aggregations

BaseDescr (org.drools.drl.ast.descr.BaseDescr)117 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)23 AndDescr (org.drools.drl.ast.descr.AndDescr)18 AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)17 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)16 PatternDescr (org.drools.drl.ast.descr.PatternDescr)15 AnnotatedBaseDescr (org.drools.drl.ast.descr.AnnotatedBaseDescr)14 BindingDescr (org.drools.drl.ast.descr.BindingDescr)13 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)11 Test (org.junit.Test)10 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)9 DeclareDescrBuilder (org.drools.drl.ast.dsl.DeclareDescrBuilder)9 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)8 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)7 OrDescr (org.drools.drl.ast.descr.OrDescr)6 AccumulateDescrBuilder (org.drools.drl.ast.dsl.AccumulateDescrBuilder)6 AnnotatedDescrBuilder (org.drools.drl.ast.dsl.AnnotatedDescrBuilder)6 AnnotationDescrBuilder (org.drools.drl.ast.dsl.AnnotationDescrBuilder)6 AttributeDescrBuilder (org.drools.drl.ast.dsl.AttributeDescrBuilder)6 BehaviorDescrBuilder (org.drools.drl.ast.dsl.BehaviorDescrBuilder)6