Search in sources :

Example 6 with DrlxParseResult

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

the class PatternDSLPattern method buildConstraint.

private void buildConstraint(PatternDescr pattern, Class<?> patternType, PatternConstraintParseResult patternConstraintParseResult) {
    DrlxParseResult drlxParseResult1 = patternConstraintParseResult.getDrlxParseResult();
    String expression = patternConstraintParseResult.getExpression();
    drlxParseResult1.accept(drlxParseResult -> {
        DSLNode constraint;
        if (drlxParseResult.getExpr() instanceof OOPathExpr) {
            constraint = new ConstraintOOPath(context, packageModel, pattern, patternType, patternConstraintParseResult, expression, drlxParseResult);
        } else {
            constraint = new PatternDSLSimpleConstraint(context, pattern, drlxParseResult);
        }
        constraint.buildPattern();
    });
}
Also used : DSLNode(org.drools.modelcompiler.builder.generator.visitor.DSLNode) OOPathExpr(org.drools.javaparser.ast.drlx.OOPathExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)

Example 7 with DrlxParseResult

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

the class EvalVisitor method visit.

public void visit(EvalDescr descr) {
    String expression = descr.getContent().toString();
    DrlxParseResult drlxParseResult = new ConstraintParser(context, packageModel).drlxParse(null, null, expression);
    drlxParseResult.accept(drlxParseSuccess -> {
        Expression rewriteExprAsLambdaWithoutThisParam = DrlxParseUtil.generateLambdaWithoutParameters(drlxParseSuccess.getUsedDeclarations(), drlxParseSuccess.getExpr(), true);
        // rewrites the DrlxParserResult expr as directly the lambda to use
        drlxParseSuccess.setExpr(rewriteExprAsLambdaWithoutThisParam);
        drlxParseSuccess.setStatic(true);
        new FlowExpressionBuilder(context).processExpression(drlxParseSuccess);
    });
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) FlowExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder)

Example 8 with DrlxParseResult

use of org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult 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 = new MethodCallExpr(parentExpression, callMethod);
    final String condition = desc.getCondition().toString();
    if (!condition.equals("true")) {
        // Default case
        when.addArgument(new StringLiteralExpr(context.getConditionId(patternType, condition)));
        when.addArgument(new NameExpr(toVar(patternRelated.getIdentifier())));
        DrlxParseResult parseResult = new ConstraintParser(context, packageModel).drlxParse(patternType, patternRelated.getIdentifier(), condition);
        parseResult.accept(parseSuccess -> when.addArgument(generateLambdaWithoutParameters(Collections.emptySortedSet(), parseSuccess.getExpr())));
    }
    MethodCallExpr then = new MethodCallExpr(when, THEN_CALL);
    MethodCallExpr rhs = onDSL(desc.getConsequence());
    then.addArgument(rhs);
    return then;
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 9 with DrlxParseResult

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

the class AccumulateVisitor method visit.

