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
475 stars 56 forks source link

How to use the whole project? #3

Closed aishjain closed 4 years ago

aishjain commented 4 years ago

Hi, I am just beginning with rule engines, honestly, it's the first time I am coming across it, I see you have added examples in the repository but can you please elaborate the readme file on how to use them and what do they actually do?It would be really helpful for beginners like me. And sorry for putting it up as an issue, didn't know where else to ask you for the help. Thank you!

zeroSteiner commented 4 years ago

Yeah the documentation could probably use some elaboration :sweat_smile: . It's kinda tough to describe how to use it, because it's abstract. You have a one or more "things" in Python that follow a structure like a list of people described as dictionaries, books described by objects, etc. and you can filter them using a rule defined by either you or the end user. That last part's important, rules are intended to be safely written by the end user without any kinds of injection flaws that might come up from doing something like eval or exec.

Take a look at the examples/ directory while I look at adding more documentation. The csv_filter and github_filter are probably the two most simple ones. For the CSV one, you can simply load a CSV file that has column names as the first row, and then apply filters on to the rows. Keep in mind though that all data is treated as strings though, so you can't do integer comparison in this particular usage.

I hope that helps you get started, it might be a few days until I can really write some more docs.

aishjain commented 4 years ago

Thank you for the quick reply, I tried running the csv_filter.py file, I end up getting errors: usage: csv_filter.py [-h] csv_file rule csv_filter.py: error: the following arguments are required: csv_file, rule

D:\IoT\rule-engine-master\rule-engine-master\examples>python csv_filter.py new.csv rule Traceback (most recent call last): File "csv_filter.py", line 81, in sys.exit(main()) File "csv_filter.py", line 76, in main for row in rule.filter(csv_reader): File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\rule_engine\engine.py", line 425, in filter yield from (thing for thing in things if self.matches(thing)) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\rule_engine\engine.py", line 425, in yield from (thing for thing in things if self.matches(thing)) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\rule_engine\engine.py", line 469, in matches return bool(self.statement.evaluate(thing)) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\rule_engine\ast.py", line 758, in evaluate return self.expression.evaluate(thing) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\rule_engine\ast.py", line 718, in evaluate value = self.context.resolve(thing, self.name, scope=self.scope) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\rule_engine\engine.py", line 346, in resolve return self.__resolver(thing, name) File "csv_filter.py", line 54, in resolve_item return rule_engine.resolve_item(thing, name) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\rule_engine\engine.py", line 87, in resolve_item raise errors.SymbolResolutionError(name, thing=thing) rule_engine.errors.SymbolResolutionError: rule

Basically I am not understanding what is meant by passing the argument called rule, what exactly is expected here.?and can you please provide a sample CSV file that can be used here? It might be a very basic question, but it would be a great help for me in understanding your work. Thank ypu!

zeroSteiner commented 4 years ago

That's saying that the symbol rule is not defined in your CSV file. That example expects that the first row is the name of each of the vars. If you're familiar with say Star Wars, you might have a CSV file to describe the characters like this:

test.csv:

firstName,lastName,affiliation,alive
Luke,Skywalker,Jedi,true
Darth,Vader,Sith,false
Han,Solo,Rebel Alliance,true

You could then query that CSV file to find characters that are alive with alive == 'true', or characters that are rebels with affiliation == 'Rebel Alliance' as the rule. Notice that in this case alive and affiliation are symbols defined in our CSV file and that are are strings (i.e. alive is not a boolean, we'd need to load a JSON file for that).