Multiple boolean conditions in a rule in drools?
Asked Answered
U

1

4

I have created a rule whose "when" condition is as follows :-

when
    $map: Map(this["key1"].equals("value1")) and Map(this["key2"].equals("value2"))  
then
...

The above condition is working fine. Now how do I add multiple boolean conditions in a rule? For eg. The above rule could be summarized as : a and b so if I want to create a rule : (a and b) or c then what would be actual drl syntax for it. I am new to drools so kindly help me with the syntax of the rule (a and b) or c.

I did create a syntax

when
    $map: Reindexing((Map(this["key1"].equals("value1")) and Map(this["key2"].equals("value2"))) or  Map(this["key3"].equals("value3"))) 
then

But I got the following exception

Error Messages: Message [id=1, level=ERROR, path=mapIterationRules.drl, line=13, column=0 text=[ERR 101] Line 13:21 no viable alternative at input '(' in rule "first rule"]

Thanks

Upsurge answered 17/6, 2016 at 10:36 Comment(4)
What is Reindexing supposed to be or do? You have facts of type Map. Using a class name within a pattern using another class name isn't valid syntax. Check the documentation.Inapplicable
Yes, we need more information about what the rule should do. Currently I'm not sure if you are checking if reindexing worked or if you want to reindex.Amenable
Reindexing is the name of the class that has this map, I guess it has nothing to do here, i was just experimenting with the correct syntax. The "a and b" rule worked perfectly so on similar grounds, i was experimenting the syntax of "(a and b) or c" rule. And @Inapplicable so if I cannot use two class names(applogies for that) then how do I create the second rule, I tried with the rule "(Map(this["key1"].equals("value1")) and Map(this["key2"].equals("value2"))) or Map(this["key3"].equals("value3"))" but this also gave me the same error.Upsurge
Hardly the same error, possibly one with the same wording but at a different location. Don't use and and or conditional element operators if all constraints should relate to the same Map - use the constraint logical operators || and &&. Again, check the documentationInapplicable
U
7

Figured out the syntax for the above rule. thanks laune and toni for the help.

here is the syntax

when
    $map: Map( this["data1"].equals("dataOutput1") ) || Map( this["data2"].equals("dataOutput2") && this["data3"].equals("dataOutput3") )

when inside the same bracket, it is not required to type the class name again.

Upsurge answered 22/6, 2016 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.