Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the advantages of Java rule engine

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "What are the advantages of Java rule engine", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "What are the advantages of Java rule engine"!

I. Overview 1 Rules Engine Concept

A rule engine is an engine that judges whether or not it can match the real-time conditions at runtime according to the specified filter conditions included in the rule to execute the actions specified in the rule.

A rule engine, evolved 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.

Rules engine execution can be divided into three processes: accepting data input, interpreting business rules, and making business decisions.

2 Rules Engine Advantages:

Declarative programming. Rules engines allow you to describe what to do rather than how to do it, and rules are easier to read than code.

Logic is separated from data. Data is stored in system objects and logic is stored in rules. It breaks the coupling of data and logic in object-oriented systems. Because logic is stored in rules, it is easier to maintain when logic changes in the future. By concentrating logic in one or several clear rules files, it replaces the previous situation where it was scattered throughout the code.

Speed and measurability. The Rete algorithm, Leaps algorithm, and Drools 'Rete and Leaps algorithms derived from them provide very efficient matching of system data objects. These algorithms have been proven through a lot of practical tests.

Centralization of knowledge. By using rules, an executable rulebase will be established. This means that the rulebase represents a unique correspondence to real-world business policies, and ideally readable rules can also be used as documents.

Interpretative mechanisms. By documenting the decisions of the rules engine along with the reasons for the decisions, the rules system provides a good "explanation mechanism."

Easy rules. By building object models and DSL (Domain Definition Language), rules can be written in a manner close to natural language. This allows non-technical people and domain experts to understand the rules with their own logic (because the complexity of the program is hidden).

3 Rules Engine Application Scenarios

Compared with business system, rule engine can be considered as a module independent of business system, responsible for some rule calculation. In general, the rules engine is mainly used in the following scenarios:

Risk control model configuration, risk control rule setting.

User points and other configurations, such as daily operation caused by points change, etc.

Simple off-line calculation, statistics with relatively small amount of various data, etc.

Note: As a separate module, the rules engine has its own bulkiness and complexity. Not all related business systems need to introduce rules engines.

Some data models have to be built.

Consider conflicts of rules, priorities, etc.

Control the complexity of the rules so that they do not impose excessive learning costs on the rule configurator.

Thus, for projects with short time periods, complex rules, and infrequent changes, the rules engine is not very suitable.

II. Brief introduction of principle

Typically, Java rules engines consist of the following internal components: Working memory (Working Memory) is the workspace, which is used to store the data object set referenced by the engine; the rule execution queue, which is used to store the activated rule execution instances; and the static rule area, which is used to store all the loaded business rules. These rules will be organized according to a certain data structure. When the data in the workspace changes, the engine needs to quickly adjust the rule execution instances in the rule execution queue according to the object status in the workspace. The Java rules engine structure diagram is shown in Figure 1.

Figure 1 Execution structure diagram of rule engine

When the engine executes, it will execute rule execution instances one by one according to the priority order in the rule execution queue. Since the execution part of the rule may change the data objects in the workspace, some rule execution instances in the queue will become invalid due to the change of conditions and must be withdrawn from the queue. It may also activate the original rules that do not meet the conditions and generate new rule execution instances into the queue. Therefore, a "dynamic" rule execution chain is produced, forming the reasoning mechanism of rules. This regular "chain reaction" is driven entirely by the data in the workspace.

III. Comparison of rule engine selection

The following projects are all open source, and there are github links in the hyperlinks, with more detailed use examples above. Table 1 shows the comprehensive comparison results.

Table 1 Comprehensive comparison of rule engines

applicable scenarios

advantages

disadvantages

activity

integrated assessment

Drools

Separation of business code and business rules; suitable for large application systems

high performance

may be integrated

maintainable

High learning costs

Full documentation

continuously updated

popular

five-star

MVEL

Defining rules using the expression language

Flexible, high performance, no type restrictions

Less information

Github updates less.

Samsung

Easy Rules

