Please Help to clear a error while changing from Drools 7 compiler to drools 8 compiler

125 views
Skip to first unread message

Murugappan krishna

unread,
May 9, 2024, 11:19:23 AMMay 9
to Drools Usage
So I had created a code which ran successfully with drools 7 compiler. Due to a requirement I change the compiler to drools 8 where I got some error. Can anyone please help to solve this as I need this fix badly thank you further down I have attached the code which   ran fine on drools  seven and thrown this error in drools  8.
This is my java file 
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 DroolsTest {

    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 !
            Message message = new Message();
            message.setMessage("Hello World");
            message.setStatus(Message.HELLO);
            kSession.insert(message);
            kSession.fireAllRules();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static class Message {

        public static final int HELLO = 0;
        public static final int GOODBYE = 1;

        private String message;

        private int status;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

    }

}

this was my .drl file

package com.sample.rules

import com.sample.DroolsTest.Message;

rule "Hello World"

when

m : Message( status == Message.HELLO, myMessage : message )

then

System.out.println( myMessage );

m.setMessage( "Goodbye cruel world" );

m.setStatus( Message.GOODBYE );

update( m );

end


rule "GoodBye"

when

Message( status == Message.GOODBYE, myMessage : message )

then

System.out.println( myMessage );

end

this is my .kmodule file

<?xml version="1.0" encoding="UTF-8"?>

<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">

<kbase name="rules" packages="com.sample.rules">

<ksession name="ksession-rules"/>

</kbase>

<kbase name="dtables" packages="com.sample.dtables">

<ksession name="ksession-dtables"/>

</kbase>

<kbase name="process" packages="com.sample.process">

<ksession name="ksession-process"/>

</kbase>

</kmodule>


this the error which i got

java.lang.RuntimeException: Cannot find a default KieSession

at org.drools.compiler.kie.builder.impl.KieContainerImpl.findKieSessionModel(KieContainerImpl.java:557)

at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:600)

at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:530)

at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:513)

at com.sample.DroolsTest.main(DroolsTest.java:17)

can any one plese help me resolve this error please

Murugappan krishna

unread,
May 9, 2024, 11:27:08 AMMay 9
to Drools Usage

Cannot invoke "org.kie.api.runtime.KieSession.insert(Object)" because "kSession" is null

at com.sample.DroolsTest.main(DroolsTest.java:23)

i get this two errors some one please help 

Richard Bourner

unread,
May 9, 2024, 9:49:21 PMMay 9
to Drools Usage
Add `drools-xml-support` and `drools-mvel` to your pom. There should be something in the exception trace about that.

Murugappan krishna

unread,
May 9, 2024, 10:30:47 PMMay 9
to Drools Usage


thank you so much for the reply sir  but i wasn't able to find any pom.xml file in my project structure i have attached my project structure so could you please help me.the project was created as a new sample drools project form eclipse IDE and this the project stsructure that i gotScreenshot 2024-05-10 at 7.53.17 AM.png

Richard Bourner

unread,
May 9, 2024, 11:10:19 PMMay 9
to Drools Usage
Since you do not seem to be using Maven (which you should BTW), you will find the list of libraries in the "Drools Library" section. 
And the same way you added what is already in it, add the 2 which I mentioned earlier.

Murugappan krishna

unread,
May 9, 2024, 11:32:33 PMMay 9
to Drools Usage

So I should get the jar files from the maven repository and put in the drools library right sir ? 

Murugappan krishna

unread,
May 10, 2024, 12:45:10 AMMay 10
to Drools Usage
So I did install the dependencies and this is what I got can you help with this please 

java.util.ServiceConfigurationError: org.drools.base.base.XMLSupport: org.drools.xml.support.XMLSupportImpl Unable to get public no-arg constructor

  at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:586)

  at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:679)

  at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1240)

  at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1273)

  at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1309)

  at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1393)

  at org.kie.api.internal.utils.KieService.load(KieService.java:44)

  at org.drools.base.base.XMLSupport$Holder.<clinit>(XMLSupport.java:38)

  at org.drools.base.base.XMLSupport.get(XMLSupport.java:42)

  at org.drools.compiler.kproject.models.KieModuleModelImpl.fromXML(KieModuleModelImpl.java:151)

  at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:201)

  at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:163)

  at org.drools.compiler.kie.builder.impl.ClasspathKieProject.discoverKieModules(ClasspathKieProject.java:131)

  at org.drools.compiler.kie.builder.impl.ClasspathKieProject.init(ClasspathKieProject.java:97)

  at org.drools.compiler.kie.builder.impl.KieContainerImpl.<init>(KieContainerImpl.java:140)

  at org.drools.compiler.kie.builder.impl.KieContainerImpl.<init>(KieContainerImpl.java:150)

  at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieClasspathContainer(KieServicesImpl.java:141)

  at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieClasspathContainer(KieServicesImpl.java:131)

  at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:102)

  at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:80)

  at com.sample.DroolsTest.main(DroolsTest.java:16)

Caused by: java.lang.NoClassDefFoundError: com/thoughtworks/xstream/io/HierarchicalStreamDriver

  at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)

  at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3373)

  at java.base/java.lang.Class.getConstructor0(Class.java:3578)

  at java.base/java.lang.Class.getConstructor(Class.java:2271)

  at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:666)

  at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:663)

  at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)

  at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:674)

  ... 19 more

