How to integrate KIE Workbench 6.1.0.Final (Drools Guvnor) Project with Java Application

1,840 views
Skip to first unread message

Ashraf Syed

unread,
Dec 24, 2014, 8:52:56 AM12/24/14
to drools...@googlegroups.com
Hello All,

I am using KIE Workbench 6.1.0.Final and my Eclipse project where I am trying to execute rules is also 6.1.0 Runtime enabled.

I like to know how I can execute/load rules created using Drools Guvnor, from my java application. Before posting this question, I have looked at the following similar questions and tried the given approaches but none of them worked for me.


I am using KIE Workbench 6.1.0.Final and my Eclipse project where I am trying to execute rules is also 6.1.0 Runtime enabled.

I like to know how I can execute/load rules created using Drools Guvnor, from my java application. Before posting this question, I have looked at the following similar questions and tried the given approaches but none of them worked for me.

Integrating Drool 6 work bench with Java Application

why does loading Drools 6 KIE JAR into code fail?

Loading Drools/KIE Workbench artifacts directly from the repository

KIE Workbench Integration Responds with 401

I have followed the steps given here click this to create a project and defined the rule. I am able to build and deploy the rules. I have tried the following code to load the projects but all fails with different exception. Pls note that I am able to access the workbench jar from the browser.

Approach 1.

public void load1(){
   
KieServices kieServices = KieServices.Factory.get();
   
ReleaseId releaseId = kieServices.newReleaseId( "com.example.rule", "RuleProject", "1.0" );
   
KieContainer kContainer = kieServices.newKieContainer( releaseId );
   
KieBaseConfiguration kieBaseConf = kieServices.newKieBaseConfiguration();
    kieBaseConf
.setOption( EventProcessingOption.STREAM );
   
KieBase kBase = kContainer.newKieBase(kieBaseConf);
   
for ( KiePackage a : kBase.getKiePackages()) {
       
for (Rule r : a.getRules()) {
           
System.out.println("KiePackage {} Rule {}"+ new Object[]{a.getName()+"-"+ r.getName()});
       
}
   
}
}
Enter code here...

Exception

java.lang.RuntimeException: Cannot find KieModule: com.example.rule:RuleProject:1.0 at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:99)

Approach 2.

public void load2(){            
   
String url = "http://example.com/drools/maven2/com/example/rule/RuleProject/1.0/RuleProject-1.0.jar";
   
ReleaseIdImpl releaseId = new ReleaseIdImpl("com.example.rule", "RuleProject", "1.0");
   
KieServices kieServices = KieServices.Factory.get();
    kieServices
.getResources().newUrlResource(url);
   
KieContainer kieContainer = kieServices.newKieContainer(releaseId);    
   
// check every 5 seconds if there is a new version at the URL
   
KieScanner kieScanner = kieServices.newKieScanner(kieContainer);
    kieScanner
.start(5000L);
   
// alternatively:
   
// kieScanner.scanNow();
   
Scanner scanner = new Scanner(System.in);
   
while (true) {
       
//runRule(kieContainer);
       
System.out.println("Press enter in order to run the test again....");
        scanner
.nextLine();
   
}
}
Enter code here...

Exception

java.lang.RuntimeException: Cannot find KieModule: com.example.rule:RuleProject:1.0 at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:99)

Approach 3.

public void load3() throws IOException{         
   
String url = "http://example.com/drools/maven2/com/example/rule/RuleProject/1.0/RuleProject-1.0.jar";
   
KieServices ks = KieServices.Factory.get();
   
KieRepository kr = ks.getRepository();
   
UrlResource urlResource = (UrlResource) ks.getResources()
           
.newUrlResource(url);
    urlResource
.setUsername("admin");
    urlResource
.setPassword("password");
    urlResource
.setBasicAuthentication("enabled");
   
InputStream is = urlResource.getInputStream();
   
KieModule kModule = kr.addKieModule(ks.getResources()
           
.newInputStreamResource(is));
   
KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId());

    kContainer
.newStatelessKieSession();        

}
Enter code here...

Exception

java.io.IOException: Server returned HTTP response code: 401 for URL: http://example.com/drools/maven2/com/example/rule/RuleProject/1.0/RuleProject-1.0.jar
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at org.drools.core.io.impl.UrlResource.grabStream(UrlResource.java:257)
    at org.drools.core.io.impl.UrlResource.getInputStream(UrlResource.java:174)

Here is my workbench jar pom.xml
 <?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
<modelVersion>4.0.0</modelVersion>
 
<groupId>com.example.rule</groupId>
 
<artifactId>RuleProject</artifactId>
 
<version>1.0</version>
 
<packaging>kjar</packaging>
 
<name>RuleProject</name>
 
<repositories>
   
