[rules-users] Catch 22 ANTLR DSL matching issue

2011-05-27 Thread jstroup
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.

___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] Catch 22 ANTLR DSL matching issue

2011-05-27 Thread jstroup
Correction: I meant the DSL Parser - Not the Guvnor DSL Editor. But the UI
could politely ask the user what alternative IS viable since it has access
to all of them.

--
View this message in context: 
http://drools.46999.n3.nabble.com/Catch-22-ANTLR-DSL-matching-issue-tp2993923p2993959.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] Catch 22 ANTLR DSL matching issue

2011-05-27 Thread Wolfgang Laun
See inline.

On 27 May 2011 20:19, jstroup jstr...@regenstrief.org wrote:

 Hi Droolers,

 Regarding this blurb from the 5.2 DSL reference:


 [snip]


 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


This will not do what the text implies because the expansion is
   Cheese(type==cheddar) Cheese(country===Italy)
and there is nothing that links these Cheese facts, which could
be one and the same but also any other pair matching the
individual constraints.



 I know that rule_2 fails because I removed the “distinctive characters” so
 ANTLR is confused on what to capture.


ANTLR has nothing to do with DSL expansion, which is entirely based on
regular expressions.


 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 DSL designer has a range of options (characters, buzz words,...) but at
the
end of the day (s)he will have to document it for the rule authors. There's
no
way they'll know what to write, out of the blue.


  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!


There is a feature for adding arbitrary constraints into a preceding
pattern.



 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.


Exactly. There is the fundamental problem of different levels of grammars
for DRL and natural languages, and this can't be overcome easily.



 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.


The DSL designer can avoid this, at the cost of slightly more verbose
phrases.



 Please tell me the planned replacement for DSL addresses
 this!


Feel free to contribute proposals (but not something like that, please).
Wolfgang



 Jeff


___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users