Search code examples
javadroolsdrools-fusion

Call java method from Drools rules


I want to create a Drools rule that says that if the CPU usage from the data I gather using a HTTP request is over a specific threshold (e.g. 20%) then call a function that lays on the class Netdata which are also passed to Drools Fusion. So what I have written until now is:

rule "CPU usage over 20% once"
    when
        $cpu : Netdata(cpuUsagePerc > 0)
        from entry-point Netdata
    then
        System.out.println("CPU usage is now over 20%");
        $cpu.setCpuOverCount();
end

But the thing is, that I get this error: java.lang.RuntimeException: wrong class format. If I delete this line $cpu.setCpuOverCount(); the rule executes, and prints CPU usage is now over 20% without any problems.

The method I am trying to call is this one:

public void setCpuOverCount() {
    cpuOverCount++;
}

Is there a way to call a method in the first place, and if yes how do I do it? If not, how can I overcome such problems? I am using Drools version 5.1.1.


Solution

  • It could be due to ECJ version not supporting your Java version as per this answer. You can try manually updating the ECJ version:

    <dependency>
      <groupId>org.eclipse.jdt.core.compiler</groupId>
      <artifactId>ecj</artifactId>
      <version>>4.5.1</version> <!-- latest is 4.6.1 -->
    </dependency>