zeroSteiner / rule-engine

A lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects.
https://zerosteiner.github.io/rule-engine/
BSD 3-Clause "New" or "Revised" License
445 stars 54 forks source link

Does it support the bracket symbol #29

Closed TaurusYin closed 2 years ago

TaurusYin commented 2 years ago

Excuse me, I would like to use "(" or ")" to control the privilege of this expression block. Will it support? (mysql.cpu_usage.feature in ["spike", "shift_up", "trend_up"] or mysql.cpu_usage.value > 90) and (mysql.active_session.feature in ["shift_up", "trend_up"] or mysql.active_session.value > 16)

zeroSteiner commented 2 years ago

Yes paranthesis are supported and can be used to alter the order of operations as they do in Python and other languages.

TaurusYin commented 2 years ago

Yes paranthesis are supported and can be used to alter the order of operations as they do in Python and other languages.

thanks for reply. Is there an example to use paranthesis? I tried the expression above but the order seems not correct.

TaurusYin commented 2 years ago

Yes paranthesis are supported and can be used to alter the order of operations as they do in Python and other languages.

It looks like the privilege of the paranthesis does not work as I tried to use multiple paranthesis

zeroSteiner commented 2 years ago

Try 1 + (2 * 4) vs (1 + 2) * 4 and you'll see a difference. There's a debug repl tool that you can use for testing this.

Details are here https://zerosteiner.github.io/rule-engine/debug_repl.html

zeroSteiner commented 2 years ago

Here's a demo:

PYTHONPATH=$(pwd)/lib python -m rule_engine.debug_repl --edit-console
edit the 'context' and 'thing' objects as necessary
>>> thing = {'mysql': {'cpu_usage': {'feature': 'spike'}, 'value': 90, 'active_session': {'feature': 'shift_up', 'value': 0}}}
>>> exit()
exiting the edit console...
rule > (mysql.cpu_usage.feature in ["spike", "shift_up", "trend_up"] or mysql.cpu_usage.value > 90) and (mysql.active_session.feature in ["shift_up", "trend_up"] or mysql.active_session.value > 16)
result: 
True
rule > 1 + (2 * 4)
result: 
Decimal('9.00')
rule > (1 + 2) * 4
result: 
Decimal('12.00')
rule >