<repository>
     
<id>guvnor-m2-repo</id>
     
<name>Guvnor M2 Repo</name>
     
<url>http://localhost:8080/drools/maven2/</url>
   
</repository>
 
</repositories>
 
<build>
   
<plugins>
     
<plugin>
       
<groupId>org.kie</groupId>
       
<artifactId>kie-maven-plugin</artifactId>
       
<version>6.1.0.Final</version>
       
<extensions>true</extensions>
     
</plugin>
   
</plugins>
 
</build>
</project>
Enter code here...

 

Here is my java application pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
<modelVersion>4.0.0</modelVersion>
 
<groupId>xcp.drool</groupId>
 
<artifactId>xcpdrool</artifactId>
 
<version>1</version>
 
<repositories>
   
<repository>
     
<releases>
       
<enabled>true</enabled>
       
<updatePolicy>always</updatePolicy>
     
</releases>
     
<id>guvnor-m2-repo</id>
     
<name>Guvnor M2 Repo</name>
     
<url>http://localhost:8080/drools/maven2/</url>
   
</repository>
   
<repository>
     
<snapshots>
       
<enabled>false</enabled>
     
</snapshots>
     
<id>central</id>
     
<name>Central Repository</name>
     
<url>http://repo.maven.apache.org/maven2</url>
   
</repository>
 
</repositories>
 
<pluginRepositories>
   
<pluginRepository>
     
<releases>
       
<updatePolicy>never</updatePolicy>
     
</releases>
     
<snapshots>
       
<enabled>false</enabled>
     
</snapshots>
     
<id>central</id>
     
<name>Central Repository</name>
     
<url>http://repo.maven.apache.org/maven2</url>
   
</pluginRepository>
 
</pluginRepositories>
 
<build>
   
<sourceDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\src\main\java</sourceDirectory>
   
<scriptSourceDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\src\main\scripts</scriptSourceDirectory>
   
<testSourceDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\src\test\java</testSourceDirectory>
   
<outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\classes</outputDirectory>
   
<testOutputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\test-classes</testOutputDirectory>
   
<resources>
     
<resource>
       
<directory>C:\p4_xb\xCP-Drool\xCPDrool1\src\main\resources</directory>
     
</resource>
   
</resources>
   
<testResources>
     
<testResource>
       
<directory>C:\p4_xb\xCP-Drool\xCPDrool1\src\test\resources</directory>
     
</testResource>
   
</testResources>
   
<directory>C:\p4_xb\xCP-Drool\xCPDrool1\target</directory>
   
<finalName>xcpdrool-1</finalName>
   
<pluginManagement>
     
<plugins>
       
<plugin>
         
<artifactId>maven-antrun-plugin</artifactId>
         
<version>1.3</version>
       
</plugin>
       
<plugin>
         
<artifactId>maven-assembly-plugin</artifactId>
         
<version>2.2-beta-5</version>
       
</plugin>
       
<plugin>
         
<artifactId>maven-dependency-plugin</artifactId>
         
<version>2.1</version>
       
</plugin>
       
<plugin>
         
<artifactId>maven-release-plugin</artifactId>
         
<version>2.0</version>
       
</plugin>
     
</plugins>
   
</pluginManagement>
   
<plugins>
     
<plugin>
       
<artifactId>maven-clean-plugin</artifactId>
       
<version>2.4.1</version>
       
<executions>
         
<execution>
           
<id>default-clean</id>
           
<phase>clean</phase>
           
<goals>
             
<goal>clean</goal>
           
</goals>
         
</execution>
       
</executions>
     
</plugin>
     
<plugin>
       
<artifactId>maven-install-plugin</artifactId>
       
<version>2.3.1</version>
       
<executions>
         
<execution>
           
<id>default-install</id>
           
<phase>install</phase>
           
<goals>
             
<goal>install</goal>
           
</goals>
         
</execution>
       
</executions>
     
</plugin>
     
<plugin>
       
<artifactId>maven-resources-plugin</artifactId>
       
<version>2.5</version>
       
<executions>
         
<execution>
           
<id>default-resources</id>
           
<phase>process-resources</phase>
           
<goals>
             
<goal>resources</goal>
           
</goals>
         
</execution>
         
<execution>
           
<id>default-testResources</id>
           
<phase>process-test-resources</phase>
           
<goals>
             
<goal>testResources</goal>
           
</goals>
         
</execution>
       
</executions>
     
</plugin>
     
<plugin>
       
<artifactId>maven-surefire-plugin</artifactId>
       
<version>2.10</version>
       
<executions>
         
<execution>
           
<id>default-test</id>
           
<phase>test</phase>
           
<goals>
             
<goal>test</goal>
           
</goals>
         
</execution>
       
