execute specific rule using agenda
by marimuthu.balasubramanian@wipro.com
Hi,
I would like to execute specific rules from the rule file(.xml).
Find the rule file content below. There are three rules are available in
the xml file(Rule names: 1.Check_NotNullof_rfqId ,
2.Check_NotNullof_partId and 3.validateFullName)
First time we are trying to execute 1.Check_NotNullof_rfqId and
2.Check_NotNullof_partId rules. next time we are trying to execute
3.validateFullName rule
We have tried the following option
The rule name start with "Check" require to set only "rfq" global
identifier.
So when we execute first two rules we followed following approach
workingMemory.setGlobal("rfq",rfqbean);
workingMemory.fireAllRules(new RuleNameStartsWithAgendaFilter( "Check"
));
but it gives following NullPointerException. It is trying to execute
validatefullname rule also.
Caused by: java.lang.NullPointerException
at
rules.Rule_Check_NotNullof_rfqId_0.eval0(Rule_Check_NotNullof_rfqId_0.ja
va:9)
at
rules.Rule_Check_NotNullof_rfqId_0Eval0Invoker.evaluate(Rule_Check_NotNu
llof_rfqId_0Eval0Invoker.java:16)
at org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:71)
... 12 more
When we execute third rule alone. we followed following approach
workingMemory.setGlobal("loanApplication",loanApplicationbean);
workingMemory.fireAllRules(new RuleNameStartsWithAgendaFilter(
"validate" ));
Please help us to resolve this issue
XML rule File
<?xml version="1.0" encoding="UTF-8"?>
<package name="rules"
xmlns="http://drools.org/drools-4.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-4.0
drools-4.0.xsd">
<import name="com.wipro.rfq.report.bean.Validator.RFQBean" />
<global identifier="rfq"
type="com.wipro.rfq.report.bean.Validator.RFQBean" />
<rule name="Check_NotNullof_rfqId"> <!--start of rule 1-->
<lhs>
<eval>(rfq.getM_rfqId() =="null") ||
(rfq.getM_rfqId().trim().equals(""))</eval>
</lhs>
<rhs>
System.out.println("The value of m_rfqId is null");
</rhs>
</rule>
<rule name="Check_NotNullof_partId">
<lhs>
<eval>(rfq.getM_partId() =="null") ||
(rfq.getM_partId().trim().equals(""))</eval>
</lhs>
<rhs>
System.out.println("The value of m_partId is null");
</rhs>
</rule>
<import name="com.wipro.loan.validation.bean.LoanApplicationBean" />
<global identifier="loanApplication"
type="com.wipro.loan.validation.bean.LoanApplicationBean" />
<rule name="validateFullName">
<lhs>
<eval>loanApplication.getFullName() == null ||
loanApplication.getFullName().trim().equals("")</eval>
</lhs>
<rhs>
System.out.println("full name is null from xml");
</rhs>
</rule>
</package>
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
www.wipro.com
17 years, 3 months
RE: rules-users Digest, Vol 10, Issue 56
by Sikkandar Nawabjan
Hi,
How to check my property contains only numeric.
for ex
when
address(telphoneno=="??????????") ---- telephone no should be numeric
then
s.o.p("error");
Basha
17 years, 3 months
About Drools
by prateek.katiyar@wipro.com
________________________________
Hi
I am also a part of the user-list.
I am using Drools 4.0 with XML rule language.
I want to use the same .xml file with the two java programs.
Here are my programs
First java program:=
package com.wipro.rfq.report.bean;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.regex.Pattern;
import org.drools.RuleBase;
import org.drools.StatefulSession;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
import org.drools.base.RuleNameStartsWithAgendaFilter;
public class Validator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String date="24-10-2000";
if(date!=""){
String dd1=date.substring(0,2);
String mm1=date.substring(3,5);
int dd=Integer.parseInt(dd1);
int mm=Integer.parseInt(mm1);
if(Pattern.matches("..-..-....",date)){
System.out.print("The entered date is in proper format");
if((dd<1)||(dd>31)||(mm<1)||(mm>12))
System.out.println(" but not valid.");
else
System.out.println(".");
}
else if(date.length()>10)
System.out.println("The entered date is not in proper format");
else
System.out.println("The entered date is not in proper format");
}
//load up the rulebase
final RuleBase ruleBase = readRule();
StatefulSession workingMemory = ruleBase.newStatefulSession();
RFQBean rfqbean=new RFQBean();
rfqbean.setM_country("India");
rfqbean.setM_deadLine("two days");
rfqbean.setM_dueDate(date);
rfqbean.setM_fsUMC("4");
rfqbean.setM_lotSize("5");
rfqbean.setM_mtrlType("6");
rfqbean.setM_partId("1100");
rfqbean.setM_product("kjhkihkj");
rfqbean.setM_rfqId("null");
//workingMemory.insert(rfqbean);
workingMemory.setGlobal("rfq",rfqbean);
workingMemory.fireAllRules(new RuleNameStartsWithAgendaFilter( "Check" ));
//Agenda a=workingMemory.getAgenda();
//System.out.println(a);
}catch(StringIndexOutOfBoundsException s){
System.out.println("The entered date is not in proper format "+s);
}catch(Exception e){
e.printStackTrace();
//System.out.println("The description of error is "+e);
}
}
private static RuleBase readRule() throws Exception {
final Reader source = new InputStreamReader( Validator.class.getResourceAsStream( "/rules/Check-new.xml" ) );
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromXml( source );
final Package pkg = builder.getPackage();
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
public static class RFQBean {
private String m_rfqId;
private String m_partId;
private String m_product;
private String m_country;
private String m_lotSize;
private String m_mtrlType;
private String m_fsUMC;
private String m_deadLine;
private String m_dueDate;
public String getM_country() {
return m_country;
}
public void setM_country(String m_country) {
this.m_country = m_country;
}
public String getM_deadLine() {
return m_deadLine;
}
public void setM_deadLine(String line) {
m_deadLine = line;
}
public String getM_dueDate() {
return m_dueDate;
}
public void setM_dueDate(String date) {
m_dueDate = date;
}
public String getM_fsUMC() {
return m_fsUMC;
}
public void setM_fsUMC(String m_fsumc) {
m_fsUMC = m_fsumc;
}
public String getM_lotSize() {
return m_lotSize;
}
public void setM_lotSize(String size) {
m_lotSize = size;
}
public String getM_mtrlType() {
return m_mtrlType;
}
public void setM_mtrlType(String type) {
m_mtrlType = type;
}
public String getM_partId() {
return m_partId;
}
public void setM_partId(String id) {
m_partId = id;
}
public String getM_product() {
return m_product;
}
public void setM_product(String m_product) {
this.m_product = m_product;
}
public String getM_rfqId() {
return m_rfqId;
}
public void setM_rfqId(String id) {
m_rfqId = id;
}
}
}
Second java program:=
package com.wipro.loan.validation.bean;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigDecimal;
import org.drools.RuleBase;
import org.drools.StatefulSession;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
import org.drools.base.RuleNameStartsWithAgendaFilter;
public class Final {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//load up the rulebase
final RuleBase ruleBase = readRule();
StatefulSession workingMemory = ruleBase.newStatefulSession();
//go !
LoanApplicationBean loanbean = new LoanApplicationBean();
loanbean.setFullName("");
workingMemory.setGlobal("loanApplication",loanbean);
workingMemory.fireAllRules(new RuleNameStartsWithAgendaFilter("validate"));
} catch ( final Throwable t ) {
t.printStackTrace();
}
}
/**
* Please note that this is the "low level" rule assembly API.
*/
private static RuleBase readRule() throws Exception {
//read in the source
final Reader source = new InputStreamReader( Final.class.getResourceAsStream( "/rules/Check-new.xml" ) );
//optionally read in the DSL (if you are using it).
//Reader dsl = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/mylang.dsl" ) );
//Use package builder to build up a rule package.
//An alternative lower level class called "DrlParser" can also be used...
final PackageBuilder builder = new PackageBuilder();
//this wil parse and compile in one step
//NOTE: There are 2 methods here, the one argument one is for normal DRL.
builder.addPackageFromXml( source );
//Use the following instead of above if you are using a DSL:
//builder.addPackageFromDrl( source, dsl );
//get the compiled package (which is serializable)
final Package pkg = builder.getPackage();
//add the package to a rulebase (deploy the rule package).
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
public static class LoanApplicationBean {
private static final long serialVersionUID = 1L;
private long loanAppId;
private String fullName;
private String mobileNumber;
private String salaryRange;
private String loanAmt;
private String loanTenure;
private String interestRate;
private String prefix;
private String extension;
private String firstName;
private int day;
private String code;
private int month;
private int year;
private String date;
private String email;
private String Name;
private String lastName;
private String ssn;
private String birthDate;
private String dayTimePhone;
private BigDecimal purchasePrice;
private long loanAmount;
private String strLoanAmount;
private String propertyCity;
private String propertyState;
private String propertyZip;
private String loanType;
private String loanTerm;
private String loanProduct;
private int confirmationNumber;
private boolean validation;
private String strResMsg;
private double interest;
private int tenure;
/**
* @return Returns the birthDate.
*/
public String getBirthDate() {
return birthDate;
}
/**
* @param birthDate The birthDate to set.
*/
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
/**
* @return Returns the confirmationNumber.
*/
public int getConfirmationNumber() {
return confirmationNumber;
}
/**
* @param confirmationNumber The confirmationNumber to set.
*/
public void setConfirmationNumber(int confirmationNumber) {
this.confirmationNumber = confirmationNumber;
}
/**
* @return Returns the dayTimePhone.
*/
public String getDayTimePhone() {
return dayTimePhone;
}
/**
* @param dayTimePhone The dayTimePhone to set.
*/
public void setDayTimePhone(String dayTimePhone) {
this.dayTimePhone = dayTimePhone;
}
/**
* @return Returns the firstName.
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName The firstName to set.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return Returns the lastName.
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName The lastName to set.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return Name returns the name
*/
public String getName() {
return Name;
}
/**
* @param Name sets the Name
*/
public void setName(String Name) {
this.Name = Name;
}
/**
* @return returns the emailid
*/
public String getEmail() {
return email;
}
/**
* @param email sets the emailid
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return returns the date
*/
public String getDate(){
return date;
}
/**
* @param date Sets the date
*/
public void setDate(String date) {
this.date=date;
}
/**
* @return returns the area code
*/
public String getCode(){
return code;
}
/**
* @param code sets the area code
*/
public void setCode(String code){
this.code=code;
}
/**
* @return returns the prefix
*/
public String getPrefix(){
return prefix;
}
/**
* @param prefix Sets the prefix of phone number
*/
public void setPrefix(String prefix){
this.prefix=prefix;
}
/**
* @return returns the extension of the phone number
*/
public String getExtension(){
return extension;
}
/**
* @param extension sets the extension of the phone number
*/
public void setExtension(String extension){
this.extension=extension;
}
/**
* @return returns the date
*/
public int getDay(){
return day;
}
/**
* @param day sets the day
*/
public void setDay(int day){
this.day=day;
}
/**
* @return returns the month
*/
public int getmonth(){
return month;
}
/**
* @param month sets the month
*/
public void setmonth(int month){
this.month=month;
}
/**
* @return returns the year
*/
public int getYear(){
return year;
}
/**
* @param year sets the year
*/
public void setYear(int year){
this.year=year;
}
/**
* @return Returns the loanAmount.
*/
public long getLoanAmount() {
return loanAmount;
}
/**
* @param loanAmount The loanAmount to set.
*/
public void setLoanAmount(long loanAmount) {
this.loanAmount = loanAmount;
}
/**
* @return returns the tenure or term
*/
public int getTenure() {
return tenure;
}
/**
* @param tenure sets the tenure or term
*/
public void setTenure(int tenure) {
this.tenure = tenure;
}
/**
* @return returns the interest
*/
public double getInterest() {
return interest;
}
/**
* @param interest sets the interest
*/
public void setInterest(double interest) {
this.interest = interest;
}
/**
* @return Returns the loanAppId.
* @hibernate.id column="LoanAppId" generator-class="identity"
*/
public long getLoanAppId() {
return loanAppId;
}
/**
* @param loanAppId The loanAppId to set.
*/
public void setLoanAppId(long loanAppId) {
this.loanAppId = loanAppId;
}
/**
* @return Returns the loanProduct.
*/
public String getLoanProduct() {
return loanProduct;
}
/**
* @param loanProduct The loanProduct to set.
*/
public void setLoanProduct(String loanProduct) {
this.loanProduct = loanProduct;
}
/**
* @return Returns the loanTerm.
*/
public String getLoanTerm() {
return loanTerm;
}
/**
* @param loanTerm The loanTerm to set.
*/
public void setLoanTerm(String loanTerm) {
this.loanTerm = loanTerm;
}
/**
* @return Returns the loanType.
*/
public String getLoanType() {
return loanType;
}
/**
* @param loanType The loanType to set.
*/
public void setLoanType(String loanType) {
this.loanType = loanType;
}
/**
* @return Returns the propertyCity.
*/
public String getPropertyCity() {
return propertyCity;
}
/**
* @param propertyCity The propertyCity to set.
*/
public void setPropertyCity(String propertyCity) {
this.propertyCity = propertyCity;
}
/**
* @return Returns the propertyState.
*/
public String getPropertyState() {
return propertyState;
}
/**
* @param propertyState The propertyState to set.
*/
public void setPropertyState(String propertyState) {
this.propertyState = propertyState;
}
/**
* @return Returns the propertyZip.
*/
public String getPropertyZip() {
return propertyZip;
}
/**
* @param propertyZip The propertyZip to set.
*/
public void setPropertyZip(String propertyZip) {
this.propertyZip = propertyZip;
}
/**
* @return Returns the purchasePrice.
*/
public BigDecimal getPurchasePrice() {
return purchasePrice;
}
/**
* @param purchasePrice The purchasePrice to set.
*/
public void setPurchasePrice(BigDecimal purchasePrice) {
this.purchasePrice = purchasePrice;
}
/**
* @return Returns the ssn.
*/
public String getSsn() {
return ssn;
}
/**
* @param ssn The ssn to set.
*/
public void setSsn(String ssn) {
this.ssn = ssn;
}
/**
* @return Returns the validation.
*/
public boolean isValidation() {
return validation;
}
/**
* @param validation The validation to set.
*/
public void setValidation(boolean validation) {
this.validation = validation;
}
/**
* @return Returns the ssn.
*/
public String getStrResMsg() {
return strResMsg;
}
/**
* @param ssn The ssn to set.
*/
public void setStrResMsg(String strResMsg) {
this.strResMsg = strResMsg;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getInterestRate() {
return interestRate;
}
public void setInterestRate(String interestRate) {
this.interestRate = interestRate;
}
public String getLoanAmt() {
return loanAmt;
}
public void setLoanAmt(String loanAmt) {
this.loanAmt = loanAmt;
}
public String getLoanTenure() {
return loanTenure;
}
public void setLoanTenure(String loanTenure) {
this.loanTenure = loanTenure;
}
public String getSalaryRange() {
return salaryRange;
}
public void setSalaryRange(String salaryRange) {
this.salaryRange = salaryRange;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getStrLoanAmount() {
return strLoanAmount;
}
public void setStrLoanAmount(String strLoanAmount) {
this.strLoanAmount = strLoanAmount;
}
}
}
My ,xml file:=
<?xml version="1.0" encoding="UTF-8"?>
<package name="rules"
xmlns="http://drools.org/drools-4.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-4.0 drools-4.0.xsd">
<import name="com.wipro.rfq.report.bean.Validator.RFQBean" />
<global identifier="rfq" type="com.wipro.rfq.report.bean.Validator.RFQBean" />
<import name="com.wipro.loan.validation.bean.Final.LoanApplicationBean" />
<global identifier="loanApplication" type="com.wipro.loan.validation.bean.Final.LoanApplicationBean" />
<rule name="Check_NotNullof_rfqId"> <!--start of rule 1-->
<lhs>
<eval>(rfq.getM_rfqId() =="null") || (rfq.getM_rfqId().trim().equals(""))</eval>
</lhs>
<rhs>
System.out.println("The value of m_rfqId is null.");
</rhs>
</rule>
<rule name="validate_FullName">
<lhs>
<eval>(loanApplication.getFullName()==null) || (loanApplication.getFullName().trim().equals(""))</eval>
</lhs>
<rhs>
System.out.println("full name is null from xml");
</rhs>
</rule>
</package>
Now i want to use the agenda group so plz give me some idea about that.
Thanks.
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
www.wipro.com
17 years, 3 months
BRMS Exception
by Drouin.Mathieu
Hi everyone,
I get this exception when I try to run the rules from the BRMS. Anybody
know what it means?
java.lang.ClassCastException: java.util.HashSet
at org.drools.rule.Package.readExternal(Package.java:172)
at
java.io.ObjectInputStream.readExternalData(ObjectInputStream.java:1755)
at
java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1717
)
at
java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305)
at
java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
at
org.drools.agent.HttpClientImpl.fetchPackage(HttpClientImpl.java:54)
at org.drools.agent.URLScanner.readPackage(URLScanner.java:137)
at org.drools.agent.URLScanner.getChangeSet(URLScanner.java:109)
at
org.drools.agent.URLScanner.loadPackageChanges(URLScanner.java:88)
at
org.drools.agent.RuleAgent.checkForChanges(RuleAgent.java:297)
at
org.drools.agent.RuleAgent.refreshRuleBase(RuleAgent.java:265)
at org.drools.agent.RuleAgent.configure(RuleAgent.java:251)
at org.drools.agent.RuleAgent.init(RuleAgent.java:183)
at org.drools.agent.RuleAgent.newRuleAgent(RuleAgent.java:192)
Thanks,
Mat
17 years, 3 months
eval() not working in Spring environment
by Sankar
Hello Brian,
I tried the same example in Spring, using the spring modules
https://springmodules.dev.java.net/ JSR 94. It works fine...
Regards,
Sankar S
Message: 1
Date: Wed, 26 Sep 2007 21:07:54 +0000 (UTC)
From: Brian Enderle <brianke(a)gmail.com>
Subject: [rules-users] eval() not working in Spring environment
To: rules-users(a)lists.jboss.org
Message-ID: <loom.20070926T204833-266(a)post.gmane.org>
Content-Type: text/plain; charset=us-ascii
I am not sure where exactly this problem lies but I can create the following
rule in a Drools project and have it execute as expected but when i use the
exact same rule in my Spring application/environment it does not work.
Within DroolsTest.java:
Boolean performCheck = new Boolean("true");
statefulSession.setGlobal("performCheck", performCheck);
Within the rule file:
global java.lang.Boolean performCheck;
rule "Display if true"
when
eval(performCheck)
then
System.out.println(performCheck);
end
When I run DroolsTest I get the output of 'true', the value of performCheck,
as I would expect. When I use the same rule in my Spring environment the
rule
fails to execute. I know the gloabl is getting to the rule with a value
of "true" as I have a second rule which always fires and reports the value
of
performCheck.
Has anyone ever experienced this and if so did you find any solution?
Thanks in advance,
Brian Enderle
17 years, 3 months
Problem in using dsl.
by Parit Bansal
hi,
I wrote the follwoing dsl file:
#trying a new way of writing.
[when]beanObject has a flavour not equal to "{status}"=beanObj : XMLDataBean
(flavour != "{status}")
[then]set the "{Flavour}" dimension.=beanObj.setDimsAndVals("{Flavour}",
beanObj.getFlavour());
[then]set the "{language}" dimension to
"{kannada}"=beanObj.setDimsAndVals("{language}",
"{kannada}");
[when]beanobject has a songCode not equal to "{yes}"=beanObj : XMLDataBean
(songCode != "{yes}")
[then]set the "{SongCode}" dimension in
beanObject=beanObj.setPropsAndVals("{SongCode}",
beanObj.getSongCode());
[then]set the category to "{category}"=beanObj.setCategory("{category}");
[then]set the "{album}" dimension=beanObj.setDimsAndVals("{album}",
beanObj.getMovie());
[when]beanObject has a dimension "{dimension}"=beanObj :
XMLDataBean("{dimension}")
[when]beanObject has a "{category}"=beanObj : XMLDataBean ("{category}")
[when]set language=beanObj : XMLDataBean(language)
and the corresponding drl file is:
import com.dataBean.XMLDataBean;
expander bollywood.dsl
rule "catgy rule 1"
agenda-group "cat";
when
beanObject has a "category"
then
set the category to "Regional"
end
rule "dim rule 1"
agenda-group "dimension";
when
beanObject has a dimension "movie"
then
set the "Album-Movie" dimension
end
rule "dim rule 2"
agenda-group "dimension";
when
beanObject has a flavour not equal to "NA"
then
set the "Flavour" dimension
end
rule "dim rule 3"
agenda-group "dimension";
when
beanObject has a dimension "language"
then
set the "Language" dimension to "Kannada"
end
rule "property rule 1"
agenda-group "dimension";
when
beanobject has a songCode not equal to "Y"
then
set the "Song Code" dimension in beanObject
end
I tried to compile the drl file but it gives errors:
Unexpected token "category"
Unexpected token "movie"
Unexpected token "language"
I have gone through some sample applications but looking at the above errors
am having no clue why these compilation errors are happening. Please help.
--
Parit Bansal
Graduate Student
DhiruBhai Ambani Institute Of Information and Communication Technology
Gandhinagar- 382007 India
Email: parit.bansal(a)gmail.com
Web: http://www.guptalab.org/paritbansal/
17 years, 3 months
rule-set tag
by prateek.katiyar@wipro.com
Hi
I am using Drools 4.0.
I want to know that can i use the rule-set tag in our .xml file for partitioning the rules.
Can i write any statement in the rhs part of a rule to go back to the java program so that other rules will not execute.
Thanks.
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
www.wipro.com
17 years, 3 months
query
by prateek.katiyar@wipro.com
Hi
I am using Drools 4.0 version with .xml file.
Here is my java program.
<-------------------------------------------------------------------------------->
package com.wipro.rfq.report.bean;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.StatefulSession;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
public class Validator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//load up the rulebase
final RuleBase ruleBase = readRule();
StatefulSession workingMemory = ruleBase.newStatefulSession();
RFQBean bean=new RFQBean();
bean.setM_country("India");
bean.setM_deadLine("two days");
bean.setM_dueDate("24/10/2007");
bean.setM_fsUMC("4");
bean.setM_lotSize("5");
bean.setM_mtrlType("6");
bean.setM_partId("1100");
bean.setM_product("lux");
bean.setM_rfqId("1234");
workingMemory.insert( bean );
workingMemory.fireAllRules();
}catch(Exception e){
System.out.println("The description of error is "+e);
}
}
private static RuleBase readRule() throws Exception {
final Reader source = new InputStreamReader( Validator.class.getResourceAsStream( "/rules/Check.xml" ) );
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromXml( source );
final Package pkg = builder.getPackage();
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
public static class RFQBean {
private String m_rfqId;
private String m_partId;
private String m_product;
private String m_country;
private String m_lotSize;
private String m_mtrlType;
private String m_fsUMC;
private String m_deadLine;
private String m_dueDate;
public String getM_country() {
return m_country;
}
public void setM_country(String m_country) {
this.m_country = m_country;
}
public String getM_deadLine() {
return m_deadLine;
}
public void setM_deadLine(String line) {
m_deadLine = line;
}
public String getM_dueDate() {
return m_dueDate;
}
public void setM_dueDate(String date) {
m_dueDate = date;
}
public String getM_fsUMC() {
return m_fsUMC;
}
public void setM_fsUMC(String m_fsumc) {
m_fsUMC = m_fsumc;
}
public String getM_lotSize() {
return m_lotSize;
}
public void setM_lotSize(String size) {
m_lotSize = size;
}
public String getM_mtrlType() {
return m_mtrlType;
}
public void setM_mtrlType(String type) {
m_mtrlType = type;
}
public String getM_partId() {
return m_partId;
}
public void setM_partId(String id) {
m_partId = id;
}
public String getM_product() {
return m_product;
}
public void setM_product(String m_product) {
this.m_product = m_product;
}
public String getM_rfqId() {
return m_rfqId;
}
public void setM_rfqId(String id) {
m_rfqId = id;
}
}
}
<----------------------------------------------------------------------------------------------------->
And my .xml file is the following:
<------------------------------------------------------------------------------------------------------>
<?xml version="1.0" encoding="UTF-8"?>
<package name="rules"
xmlns="http://drools.org/drools-4.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-4.0 drools-4.0.xsd">
<import name="com.wipro.rfq.report.bean.Validator.RFQBean" />
<import name="java.util.regex.Pattern" />
<global identifier="bean" type="com.wipro.rfq.report.bean.Validator.RFQBean" />
<rule name="Check_NotNullof_rfqId"> <!--start of rule 1-->
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_rfqId">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_rfqId is null");
</rhs>
</rule> <!--end of rule 1-->
<rule name="Check_NotNullof_partId">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_partId">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_partId is null");
</rhs>
</rule>
<rule name="Check_NotNullof_product">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_product">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_product is null");
</rhs>
</rule>
<rule name="Check_NotNullof_country">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_country">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_country is null");
</rhs>
</rule>
<rule name="Check_NotNullof_lotSize">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_lotSize">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_lotSize is null");
</rhs>
</rule>
<rule name="Check_NotNullof_mtrlType">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_mtrlType">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_mtrlType is null");
</rhs>
</rule>
<rule name="Check_NotNullof_fsUMC">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_fsUMC">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_fsUMC is null");
</rhs>
</rule>
<rule name="Check_NotNullof_deadLine">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_deadLine">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_deadLine is null");
</rhs>
</rule>
<rule name="Check_NotNullof_Date">
<lhs>
<pattern identifier="bean" object-type="RFQBean" >
<field-constraint field-name="m_dueDate">
<literal-restriction evaluator="==" value="null" />
</field-constraint>
</pattern>
</lhs>
<rhs>
System.out.println("The value of m_dueDate is null");
</rhs>
</rule>
<rule name="temp">
<lhs>
<eval>bean.getM_dueDate().trim().equals("24/10/2007")</eval>
</lhs>
<rhs>
System.out.println("The entered date is "+bean.getM_dueDate());
</rhs>
</rule>
</package>
<---------------------------------------------------------------------------------------------------------------------------------------------------->
Now i want to know that how can i use a method of "RFQBean" class inside the <lhs></lhs> and <rhs></rhs>tag
And please mail me the description of all the tags that i can use in a rule based .xml file.
Also how can i know that whether a particular rule has been fired or not.
The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
www.wipro.com
17 years, 3 months
Re: [rules-users] how to specify classpath dependencies in .drl files.
by Mark Proctor
Godmar Back wrote:
> PS: I looked at what Drools does in the Eclipse compiler configuration.
>
> It appears to assume that a class's bytecode can be found as a system
> resource, via getResourceAsStream(). See
> org.drools.common.jci.compilers.EclipseJavaCompiler.java.
>
> In other words, if the compiler asks for the byte code for Class
> org.jboss.somepackage.SomeClass, Drools will ask the class loader for
> the resource with path "org/jboss/somepackage/SomeClass.class".
> Although most class loaders would at this point return an InputStream
> from which the bytecode of this class can be read, there is no
> requirement that they do so - or that this resource exists at all. For
> instance, class loaders that rewrite bytecode on the fly would provide
> different bytecode from this system resource than otherwise.
>
> Nevertheless, it is a reasonable assumption, and it means that
> implementing the import from functionality may not be difficult. I'm
> getting the feeling though, that you have doubts about its usefulness.
>
> - Godmar
>
Again you are reading this wrong, I think you have a fundamental
misunderstanding of how classloaders work in Java. See my previous
email. Simple add the class at runtime to a classloader and use that
classloader as the root classloader of the packagebuilder.
> On 9/26/07, Godmar Back <godmar(a)gmail.com> wrote:
>
>> On 9/26/07, Mark Proctor <mproctor(a)codehaus.org> wrote:
>>
>>> Godmar Back wrote:
>>> [off list]
>>>
>>>
>>> Like I said, I doubt that this works.
>>>
>>> Classloaders don't produce the bytecode a compiler needs. It's not
>>> part of their interface.
>>>
>>> Don't produce what bytecode? If you are dynnamically loading classes are
>>> runtime, that .class has the bytecode. This is what people are doing now
>>> already with web services. Drools has its own runtime compiler and will
>>> compile fine against dynamically loaded classes in a parent classloader.
>>>
>>>
>> No, that's not how class loaders work.
>> A class loader, fundamentally, implements a method
>>
>> public Class loadClass(String name) throws ClassNotFoundException
>>
>> that maps class names to class objects. For instance, the name
>> "org.jboss.pkg.SomeClass" may be mapped to an object of type
>> java.lang.Class - such as one that would be returned by defineClass().
>>
>> Although the class loader internally reads the content of .class files
>> - either from a URL, from a jar, or from disk, there is no interface
>> to obtain the actual bytecode from a class loader.
>> There is no method: "byte [] getByteCode(String name)". A class loader
>> will internally read the (bytes constituting) a .class file, then
>> passes it to "defineClass()", which is where the JVM parses the
>> bytecode and creates a java.lang.Class instance.
>>
>> A compiler that is compiling code that refers to a class needs access
>> to that class's bytecode, directly. Access to a class object as
>> provided by a ClassLoader is insufficient. (Also, it would be
>> dangerous - reflexive examination may trigger a "first active use"
>> (See JLS - 12.4.1 in 1st edition) - you don't want the compilation to
>> trigger static initializers of referred classes.)
>>
>> Given that, the drools compiler would need to have a way to load the
>> actual bytecode, something a classloader cannot provide.
>>
>> Hence my suggestion to enhance it allow a user-controlled way of
>> providing that bytecode.
>>
>> - Godmar
>>
>>
>
>
17 years, 3 months