0

I have created a rule

rule "NullCheck_Rule1"
salience 1
dialect "mvel"
date-effective "16-Nov-2017 00:00"
no-loop  
when 
$cdr : JSONObject( $cdr.optString("name") == null)   
then
$cdr.put("Action_1" ,"SEND MAIL");end

and for not null

rule "NullCheck_Rule1"
salience 1
dialect "mvel"
date-effective "16-Nov-2017 00:00"
no-loop  
when
$cdr : JSONObject( $cdr.optString("name") != null)   
then
$cdr.put("Action_1" , "SEND MAIL");end

and my input is something like this "{\"id\":100,\"name\":null,\"city\":\"Pune\"}"

But its not matching the conditions even if I used condition like $cdr.optString("name") IS NULL OR $cdr.optString("name") IS NOT NULL

So how to handle null and not null in drools. The version of drools which I am using is 6.5.0.Final.

  • Several possibilities: (1) there is no JSONObject ni Working Memory. (2) The JSONObject does not have a component optString. (3) The rule fires but you have failed to check correctly that the `put` works. – laune Nov 17 '17 at 07:08
  • For the point (1) I am converting JSONString to JSONObject. So there is JSONObject in working memory (2) The optString is available in JSONObject. You can refer this https://developer.android.com/reference/org/json/JSONObject.html – Jayanth S N Nov 17 '17 at 11:30
  • Use a rule with just `JSONObject()` - i.e., no constraint - and a println of the result of `$cdr.optString("name")` as the consequence. Does it fire? What does it print? -- If it doesn't fire, you don't have such a fact in Working MEmory. – laune Nov 17 '17 at 19:29
  • It works If my rule is `JSONObject( $cdr.optString("name") == "")` and even with `JSONObject( $cdr.isNull("name"))` but not with `JSONObject( $cdr.optString("name") == null)` – Jayanth S N Nov 21 '17 at 06:37
  • Not using any JSON but coding plain Java + DRL, both rules (using ==null and !=null) work in 5.5.0. - If it is a bug, it won't be fixed in that old version. If you add your Java (!) class for JSONObject to the question, I might have a look at it. – laune Nov 21 '17 at 09:45

0 Answers0