# Rules Engine
![PyPI](https://img.shields.io/pypi/v/funnel-rules-engine)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/funnel-rules-engine)
![PyPI - Status](https://img.shields.io/pypi/status/funnel-rules-engine)
![PyPI - License](https://img.shields.io/pypi/l/funnel-rules-engine)
[![Python package](https://github.com/funnel-io/funnel-rules-engine/actions/workflows/python-package.yml/badge.svg)](https://github.com/funnel-io/funnel-rules-engine/actions/workflows/python-package.yml)
A simple, generic rules engine. Inspired by [Martin Fowler's 2009 blog post](https://www.martinfowler.com/bliki/RulesEngine.html).
This package was extracted from [Funnel](https://funnel.io)'s production code and made open source.
For the story behind its inception, raison d'être, and overall concept, you can watch the talk [Rules Rule](https://youtu.be/Lsi1ZhmbNDc) held by [Lennart Fridén](https://github.com/DevL) at [PyCon Sweden](https://www.pycon.se) 2021.
## Installation
Install the package `funnel_rules_engine` version `1.3+` from PyPi.
The recommended `requirements.txt` line is `funnel_rules_engine~=1.3`.
## Current Functionality
### `Rule`
A combination of a condition and an action, both of which are callables such as functions or lambdas. The callables must have an arity of one. If the conditional returns true given a single object, the action will be called with the same object as its argument.
When creating a rule, it can also be tagged with a list of tags (see `RulesEngine.reject` and `RulesEngine.select`).
#### `Otherwise`
Special case of Rule where the action always fires. Suitable as a catch-all last rule.
#### `NoAction`
Special case of Rule where `None` is returned if the condition is met. Useful for halting the execution of `.run()`
### `RulesEngine`
A generic rules engine that accepts a list of `Rule`s and some input to apply the rules to. The rules engine can either apply the first rule that matches (`run`) or all the rules that matches (`run_all`). In addition, rules can be evaluated and executed in parallel (`run_all_in_parallel`). The latter two cases can optionally be lazily executed by returning a generator rather than a list as the result.
For more on rules engines, see [Martin Fowler's blog post](https://martinfowler.com/bliki/RulesEngine.html).
#### `run`
Only apply the first rule that matches and return its result. This is comparable to the behaviour of a [structured switch statement](https://en.wikipedia.org/wiki/Switch_statement#Semantics) or an arbitrary [conditional statement](https://en.wikipedia.org/wiki/Conditional_(computer_programming)).
#### `run_all`
Apply all rules that match. The result is returned as a list, or as a generator if the optional parameter `lazy` is passed as `True`.
#### `run_all_in_parallel`
Evaluate and apply all rules in parallel. The result is returned as a list, or as a generator if the optional parameter `lazy` is passed as `True`.
#### `select` and `reject`
These methods return a new rules engine containing the rules matching/not matching the given tags.
### `when` and `then`
These are convenience functions for creating simple conditions and actions. They both accept a single value that in the case of `when` will be used to check equality with the passed state and in the case of `then` will be returned, ignoring the passed state.
Raw data
{
"_id": null,
"home_page": "https://github.com/funnel-io/funnel-rules-engine",
"name": "funnel-rules-engine",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "[\"rules engine\"]",
"author": "The Funnel Dev Team",
"author_email": "open-source@funnel.io",
"download_url": "https://files.pythonhosted.org/packages/02/98/67a6b67bbecbfeec5d683d9e54e1fae5af66a2853eb3a553b528e201c686/funnel_rules_engine-1.3.1.tar.gz",
"platform": null,
"description": "# Rules Engine\n\n![PyPI](https://img.shields.io/pypi/v/funnel-rules-engine)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/funnel-rules-engine)\n![PyPI - Status](https://img.shields.io/pypi/status/funnel-rules-engine)\n![PyPI - License](https://img.shields.io/pypi/l/funnel-rules-engine)\n[![Python package](https://github.com/funnel-io/funnel-rules-engine/actions/workflows/python-package.yml/badge.svg)](https://github.com/funnel-io/funnel-rules-engine/actions/workflows/python-package.yml)\n\nA simple, generic rules engine. Inspired by [Martin Fowler's 2009 blog post](https://www.martinfowler.com/bliki/RulesEngine.html).\n\nThis package was extracted from [Funnel](https://funnel.io)'s production code and made open source.\n\nFor the story behind its inception, raison d'\u00eatre, and overall concept, you can watch the talk [Rules Rule](https://youtu.be/Lsi1ZhmbNDc) held by [Lennart Frid\u00e9n](https://github.com/DevL) at [PyCon Sweden](https://www.pycon.se) 2021.\n\n\n## Installation\n\nInstall the package `funnel_rules_engine` version `1.3+` from PyPi.\nThe recommended `requirements.txt` line is `funnel_rules_engine~=1.3`.\n\n## Current Functionality\n\n### `Rule`\n\nA combination of a condition and an action, both of which are callables such as functions or lambdas. The callables must have an arity of one. If the conditional returns true given a single object, the action will be called with the same object as its argument.\n\nWhen creating a rule, it can also be tagged with a list of tags (see `RulesEngine.reject` and `RulesEngine.select`).\n\n#### `Otherwise`\n\nSpecial case of Rule where the action always fires. Suitable as a catch-all last rule.\n\n#### `NoAction`\n\nSpecial case of Rule where `None` is returned if the condition is met. Useful for halting the execution of `.run()`\n\n### `RulesEngine`\n\nA generic rules engine that accepts a list of `Rule`s and some input to apply the rules to. The rules engine can either apply the first rule that matches (`run`) or all the rules that matches (`run_all`). In addition, rules can be evaluated and executed in parallel (`run_all_in_parallel`). The latter two cases can optionally be lazily executed by returning a generator rather than a list as the result.\n\nFor more on rules engines, see [Martin Fowler's blog post](https://martinfowler.com/bliki/RulesEngine.html).\n\n#### `run`\n\nOnly apply the first rule that matches and return its result. This is comparable to the behaviour of a [structured switch statement](https://en.wikipedia.org/wiki/Switch_statement#Semantics) or an arbitrary [conditional statement](https://en.wikipedia.org/wiki/Conditional_(computer_programming)).\n\n#### `run_all`\n\nApply all rules that match. The result is returned as a list, or as a generator if the optional parameter `lazy` is passed as `True`.\n\n#### `run_all_in_parallel`\n\nEvaluate and apply all rules in parallel. The result is returned as a list, or as a generator if the optional parameter `lazy` is passed as `True`.\n\n#### `select` and `reject`\n\nThese methods return a new rules engine containing the rules matching/not matching the given tags.\n\n### `when` and `then`\n\nThese are convenience functions for creating simple conditions and actions. They both accept a single value that in the case of `when` will be used to check equality with the passed state and in the case of `then` will be returned, ignoring the passed state.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A simple, generic rules engine.",
"version": "1.3.1",
"project_urls": {
"Bug Reports": "https://github.com/funnel-io/funnel-rules-engine/issues",
"Homepage": "https://github.com/funnel-io/funnel-rules-engine",
"Source": "https://github.com/funnel-io/funnel-rules-engine"
},
"split_keywords": [
"[\"rules",
"engine\"]"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6dff5db870669864dc407dd56095297576498fb35d8ca8875ab6414064a1e23a",
"md5": "dc51cf551ed88165f32938db49399539",
"sha256": "738992873bbcbb7fe640f6c360e2e5d780bda631b3f2a6bea6a05debe05e433a"
},
"downloads": -1,
"filename": "funnel_rules_engine-1.3.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "dc51cf551ed88165f32938db49399539",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5031,
"upload_time": "2024-03-20T10:51:23",
"upload_time_iso_8601": "2024-03-20T10:51:23.184648Z",
"url": "https://files.pythonhosted.org/packages/6d/ff/5db870669864dc407dd56095297576498fb35d8ca8875ab6414064a1e23a/funnel_rules_engine-1.3.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "029867a6b67bbecbfeec5d683d9e54e1fae5af66a2853eb3a553b528e201c686",
"md5": "910611c8e1f57dcd25538f63327e5c42",
"sha256": "e5e8a9c601103de0e4494fdc5cf8ab191f4ce27ad67cc4b4de003ae6d19fbfd8"
},
"downloads": -1,
"filename": "funnel_rules_engine-1.3.1.tar.gz",
"has_sig": false,
"md5_digest": "910611c8e1f57dcd25538f63327e5c42",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4701,
"upload_time": "2024-03-20T10:51:25",
"upload_time_iso_8601": "2024-03-20T10:51:25.089698Z",
"url": "https://files.pythonhosted.org/packages/02/98/67a6b67bbecbfeec5d683d9e54e1fae5af66a2853eb3a553b528e201c686/funnel_rules_engine-1.3.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-20 10:51:25",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "funnel-io",
"github_project": "funnel-rules-engine",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "funnel-rules-engine"
}