Search in sources :

Example 1 with GeneratedClassWithPackage

use of org.drools.modelcompiler.builder.GeneratedClassWithPackage in project drools by kiegroup.

the class LegacyAccumulate method createAllAccumulateClass.

private GeneratedClassWithPackage createAllAccumulateClass(Set<String> imports, String packageName) {
    final String allAccumulatesClass = new JavaRuleClassBuilder().buildRule(ruleBuildContext);
    final CompilationUnit parsedAccumulateClass = JavaParser.parse(allAccumulatesClass);
    return new GeneratedClassWithPackage((ClassOrInterfaceDeclaration) parsedAccumulateClass.getType(0), packageName, imports);
}
Also used : CompilationUnit(org.drools.javaparser.ast.CompilationUnit) GeneratedClassWithPackage(org.drools.modelcompiler.builder.GeneratedClassWithPackage) JavaRuleClassBuilder(org.drools.compiler.rule.builder.dialect.java.JavaRuleClassBuilder)

Example 2 with GeneratedClassWithPackage

use of org.drools.modelcompiler.builder.GeneratedClassWithPackage in project drools by kiegroup.

the class LegacyAccumulate method build.

public void build() {
    new PatternBuilder().build(ruleBuildContext, basePattern);
    final Set<String> imports = ruleBuildContext.getPkg().getImports().keySet();
    final String packageName = ruleBuildContext.getPkg().getName();
    GeneratedClassWithPackage generatedClassWithPackage = createAllAccumulateClass(imports, packageName);
    packageModel.addGeneratedAccumulateClasses(generatedClassWithPackage);
    GeneratedClassWithPackage invokerGenerated = createInvokerClass(imports, packageName);
    packageModel.addGeneratedAccumulateClasses(invokerGenerated);
    final String typeWithPackage = String.format("%s.%s", packageName, invokerGenerated.getGeneratedClass().getName().asString());
    final ClassExpr accFunctionName = new ClassExpr(JavaParser.parseType(typeWithPackage));
    final MethodCallExpr accFunctionCall = new MethodCallExpr(null, "accFunction", nodeList(accFunctionName));
    final NameExpr bindingVariable = new NameExpr(toVar(basePattern.getIdentifier()));
    final MethodCallExpr asDSL = new MethodCallExpr(accFunctionCall, "as", nodeList(bindingVariable));
    context.addExpression(asDSL);
}
Also used : GeneratedClassWithPackage(org.drools.modelcompiler.builder.GeneratedClassWithPackage) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) PatternBuilder(org.drools.compiler.rule.builder.PatternBuilder) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 3 with GeneratedClassWithPackage

use of org.drools.modelcompiler.builder.GeneratedClassWithPackage in project drools by kiegroup.

the class LegacyAccumulate method createInvokerClass.

private GeneratedClassWithPackage createInvokerClass(Set<String> imports, String packageName) {
    final String invokerClass = ruleBuildContext.getInvokers().values().iterator().next();
    final CompilationUnit parsedInvokedClass = JavaParser.parse(invokerClass);
    Set<String> allImports = new HashSet<>();
    allImports.addAll(imports);
    allImports.addAll(parsedInvokedClass.getImports().stream().map(importDeclaration -> importDeclaration.getName().toString()).collect(Collectors.toList()));
    return new GeneratedClassWithPackage((ClassOrInterfaceDeclaration) parsedInvokedClass.getType(0), packageName, allImports);
}
Also used : CompilationUnit(org.drools.javaparser.ast.CompilationUnit) GeneratedClassWithPackage(org.drools.modelcompiler.builder.GeneratedClassWithPackage) HashSet(java.util.HashSet)

Aggregations

GeneratedClassWithPackage (org.drools.modelcompiler.builder.GeneratedClassWithPackage)3 CompilationUnit (org.drools.javaparser.ast.CompilationUnit)2 HashSet (java.util.HashSet)1 PatternBuilder (org.drools.compiler.rule.builder.PatternBuilder)1 JavaRuleClassBuilder (org.drools.compiler.rule.builder.dialect.java.JavaRuleClassBuilder)1 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 NameExpr (org.drools.javaparser.ast.expr.NameExpr)1