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
433 stars 54 forks source link

Rule compare #48

Closed scainet closed 1 year ago

scainet commented 1 year ago

May be interesting make Rule comparison

r1 = rule_engine.Rule("a == 'HELLO' and b == 'BYE'")
r2 = rule_engine.Rule("b == 'BYE' and a == 'HELLO'")
r1 == r2
True
zeroSteiner commented 1 year ago

What would the use case be for this?

0ge commented 3 months ago

I would be interested in this.

My use case is the following: I have a list of tuples containing a rule and an action. If there are two matching rules, only the first action will be executed.

As a sanity check I would like to iterate over the rules to identify rules that would never be evaluated. Something like this:

r1 = rule_engine.Rule("a == 'HELLO'")
r2 = rule_engine.Rule("a == 'HELLO' and b == 'WORLD'")

if r1 > r2:
     print("Warning: Rule `r2` will never be evaluated.")

(Not sure the normal comparison operators are very intuitive here)