Not able to run the "Scorecard" rules from Java
ankitd Mar 19, 2013 6:00 AMi belong to a development team, and in our product, we are trying to bring in Drools inplace of Jrules.
We are using Guvnor for creating rules and then trying to run the rules from our application(JAVA).We are able to run and execute the "Simple rules" as well as "Decision tables" from our application.
Now we have requirement of "Scorecard" in our application, which is one the module in the latest release of Drools(5.5.0).
Here is the approach we have taken:
Inside the Guvnor UI
1) Created the Package called org.scorecard.
2) Uploaded the Pojo model jar containg class Person(int:age,String:residence,double:personScore). Validated and saved it.
3) Created the Category "personCategory".
4) Then added the Scorecard rule using as Scorcard(Web Guided Editor) type. Validated and saved it
5) Created the Package snapshot and copied the Url for using it inside the person-changeset.xml file.Here is how it looks:
<?xml version="1.0" encoding="UTF-8"?>
<change-set xmlns="http://drools.org/drools-5.0/change-set"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd">
<add>
<!-- Note: the host (localhost), port (8080) and context root (guvnor) might be different
depending on your app server configuration and war filename. -->
<resource source="http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/org.person/personSnapshot"
type="PKG" basicAuthentication="enabled" username="admin" password="admin" />
</add>
</change-set>
Here is the Java client code that i am using to run the rules.
StatefulKnowledgeSession ksession = null;
KnowledgeBase kbase = readKnowledgeBase();
Person person =new Person();
person.setAge(26);
person.setPlaceOfResidence("City");
ksession = kbase.newStatefulKnowledgeSession();
ksession.insert(person);
ksession.fireAllRules();
String message = "Message is:\n" + person.getPersonScore();
System.out.println(message);
JOptionPane.showMessageDialog(null, message, "Result", JOptionPane.INFORMATION_MESSAGE);
if (ksession != null) {
ksession.dispose();
}
private static KnowledgeBase readKnowledgeBase() {
KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("MortgageAgent");
Resource changeset = ResourceFactory.newClassPathResource(
"org/scorecard/personClassModel/person-changeset.xml");
kagent.applyChangeSet(changeset);
KnowledgeBase kbase = kagent.getKnowledgeBase();
kagent.dispose();
return kbase;
}
At the end of the execuiton, its expected that the personScore attribute of the person object created stroes the final score based on the rules excuted.
Everytime i am getting same value ie. 0.0
we are using tomcat for running the Guvnor.I have deployed the drools_guvnor.war(5.5.0 version) inside the webapps folder.
In the tomacat/lib folder have added the following jars:
antlr-2.7.7.jar
antlr-3.3.jar
antlr-runtime-3.3.jar
log4j-1.2.12.jar
Also my Java class path looks like this.
drools-core-5.5.0.Final.jar
Knowledge-api-5.5.0.Final.jar
Knowledge-internal-api-5.5.0.Final.jar
mvel2-2.1.3.Final.jar
slf4j-api-1.7.2,jar
When running the simple rules and decision tables the application is running smooth and giving the expected o/p.In both the cases we are taking Decalrative model approach and we are creating Facts inside Guvnor itself.
For Scorecard we have to take the "Upload Pojo Model Jar" approach as "Resultant score" field expects a attribute of "double" type and inside the Fact its not possible to create the double type attribute.Even if you try to create a fact attribute
with decimal Number type it does considers it as "Resultant score " field. So the only option left was to upload Pojo model jar.
i want to know where we are goin wron incase of scorecard.
Any help would be appreciated.
Thanks