</executions>
     
</plugin>
     
<plugin>
       
<artifactId>maven-compiler-plugin</artifactId>
       
<version>2.3.2</version>
       
<executions>
         
<execution>
           
<id>default-testCompile</id>
           
<phase>test-compile</phase>
           
<goals>
             
<goal>testCompile</goal>
           
</goals>
         
</execution>
         
<execution>
           
<id>default-compile</id>
           
<phase>compile</phase>
           
<goals>
             
<goal>compile</goal>
           
</goals>
         
</execution>
       
</executions>
     
</plugin>
     
<plugin>
       
<artifactId>maven-jar-plugin</artifactId>
       
<version>2.3.2</version>
       
<executions>
         
<execution>
           
<id>default-jar</id>
           
<phase>package</phase>
           
<goals>
             
<goal>jar</goal>
           
</goals>
         
</execution>
       
</executions>
     
</plugin>
     
<plugin>
       
<artifactId>maven-deploy-plugin</artifactId>
       
<version>2.7</version>
       
<executions>
         
<execution>
           
<id>default-deploy</id>
           
<phase>deploy</phase>
           
<goals>
             
<goal>deploy</goal>
           
</goals>
         
</execution>
       
</executions>
     
</plugin>
     
<plugin>
       
<artifactId>maven-site-plugin</artifactId>
       
<version>3.0</version>
       
<executions>
         
<execution>
           
<id>default-site</id>
           
<phase>site</phase>
           
<goals>
             
<goal>site</goal>
           
</goals>
           
<configuration>
             
<outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
             
<reportPlugins>
               
<reportPlugin>
                 
<groupId>org.apache.maven.plugins</groupId>
                 
<artifactId>maven-project-info-reports-plugin</artifactId>
               
</reportPlugin>
             
</reportPlugins>
           
</configuration>
         
</execution>
         
<execution>
           
<id>default-deploy</id>
           
<phase>site-deploy</phase>
           
<goals>
             
<goal>deploy</goal>
           
</goals>
           
<configuration>
             
<outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
             
<reportPlugins>
               
<reportPlugin>
                 
<groupId>org.apache.maven.plugins</groupId>
                 
<artifactId>maven-project-info-reports-plugin</artifactId>
               
</reportPlugin>
             
</reportPlugins>
           
</configuration>
         
</execution>
       
</executions>
       
<configuration>
         
<outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
         
<reportPlugins>
           
<reportPlugin>
             
<groupId>org.apache.maven.plugins</groupId>
             
<artifactId>maven-project-info-reports-plugin</artifactId>
           
</reportPlugin>
         
</reportPlugins>
       
</configuration>
     
</plugin>
   
</plugins>
 
</build>
 
<reporting>
   
<outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
 
</reporting>
 
<profiles>
   
<profile>
     
<activation>
       
<activeByDefault>true</activeByDefault>
     
</activation>
     
<repositories>
       
<repository>
         
<releases>
           
<enabled>true</enabled>
           
<updatePolicy>always</updatePolicy>
         
</releases>
         
<id>guvnor-m2-repo</id>
         
<name>Guvnor M2 Repo</name>
         
<url>http://localhost:8080/drools/maven2/</url>
       
</repository>
     
</repositories>
   
</profile>
 
</profiles>
</project>
Enter code here...






Michael Anstis

unread,
Dec 24, 2014, 9:10:01 AM12/24/14
to drools...@googlegroups.com

For option #3 you will need to configure a User ID and password in your Maven settings.xml file. The User and password should be for a workbench user with the admin role as the workbench Maven repository requires authentication. I suspect when downloading the JAR from the browser you have already been authenticated for the session.

Sent on the move

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/5d27e6fa-c1fb-42bc-b9e1-d0046f4106ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ashraf Syed

unread,
Dec 24, 2014, 9:52:03 AM12/24/14
to drools...@googlegroups.com
Thanks Michael for a quick reply. I will try your suggestion and let you know.

Can you also help me fixing approach1 which is more close to my usecase ?

thanks again.

--
You received this message because you are subscribed to a topic in the Google Groups "Drools Usage" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/drools-usage/jSjj925b96o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards
Ashraf Ali Syed.
Email : ashra...@ymail.com
Phone : +91-9980914348

Michael Anstis

unread,
Dec 24, 2014, 10:15:25 AM12/24/14
to drools...@googlegroups.com

To be honest, from what I know about KieScanner, I'd have expected options 1, 2 and 3 to work as you have the workbench repository configured in your consuming application's pom. 1 and 2 might need the credentials configured in settings.xml - perhaps the error reporting us misleading.

@mfusco.. any ideas?

Sent on the move

Mark Proctor

