[rules-users] Re: variable 'or' constraints with a DSL

Matt Geis mgeis at yahoo.com
Wed Jul 16 19:12:51 EDT 2008


Hi Reid,

>>The user has to be able to specify any of the attributes or any combination thereof and not necessarily in any order or all of them.  
[snip]
>>I think answer is adding constraints with the dash syntax

No, dash syntax is not the answer.  Clever construction of the DSL mapping will get you what you need.

Allow me to take your example and modify it.

[condition][File]file_name = fileName
[condition][File]dir_name = dirName
[condition][File]group_name = gName
[condition][File]user_name = uName
 
[condition][]file where {constraints}=$f: File(where {constraints})
[condition][]where {attr} matches {value}={attr} matches {value} 
[condition][]and {attr} matches {value}=,{attr} matches {value}
[condition][]or {attr} matches {value}= || {attr} matches {value}

So if the user requests specifies jpg's owned by Bob, you can generate:

file where file_name matches "*.jpg" and user_name matches "Bob"

Which generates appropriate 'when' syntax in DRL:

$f : File(fileName matches "*.jpg", uName matches "bob")

A good way to get ORs with the dash syntax to get something like this:

file where file_name matches "*.jpg" or user_name matches "Bob"

which expands to 

$f : File(fileName matches "*.jpg" || uName matches "bob")

Now, I mentioned that  you *might* have to use the variable typing feature new to Drools 5.0, but you may not.

If you do, the attribute lines might look a bit like this...

[condition][File](?i:where)\s+{attr:[A-Za-z0-9]+}\s+matches\s+{value:".*?"}={attr} matches {value} 
[condition][File](?i:and)\s+{attr:[A-Za-z0-9]+}\s+matches\s+{value:".*?"}=,{attr} matches {value}
[condition][File](?i:or)\s+{attr:[A-Za-z0-9]+}\s+matches\s+{value:".*?"}= || {attr} matches {value}

What you're seeing here is a few things.
The "?i" at the start of any literals makes it case-insensitive.  This, of course, is up to you.
The \s+ instead of a space matches on one or more whitespace characters.
The pattern after the variable name contstrains the match rather tightly.  I have found that you need this if you're trying to allow a user to enter an arbitrary number of constraints in whatever order they please.

Good luck!

Matt


      



More information about the rules-users mailing list