[jboss-jira] [JBoss JIRA] Commented: (JBRULES-1055) Regex-Special characters can not be used in DSLs.

Edson Tirelli (JIRA) jira-events at lists.jboss.org
Wed Aug 22 11:26:18 EDT 2007


    [ http://jira.jboss.com/jira/browse/JBRULES-1055?page=comments#action_12373328 ] 
            
Edson Tirelli commented on JBRULES-1055:
----------------------------------------

This is actually a documentation problem and not a bug. DSL was augmented in Drools 4.0 to support regexps as part of the mappings. This intents to make DSL mappings more robust and allowing users to handle more situations with less effort. For instance, if you want to handle multiple values, you could write a mapping like:

[when][]Customer code is [ABCD]=Customer( code == "premium")

The above mapping will match any of the following statements:

Customer code is A
Customer code is B
Customer code is C
Customer code is D

As [ABCD] is interpreted as a regexp.

So, in your case, the only fix you need to do is escape your [] in the same way you escape them in regular expressions. Look at the following example:

[when][]attr {attr_name} is in \[ {values} \]={attr_name} in ( {values} )

The above mapping will match:

attr name is in [ 'Edson', 'Bob' ]

And translate that into:

name in ( 'Edson', 'Bob' )

I will look at fixing documentation. This is not backward compatible, but we feel the added flexibility was worth breaking the compatibility in the major version release.


> Regex-Special characters can not be used in DSLs.
> -------------------------------------------------
>
>                 Key: JBRULES-1055
>                 URL: http://jira.jboss.com/jira/browse/JBRULES-1055
>             Project: JBoss Rules
>          Issue Type: Bug
>      Security Level: Public(Everyone can see) 
>          Components: Drl Parser/Builder
>    Affects Versions:  4.0.0.GA
>         Environment: Windows XP, Java 5
>            Reporter: Michael Wiedmer
>         Assigned To: Edson Tirelli
>             Fix For: 4.0.1
>
>
> Up until Drools 3.0.6 one could write DSLs to allow expressions such as
> EXAMPLE IS IN [ "one" "two"]
> This has changed now. The square brackes (being part of special characters for regular expressions) deny the DefaultDSLMappingEntry class to define a valid keyPattern.
> Here is a (not) working example, using the default sample project:
> 8<---- regexError.drl -----
> package some.test
> import com.sample.*;
> expander regexError.dsl
> rule "Test"
> 	when
> 		STRING "UK" IS IN < "USA", "UK", "GERMANY" >
> 	then 
> 		LOG "Square Brackets will not work."
> end
> 8<---- regexError.dsl -----
> [consequence][]LOG {msg}=System.out.println({msg});
> [condition][]STRING "{string}" IS IN < {sequence} >=eval(true == true)
> 8<---- DroolsTest.javal -----
> package com.sample;
> import java.io.InputStreamReader;
> import java.io.Reader;
> import org.drools.RuleBase;
> import org.drools.RuleBaseFactory;
> import org.drools.WorkingMemory;
> import org.drools.compiler.PackageBuilder;
> import org.drools.rule.Package;
> public class DroolsTest {
>     public static final void main(String[] args) {
>         try {
>             // load up the rulebase
>             RuleBase ruleBase = readRule();
>             WorkingMemory workingMemory = ruleBase.newStatefulSession();
>             // go !
>             Message message = new Message();
>             message.setMessage("Hello World");
>             message.setStatus(Message.HELLO);
>             workingMemory.insert(message);
>             workingMemory.fireAllRules();
>         } catch (Throwable t) {
>             t.printStackTrace();
>         }
>     }
>     private static RuleBase readRule() throws Exception {
>         Reader source = new InputStreamReader(DroolsTest.class.getResourceAsStream("/regexError.drl"));
>         Reader dsl = new InputStreamReader(DroolsTest.class.getResourceAsStream("/regexError.dsl"));
>         PackageBuilder builder = new PackageBuilder();
>         builder.addPackageFromDrl(source, dsl);
>         Package pkg = builder.getPackage();
>         RuleBase ruleBase = RuleBaseFactory.newRuleBase();
>         ruleBase.addPackage(pkg);
>         return ruleBase;
>     }
> }
> If one was to change the files regexError.dsl and regexError.drl and use square brackets instead of angle brackets, the DroolsTest will fail with the same error as shown in the Eclipse IDE:
> org.drools.rule.InvalidRulePackage: org.drools.lang.ExpanderException at 14c1103[9,2]: unknown:9:2 Unexpected token 'STRING'[9,22]: unknown:9:22 mismatched token: [@31,115:119='"USA"',<20>,9:22]; expecting type RIGHT_SQUARE[11,1]: unknown:11:1 mismatched token: [@43,150:155='System',<8>,11:1]; expecting type RIGHT_PAREN[11,20]: unknown:11:20 mismatched token: [@49,169:200='"Square Brackets will not work."',<20>,11:20]; expecting type RIGHT_PAREN[12,0]: unknown:12:0 mismatched token: [@53,205:207='end',<15>,12:0]; expecting type THEN
> 	at org.drools.rule.Package.checkValidity(Package.java:408)
> 	at org.drools.common.AbstractRuleBase.addPackage(AbstractRuleBase.java:288)
> 	at com.sample.DroolsTest.readRule(DroolsTest.java:44)
> 	at com.sample.DroolsTest.main(DroolsTest.java:18)
> It does not matter what the RHS of the DSL says, as the LHS will not be matched in DefaultExpander.expandLHS(final String lhs, int lineOffset), line 231.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://jira.jboss.com/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the jboss-jira mailing list