Search in sources :

Example 1 with DrlxParseSuccess

use of org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess in project drools by kiegroup.

the class OOPathExprGenerator method toPatternExpr.

private void toPatternExpr(String bindingId, List<DrlxParseResult> list) {
    MethodCallExpr patternExpr = new MethodCallExpr(null, "pattern");
    patternExpr.addArgument(toVar(bindingId));
    for (DrlxParseResult drlx : list) {
        if (drlx instanceof DrlxParseSuccess) {
            MethodCallExpr expr = (MethodCallExpr) expressionBuilder.buildExpressionWithIndexing(((DrlxParseSuccess) drlx));
            expr.setScope(patternExpr);
            patternExpr = expr;
        }
    }
    context.addExpression(patternExpr);
}
Also used : DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with DrlxParseSuccess

use of org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess in project drools by kiegroup.

the class OOPathExprGenerator method toFlowExpr.

private void toFlowExpr(String bindingId, List<DrlxParseResult> list) {
    final List<DrlxParseSuccess> value = list.stream().filter(r -> r instanceof DrlxParseSuccess).map(DrlxParseSuccess.class::cast).collect(Collectors.toList());
    if (value.size() == 1) {
        context.addExpression(expressionBuilder.buildExpressionWithIndexing(value.get(0)));
    } else {
        final MethodCallExpr andDSL = new MethodCallExpr(null, "and");
        value.forEach(e -> {
            final Expression expression = expressionBuilder.buildExpressionWithIndexing(e);
            andDSL.addArgument(expression);
        });
        context.addExpression(andDSL);
    }
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 3 with DrlxParseSuccess

use of org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess in project drools by kiegroup.

the class FlowExpressionBuilder method buildExpression.

private MethodCallExpr buildExpression(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
    final List<String> usedDeclarationsWithUnification = new ArrayList<>();
    if (drlxParseResult.isPatternBindingUnification()) {
        usedDeclarationsWithUnification.add(drlxParseResult.getPatternBinding());
    } else {
        if (drlxParseResult.getPatternBinding() != null && !drlxParseResult.getUsedDeclarations().contains(drlxParseResult.getPatternBinding())) {
            exprDSL.addArgument(new NameExpr(toVar(drlxParseResult.getPatternBinding())));
        }
    }
    usedDeclarationsWithUnification.addAll(drlxParseResult.getUsedDeclarations());
    usedDeclarationsWithUnification.stream().filter(s -> !(drlxParseResult.isSkipThisAsParam() && s.equals(drlxParseResult.getPatternBinding()))).map(x -> QueryGenerator.substituteBindingWithQueryParameter(context, x)).forEach(exprDSL::addArgument);
    exprDSL.addArgument(buildConstraintExpression(drlxParseResult, drlxParseResult.getExpr()));
    return exprDSL;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.ModelGenerator.BIND_AS_CALL) QueryGenerator(org.drools.modelcompiler.builder.generator.QueryGenerator) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) JavaParser(org.drools.javaparser.JavaParser) ArrayList(java.util.ArrayList) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DrlxParseUtil.generateLambdaWithoutParameters(org.drools.modelcompiler.builder.generator.DrlxParseUtil.generateLambdaWithoutParameters) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) Parameter(org.drools.javaparser.ast.body.Parameter) Collection(java.util.Collection) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Objects(java.util.Objects) UnknownType(org.drools.javaparser.ast.type.UnknownType) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Stream(java.util.stream.Stream) ArrayList(java.util.ArrayList) NameExpr(org.drools.javaparser.ast.expr.NameExpr)

Example 4 with DrlxParseSuccess

use of org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess in project drools by kiegroup.

the class PatternExpressionBuilder method buildExpressionWithIndexing.

@Override
public MethodCallExpr buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
    if (drlxParseResult instanceof MultipleDrlxParseSuccess) {
        MultipleDrlxParseSuccess multi = (MultipleDrlxParseSuccess) drlxParseResult;
        MethodCallExpr exprDSL = new MethodCallExpr(null, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_OR_CALL : EXPR_AND_CALL);
        for (DrlxParseSuccess child : multi.getResults()) {
            MethodCallExpr childExpr = buildExpressionWithIndexing(child);
            childExpr.setScope(exprDSL);
            exprDSL = childExpr;
            if (child instanceof SingleDrlxParseSuccess && child.getExprBinding() != null) {
                SingleDrlxParseSuccess singleDrlxChild = (SingleDrlxParseSuccess) child;
                context.addDeclaration(child.getExprBinding(), singleDrlxChild.getLeftExprRawClass());
                Expression dslExpr = buildBinding(singleDrlxChild);
                context.addExpression(dslExpr);
            }
        }
        return new MethodCallExpr(exprDSL, multi.getOperator() == BinaryExpr.Operator.OR ? EXPR_END_OR_CALL : EXPR_END_AND_CALL);
    }
    return buildSingleExpressionWithIndexing((SingleDrlxParseSuccess) drlxParseResult);
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) MultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) MultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 5 with DrlxParseSuccess

