Filip Drools - DSL

From Training Material
Revision as of 09:48, 14 September 2018 by Fstachecki (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


What is Domain Specific Language

ClipCapIt-160929-194441.PNG

  • "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); 
  1. Create New -> New Rule
  2. 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

ClipCapIt-150716-052324.PNG

  • 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

ClipCapIt-160916-132246.PNG

  • Examples
[condition][]There is a Shopping Cart that=$sc : ShoppingCart()
[consequence][]Show price=System.out.println($sc.totalPrice)

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

NameDescription
ucConverts all letters to upper case.
lcConverts all letters to lower case.
ucfirstConverts the first letter to upper case, and all other letters to lower case.
numExtracts 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/cCompares 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"
WordDescription
resultPrints the resulting DRL text, with line numbers.
stepsPrints each expansion step of condition and consequence lines.
keywordDumps the internal representation of all DSL entries with scope "keyword".
whenDumps the internal representation of all DSL entries with scope "when" or "*".
thenDumps the internal representation of all DSL entries with scope "then" or "*".
usageDisplays a usage statistic of all DSL entries.
Examples
#/ debug display result, steps and usage
#/ debug display result and usage
#/ debug display steps