protected Optional<AccumulateVisitorPatternDSL.NewBinding> visit(RuleContext context, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr accumulateDSL, PatternDescr basePattern, boolean inputPatternHasConstraints) {
    context.pushExprPointer(accumulateDSL::addArgument);
    final MethodCallExpr functionDSL = new MethodCallExpr(null, "accFunction");
    final String expression = function.getParams()[0];
    final Expression expr = DrlxParseUtil.parseExpression(expression).getExpr();
    final String bindingId = Optional.ofNullable(function.getBind()).orElse(basePattern.getIdentifier());
    Optional<AccumulateVisitorPatternDSL.NewBinding> newBinding = Optional.empty();
    if (expr instanceof BinaryExpr) {
        final DrlxParseResult parseResult = new ConstraintParser(context, packageModel).drlxParse(Object.class, bindingId, expression);
        newBinding = parseResult.acceptWithReturnValue(new ParseResultVisitor<Optional<AccumulateVisitorPatternDSL.NewBinding>>() {

            @Override
            public Optional<AccumulateVisitorPatternDSL.NewBinding> onSuccess(DrlxParseSuccess drlxParseResult) {
                final AccumulateFunction accumulateFunction = AccumulateVisitor.this.getAccumulateFunction(function, drlxParseResult.getExprType());
                final String bindExpressionVariable = context.getExprId(accumulateFunction.getResultType(), drlxParseResult.getLeft().toString());
                drlxParseResult.setExprBinding(bindExpressionVariable);
                context.addDeclarationReplacing(new DeclarationSpec(drlxParseResult.getPatternBinding(), drlxParseResult.getExprType()));
                functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
                final MethodCallExpr newBindingFromBinary = AccumulateVisitor.this.buildBinding(bindExpressionVariable, drlxParseResult.getUsedDeclarations(), drlxParseResult.getExpr());
                context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, drlxParseResult.getExprType()));
                functionDSL.addArgument(new NameExpr(toVar(bindExpressionVariable)));
                return Optional.of(new AccumulateVisitorPatternDSL.NewBinding(Optional.empty(), newBindingFromBinary));
            }

            @Override
            public Optional<AccumulateVisitorPatternDSL.NewBinding> onFail(DrlxParseFail failure) {
                return Optional.empty();
            }
        });
    } else if (expr instanceof MethodCallExpr) {
        final DrlxParseUtil.RemoveRootNodeResult methodCallWithoutRootNode = DrlxParseUtil.removeRootNode(expr);
        final String rootNodeName = getRootNodeName(methodCallWithoutRootNode);
        final TypedExpression typedExpression = parseMethodCallType(context, rootNodeName, methodCallWithoutRootNode.getWithoutRootNode());
        final Class<?> methodCallExprType = typedExpression.getType();
        final AccumulateFunction accumulateFunction = getAccumulateFunction(function, methodCallExprType);
        final Class accumulateFunctionResultType = accumulateFunction.getResultType();
        functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
        // Every expression in an accumulate function gets transformed in a bind expression with a generated id
        // Then the accumulate function will have that binding expression as a source
        final String bindExpressionVariable = context.getExprId(accumulateFunctionResultType, typedExpression.toString());
        Expression withThis = DrlxParseUtil.prepend(DrlxParseUtil._THIS_EXPR, typedExpression.getExpression());
        DrlxParseSuccess result = new DrlxParseSuccess(accumulateFunctionResultType, "", rootNodeName, withThis, accumulateFunctionResultType).setLeft(typedExpression).setExprBinding(bindExpressionVariable);
        final MethodCallExpr binding = expressionBuilder.buildBinding(result);
        newBinding = Optional.of(new AccumulateVisitorPatternDSL.NewBinding(Optional.of(result.getPatternBinding()), binding));
        context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, methodCallExprType));
        functionDSL.addArgument(new NameExpr(toVar(bindExpressionVariable)));
        context.addDeclarationReplacing(new DeclarationSpec(bindingId, accumulateFunctionResultType));
    } else if (expr instanceof NameExpr) {
        final Class<?> declarationClass = context.getDeclarationById(expr.toString()).orElseThrow(RuntimeException::new).getDeclarationClass();
        final String nameExpr = ((NameExpr) expr).getName().asString();
        final AccumulateFunction accumulateFunction = getAccumulateFunction(function, declarationClass);
        functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
        functionDSL.addArgument(new NameExpr(toVar(nameExpr)));
        Class accumulateFunctionResultType = accumulateFunction.getResultType();
        if (accumulateFunctionResultType == Comparable.class && (Comparable.class.isAssignableFrom(declarationClass) || declarationClass.isPrimitive())) {
            accumulateFunctionResultType = declarationClass;
        }
        context.addDeclarationReplacing(new DeclarationSpec(bindingId, accumulateFunctionResultType));
    } else {
        throw new UnsupportedOperationException("Unsupported expression " + expr);
    }
    final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, "as");
    asDSL.addArgument(new NameExpr(toVar(bindingId)));
    accumulateDSL.addArgument(asDSL);
    context.popExprPointer();
    return newBinding;
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 10 with DrlxParseResult

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

the class PatternDSL method parseConstraint.

PatternConstraintParseResult parseConstraint(PatternDescr pattern, Class<?> patternType, BaseDescr constraint) {
    final boolean isPositional = isPositional(constraint);
    final String patternIdentifier = pattern.getIdentifier();
    String expression = getConstraintExpression(patternType, constraint, isPositional);
    int unifPos = expression.indexOf(":=");
    if (unifPos > 0) {
        expression = expression.substring(unifPos + 2).trim() + " == " + expression.substring(0, unifPos).trim();
    }
    final DrlxParseResult drlxParseResult = new ConstraintParser(context, packageModel).drlxParse(patternType, patternIdentifier, expression, isPositional);
    return new PatternConstraintParseResult(expression, patternIdentifier, drlxParseResult);
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)

Aggregations

DrlxParseResult (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult)10 ConstraintParser (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser)7 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)5 Expression (org.drools.javaparser.ast.expr.Expression)4 NameExpr (org.drools.javaparser.ast.expr.NameExpr)4 OOPathExpr (org.drools.javaparser.ast.drlx.OOPathExpr)3 DrlxParseSuccess (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess)3 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)2 FlowExpressionBuilder (org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder)2 DSLNode (org.drools.modelcompiler.builder.generator.visitor.DSLNode)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)1 OOPathChunk (org.drools.javaparser.ast.drlx.OOPathChunk)1 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)1 BooleanLiteralExpr (org.drools.javaparser.ast.expr.BooleanLiteralExpr)1 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)1 DrlxParseFail (org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail)1