Search in sources :

Example 1 with UnknownType

use of org.drools.javaparser.ast.type.UnknownType in project drools by kiegroup.

the class AccumulateVisitor method buildConstraintExpression.

protected Expression buildConstraintExpression(Expression expr, Collection<String> usedDeclarations) {
    LambdaExpr lambdaExpr = new LambdaExpr();
    lambdaExpr.setEnclosingParameters(true);
    usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
    lambdaExpr.setBody(new ExpressionStmt(expr));
    return lambdaExpr;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) JavaParser(org.drools.javaparser.JavaParser) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) DrlxParseUtil.toType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toType) AccumulateUtil(org.drools.compiler.rule.builder.util.AccumulateUtil) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DrlxParseUtil.forceCastForName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.forceCastForName) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Statement(org.drools.javaparser.ast.stmt.Statement) PackageModel(org.drools.modelcompiler.builder.PackageModel) 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) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Optional(java.util.Optional) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AbstractExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.AbstractExpressionBuilder) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) DrlxParseUtil.rescopeNamesToNewScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.rescopeNamesToNewScope) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) IoUtils(org.drools.core.util.IoUtils) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) StringUtil(org.drools.modelcompiler.util.StringUtil) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) ClassOrInterfaceDeclaration(org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) VariableDeclarator(org.drools.javaparser.ast.body.VariableDeclarator) IOException(java.io.IOException) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) CompilationUnit(org.drools.javaparser.ast.CompilationUnit) Type(org.drools.javaparser.ast.type.Type) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) Collectors.toList(java.util.stream.Collectors.toList) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) Modifier(org.drools.javaparser.ast.Modifier) UnknownType(org.drools.javaparser.ast.type.UnknownType) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt)

Example 2 with UnknownType

use of org.drools.javaparser.ast.type.UnknownType in project drools by kiegroup.

the class Consequence method executeCall.

private MethodCallExpr executeCall(BlockStmt ruleVariablesBlock, BlockStmt ruleConsequence, Collection<String> verifiedDeclUsedInRHS, MethodCallExpr onCall) {
    boolean requireDrools = rewriteRHS(ruleVariablesBlock, ruleConsequence);
    MethodCallExpr executeCall = new MethodCallExpr(onCall, EXECUTE_CALL);
    LambdaExpr executeLambda = new LambdaExpr();
    executeCall.addArgument(executeLambda);
    executeLambda.setEnclosingParameters(true);
    if (requireDrools) {
        executeLambda.addParameter(new Parameter(new UnknownType(), "drools"));
    }
    verifiedDeclUsedInRHS.stream().map(x -> new Parameter(new UnknownType(), x)).forEach(executeLambda::addParameter);
    executeLambda.setBody(ruleConsequence);
    return executeCall;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) BitMask(org.drools.model.BitMask) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseUtil.isNameExprWithName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isNameExprWithName) DroolsImpl(org.drools.modelcompiler.consequence.DroolsImpl) JavaParser(org.drools.javaparser.JavaParser) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) BREAKING_CALL(org.drools.modelcompiler.builder.generator.visitor.NamedConsequenceVisitor.BREAKING_CALL) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) Collectors.toSet(java.util.stream.Collectors.toSet) PackageModel(org.drools.modelcompiler.builder.PackageModel) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) DrlxParseUtil.hasScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.hasScope) Parameter(org.drools.javaparser.ast.body.Parameter) ClassUtils(org.drools.core.util.ClassUtils) Collection(java.util.Collection) DrlxParseUtil.parseBlock(org.drools.modelcompiler.builder.generator.DrlxParseUtil.parseBlock) Set(java.util.Set) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) 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) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) DrlxParseUtil.findAllChildrenRecursive(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findAllChildrenRecursive) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Modifier(org.drools.javaparser.ast.Modifier) SimpleName(org.drools.javaparser.ast.expr.SimpleName) StringUtils(org.drools.core.util.StringUtils) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 3 with UnknownType

use of org.drools.javaparser.ast.type.UnknownType in project drools by kiegroup.

the class DrlxParseUtil method generateLambdaWithoutParameters.

