Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-3880

Bug when parsing fact from String to Double in Drools

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • Major
    • None
    • 7.0.0.Final
    • core engine
    • None
    • 2019 Week 14-16
    • Hide
      rule "rule_1"
      dialect "mvel"
      salience -10
      
      when
      
      then
          Resource resource = new Resource();
          resource.setAmount("10"); //note - this accepts only Strings
          insert(resource);
      end
      
      rule "rule_2"
      dialect "mvel"
      salience -20
      
      when
          $resource: Resource(Double.parseDouble(amount) > 0.0) //exception
          //$resource: Resource(amount == "10") - no error here
      then
          System.out.println("$resource.value = " + $resource.amount);
      end
      
      

      Exception is: Caused by: [Error: Double.parseDouble(amount): null]
      [Near :

      {... Double.parseDouble(amount) > 0 ....}

      ]
      --------------^

      Show
      rule "rule_1" dialect "mvel" salience -10 when then Resource resource = new Resource(); resource.setAmount( "10" ); //note - this accepts only Strings insert(resource); end rule "rule_2" dialect "mvel" salience -20 when $resource: Resource( Double .parseDouble(amount) > 0.0) //exception //$resource: Resource(amount == "10" ) - no error here then System .out.println( "$resource.value = " + $resource.amount); end Exception is: Caused by: [Error: Double.parseDouble(amount): null] [Near : {... Double.parseDouble(amount) > 0 ....} ] --------------^
    • NEW
    • NEW

    Description

      When one inserts a new object in drl file and afterward wants to compare its String representation of a number with another number in next rules Drools gives an exception. It is much clearer with an example (below in code section)

      Interesting that when you compare it as a String (amount == "10") - it works fine. But when you try to parse "amount" to Double then Java doesn't know that there is any value at all (thinks amount == null and therefore cannot parse it). The second interesting thing is that if you created this object (resource) in java and inserted it before "fireAllRules()" - "amount" field become visible to the parser and it works.
      But the matter is that I cannot create this object before firing rules because I don't know wheter there will be a need of it. And my fields must be String as well. And I have to compare it like "more than" or "less than", so comparing strings doesn't apply.

      Attachments

        Activity

          People

            mfusco@redhat.com Mario Fusco
            almastoimbekov Almas Toimbekov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: