Drool is the rule engine of JBoss. I have long wanted to see it, and there is no time for busy periods. take the time to learn. record your learning experience and give it an explanation this year.
Yes
I. Environment configuration.
The fastest way:
Download the drool Eclipse plug-in:
Http://www.jboss.org/drools/downloads.html
This plug-in has good functions and is easy to write Rule, and can create drool projects, all Dependencies
Jar packages are included,
Ii. Hello World
The obscure theory should be put aside and run a hello World:
- Package com. sample;
- Import java. Io. inputstreamreader;
- Import java. Io. reader;
- Import org. drools. rulebase;
- Import org. drools. rulebasefactory;
- Import org. drools. workingmemory;
- Import org. drools. compiler. packagebuilder;
- Import org. drools. Rule. package;
- /**
- * This is a sample file to launch a rule package from a rule source file.
- */
- Public class droolstest {
- Public static final void main (string [] ARGs ){
- Try {
- // Load up the rulebase
- Rulebase = readrule ();
- Workingmemory = rulebase. newstatefulsession ();
- // Go!
- Message message = new message ();
- Message. setmessage ("Hello World ");
- Message. setstatus (message. Hello );
- Workingmemory. insert (Message );
- Workingmemory. fireallrules ();
- } Catch (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
- Reader source = new inputstreamreader (droolstest. Class. getresourceasstream ("/sample. DRL "));
- // 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...
- 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. addpackagefromdrl (source );
- // Use the following instead of above if you are using a DSL:
- // Builder. addpackagefromdrl (source, DSL );
- // Get the compiled package (which is serializable)
- Package PKG = builder. getpackage ();
- // Add the package to a rulebase (deploy the rule package ).
- Rulebase = rulebasefactory. newrulebase ();
- Rulebase. addpackage (PKG );
- Return rulebase;
- }
- Public static class message {
- Public static final int Hello = 0;
- Public static final int goodbye = 1;
- Public static final int game_over = 2;
- Private string message;
- Private int status;
- Public String getmessage (){
- Return this. message;
- }
- Public void setmessage (string message ){
- This. Message = message;
- }
- Public int getstatus (){
- Return this. status;
- }
- Public void setstatus (INT status ){
- This. Status = status;
- }
- }
- }
Rule file:
Sample. DRL
- Package com. Sample
- Import com. sample. droolstest. message;
- Rule "Hello World"
- When
- M: Message (status = message. Hello, message: Message)
- Then
- System. Out. println (Message );
- M. setmessage ("Goodbye cruel world ");
- M. setstatus (message. Goodbye );
- Update (m );
- End
- Rule "Goodbye"
- No-loop true
- When
- M: Message (status = message. Goodbye, message: Message)
- Then
- System. Out. println (Message );
- M. setstatus (message. game_over );
- M. setmessage ("game over now! ");
- Update (m );
- End
- Rule "game over"
- When
- M: Message (status = message. game_over)
- Then
- System. Out. println (M. getmessage ());
- End
- The Java code will not be mentioned. A brief explanation of the Rule file is as follows:
- Rule "Hello World"
- When
- M: Message (status = message. Hello, message: Message)
- Then
- System. Out. println (Message );
- M. setmessage ("Goodbye cruel world ");
- M. setstatus (message. Goodbye );
- Update (m );
- End
The Java code will not be mentioned. It mainly describes the rule file and familiarize yourself with the rule Syntax:
1: subject structure of the rule:
Rule "name"
Attributes
When
LHS
Then
RHS
End
Where:
LHS (left hand side) is the condition part of the rule, and RHS (right hand side) is the self-built block after the condition is met.
2: Writing of conditions:
M: Message (status = message. Hello, message: Message)
Meaning:
In working memory, if the instance of a message meets the following conditions:
Its status is similar to message. Hello.
The message instance that meets the condition is represented by the variable M, and its message attribute is represented by the message variable so that it can be used in RHS.
3: self-built blocks (RHs) after conditions are met)
System. Out. println (Message );
M. setmessage ("Goodbye cruel world ");
M. setstatus (message. Goodbye );
Update (m );
The variables defined in LHS are used: m and message.
Update (m) indicates updating the message instance in the working memory. This triggers the rule named "goodbye", and then triggers the rule named "game over.
Below is the demo provided by drools. After a little sorting, You can import it directly in eclipse.
Http://lcllcl987.javaeye.com/blog/255404
(Prerequisite: download and install the drool Eclipse plug-in drools 4.0.7 eclipse workbench for 3.2)
We recommend that you first look:
Fibonacciexample: drools Implementation of the Fibonacci series.
Golfingexample: the famous drools Implementation of golf intellectual problems. The comments of golf. DRL are the text descriptions of this problem.
Shoppingexample: A shopping discount demo, with a discount of more than 100 yuan and 10 yuan.
Stateexampleusingagendgroup: by setting dynamic to true, drools will use JavaBean, propertychangelisteners so you don't have to call Update ().
Stateexamplewithdynamicrules: a demo for dynamically loading rule (loading stateexampleusingsalience. DRL and loading stateexampledynamicrule. DRL ).
Troubleticketexample:
The duration rule feature is used to delay the specified rule for a certain period of time.
The rule engine runs in another thread. Therefore, the duration feature can be used to delay the specified rule for a certain period of time.
However, you need to pay attention to the corresponding latency in the main thread. Otherwise, the results of the sub-thread will not be processed after the main thread ends.
Salience and duration affect the execution sequence of rule.
The demo also defines a rule function.