[rules-users] Catch 22 ANTLR DSL matching issue

jstroup jstroup at regenstrief.org
Fri May 27 14:19:39 EDT 2011


Hi Droolers,

Regarding this blurb from the 5.2 DSL reference:

"It is important to note that the compiler transforms DSL rule files line by
line. In the above example, all the text after "Something is " to the end of
the line is captured as the replacement   value for "{colour}", and this is
used for interpolating the target string. This may not be exactly what you
want. For instance, when you intend to merge different DSL expressions to
generate a composite DRL pattern, you need to transform a DSLR line in
several independent operations. The best way to achieve this is to ensure
that the captures are surrounded by characteristic text - words or even
single characters. As a result, the matching operation done by the parser
plucks out a substring from somewhere within the line. In the example below,
quotes are used as distinctive characters. Note that the characters that
surround the capture are not included during interpolation, just the
contents between them."

Given this DSL (test_expander.dsl)

  [when](C|c)heese is "{type}"=Cheese(type=="{type}")
  [when](is|hails|comes) from {country}=Cheese(country=="{country}")
  [then]Add the message {message}=System.out.println({message});

And this DSLR

  package com.sample
  import com.sample.DroolsTest.Cheese;
  expander test_expander.dsl

  rule "rule_1" 
        when 
		   Cheese is "cheddar" and is from Italy
        then 
            Add the message "Cheddar IS from Italy"
  end 

  rule "rule_2" 
        when 
		   cheese is cheddar and comes from Italy
        then 
            Add the message "Cheddar COMES from Italy"
  end

I know that rule_2 fails because I removed the “distinctive characters” so
ANTLR is confused on what to capture. It just seems so unnatural for a rule
author to have to somehow magically know to add quotes in order to merge
different DSL expressions to generate a composite DRL pattern. The
documentation suggests also surrounding it with distinctive words so I sneak
an “and” into the DSL entry.

  [when](C|c)heese is {type} and=Cheese(type=="{type}")

This allows me to write my rules more naturally.

  rule "rule_1" 
        when 
		   Cheese is cheddar and is from Italy
        then 
            Add the message "Cheddar IS from Italy"
  end 

  rule "rule_2" 
        when 
		   cheese is cheddar and comes from Italy
        then 
            Add the message "Cheddar COMES from Italy"
  end

In the first example the “and” in 

  Cheese is "cheddar" and is from Italy

is actually a logical AND - not a matching character – which is nice because
we can use it to logically connect the 2 expressions or just match the first
DSL expression by itself as in

  Cheese is cheddar

And since the second expression is not present we can drop the quotes. (But
how on earth will a rule author know this?). But if we add the “and” as part
of the first DSL expression 

  1) It is no  longer a connecting logical character but is part of the
expression to match (yuk)
  2) We can no longer just write “Cheese is cheddar” by itself but are
forced to write “Cheese is cheddar and” if we want to match only the first
expression. You might as well just combined the two expressions into one
since there is no real advantage now to having two expressions!

One nice thing would be for the Guvnor DSL editor to 

  1)try the capture greedily first, 
  2)get the annoying “no viable alternative error”, 
  3)CATCH the error instead of just giving up, 
  4)capture non-greedily and match only the first word, 
  5)then search for matching DSL expressions beyond that.

Or something like that. The only time you should have to add quotes is if
the text you are matching actually contains multiple words. Anything else is
counter-intuitive. Please tell me the planned replacement for DSL addresses
this!

Jeff

--
View this message in context: http://drools.46999.n3.nabble.com/Catch-22-ANTLR-DSL-matching-issue-tp2993923p2993923.html
Sent from the Drools: User forum mailing list archive at Nabble.com.




More information about the rules-users mailing list