Search in sources :

Example 6 with ClassExpr

use of org.drools.javaparser.ast.expr.ClassExpr in project drools by kiegroup.

the class ModelGenerator method addUnitData.

private static void addUnitData(String unitVar, Class<?> type, BlockStmt ruleBlock) {
    Type declType = classToReferenceType(type);
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(UnitData.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(unitVar), Modifier.FINAL);
    MethodCallExpr unitDataCall = new MethodCallExpr(null, UNIT_DATA_CALL);
    unitDataCall.addArgument(new ClassExpr(declType));
    unitDataCall.addArgument(new StringLiteralExpr(unitVar));
    AssignExpr var_assign = new AssignExpr(var_, unitDataCall, AssignExpr.Operator.ASSIGN);
    ruleBlock.addStatement(var_assign);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) UnitData(org.drools.model.UnitData) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Example 7 with ClassExpr

use of org.drools.javaparser.ast.expr.ClassExpr in project drools by kiegroup.

the class ModelGenerator method addVariable.

private static void addVariable(KnowledgeBuilderImpl kbuilder, BlockStmt ruleBlock, DeclarationSpec decl) {
    if (decl.getDeclarationClass() == null) {
        kbuilder.addBuilderResult(new UnknownDeclarationError(decl.getBindingId()));
        return;
    }
    Type declType = classToReferenceType(decl.getDeclarationClass());
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(Variable.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(decl.getBindingId()), Modifier.FINAL);
    MethodCallExpr declarationOfCall = new MethodCallExpr(null, DECLARATION_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(decl.getType()));
    declarationOfCall.addArgument(new StringLiteralExpr(decl.getVariableName().orElse(decl.getBindingId())));
    decl.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
    decl.getEntryPoint().ifPresent(ep -> {
        MethodCallExpr entryPointCall = new MethodCallExpr(null, "entryPoint");
        entryPointCall.addArgument(new StringLiteralExpr(ep));
        declarationOfCall.addArgument(entryPointCall);
    });
    for (BehaviorDescr behaviorDescr : decl.getBehaviors()) {
        MethodCallExpr windowCall = new MethodCallExpr(null, "window");
        if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.TIME");
            windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
        }
        if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.LENGTH");
            windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
        }
        declarationOfCall.addArgument(windowCall);
    }
    AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
    ruleBlock.addStatement(var_assign);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) Variable(org.drools.model.Variable) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Example 8 with ClassExpr

use of org.drools.javaparser.ast.expr.ClassExpr in project drools by kiegroup.

the class QueryGenerator method processQueryDef.

public static void processQueryDef(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, QueryDescr queryDescr, boolean isPattern) {
    RuleContext context = new RuleContext(kbuilder, packageModel, queryDescr, typeResolver, isPattern);
    String queryName = queryDescr.getName();
    final String queryDefVariableName = toQueryDef(queryName);
    context.setQueryName(Optional.of(queryDefVariableName));
    parseQueryParameters(context, packageModel, queryDescr);
    ClassOrInterfaceType queryDefType = getQueryType(context.getQueryParameters());
    MethodCallExpr queryCall = new MethodCallExpr(null, QUERY_CALL);
    if (!queryDescr.getNamespace().isEmpty()) {
        queryCall.addArgument(new StringLiteralExpr(queryDescr.getNamespace()));
    }
    queryCall.addArgument(new StringLiteralExpr(queryName));
    for (QueryParameter qp : context.getQueryParameters()) {
        queryCall.addArgument(new ClassExpr(JavaParser.parseType(qp.type.getCanonicalName())));
        queryCall.addArgument(new StringLiteralExpr(qp.name));
    }
    packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
Also used : StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 9 with ClassExpr

use of org.drools.javaparser.ast.expr.ClassExpr 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 10 with ClassExpr

use of org.drools.javaparser.ast.expr.ClassExpr in project drools by kiegroup.

the class PatternExpressionBuilder method buildIndexedBy.

private Optional<MethodCallExpr> buildIndexedBy(DrlxParseSuccess drlxParseResult) {
    if (!hasIndex(drlxParseResult)) {
        return Optional.empty();
    }
    IndexUtil.ConstraintType decodeConstraintType = drlxParseResult.getDecodeConstraintType();
    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"), decodeConstraintType.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(null, drlxParseResult.isBetaNode() ? BETA_INDEXED_BY_CALL : ALPHA_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 if (usedDeclarations.size() == 1) {
        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 Optional.of(indexedByDSL);
}
Also used : IndexUtil(org.drools.core.util.index.IndexUtil) 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)

Aggregations

ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)12 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)12 NameExpr (org.drools.javaparser.ast.expr.NameExpr)7 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)6 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)5 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)5 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)5 Type (org.drools.javaparser.ast.type.Type)5 Parameter (org.drools.javaparser.ast.body.Parameter)4 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)4 Objects (java.util.Objects)3 Expression (org.drools.javaparser.ast.expr.Expression)3 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)3 BlockStmt (org.drools.javaparser.ast.stmt.BlockStmt)3 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)3 UnknownType (org.drools.javaparser.ast.type.UnknownType)3 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2