Arjun A J
Arjun A J

Reputation: 436

Integrating KIE workbench from a Java application

I'm trying to integrate Drools kie workbench with a Java application. I'm Using jboss-as-7.1.1.Final as my workbench. Here is the rule definition:

package adeveloperdairy.admission;
rule "set isEligible"
ruleflow-group "sample"
lock-on-active true
when
    $p : Student(gpa > 2.0)
then
    System.out.println("GPA is greater than 2..");
    $p.setEligible(true);
end

Here is my Java code:

    String url = "http://localhost:8080/kie-wb-distribution-wars-6.2.0.Final-as7/maven2/adeveloperdiary/Admission/1.0/Admission-1.0.jar";

    String userpassword = "user4:456";
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection();
    String authEnc = new Base64Encoder().encode(userpassword.getBytes());
    http.setRequestProperty("Authorization", "Basic "+ authEnc);
    InputStream is = http.getInputStream();
    KieModule kModule = kr.addKieModule(ks.getResources().newInputStreamResource(is));

    KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId());
    System.out.println(kModule.getReleaseId().toString());

    KieBase kBase = kContainer.getKieBase();
    System.out.println(kBase.getKiePackages());
    System.out.println(kBase.getKiePackage("adeveloperdiary.admission").getRules());
    KieSession kSession = kBase.newKieSession();

    Student s = new Student();
    s.setName("Raj");
    s.setGpa(5.0f); 
    kSession.insert(s);
    kSession.fireAllRules();

Here is the Student POJO class:

 package adeveloperdiary.admission;

 /**
 * This class was automatically generated by the data modeler tool.
 */

 public class Student implements java.io.Serializable
{

 static final long serialVersionUID = 1L;

 private java.lang.String name;
 private java.lang.Float gpa;
 private java.lang.Boolean eligible;

 public Student()
 {
 }

 public java.lang.String getName()
 {
  return this.name;
 }

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

 public java.lang.Float getGpa()
 {
  return this.gpa;
 }

 public void setGpa(java.lang.Float gpa)
 {
  this.gpa = gpa;
 }

 public java.lang.Boolean getEligible()
 {
  return this.eligible;
 }

 public void setEligible(java.lang.Boolean eligible)
 {
  this.eligible = eligible;
 }

 public Student(java.lang.String name, java.lang.Float gpa,
     java.lang.Boolean eligible)
 {
  this.name = name;
  this.gpa = gpa;
  this.eligible = eligible;
 }

}

I'm able to get the Maven deployment - adeveloperdiary:Admission:1.0, package - [Package name=adeveloperdiary.admission] as well as the rule - [Rule name=set isEligible, agendaGroup=MAIN, salience=0, no-loop=false].

But it's not able to execute the rule. May be I'm missing something here.

Upvotes: 1

Views: 1706

Answers (2)

Naved Alam
Naved Alam

Reputation: 827

I see you are having problem in using the POJO class in your kie workbench. Here are the step to import a simple POJO in workbench and use the same in rule. This has been tested last tested to work with 6.4.0.FINAL version

Adding external POJO model to the rules

The POJO model class that is needed by the workbench project need to be exported as a maven jar and uploaded to the workbench. If not maven jar, you could export it to a simple jar. If you are using eclipse, it's a simple export.

Uploading the POJO model class jar

  • In kie workbench, go to Authoring -> Artifact Repository -> Upload -> Browse to the Jar and upload
  • If Jar does not contain a pom.xml file, you will be asked to manually enter a group and artifact id.

Adding model to the Project

  • Navigate to the project -> Open Project Editor
  • In project editor, from the dropdown, select Dependencies.
  • Add from repository -> Select the model class jar file.

Adding and using model class in Rules.

  • Open guided rule editor -> Navigate to the ‘Data Object’ tab.
  • New item -> Choose a type.
  • The type can be then used in rule editor.

Upvotes: 2

laune
laune

Reputation: 31290

Omit the ruleflow-group "sample" from your DRL code and try again.

You can test whether correct rules would fire by using a single (!) rule

rule "hello"
when
then
    System.out.println( "hello" );
end

If this doesn't fire, you have a problem in your setup. If it fires, you have a problem in your rule(s).

Upvotes: 0

Related Questions