Drools runtime engine not working in IntelliJ IDEA
I am using IntelliJ IDEA Ultimate 17.2.
I tried running several demo projects of Drools, they compile and run but the DRL files in the resource folder are not being processed.
The result set is unchanged as though the rule is not being fired. If I run the exact same code in Eclipse, it works as expected.
I verified that IntelliJ is able to locate the DRL file by dynamically loading it as follows:
kbuilder.add(ResourceFactory.newClassPathResource("booking.drl", BookingTest.class), ResourceType.DRL);
Altering the path results in a runtime exception, so it appears as though the Drools Runtime Engine does not work in IntelliJ.
Other attempts involved creating a project with Drools 6.2 Framework, altering the Maven dependency for drools-compiler to versions 6.2.0.Final and 7.1.0.Final.
src/main/java.com.sample.BookingTest:
package com.sample;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
/**
* This is a sample class to launch a rule.
*/
public class BookingTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// go !
Booking booking = new Booking();
booking.setDiscount(true);
booking.setPrice(50);
System.out.println("Initial price: " + booking.getPrice());
kSession.insert(booking);
kSession.fireAllRules();
System.out.println("New price: " + booking.getPrice());
} catch (Throwable t) {
t.printStackTrace();
}
}
public static class Booking {
boolean discount;
int price;
public boolean applyDiscount() {
return discount;
}
public void setDiscount(boolean discount) {
this.discount = discount;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
}
}
src/main/resources/booking.drl:
package com.sample
import com.sample.BookingTest.Booking;
rule "Apply discount"
when
b : Booking( applyDiscount()==true )
then
System.out.println("Apply discount rule fired");
b.setPrice(60);
end
src/main/resources/META-INF/kmodule.xml:
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules" packages="rules">
<ksession name="ksession-rules"/>
</kbase>
</kmodule>
Eclipse output:
Initial price: 50
Apply discount rule fired
New price: 60
IntelliJ Idea output:
Initial price: 50
New price: 50
Any ideas?
Please sign in to leave a comment.
Please file a bug at https://youtrack.jetbrains.com/issues/IDEA and attach a reproducible test case.
Thanks Serge.
After importing the working Eclipse project into IntelliJ I was able to find the problem and resolve it.
The drl file was located in the /resources folder whereas it should have been placed in /resources/rules.
Bruce,
I am setting up drools in my Scala+Spark application using IntelliJ. Can you share the list of configurations/installations you have done to setup to execute the above sample code. Any other tips/tricks which might be of help to me will be highly appreciated.
Cheers!
Abida.
Hello Abida,
Have you implemented Scala + Spark application using IntelliJ ? I am trying to use like above Scala + Spark + Drools in intellij . Any comments really appreciable.