Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Define a Rule Resource | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » Define a Rule Resource

Define a Rule Resource








Article Posted On Date : Friday, May 22, 2009


Define a Rule Resource
Advertisements

HTML clipboard

Define a Rule Resource

Drools is based on rule resources. Most of the time, the rules are mapped in files with the .drl extension or in a spreadsheet format as decision tables (supported formats are Excel and CSV). A basic .drl file is a list of rules written in a file like this:
#package name  #list any import classes here.  #declare any global variables here     rule "Your First Rule"              when            #conditions       then             #actions     end     rule "Your Second Rule"       #include attributes such as "salience" here...       when            #conditions       then             #actions              end

As you can see, every rule is encapsulated between the rule and end keywords. A rule has a name and a when-then pair that represents the condition and the action. The optional parts are marked with pound signs (#). For a better understanding of what goes on in the code, consider the following code sample written as a .drl file (vote.drl):

Listing vote.drl     package com.sample     import com.sample.DroolsTest.Vote;     rule "WrongValue � less than 0"       when            m : Vote( average < 0.0, vote : vote )       then                      m.setAverage(0.0f);            update( m );                 end     rule "WrongValue � bigger than 10"       when            m : Vote( average > 10.0, vote : vote )       then                      m.setAverage(10.0f);            update( m );                 end     rule "BadValue � between 0-3"       when            m : Vote( average >= 0.0 && average <=3.0, vote : vote )       then             m.setVote("Bad!");            System.out.println( m.getVote() );  end     rule "GoodValue � between 3-6"       when            m : Vote( average >3.0 && average <=6.0, vote : vote )       then             m.setVote("Good!");            System.out.println( m.getVote() );  end     rule "VeryGoodValue � between 6-9"       when            m : Vote( average >6.0 && average <=9.0, vote : vote )       then             m.setVote("Very Good!");            System.out.println( m.getVote() );  end     rule "ExcellentValue � between 9-10"       when            m : Vote( average >9.0 && average <=10.0, vote : vote )       then             m.setVote("Excellent!");            System.out.println( m.getVote() );  end

The getVote, setVote, getAverage, and setAverage functions are provided by a simple POJO like this (Vote.java):

Vote.java     public static class Vote {                          private String vote;            private float average;               public String getVote() {                 return this.vote;            }               public void setVote(String vote) {                 this.vote = vote;            }               public float getAverage() {                 return this.average;            }               public void setAverage(float average) {                 this.average = average;            }                   }

As to decision tables, consider them if you have rules that can be expressed as rule templates plus data. In each row of a decision table, you collect data and combine it with the templates to generate a rule.

Author's Note: Keep in mind that in a decision table, each row is a rule and each column in that row is either a condition or an action for that rule.
In practice, you build decision tables on a set of keywords that you place in the right rows and columns. For example, the RuleTable keyword indicates the start of a rule table (both the starting row and column). Usually under the starting row, you will have a row that contains the keywords CONDITION and/or ACTION. Be sure to indicate that the data in the columns below are for either the LHS (Left Hand Side) or the RHS (Right Hand Side) parts of a rule. The next row will contain declarations of ObjectTypes. When you use this row, the values in the cells below become constraints on that object type.

Further down, you have the rule templates themselves. Here you can use the $param placeholder to indicate where data from the cells below will be populated (or $1, $2, $3, etc. to indicate parameters from a comma-separated list in a cell below). Finally, the last two rows will contain data, which are interpolated with templates, to generate rules.

Table 1 lists more keywords (this table is available in the Drools Reference Manual).

Table 1. Drools Keywords
 

Keyword Description Required
RuleSet The cell to the right of RuleSet contains the ruleset name. One only (or default)
Sequential The cell to the right of Sequential can be true or false. If true, then "salience" is used to ensure that rules fire from the top down. Optional
Import The cell to the right of Import contains a comma-separated list of Java classes to import. Optional
RuleTable A cell starting with RuleTable indicates the start of a definition of a rule table. The actual rule table starts the next row down. The rule table is read left to right and top down until there is one blank row. At least one. If there are more, then they are all added to the one ruleset.
CONDITION This keyword indicates that this column will be for rule conditions. At least one per rule table
ACTION This keyword indicates that this column will be for rule consequences. At least one per rule table
PRIORITY This keyword indicates that this column's values will set the "salience" values for the rule row. It overrides the 'Sequential' flag. Optional
DURATION This keyword indicates that this column's values will set the duration values for the rule row. Optional
NAME This keyword indicates that this column's values will set the name for the rule generated from that row. Optional
Functions The cell immediately to the right of Functions can contain functions, which can be used in the rule snippets. Drools supports functions defined in the DRL, allowing logic to be embedded in the rule, and changed without hard coding. Use with care and with the same syntax as regular DRL. Optional
Variables The cell immediately to the right of Variables can contain global declarations, which Drools supports. This is a type, followed by a variable name. (If multiple variables are required, comma-separate them). Optional
UNLOOP This keyword indicates that if there are cell values in this column, the no-loop attribute should be set. Optional
XOR-GROUP Cell values in this column mean that the rule-row belongs to the given XOR/activation group. An activation group means that only one rule in the named group will fire (i.e., the first one to fire cancels the other rule activations). Optional
Worksheet By default, only the first worksheet is looked at for decision tables. N/A

 






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.