unread,
Dec 24, 2014, 11:39:33 AM12/24/14
to drools...@googlegroups.com
Look up settings.xml for maven both in drools and maven docs. There you will see how to add remote repositories and set login/pass.

Mark

Drools User

unread,
Jan 12, 2015, 5:42:56 AM1/12/15
to drools...@googlegroups.com
Hello Ashraf,
Were you able to fix issue with any approach? I am facing similar issues and I have also tried all possiblities.
...

Ashish Wekhande

unread,
Jan 14, 2015, 1:13:33 PM1/14/15
to drools...@googlegroups.com
Change your eclipse runtime to 6.0 0 and keep the workbench to 6.1.0 final. This should work with your approach 3. However you won’t be able to use KieScanner.

Kunal Ransing

unread,
Sep 29, 2015, 5:41:51 AM9/29/15
to Drools Usage
Hi,
I am facing problem for same thing(using approach 3) except I am using 6.3.0 Final & simple java web project.
I opened post: Drool 6.2 - Failed to run rule using loading kie project artifact jar in "non-maven java web project
I tried with 6.3.0Final But get error java.lang.RuntimeException: Cannot find KieModule: com.eno:Test1:LATEST.
Please see my post for more detail. Please help me to solve it.

Sasha S

unread,
Jan 17, 2017, 10:43:29 AM1/17/17
to Drools Usage
KieScanner can't be used in approaches 2,3 because the scanner check only repositories listed in settings.xml file (placed at default location user_home/.m2/settings.xml or customized  by use kie.maven.settings.custom System property). In other side, once you provided correct settings.xml file with
repository url, there is not needed to specify the jar's url since Drools does it itself and better (it takes in account Ranged versions, LATEST flag and other) then the described above trivial direct resolution of GAV (groupId:ArtifactId:version) to an Url.

Resume:
Approach 1 is preferable once settings.xml supported correctly, since the scanner will also work.
Approaches 2,3 will work for only one-time artifact loading and it is not correct to use scanner here.

Tested with Drools 6.4.0.Final, with Artifactory remote repository and rather with just folder on disk which contains my artifacts (has correct Maven structure) 

среда, 24 декабря 2014 г., 15:52:56 UTC+2 пользователь Ashraf Syed написал:

Sasha S

unread,
Jan 17, 2017, 11:13:30 AM1/17/17
to Drools Usage
1. create settings xml like this:

<?xml version='1.0' encoding='UTF-8'?>
<settings xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd' xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
 
<profiles>
   
<profile>
     
<id>DefaultProfile</id>

     
<activation>
       
<activeByDefault>true</activeByDefault>
     
</activation>
     
<repositories>
       
<repository>

         
<id>kieProjects</id>
         
<name>Kie Projects</name>
         
<url>http://your_host:your_port/artifactory/libs-release-local</url>
         
<layout>default</layout>
       
</repository>
     
</repositories>
   
</profile>
 
</profiles>
</settings>

You may add server with the same id as repository to provide authentication credentials, you also may use some local folder as url ("file:/C:/some_local_repositoryfolder"). In your case it will probably "http://example.com/drools/maven2"


2. pass settings.xml file's path as following system property kie.maven.settings.custom to you application.

3. The Code:

import org.kie.api.KieBase
import org.kie.api.KieServices
import org.kie.api.builder.KieRepository
import org.kie.api.builder.KieScanner
import org.kie.api.runtime.KieContainer
import org.kie.api.runtime.KieSession
. . .
KieServices ks = KieServices.Factory.get();
// Creates virtual local repository
KieRepository kieRepository = ks.getRepository();

ReleaseId releaseId = kieServices.newReleaseId( "com.example.rule", "RuleProject", "1.0" );
KieBase kieBase = null;
try {
 KieContainer kieContainer = ks.newKieContainer(releaseId);

 // check every 5 seconds if there is a new version at the URL
 KieScanner kieScanner = ks.newKieScanner(kieContainer);
 //kieScanner.scanNow();
 kieBase = kieContainer.getKieBase();
 kieScanner.start(5000L);
 
 
} catch (Exception e) {
 // Failed to find required KieArtifact
}
 . . .
 
KieSession kieSession
try{
 kieSession = kieBase.newKieSession();
 kieSession.insert("some fact");
 kieSession.fireAllRules();
}
finally {
 if(kieSession) {
  kieSession.dispose();
 }
}

Good Luck!

вторник, 17 января 2017 г., 17:43:29 UTC+2 пользователь Sasha S написал:

Kunal Ransing

unread,
Jan 18, 2017, 1:09:42 AM1/18/17
to Drools Usage
Thanks for reply but I already implemented using setting.xml & for runtime update using version as com.eno:xyz:1-SNAPSHOT.
Reply all
Reply to author
Forward
0 new messages