drools

Syntax to include null or blank within "IN" list?


What is the proper syntax to include a blank or null option within an IN list?
I've unsuccessfully tried various options with the intent to say:: If CLASS-A = *ABC *and CLASS-B is either VALUE1, VALUE2, *VALUE3 *-- or there is NO CLASS-B...

I've tried NULL, null, Null, as well as "" and " " - thought maybe there is something obvious I am missing.

calcrt.get("CLASS-A") == ("ABC") &&
    calcrt.get("CLASS-B") in ("VALUE1" , "VALUE2" , "VALUE3", NULL)

Appreciate any guidance - total noob here!


Solution

  • You can use both null and "" in a list with in operator:

    MyObject( someValue in (null, "", "1", "something", "hello world!") )
    

    Will match:

    Will not match:


    That being said, if you're working with a Map and you're checking that something is not present, you may want to consider the case that a 'null' for get("Class-B") might not mean the key is not present, but rather that the value is null. In that case you might consider checking that "Class-B" is present in the keyset.

    rule "no class-b"
    when
      $inputs: Map( this["CLASS-A"] == "ABC" )
      Set( this not contains "CLASS-B" ) from $inputs.keySet()
    then
      System.out.println("CLASS-A is ABC and there is no CLASS-B");
    end
    
    rule "class-b meets some criteria"
    when
      $inputs: Map( this["CLASS-A"] == "ABC",
                    this["CLASS-B"] in ("VALUE1", "VALUE2", "VALUE3") )
    then
      System.out.println("CLASS-A is ABC and CLASS-B is VALUE1/2/3");
    end