Assign either of value that exists or not null RuleInput($date = (this["startDate"] || this["startingDate"]))

44 Views Asked by At

Inside RuleInput there may be either "startDate" or "startingDate" , I need to assign value to $date variable either of the value that exists.

Something Like,

RuleInput($date = (this["startDate"] || this["endDate"]))

What would be the correct syntax for drools?

1

There are 1 best solutions below

0
Roddy of the Frozen Peas On

The correct syntax is to have two rules.

rule "With start date"
when
 RuleInput( $date: startDate != null, endDate == null )
then
  // do something
end

rule "With end date"
when
  RuleInput( $date: endDate != null, startDate == null )
then
  // do something
end

Since the rules are mutually exclusive you won't accidentally fire both.


As a side note, you used an interesting syntax in your question:

RuleInput( $value: this["foo"] )

The this["key"] syntax is very old, but it works with objects that have a method of the form public Object get(String key). It's most commonly used with Map. You'd hardly ever want to use it in modern drools (and you don't want to pass Maps into rules in general because they serialize very poorly.)