A rules engine is a piece of software that allows you to define rules and then apply those rules to data. This can be useful for a variety of purposes, but it is particularly valuable for IoT applications. IoT devices generate a lot of data, and that data can be difficult to make sense of. A rules engine can help you sort through all of the data and find the information that you need.
Rules engines have been around for a long time, but they are becoming more and more important as IoT becomes more prevalent. Let's take a look at the history of rules engines and see why they are so valuable for IoT applications.
There are many types of rules engines.
The two most common types of rules engines:
Decision Trees Rules Engine
Decision trees are based on if-then statements. You define a set of conditions and then specify what should happen if those conditions are met.
Forward Chaining Rules Engine
Forward chaining is more flexible than decision trees. With forward chaining, you can define a set of conditions and then specify what should happen if those conditions are met, but you can also specify what should happen if new information is learned.
Rules engines can be very complex to be implement, but they don't have to be. IoT applications often need rules engines that are simple and easy to use.
How To Implement A Simple IoT Rules Engine
The above block diagram shows the simple implementation of an IoT rules engine. The implementation consists of a hash map that stores the latest values of the metrics e.g. sensor1.temp. When the user creates a new rule using an expression, e.g. sensor1.temp > sensor2.temp, the expression is compiled and added to the compiled expression table. The dependent variables (metrics) in the expression are extracted and used to create a dependency between the compiled expression in the expression table and the hash map with current values of metrics. Each entry in the compiled expression map can have a list of actions associated with the condition. These are actions that must be executed if the condition evaluates as true.
When a metric arrives in a data stream, e.g. sensor1.temp in the diagram above, the metric is looked up in the hash map. The value in the hash map is updated. If there are multiple metrics in the data stream, all the metrics are updated in the hash map.
After updating the current values in the hash map, the expression dependencies associated with each changed entry in the hash map are evaluated. Once an expression is evaluated, a flag is set to mark it as evaluated. This also prevent duplicate evaluation of expressions.
If an expression evaluates to true, the list of actions associated with the expression are evaluated.
This is a simple implementation of an IoT rules engine. For a more detailed look at a popular implementation, refer to Rete's algorithm: https://en.wikipedia.org/wiki/Rete_algorithm
The bottom line is that IoT devices generate a lot of data, and rules engines can help you make sense of that data. If you are working on an IoT project, you should consider using software that has an integrated rules engine such as Machinechat JEDI.