A rule engine, developed from an inference engine, is a component embedded in an application that enables business decisions to be separated from application code and written using predefined semantic modules. Accept data input, interpret business rules, and make business decisions based on the rules.
introduce
Rules engines can separate the business decision logic from the system logic and allow the two logics to vary independently of each other, which can significantly reduce the maintenance costs of both logics.
For example in the Internet of things platform, various types of equipment connection, data format, data types, but again want to face the demand of the new device access, can’t say that every access to a device that is to write a set of equipment, the logic of data processing, and then upgrade the function of the publishing system, equipment processing logic can be written, but can affect the function of the platform of the minimum. At this point, it is best to have a rules engine that can flexibly handle data from various devices.
design
Train of thought
Already know why want to do a rules engine, how to deal with, reference QLExpress instructions article: www.jianshu.com/p/c1fa9c4a0…
QLExpress is a Java rules engine that can dynamically execute scripts and bind some Java functions we have written to dynamically execute scripts. Then we will encapsulate the most common content in data processing into a QLExpress component, which can be used directly in data processing. In fact, we can consider only data processing, which components may be:
- Gets a data point (property) component that quickly retrieves some desired part of the data
- The feedback component invokes functions in the system after the execution of rules to inform the business of the completion of the execution of rules
- Protocol components, which can be HTTP, TCP, UDP, etc., encapsulate the call address, packet, request address in the component
- JSON, XML data serialization components may generate new data that needs to be processed again
I think the above components are sufficient for most data scenarios, with sources, processing, and feedback. Of course, in complex business situations, new components may be added continuously, but each time you add a component, think about whether it is really needed.
And because QLExpress already supports dynamic scripting, it has some programming language features, enough flexibility to encapsulate our components only for those more general, common scenarios, and the most common things are generally not very many.
Provide functional
- Default rule components, and instructions for using them
- Rule add delete change check interface
- Rule execution interface
- Extra is off the table
In the process
1 Sort out the most commonly used functions in the system, encapsulate them, and register the most commonly used functions in a certain way (configuration file, dynamic loading) as an operator of QLExpress. The following
runner.addFunctionOfClassMethod("Take the absolute value", Math.class.getName(), "abs",
new String[] { "double" }, null);
runner.addFunctionOfClassMethod("Convert to uppercase", BeanExample.class.getName(),
"upper", new String[] { "String" }, null);
runner.addFunctionOfServiceMethod("Print", System.out, "println",new String[] { "String" }, null);
runner.addFunctionOfServiceMethod("contains", new BeanExample(), "anyContains",
new Class[] { String.class, String.class }, null);
Copy the code
2 In different business scenarios, QLExpress statements can be written to simplify statements using the functions registered in QLExpres in the previous step. Dynamic script but simple enough that the business needs to be able to meet this requirement without writing a few lines of code, if it has to write too much syntax, it is a little unfriendly.
3 Provide rule execution test scripts for each rule. Data isolation prevents dirty data and verifies whether the rule scripts are correctly executed.
Integrate rules engine functionality into the platform.
The last
QLExpress script engine is widely used in Alibaba’s e-commerce business scenarios. It supports common programming syntax and is powerful enough to build its own business rules engine on top of QLExpress.
Reference: QLExpress project QLExpress instructions