Получение неправильного формата класса в Drools при использовании статического метода в RHS-части файла drool - PullRequest
0 голосов
/ 26 декабря 2018

В настоящее время я пишу правило для проверки дубликатов записей сотрудника.У меня есть EmployeeCache для хранения данных сотрудника в Map как идентификатор сотрудника в качестве ключа и объект Employee в качестве значения, когда запись не найдена.

Я использую версию drools engine 5.6 и имею JDK 1.8 и Springboot 2.5 и использование Intellij IDE версии 2018.

Ниже приведены зависимости, которые я использую,

     <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>5.6.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>5.6.0.Final</version>
    </dependency>

Ниже приведены мои файлы классов и файл правил,

//Класс pojo сотрудников,

public class Employee {
private String name;
private Double salary;
private String department;
private Integer id;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Double getSalary() {
    return salary;
}

public void setSalary(Double salary) {
    this.salary = salary;
}

public String getDepartment() {
    return department;
}

public void setDepartment(String department) {
    this.department = department;
}

}

// Класс обслуживания EmployeeCache,

public class EmployeeCache {

private static Map<Integer,Employee>  employeeDetails = new HashMap<>();


public static void setEmployeeDetails(Integer empId, Employee employee){
    employeeDetails.put(empId,employee);
}

public static Map<Integer,Employee> getEmployeeDetails(){return employeeDetails;}
}

Это мое основное приложение для весенней загрузки, в котором я указал базы знаний и KnowledgeBuilderобработать правило.

@SpringBootApplication
public class Springmicroservice1Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Springmicroservice1Application.class);
Logger logger = LoggerFactory.getLogger(Springmicroservice1Application.class);

public static void main(String args[]) {
    SpringApplication.run(Springmicroservice1Application.class);

    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    // this will parse and compile in one step
    kbuilder.add(ResourceFactory.newClassPathResource("testRule.drl", Employee.class), ResourceType.DRL);
    // Check the builder for errors
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors().toString());
        throw new RuntimeException("Unable to compile \"accountValidation.drl\".");
    }
    // get the compiled packages (which are serializable)
    final Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
    // add the packages to a KnowledgeBase (deploy the knowledge packages).
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(pkgs);
    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    Employee employee = new Employee();
    employee.setDepartment("Computer Science");
    employee.setName("Nageswara rao");
    employee.setSalary(30000.00);
    employee.setId(471088);

    ksession.insert(employee);

    ksession.fireAllRules();
 }

Это мой файл правил,

import com.example.springmicroservice1.ruleClasses.temp.Employee
import com.example.springmicroservice1.ruleClasses.temp.EmployeeCache;


rule "Duplicate account validation"
when

$employee : Employee($id : id);
then
    if(EmployeeCache.getEmployeeDetails().containsKey($id)){
        if(EmployeeCache.getEmployeeDetails().get($id).getDepartment().equalsIgnoreCase($employee.getDepartment()) &&
           EmployeeCache.getEmployeeDetails().get($id).getName().equalsIgnoreCase($employee.getName() )){
            System.out.println("Duplicate data");
        }
    }else {
        Employee employee = new Employee();
        employee.setSalary($employee.getSalary());
        employee.setName($employee.getName());
        employee.setDepartment($employee.getDepartment());
        employee.setId($employee.getId());
        EmployeeCache.setEmployeeDetails($employee.getId(),employee);
    }
 end

Когда я запускаю этот проект, я получаю ошибку ниже,

Exception in thread "main" java.lang.RuntimeException: wrong class format
at org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:263)
at org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:203)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:102)
at org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding.resolve(UnresolvedReferenceBinding.java:49)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.java:122)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getTypeFromTypeSignature(LookupEnvironment.java:1188)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getTypeFromVariantTypeSignature(LookupEnvironment.java:1244)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getTypeArgumentsFromSignature(LookupEnvironment.java:1031)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getTypeFromTypeSignature(LookupEnvironment.java:1193)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.createMethod(BinaryTypeBinding.java:495)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.createMethods(BinaryTypeBinding.java:577)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.cachePartsFrom(BinaryTypeBinding.java:327)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createBinaryTypeFrom(LookupEnvironment.java:640)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createBinaryTypeFrom(LookupEnvironment.java:619)
at org.eclipse.jdt.internal.compiler.Compiler.accept(Compiler.java:295)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:107)
at org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding.resolve(UnresolvedReferenceBinding.java:49)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.java:122)
at org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding.resolve(ParameterizedTypeBinding.java:851)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.java:100)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveTypesFor(BinaryTypeBinding.java:1016)
at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.getExactMethod(BinaryTypeBinding.java:771)
at org.eclipse.jdt.internal.compiler.lookup.Scope.findExactMethod(Scope.java:893)
at org.eclipse.jdt.internal.compiler.lookup.Scope.getMethod(Scope.java:2234)
at org.eclipse.jdt.internal.compiler.ast.MessageSend.resolveType(MessageSend.java:401)
at org.eclipse.jdt.internal.compiler.ast.MessageSend.resolveType(MessageSend.java:314)
at org.eclipse.jdt.internal.compiler.ast.Expression.resolveTypeExpecting(Expression.java:943)
at org.eclipse.jdt.internal.compiler.ast.IfStatement.resolve(IfStatement.java:254)
at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolveStatements(AbstractMethodDeclaration.java:447)
at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.resolveStatements(MethodDeclaration.java:189)
at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolve(AbstractMethodDeclaration.java:406)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1131)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1219)
at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.resolve(CompilationUnitDeclaration.java:528)
at org.eclipse.jdt.internal.compiler.Compiler.process(Compiler.java:759)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:464)
at org.drools.commons.jci.compilers.EclipseJavaCompiler.compile(EclipseJavaCompiler.java:389)
at org.drools.commons.jci.compilers.AbstractJavaCompiler.compile(AbstractJavaCompiler.java:49)
at org.drools.rule.builder.dialect.java.JavaDialect.compileAll(JavaDialect.java:410)
at org.drools.compiler.DialectCompiletimeRegistry.compileAll(DialectCompiletimeRegistry.java:46)
at org.drools.compiler.PackageRegistry.compileAll(PackageRegistry.java:103)
at org.drools.compiler.PackageBuilder.compileAll(PackageBuilder.java:1196)
at org.drools.compiler.PackageBuilder.compileAllRules(PackageBuilder.java:949)
at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:938)
at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:470)
at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:698)
at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:51)
at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:40)
at com.example.springmicroservice1.Springmicroservice1Application.main(Springmicroservice1Application.java:46)
Caused by: org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
at org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader.<init>(ClassFileReader.java:372)
at org.drools.commons.jci.compilers.EclipseJavaCompiler$2.createNameEnvironmentAnswer(EclipseJavaCompiler.java:287)
at org.drools.commons.jci.compilers.EclipseJavaCompiler$2.findType(EclipseJavaCompiler.java:258)

Я искал в google и stackoverflow, но не нашел точного следа для решения этой проблемы.

Заранее спасибо!

...