Drools initial contact

Source: Internet
Author: User

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:

 

  1. Package com. sample;
  2. Import java. Io. inputstreamreader;
  3. Import java. Io. reader;
  4. Import org. drools. rulebase;
  5. Import org. drools. rulebasefactory;
  6. Import org. drools. workingmemory;
  7. Import org. drools. compiler. packagebuilder;
  8. Import org. drools. Rule. package;
  9. /**
  10. * This is a sample file to launch a rule package from a rule source file.
  11. */
  12. Public class droolstest {
  13. Public static final void main (string [] ARGs ){
  14. Try {
  15. // Load up the rulebase
  16. Rulebase = readrule ();
  17. Workingmemory = rulebase. newstatefulsession ();
  18. // Go!
  19. Message message = new message ();
  20. Message. setmessage ("Hello World ");
  21. Message. setstatus (message. Hello );
  22. Workingmemory. insert (Message );
  23. Workingmemory. fireallrules ();
  24. } Catch (throwable t ){
  25. T. printstacktrace ();
  26. }
  27. }
  28. /**
  29. * Please note that this is the "Low Level" rule Assembly API.
  30. */
  31. Private Static rulebase readrule () throws exception {
  32. // Read in the source
  33. Reader source = new inputstreamreader (droolstest. Class. getresourceasstream ("/sample. DRL "));
  34. // Optionally read in the DSL (if you are using it ).
  35. // Reader DSL = new inputstreamreader (droolstest. Class. getresourceasstream ("/mylang. dsl "));
  36. // Use package builder to build up a rule package.
  37. // An alternative lower level class called "drlparser" can also be used...
  38. Packagebuilder builder = new packagebuilder ();
  39. // This wil parse and compile in one step
  40. // Note: There are 2 methods here, the one argument one is for normal DRL.
  41. Builder. addpackagefromdrl (source );
  42. // Use the following instead of above if you are using a DSL:
  43. // Builder. addpackagefromdrl (source, DSL );
  44. // Get the compiled package (which is serializable)
  45. Package PKG = builder. getpackage ();
  46. // Add the package to a rulebase (deploy the rule package ).
  47. Rulebase = rulebasefactory. newrulebase ();
  48. Rulebase. addpackage (PKG );
  49. Return rulebase;
  50. }
  51. Public static class message {
  52. Public static final int Hello = 0;
  53. Public static final int goodbye = 1;
  54. Public static final int game_over = 2;
  55. Private string message;
  56. Private int status;
  57. Public String getmessage (){
  58. Return this. message;
  59. }
  60. Public void setmessage (string message ){
  61. This. Message = message;
  62. }
  63. Public int getstatus (){
  64. Return this. status;
  65. }
  66. Public void setstatus (INT status ){
  67. This. Status = status;
  68. }
  69. }
  70. }

Rule file:
Sample. DRL

  1. Package com. Sample
  2. Import com. sample. droolstest. message;
  3. Rule "Hello World"
  4. When
  5. M: Message (status = message. Hello, message: Message)
  6. Then
  7. System. Out. println (Message );
  8. M. setmessage ("Goodbye cruel world ");
  9. M. setstatus (message. Goodbye );
  10. Update (m );
  11. End
  12. Rule "Goodbye"
  13. No-loop true
  14. When
  15. M: Message (status = message. Goodbye, message: Message)
  16. Then
  17. System. Out. println (Message );
  18. M. setstatus (message. game_over );
  19. M. setmessage ("game over now! ");
  20. Update (m );
  21. End
  22. Rule "game over"
  23. When
  24. M: Message (status = message. game_over)
  25. Then
  26. System. Out. println (M. getmessage ());
  27. End
  28. The Java code will not be mentioned. A brief explanation of the Rule file is as follows:
  29. Rule "Hello World"
  30. When
  31. M: Message (status = message. Hello, message: Message)
  32. Then
  33. System. Out. println (Message );
  34. M. setmessage ("Goodbye cruel world ");
  35. M. setstatus (message. Goodbye );
  36. Update (m );
  37. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.