[rules-users] Re: variable 'or' constraints with a DSL
Hi Reid, There is a way to do this, but it doesn't involve the "-" (AND) hyphen shortcut. You would write a regular DSL, and the RHS of your rule would be one line. It's important to remember that DSL entry order is very important in your file. The domain-specific code you write can (either intentionally or unintentionally) be expanded many times before it arrives at the DRL language that is fed into the rules engine. In your case, the RHS of your rule will be tested once for a match against every "condition" entry in your DSL file. What this means is that you want to first write your rule in standard drools syntax, then work backward to see how expansion of DSL entries slowly moves your DSL rule closer to, and ultimately into, a DRL rule. You should be able to write something close to There is a person with {color} hair or with {color} eyes or that is {height} inches tall or that is {age} years old You *might* have to use the variable typing feature new to Drools 5.0, but you may not. A couple approaches that may help you out are... 1. Write a unit test, and pass your DSL code through the DSLExpander and see what DRL code it generates (which may or may not be valid, but at least you'll see what the DSL engine is doing to your DSL rule). 2. Download the source and use a step-through debugger with a breakpoint so you can examine the consequences of every match attempted against the RHS of the rule, and the consequent results of the replacement if a match is found. 3. If you don't want to run a debugger, you could write some AOP code to emit the results of the RHS with each step of the expansion, but this approach would be more work than just running a debugger. If you'd like to write a self-contained unit test with your DRL rule (the one that looks like the one I provided above, but verified to work), I can take a look at it and see what I'd do DSL-wise. Matt ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Re: variable 'or' constraints with a DSL
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 ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Re: variable 'or' constraints with a DSL
You should be able to write something close to There is a person with {color} hair or with {color} eyes or that is {height} inches tall or that is {age} years old Thanks for the strategies on writing DSL. I'm actually having good luck converting my domain specific code into DRL (using eclipse to play with it) and can see how I'd form DSL for the example above. The example is too simple for my needs, however. 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. How do I write DSL for that? I think answer is adding constraints with the dash syntax, which I brought up in my last email, BUT that doesn't help me if the user needs to specify "There is a person with {color} hair OR with {color} eyes". Let me get into specifics and leave the theoretical example behind. In my app, the user specifies any number of file attributes with which a file will be classified. For example, the user can create a policy that applies to files that are greater than 50 MB, or another that applies to files in directory abc OR xyz. With input from the user, it generates rules in a Domain Specific Language (that gets converted to DRL using our DSL file). The point of using DSL in this case is to isolate user-level concepts from actual implementation details. Snippet from my DSL file: [condition][File]file where=$f : File() [condition][File]- file_name matches {expr}=fileName matches {expr} [condition][File]- dir_name matches {expr}=dirName matches {expr} [condition][File]- group_name matches {expr}=gName matches {expr} [condition][File]- user_name matches {expr}=uName matches {expr} ... So if the user requests specifies jpg's owned by Bob, I can generate: file where - file_name matches "*.jpg" - user_name matches "Bob" Which generates appropriate 'when' syntax in DRL: $f : File(fileName matches "*.jpg", uName matches "bob") but I haven't figured out a good way to get ORs with the dash syntax to get something like this: $f : File(fileName matches "*.jpg" || uName matches "bob") Please let me know if there is a good way to do this. reid You *might* have to use the variable typing feature new to Drools 5.0, but you may not. A couple approaches that may help you out are... 1. Write a unit test, and pass your DSL code through the DSLExpander and see what DRL code it generates (which may or may not be valid, but at least you'll see what the DSL engine is doing to your DSL rule). 2. Download the source and use a step-through debugger with a breakpoint so you can examine the consequences of every match attempted against the RHS of the rule, and the consequent results of the replacement if a match is found. 3. If you don't want to run a debugger, you could write some AOP code to emit the results of the RHS with each step of the expansion, but this approach would be more work than just running a debugger. If you'd like to write a self-contained unit test with your DRL rule (the one that looks like the one I provided above, but verified to work), I can take a look at it and see what I'd do DSL-wise. Matt ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Re: variable 'or' constraints with a DSL
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} Matt, I'm catching on now -- Drools can make multiple substitutions on a line. I'll see what I can do with this. Thanks for your examples and help! reid ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users