Defining rules using the expression language

Easy to learn, based on POJO, supports compound rules, and has various modes

Github has few maintainers

The latest version was released in April.

four-star

Aviator

Dynamic Evaluation of Various Expressions

High performance; lightweight; support for multiple types

Github has few maintainers

More information, more examples.

four-star

3.1 Drools

Drools is an engine focused on resolving the separation of business code and business rules. Drools rules are run on Java applications, the order of steps to be executed is determined by code, and the Drools rules engine converts business rules into execution trees. See Figure 2 for specific structure.

Figure 2 Drools Flow Chart

Pros: Very active community support; ease of use; fast execution speed; popularity among Java developers; compatibility with Java Rule Engine API (JSR 94).

3.2 MVEL

MVEL is a powerful expression language for Java-based applications. As an expression language, it is designed to be more efficient, for example, by directly supporting operations such as sets, arrays, and string matching, as well as regular expressions. MVEL is used to execute expressions written using Java syntax.

Unlike Java, MVEL is dynamically typed (with optional classification), meaning there are no type restrictions in the source file. An MVEL expression can be as simple as a single identifier, or as complex as a large Boolean expression filled with method calls and internal collection creation.

The latest version is version 2.0, with the following features:

(1) Dynamic JIT Optimizer. Bytecode is selectively generated when the load exceeds a threshold that ensures code generation, which greatly reduces memory usage. New static type checking and attribute support, allowing integrated type-safe expressions;

(2)Error reporting improvements, including row and column error messages;

(3)New scripting language features. MVEL 2.0 includes function definitions such as closures, lambda definitions, standard loop constructs (for, while, do-while, do-until…).

(4)Improved integration functionality. To cater to mainstream needs, MVEL 2.0 supports basic types of personalized attribute processors, integrated into the JIT;

(5)Poor integration, slow compilation time for bytecode generation, and added scalability issues; very slow runtime execution without bytecode generation.

3.3 Easy Rules

Easy Rules is a Java rules engine, easy-rules first integrates mvel expressions, later may integrate SpEL a lightweight rules engine, Easy Rules provides rules abstraction to create rules with conditions and actions, and run a set of rules to evaluate conditions and perform actions RulesEngine API.

Easy rules have the following characteristics: lightweight framework; low learning cost; POJO based; provides useful abstractions and simple applications for defining business engines; and combines primitive rules into complex rules. It consists of several main classes or interfaces: Rule, RulesEngine, RuleListener, Facts, and several main annotations: @Action,@Condition,@Fact,@Priority,@Rule

3.4 Aviator

Aviator is a high-performance, lightweight expression evaluation engine implemented in Java language, mainly used for dynamic evaluation of various expressions. Aviator is designed to be lightweight and high performance, and is very small compared to Groovy and JRuby.

But Aviator's syntax is limited; it is not a complete language, but a collection of small parts of it. Second, Aviator's implementation idea is very different from other lightweight evaluators. Other evaluators generally run by interpretation, while Aviator compiles expressions directly into Java bytecode and hands them to JVM for execution. Its main features are as follows:

(1) Support most operators, including arithmetic operators, relational operators, logical operators, regular matching operators (=~), ternary expressions?: , and supports operator priority and parenthesis enforcement priority, please see the operator list below for details;

(2) Support function calls and custom functions;

(3) Support regular expression matching, similar to Ruby, Perl matching syntax, and support Ruby-like $digit pointing to matching groups. Automatic type conversion, when the operation is performed, it will automatically determine the operand type and make the corresponding conversion. If it cannot be converted, it will throw an exception;

(4) Support incoming variables, support nested variable access similar to a.b.c.;

(5) Excellent performance;

(6) Aviator restrictions, no if else, do while and other statements, no assignment statements, only support logical expressions, arithmetic expressions, ternary expressions and regular matching, no bit operators.

At this point, I believe that everyone has a deeper understanding of "what advantages Java rule engine has", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 245

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report