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

Support for INTERSECT? #11

Closed kuldeeprishi closed 3 years ago

kuldeeprishi commented 3 years ago

Hi, is it possible to add support for intersect? eg:

 rule_engine.Rule('role INTERSECT {"R1", "R2"}').matches({"role": ["R1"]})
zeroSteiner commented 3 years ago

I like this idea but I'd like to see it either implemented as:

  1. A proper set datatype with support for bitwise operations like my_roles & their_roles
  2. Using list comprehension like r for r in my_roles if r in their_roles

Neither of those would be particularly easy to implement so it might be a little while before I get around to this.

fran6w commented 3 years ago

Hello, I am also interested in the intersect feature between arrays. My need would be to know if 2 lists have any element in common. Thank you!

iuvbio commented 3 years ago

+1 for set support

zeroSteiner commented 3 years ago

I've started the work for a SET data type which would support the bitwise operators and, or and xor (&, |, and ^ respectively). Usage of the bitwise and operator will have the desired effect of checking for the intersection of two SETs just like it does in Python. There will be accompanying attributes for converting arrays and strings into sets. This will be in the 3.1 release (probably) in the next couple of weeks. A preview is available in the feat/v3.1 branch.

I'd still like to have array comprehension support, but that can come later. These two features don't seem like they need to be mutually exclusive and a SET data type will solve the problem at hand more directly.

zeroSteiner commented 3 years ago

Alright this is done in the v3.1.0 release which is now on PyPi. You can use the new SET data type along with the bitwise and operator &. For example:

python -m rule_engine.debug_repl
rule > {"R1", "R2", "R3"} & {"R3", "R4"}
result: 
{'R3'}
rule > 'Testing'.to_set & {'a', 'e', 'i', 'o', 'u'}
result: 
{'e', 'i'}
rule > 

Both the ARRAY and STRING data types have a to_set attribute that will convert them as well.