Caused by: java.lang.ClassNotFoundException: com.thoughtworks.xstream.io.HierarchicalStreamDriver

  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)

  at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)

  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)

  ... 27 more


On Friday 10 May 2024 at 08:40:19 UTC+5:30 ria...@gmail.com wrote:

Murugappan krishna

unread,
May 10, 2024, 1:36:48 AMMay 10
to Drools Usage

So i did installed some more dependencies following the errors 

Attachment.jpeg

Right now it runs fine but the rules dint execute 

I sysout some thing in the Java file to check this is what I got 

Attachment.jpeg

Please do help me 


Gabriele Cardosi

unread,
May 10, 2024, 2:50:40 AMMay 10
to Drools Usage
Hi Murugappan,
sorry, but it's pretty hard to fully understand what's going on based only on pieces of stacktrace and images.
First, I would suggest to create and share a reproducer (i.e. put the project on some public repository so that someone may be able to give it a try).
Then, if not already in place, use maven to solve dependencies (it has been mentioned before, so I'm not sure about that).
Last, I notice:


Caused by: java.lang.ClassNotFoundException: com.thoughtworks.xstream.io.HierarchicalStreamDriver

  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)

  at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)

  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)

  ... 27 more

Did you alredy fix it ?

Best

Gabriele

Murugappan krishna

unread,
May 10, 2024, 12:07:38 PMMay 10
to Drools Usage
Thank So much for your help sir very grate full for it as you said i have made a project using maven 
The code runs fine now but the rules are not getting executed couldn't figure it why could any one please help me out 
                                            gh repo clone Murugappankrishna/Drools8Trial

int rulesfired= kieSession1.fireAllRules();
this always was zero 



 Screenshot 2024-05-10 at 7.53.17 AM.png

this was my java code 
package org.example;

import java.util.Scanner;

import org.kie.api.KieBase;

import org.kie.api.KieServices;

import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;




/**
* This is a sample class to launch a rule.
*/
public class Measurement {


public static final void main(String[] args) {
try {
// load up the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieBase kBase1 = kContainer.getKieBase("rules");
KieSession kieSession1 = kContainer.newKieSession("ksession-rules");
Calculator calc=new Calculator();
calc.setOperator();
calc.setNumber1(9);
calc.setNumber2(10);
kieSession1.insert(calc);

int rulesfired= kieSession1.fireAllRules();
System.out.println("number of rules fired "+rulesfired);

} catch (Throwable t) {
t.printStackTrace();
}
}


public static class Calculator{
private String operator;
private int number1;
private int number2;
Scanner ip = new Scanner(System.in);
public String getOperator() {
return operator;
}
public void setOperator() {
System.out.println("Enter the operation to be performed (+ or - or * or / )");
String operator = ip.next();
this.operator=operator;


}
public int getNumber1() {
return number1;
}
public void setNumber1(int number1) {
this.number1 = number1;
}
public int getNumber2() {
return number2;
}
public void setNumber2(int number2) {
this.number2 = number2;
}




}


}
This was my .drl file
package com.sample

import org.example.Measurement.Calculator;

rule "Addition"

when
c : Calculator( operator == "+", numone : number1, numtwo : number2 )
then
System.out.println( numone + numtwo );

System.out.println( "rule for addition executed" );
end
rule "Subraction"

when
c : Calculator( operator == "-", numone : number1, numtwo : number2 )
then
if(numone>numtwo){
System.out.println( numone - numtwo );


}
else{

System.out.println( "give appropriate values " );

}
System.out.println( "rule for Subraction executed" );
end

rule "Multiplication"

when
c : Calculator( operator == "*", numone : number1, numtwo : number2 )
then

System.out.println( numone * numtwo );




System.out.println( "rule for Multiplication executed" );
end

rule "Division"

when
c : Calculator( operator == "/", numone : number1, numtwo : number2 )
then

System.out.println( numone / numtwo );




System.out.println( "rule for Division executed" );
end




This was my kmodule.xml
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.drools.org/xsd/kmodule">

<kbase name="rules" default="true" packages="org.example">

<ksession name="ksession-rules" default="true"/>
</kbase>

</kmodule>
is there any fix please help me out i need it badly please

and this is the output that i got
Enter the operation to be performed (+ or - or * or /  )
+(user input)
number of rules fired 0

Process finished with exit code 0

Gabriele Cardosi

unread,
May 13, 2024, 4:40:50 AMMay 13
to Drools Usage
Hi Murugappan,
I would like to help, but I cloned your repo, and find out:
1. the drools version is 9.44.0.Final (since you are talking of drools 8, I would expect 8.44.2.Final)
2. the project need some fix (it does not compile without slight modification in pom: I had to define the maven-surefire-plugin version)
3. the unit test is commented and refer to some code not currently existing.

So, I would suggest you to
1. check your reproducible, so that anyone can download, start and debug it
2. verify that the reproducer actually reproduce the issue

Thanks

Gabriele

Murugappan krishna

unread,
May 13, 2024, 2:14:31 PMMay 13
to Drools Usage
Thank so much sir
like i don't want a unit test in my file i just wanted to run directly(like a print statement in the .drl file as the rule gets fired)(that why i commented out the test file) 
i hadn't noticed any thing as you mentioned in the second point 
i was ok with anything equal and above 8 
my issue is that no rules fired  could you  please help me with that 
and without any unit test and like the way how i mention above is there any drools project which i can modify and it works out of the box if so please give me 
Reply all
Reply to author
Forward
0 new messages