Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it support running rules on complex objects? #30

Closed
durja opened this issue Dec 22, 2021 · 6 comments
Closed

Does it support running rules on complex objects? #30

durja opened this issue Dec 22, 2021 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@durja
Copy link

durja commented Dec 22, 2021

Does it support running rules on complex objects? Like running rules on address.country == 'USA"?

{ "name": "test_user", "email_address": "test@gmail.com", "address": { "address_1": "high st", "address_2": "unit-4", "state": "AZ", "country": "USA" } }

Can you give an example as i see complex types are supported

@zeroSteiner
Copy link
Owner

Your best bet is going to be in the Getting Started page. https://zerosteiner.github.io/rule-engine/getting_started.html

What your showing should work with the default getitem resolver. You can experiment with it using the debug repl https://zerosteiner.github.io/rule-engine/debug_repl.html

I'm enjoying the holidays now and won't be able to provide a more detailed example until I'm back at my computer next week. I don't see any issue with the example you provided yourself. All the data types I see are fully supported.

@zeroSteiner
Copy link
Owner

Here's a larger example using the debug repl.

PYTHONPATH=$(pwd)/lib python -m rule_engine.debug_repl --edit-console
edit the 'context' and 'thing' objects as necessary
>>> thing = { "name": "test_user", "email_address": "test@gmail.com", "address": { "address_1": "high st", "address_2": "unit-4", "state": "AZ", "country": "USA" } }
>>> exit()
exiting the edit console...
rule > address.country == 'USA'
result: 
True
rule > email_address =~ '@gmail\.com$'
result: 
False
rule > email_address =~~ '@gmail\.com$'
result: 
True
rule > 'state' in address
result: 
True
rule > address['state'].as_lower == 'az'
result: 
True
rule > 

You can access MAPPING keys using the dot syntax like mapping.key, and that's mostly kept for backwards compatibility purposes. There could be name collisions with attributes though which is why I recommend you use the other syntax like mapping['key']. The default context should work just fine for this since it uses the getitem resolver.

@zeroSteiner zeroSteiner added the question Further information is requested label Dec 28, 2021
@zeroSteiner zeroSteiner self-assigned this Dec 28, 2021
@durja
Copy link
Author

durja commented Dec 29, 2021

Thank you for this example. It's very helpful. In the below example, is it possible to match all users whose friends association == 'school'

`

python -m rule_engine.debug_repl --edit-console
edit the 'context' and 'thing' objects as necessary
thing = {
"user_id": "abc123",
"address": {
"address_1": "123 high st",
"address_2": "unit 101",
"city": "jersey city",
"state": "nj",
"country": "usa"
},
"friends": [
{
"user_id": "def123",
"association": "school",
},
{
"user_id": "ghi123",
"association": "college",
},
{
"user_id": "jkl789",
"association": "school",
}
]
}... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
exit()
exiting the edit console...
rule > address.country == 'usa'
result:
True
rule > friends[0].association == 'school'
result:
True
rule > friends.association == 'school' 🤔 Is there a way to do something like this and get a filtered list of friends?
`

@zeroSteiner
Copy link
Owner

Yeah you should be able to use array comprehension (like Python's own list comprehension) to do that. https://zerosteiner.github.io/rule-engine/syntax.html#array-comprehension

Something like:
[ friend for friend in friends if friend.association == 'school']

The resulting expression will be a list so you can check the length on it and do stuff like that.

@durja
Copy link
Author

durja commented Dec 29, 2021

In my specific case, I am trying to create a generic function that can filter a list without knowing the type of objects it has. Thanks for answering my questions.

@zeroSteiner
Copy link
Owner

Cool so it sounds like you're all set so I'll close this out. If you have any other questions just post them here or in a new issue if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants