![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
json-rules-engine
Advanced tools
The json-rules-engine npm package is a powerful tool for creating and executing business rules in JavaScript. It allows you to define rules in JSON format and evaluate them against a set of facts. This can be useful for a variety of applications, including decision-making systems, workflow automation, and more.
Defining Rules
You can define rules using JSON format. Each rule consists of conditions and an event. The conditions specify the criteria that need to be met, and the event specifies what happens when the conditions are met.
{"rules":[{"conditions":{"all":[{"fact":"temperature","operator":"greaterThanInclusive","value":100}]},"event":{"type":"highTemperature","params":{"message":"Temperature is too high!"}}}]}
Evaluating Rules
You can evaluate rules against a set of facts. If the facts meet the conditions specified in the rules, the corresponding event will be triggered.
{"facts":{"temperature":101},"engine":{"addRule":"rule","run":"facts"}}
Combining Multiple Conditions
You can combine multiple conditions using logical operators like 'all' and 'any'. This allows you to create complex rules that evaluate multiple criteria.
{"rules":[{"conditions":{"all":[{"fact":"temperature","operator":"greaterThanInclusive","value":100},{"fact":"humidity","operator":"lessThan","value":30}]},"event":{"type":"alert","params":{"message":"High temperature and low humidity!"}}}]}
nools is a rules engine for JavaScript that uses a domain-specific language (DSL) for defining rules. It is more powerful and flexible than json-rules-engine but has a steeper learning curve. It is suitable for complex rule-based systems.
A rules engine expressed in JSON
json-rules-engine
is a powerful, lightweight rules engine. Rules are composed of simple json structures, making them human readable and easy to persist.
ALL
and ANY
boolean operators, including recursive nesting$ npm install json-rules-engine
It's best to start with the overview to understand the terminology. Next, see the walkthrough and try out some examples.
To dive right in, start with the basic example.
import { Engine } from 'json-rules-engine'
import { Rule } from 'json-rules-engine'
/**
* Setup a new engine
*/
let engine = new Engine()
/**
* Create a rule
*/
let rule = new Rule()
// define the 'conditions' for when "hello world" should display
rule.setConditions({
all: [{
fact: 'displayMessage',
operator: 'equal',
value: true
}]
})
// define the 'event' that will fire when the condition evaluates truthy
rule.setEvent({
type: 'message',
params: {
data: 'hello-world!'
}
})
// add rule to engine
engine.addRule(rule)
/**
* Pass initial values into the engine.
* Fact values do NOT need to be known at engine runtime; see the
* examples for how to pull in data asynchronously throughout a run()
*/
let facts = { displayMessage: true }
// run the engine
engine
.run(facts)
.then(triggeredEvents => { // run() return events with truthy conditions
triggeredEvents.map(event => console.log(event.params.data))
})
.catch(console.log)
/*
* hello-world!
*/
Rules may be easily converted to JSON and persisted to a database, file system, or elsewhere. To convert a rule to JSON, simply call the rule.toJSON()
method. Later, a rule may be restored by feeding the json into the Rule constructor.
// save somewhere...
let jsonString = rule.toJSON()
// ...later:
let rule = new Rule(jsonString)
Why aren't "fact" methods persistable? This is by design, for several reasons. Firstly, facts are by definition business logic bespoke to your application, and therefore lie outside the scope of this library. Secondly, many times this request indicates a design smell; try thinking of other ways to compose the rules and facts to accomplish the same objective. Finally, persisting fact methods would involve serializing javascript code, and restoring it later via eval()
. If you have a strong desire for this feature, the node-rules project supports this (though be aware the capability is enabled via eval()
.
To see what the engine is doing under the hood, debug output can be turned on via:
DEBUG=json-rules-engine
1.0.3 / 2016-06-15
FAQs
Rules Engine expressed in simple json
The npm package json-rules-engine receives a total of 0 weekly downloads. As such, json-rules-engine popularity was classified as not popular.
We found that json-rules-engine demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.