How to create a custom function? #90
Unanswered
eshan-pragma
asked this question in
Q&A
Replies: 1 comment 4 replies
-
The type resolver isn't the issue. That's only necessary if you want to type hinting. Your function definition for import rule_engine
orders = [
{
'id': 1,
# ...
},
{
'id': 2,
},
{
'id': 3,
},
{
'id': 4,
},
]
def past_orders_found(id):
return id in [2,3]
class CustomContext(rule_engine.Context):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.builtins = rule_engine.builtins.Builtins.from_defaults(
{'past_orders_found': past_orders_found}
)
rule = rule_engine.Rule('$past_orders_found(id) == true', CustomContext())
for order in orders:
print("order#{} result: {}".format(order['id'], rule.matches(order))) If you didn't want it as a builtin symbol, you could tweak it slightly: def past_orders_found(id):
return id in [2,3]
rule = rule_engine.Rule('past_orders_found(id) == true')
for order in orders:
print("order#{} result: {}".format(order['id'], rule.matches({'past_orders_found': past_orders_found} | order))) A function is just a symbol that's callable, so you pass it around the same as any other by either adding it to the namespace of the thing that's evaluated Rule.matches or the builtin symbols. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Please consider the following data
and a custom function:
Now i want to evaluate the data via my custom function like:
I am unable to achieve this. I also tried creating a
type_resolver
and then pass thecontext
to therule
like below, and still did not work.Beta Was this translation helpful? Give feedback.
All reactions