use of org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess in project drools by kiegroup.

the class PatternDSL method addNullSafeExpr.

private void addNullSafeExpr(ConstraintParser constraintParser, String patternIdentifier, List<PatternConstraintParseResult> patternConstraintParseResults) {
    final List<Expression> nullSafeExpressions = patternConstraintParseResults.stream().flatMap(r -> r.getDrlxParseResult().acceptWithReturnValue(new ParseResultVisitor<List<Expression>>() {

        @Override
        public List<Expression> onSuccess(DrlxParseSuccess t) {
            return t.getNullSafeExpressions();
        }

        @Override
        public List<Expression> onFail(DrlxParseFail failure) {
            return Collections.emptyList();
        }
    }).stream()).collect(Collectors.toList());
    List<Expression> newNullSafeExpressions = reverseDistinct(nullSafeExpressions);
    newNullSafeExpressions.forEach(expr -> {
        String nullSafeExpression = printNode(expr);
        DrlxParseResult nullSafeExpressionParsed = constraintParser.drlxParse(patternType, patternIdentifier, nullSafeExpression, false);
        patternConstraintParseResults.add(0, new PatternConstraintParseResult(nullSafeExpression, patternIdentifier, nullSafeExpressionParsed));
    });
}
Also used : DrlxParseUtil.validateDuplicateBindings(org.drools.modelcompiler.builder.generator.DrlxParseUtil.validateDuplicateBindings) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AggregateKey(org.drools.modelcompiler.builder.generator.AggregateKey) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) PatternSourceDescr(org.drools.drl.ast.descr.PatternSourceDescr) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) DrlxParseUtil.getPatternListenedProperties(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getPatternListenedProperties) PatternDescr(org.drools.drl.ast.descr.PatternDescr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) GENERATED_VARIABLE_PREFIX(org.drools.model.impl.VariableImpl.GENERATED_VARIABLE_PREFIX) ConstraintUtil(org.drools.modelcompiler.builder.generator.ConstraintUtil) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) NamesGenerator.generateName(org.drools.model.impl.NamesGenerator.generateName) Expression(com.github.javaparser.ast.expr.Expression) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) PackageModel(org.drools.modelcompiler.builder.PackageModel) WindowReferenceGenerator(org.drools.modelcompiler.builder.generator.WindowReferenceGenerator) ClassUtils(org.drools.core.util.ClassUtils) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) PatternBuilder.lookAheadFieldsOfIdentifier(org.drools.compiler.rule.builder.PatternBuilder.lookAheadFieldsOfIdentifier) StreamUtils.optionalToStream(org.drools.modelcompiler.util.StreamUtils.optionalToStream) FromVisitor(org.drools.modelcompiler.builder.generator.visitor.FromVisitor) Collectors(java.util.stream.Collectors) BaseDescr(org.drools.drl.ast.descr.BaseDescr) Objects(java.util.Objects) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) DSLNode(org.drools.modelcompiler.builder.generator.visitor.DSLNode) ParseResultVoidVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVoidVisitor) Watch(org.kie.api.definition.rule.Watch) Optional(java.util.Optional) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) Collections(java.util.Collections) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) ConstraintExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail)

Aggregations

DrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess)11 DrlxParseResult (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)8 SingleDrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess)7 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)5 ArrayList (java.util.ArrayList)5 ConstraintParser (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser)5 DrlxParseFail (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail)5 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)4 Expression (com.github.javaparser.ast.expr.Expression)4 Collection (java.util.Collection)4 List (java.util.List)4 Objects (java.util.Objects)4 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)4 BaseDescr (org.drools.drl.ast.descr.BaseDescr)4 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)4 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)4 DrlxParseUtil (org.drools.modelcompiler.builder.generator.DrlxParseUtil)4 ParseResultVisitor (org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor)4 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3