Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-1055

Regex-Special characters can not be used in DSLs.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 4.0.0.GA
    • Fix Version/s: 4.0.1
    • Component/s: drools-compiler
    • Labels:
      None
    • Environment:

      Windows XP, Java 5

    • Affects:
      Compatibility/Configuration
    • Workaround:
      Workaround Exists
    • Workaround Description:
      Hide

      Users could replace the square brackets with other characters, e.g. the angle brackets. I do not want to be the one who has to bring it to the users to use a new syntax, though.

      Show
      Users could replace the square brackets with other characters, e.g. the angle brackets. I do not want to be the one who has to bring it to the users to use a new syntax, though.
    • Estimated Difficulty:
      Low

      Description

      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@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.

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                tirelli Edson Tirelli
                Reporter:
                moxim Michael Wiedmer
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: