Michael Plautz
Michael Plautz

Reputation: 3778

Drools - How to check if a fact exists in a decision table

I am using Drools 5.5.0, and I have a decision table, demonstrated below:

Example Drools Decision Table

When I run the rules engine, I only ever insert one SecurityContext and once JSONWrapper at a time.

Based on this table alone, none of my rules ever get fired (however they all get evaluated). I believe this is because for the second condition, the cells are blank for each role/rule.

This is what I am trying to go for, in DRL:

package com.acme.security.rules.widget;

import com.acme.test.RuleTableTest.SecurityContext; 
import com.acme.test.RuleTableTest.JSONWrapper;

rule "widget_accessibility_store_manager"
  when
    $sc : SecurityContext()
    $output : JSONWrapper()
    eval($sc.hasRole("Store Manager"))
  then
    $output.setFeatureVisibility("feature1", "yes");
    $output.setFeatureVisibility("feature2", "yes");
    $output.setFeatureVisibility("feature3", "yes");
    $output.setFeatureVisibility("feature4", "yes");
    $output.setFeatureVisibility("feature5", "yes");
end

This rule fires just fine.

How do I have a condition in my decision table that just checks for the presence of an object, without any other constraints? (Just like in my DRL) I need this object so I can use it as an output in the action statements. I also am trying to leave the cells for each rule in the column blank for simplicity.

Upvotes: 1

Views: 3988

Answers (2)

greenJavaDev
greenJavaDev

Reputation: 1037

I know it's too late for original poster at this point, but confronted with a similar problem my solution was to look for a value in that object that for sure isn't going to be null. For example:

rule "widget_accessibility_store_manager"
when
    $sc : SecurityContext(role != null)
    $output : JSONWrapper(featureVisibility!=null)
    eval($sc.hasRole("Store Manager"))

then
    $output.setFeatureVisibility("feature1", "yes");
    $output.setFeatureVisibility("feature2", "yes");
    $output.setFeatureVisibility("feature3", "yes");
    $output.setFeatureVisibility("feature4", "yes");
    $output.setFeatureVisibility("feature5", "yes");
end

And then your excel file would look kind of like this:

enter image description here

If you don't have mandatory parameters on these objects, you may even be able to skip the inner parameter and check if SecurityContext and itself is null. Alternatively you could check for "exists SecurityContext".

Upvotes: 0

laune
laune

Reputation: 31290

It would be more convenient to create the "output object" on the right and side, and you can insert it or pass it to a global collection.

The somewhat contrived workaround for including a condition for the mere presence of a fact looks like this:

CONDITION
$output : JSONWrapper
/*$param*/
mark below to force inclusion
x

Note that you can join cells vertically.

Upvotes: 2

Related Questions