BRE and Liferay
Business rule framework using Drools
In most of the software products the complexities imposed by requirements are behavioral and functional. Usually the business logic is implemented in a J2SE/J2EE application by writing Java code which in most cases are very intricate. The complexity of the code makes maintaining and updating the application's business logic very cumbersome and increases the possiblity of human errors and result is bugs in the product. Also any change incurs recompilation and redeployment costs.
Drools facilitates rule based validation. The advantage of using a rule engine is in the ability to externalise the business rules from the application and enable them to be edited without redeploying the application.
About Drools
Drools is an open source rules engine, written in the Java language, that uses the Rete algorithm to evaluate the rules you write. Drools lets you express your business logic rules in a declarative way. You can write rules using a non-XML native language that is quite easy to learn and understand. And you can embed Java code directly in a rules file, which makes the experience of learning Drools even more attractive. Drools also has other advantages, such as:
- Supported by an active community
- Easy to use
- Quick to execute
- Gaining popularity among Java developers
- Compliant with the Java Rule Engine API (JSR 94)
- Free
Example
Following is a very basic example:
The POJO (.java file)
package
com.company.license;
rule
"Is of valid age"
when
$a : Applicant( age <
18
)
then
$a.setValid(
false
);
end
The rule consists of conditions (when) and consequences (then). In this rule, when the Applicant age property is less than 18, the valid property is set to false.Often, business rules will need to access collaborators such as services or DAOs. These can be injected as "globals" in the rule definition. For example, the scenario above could be modified so that the applicant needs to be loaded from a DAO in order to check the age.
package
com.acme.app.rules
import
com.acme.app.form.ApplicationForm
import
com.acme.app.model.Applicant
import
com.acme.app.validation.Errors
import
com.acme.app.dao.ApplicantDao
global ApplicantDao applicantDao
package
com.acme.app.rules
import
com.acme.app.form.ApplicationForm
import
com.acme.app.model.Applicant
import
com.acme.app.validation.Errors
import
com.acme.app.dao.ApplicantDao
global ApplicantDao applicantDao
rule
"Check applicant age"
when
$a : ApplicationForm()
$errors : Errors()
$applicant:Applicant( age <
18
) from applicantDao.findApplicant($a.getApplicantId())
then
$errors.addError( $a,
"applicantId"
,
"Applicant age < 18"
);
end
Integration with Liferay
Detailed steps of integrating Drools with Liferay can be found at Blog writetn by Venkata Mukku at http://www.liferay.com/web/venkata.mukku/blog/-/blogs/7871213
Comments
Post a Comment