Filip Drools - DSL
Jump to navigation
Jump to search
What is Domain Specific Language
- "A way of creating a rule language that is dedicated to your problem domain"
- DSL is a way non-technical people can write executable rules in Drools environment
- DSL is ultimately transformed to rules (mvel or java)
- .dsl file contain the transformations (mapping)
- DSLs have no impact on the rule engine at runtime, they are just a compile time feature
When to use DSL
- Your requirements should be executable and always in sync with what is actually executed
- Analysts can focus on "logic proper"
- You already have rules written in SBVR or other standard (which is consistent and detailed enough to be easily translated into executable rules)
- You need "templates" for conditional elements and consequence actions that are used repeatedly in your rules (with minor variations)
DSL in KIE Workbench
Create New -> New DSL
[when]Order is greater than {totalOrder} = sc :ShoppingCart( $totalPrice : totalPrice > {totalOrder}) [then]Set discount to {discount} = sc.setTotalPrice( $totalPrice * (1-{discount}) ); update(sc);
- Create New -> New Rule
- Type (format) of rule: DSL or Guided Rule
dilect "mvel" when Order is greater than 10000 then Set discount to 0.1
Use the test created for tall order discount
Eclipse
See 08.1DSL folder
- DSLR file contains rules defined using business context
- DSL file contains the translations between business context and drools language
Connecting DSL file with DSRL file
- It's not required to use expander to refer to DSL file in Drools 6.4 if all DSL files included in KIE Base are going to be used.
DSL file
- The DSL file is a text file containing DSL entries.
- Entry syntax
# This is a comment to be ignored
- Examples
[condition][]There is a Shopping Cart that=$sc : ShoppingCart()
[consequence][]Show price=System.out.println($sc.totalPrice)
- <text expression> can contain Java regular expressions
https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
Nesting entries
- Starting new entry with a hyphen sign "-" allows to add this expression to the last pattern
- Examples
[condition][]There is a Shopping Cart that=$sc : ShoppingCart()
[condition][]- total price is greater than 1000 =totalPrice > 1000
- Result
$sc : ShoppingCart(totalPrice > 1000)
- Hyphens can be used in consequence entries as part of a modify statement
Variables
- Variables can be defined using curly brackets { and }
- Examples
[condition][]There is a Shopping Cart that=$sc : ShoppingCart()
[condition][]- total price is greater than {price}=totalPrice >{price}
Keywords
- Keyword is valid in any part of DSLR file
[keyword] no loops = no-loop true
[keyword][]regula=rule
String transformation functions
Name | Description |
---|---|
uc | Converts all letters to upper case. |
lc | Converts all letters to lower case. |
ucfirst | Converts the first letter to upper case, and all other letters to lower case. |
num | Extracts all digits and "-" from the string. If the last two digits in the original string are preceded by "." or ",", a decimal period is inserted in the corresponding position. |
a?b/c | Compares the string with string a, and if they are equal, replaces it with b, otherwise with c. But c can be another triplet a, b, c, so that the entire structure is, in fact, a translation table. |
Examples
# definitions for conditions
[when][]There is an? {entity}=${entity!lc}: {entity!ucfirst}()
[when][]- with an? {attr} greater than {amount}={attr} >= {amount!num}
[when][]- with a {what} {attr}={attr} {what!positive?>0/negative?%lt;0/zero?==0/ERROR}
Debug options for DSL expansion
- Drools can log a DSL translation process
- To get access to those details use special type of statement starting with "#/ debug display"
Word | Description |
---|---|
result | Prints the resulting DRL text, with line numbers. |
steps | Prints each expansion step of condition and consequence lines. |
keyword | Dumps the internal representation of all DSL entries with scope "keyword". |
when | Dumps the internal representation of all DSL entries with scope "when" or "*". |
then | Dumps the internal representation of all DSL entries with scope "then" or "*". |
usage | Displays a usage statistic of all DSL entries. |
Examples
#/ debug display result, steps and usage
#/ debug display result and usage
#/ debug display steps