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

Any way to show calculated values? #96

Closed kuldeeps48 closed 1 week ago

kuldeeps48 commented 1 week ago

I have a requirement to show the actual final values of my rule along with the result. Eg:

comics = {
    "issue": 89,
    "test": 45
}

rule = rule_engine.Rule(
    "issue - test > 10",
)
print(rule.matches(comics))

I need to show the 'True', 'False' but also the actual value of (issue-test) expression. Is it possible to access this info? Through AST or any final object before it is evaluated?

Thank you!

zeroSteiner commented 1 week ago

Kind of. So you're probably use to using the Rule.matches method which returns a boolean as would be the final result of your expression. There's also a Rule.evaluate method that returns the value of the final result of expression evaluation. In your case though, that's still going to be a boolean. If your rule was simply issue - test then it'd return 44.

What you can't do is reach into the AST and determine the expression evaluation at a particular stage / for a particular node.

kuldeeps48 commented 1 week ago

Thank you! That's helpful. I can ask them to take expressions to evaluate for calculation, which would be separate from the rule.