public static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr, boolean skipFirstParamAsThis) {
    if (skipFirstParamAsThis && usedDeclarations.isEmpty()) {
        return expr;
    }
    LambdaExpr lambdaExpr = new LambdaExpr();
    lambdaExpr.setEnclosingParameters(true);
    if (!skipFirstParamAsThis) {
        lambdaExpr.addParameter(new Parameter(new UnknownType(), "_this"));
    }
    usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
    lambdaExpr.setBody(new ExpressionStmt(expr));
    return lambdaExpr;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) Expression(org.drools.javaparser.ast.expr.Expression) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) Optional.of(java.util.Optional.of) JavaParser(org.drools.javaparser.JavaParser) Operator(org.drools.javaparser.ast.expr.BinaryExpr.Operator) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ConstraintType(org.drools.core.util.index.IndexUtil.ConstraintType) ClassUtil(org.drools.modelcompiler.util.ClassUtil) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) PrimitiveType(org.drools.javaparser.ast.type.PrimitiveType) Method(java.lang.reflect.Method) LongLiteralExpr(org.drools.javaparser.ast.expr.LongLiteralExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) Parameter(org.drools.javaparser.ast.body.Parameter) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) IndexUtil(org.drools.core.util.index.IndexUtil) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) NodeWithSimpleName(org.drools.javaparser.ast.nodeTypes.NodeWithSimpleName) ClassUtil.findMethod(org.drools.modelcompiler.util.ClassUtil.findMethod) StringUtils(org.drools.core.util.StringUtils) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) ArrayAccessExpr(org.drools.javaparser.ast.expr.ArrayAccessExpr) NodeWithTraversableScope(org.drools.javaparser.ast.nodeTypes.NodeWithTraversableScope) CastExpr(org.drools.javaparser.ast.expr.CastExpr) CharLiteralExpr(org.drools.javaparser.ast.expr.CharLiteralExpr) Deque(java.util.Deque) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) DrlxParser(org.drools.drlx.DrlxParser) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) LinkedList(java.util.LinkedList) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) ClassUtils(org.drools.core.util.ClassUtils) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Field(java.lang.reflect.Field) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Type(org.drools.javaparser.ast.type.Type) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) DoubleLiteralExpr(org.drools.javaparser.ast.expr.DoubleLiteralExpr) NodeWithOptionalScope(org.drools.javaparser.ast.nodeTypes.NodeWithOptionalScope) Collections(java.util.Collections) Node(org.drools.javaparser.ast.Node) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt)

Example 4 with UnknownType

use of org.drools.javaparser.ast.type.UnknownType in project drools by kiegroup.

the class FlowExpressionBuilder method buildIndexedBy.

private MethodCallExpr buildIndexedBy(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
    if (!hasIndex(drlxParseResult)) {
        return exprDSL;
    }
    TypedExpression left = drlxParseResult.getLeft();
    TypedExpression right = drlxParseResult.getRight();
    Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
    ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
    // not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
    FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), drlxParseResult.getDecodeConstraintType().toString());
    LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
    indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
    boolean leftContainsThis = left.getExpression().toString().contains("_this");
    indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
    MethodCallExpr indexedByDSL = new MethodCallExpr(exprDSL, INDEXED_BY_CALL);
    indexedByDSL.addArgument(indexedBy_indexedClass);
    indexedByDSL.addArgument(indexedBy_constraintType);
    indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
    indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
    Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
    if (isAlphaIndex(usedDeclarations)) {
        indexedByDSL.addArgument(right.getExpression());
    } else {
        LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
        indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
        indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
        indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
    }
    return indexedByDSL;
}
Also used : LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) UnknownType(org.drools.javaparser.ast.type.UnknownType) Objects(java.util.Objects) Parameter(org.drools.javaparser.ast.body.Parameter) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 5 with UnknownType

use of org.drools.javaparser.ast.type.UnknownType in project drools by kiegroup.

the class PatternExpressionBuilder method generateLambdaWithoutParameters.

public Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr, boolean skipFirstParamAsThis) {
    if (skipFirstParamAsThis && usedDeclarations.isEmpty()) {
        return expr;
    }
    LambdaExpr lambdaExpr = new LambdaExpr();
    lambdaExpr.setEnclosingParameters(true);
    if (!skipFirstParamAsThis) {
        lambdaExpr.addParameter(new Parameter(new UnknownType(), "_this"));
    }
    usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
    lambdaExpr.setBody(new ExpressionStmt(expr));
    return lambdaExpr;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) QueryGenerator(org.drools.modelcompiler.builder.generator.QueryGenerator) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) Optional.of(java.util.Optional.of) JavaParser(org.drools.javaparser.JavaParser) ArrayList(java.util.ArrayList) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) 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) IndexUtil(org.drools.core.util.index.IndexUtil) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Stream(java.util.stream.Stream) Optional(java.util.Optional) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt)

Aggregations

Parameter (org.drools.javaparser.ast.body.Parameter)6 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)6 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)6 NameExpr (org.drools.javaparser.ast.expr.NameExpr)6 UnknownType (org.drools.javaparser.ast.type.UnknownType)6 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 List (java.util.List)4 Objects (java.util.Objects)4 JavaParser (org.drools.javaparser.JavaParser)4 Expression (org.drools.javaparser.ast.expr.Expression)4 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)4 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)4 Optional (java.util.Optional)3 IndexUtil (org.drools.core.util.index.IndexUtil)3 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)3 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)3 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)3 Optional.of (java.util.Optional.of)2