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."); elseSystem.
out.println(".");}
else if(date.length()>10)System.
out.println("The entered date is not in proper format"); elseSystem.
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;}
}
}
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;}
}
}
<?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>