Explanation of Java Rule Engine and Its APIs

Source: Internet
Author: User

This paper gives a detailed introduction to Java Rule Engine and Its API (JSR-94) and related implementation, and gives a detailed description of its architecture and API application, and points out that Java rule engine, rule Language, The Relationship Between JSR-94, and the shortcomings and prospects of JSR-94

The development of complex enterprise-level projects and business logic, which constantly changes with external conditions, urgently need to separate the commercial decision-making logic of business decision makers and the technical decision-making of application developers, and place these commercial decisions in central databases or other unified places so that they can be dynamically managed and modified at runtime (that is, business time) to provide software system flexibility and adaptability. The rule engine is a solution applied to the above dynamic environment.

The first part briefly introduces the background of the rule engine and the rule-based expert system. The second part introduces what is the rule engine and its architecture and algorithm, the third part introduces commercial products and open-source project implementation and other Java rule engine, the fourth part of the Java Rule Engine API (JSR-94) is described in detail, explain its architecture, the fifth part discusses the Rule Language and Its Standardization. The sixth part provides a simple example of using the Java Rule Engine API, the seventh part is a summary and outlook.

  1. Introduction

  1.1 Background of the Rule Engine

Enterprise managers have the following requirements for enterprise-level IT system development: (1) to improve efficiency, management processes must be automated, even if modern business rules are exceptionally complex (2) the market requires frequent changes in business rules, IT systems must be updated quickly and at a low cost based on changes in business rules (3) in order to quickly and at a low cost, business personnel should be able to directly manage rules in IT systems without the involvement of program developers.

The project developers encountered the following problems: (1) program = Algorithm + data structure, some complex business rules are difficult to derive algorithms and abstract data models (2) software Engineering requires requirements from requirement-> Design-> coding. However, business rules may not be clearly defined at the demand stage and are still changing after design and coding, business Rules are often embedded in code everywhere in the system (3) for programmers, the system has been maintained, difficult to update, and cannot be managed by business personnel.

The emergence of a rule-based expert system gives developers an opportunity to solve the problem. The rule engine is developed by the inference engine in the rule-based expert system. The following describes a rule-based expert system.

  1.2 Rule-Based Expert System (RBES)

An expert system is a branch of artificial intelligence. It imitates human reasoning methods, uses exploratory methods for reasoning, and uses terminologies that humans can understand to explain and prove their reasoning conclusions. Expert Systems are classified into neural networks, case-based reasoning, and rule-based systems.

RBES includes three parts: Rule Base (knodge DGE base), Working Memory (fact base), and Inference Engine (Inference Engine ). Their structure is as follows:

Figure 1. Composition of a rule-based Expert System

As shown in, the inference Engine consists of three parts: Pattern Matcher, Agenda, and Execution Engine. Pattern Matcher specifies the rule to be executed. Agenda manages the Execution sequence of the Rule selected by PatternMatcher. Execution Engine executes the rule and other actions.

The inference engine is added to the agenda by deciding which rules meet the facts or goals and granting the rule priority. There are two ways of reasoning: Forward-Chaining and Backward-Chaining ). The explain method starts from an initial fact and constantly applies rules to draw conclusions (or execute specified actions ). The induction rule is to start from assumptions and constantly look for facts that conform to the assumptions.

  2. Rule Engine

  2.1 Business Rules
 
A business rule contains a set of conditions and operations executed under these conditions, which indicate a piece of business logic of the business rule application. Business Rules should usually be developed and modified by business analysts and policy managers. However, some complicated business rules can also be customized by technical personnel using object-oriented technical languages or scripts. The theoretical basis of business rules is to set one or more conditions to trigger one or more operations when these conditions are met.

  2.2 Rule Engine

What is the rule engine? How does the Rule Engine execute rules? This can be called the question of "what" and "how. In the end what the rule engine is or is currently a relatively controversial issue in the industry, there is almost no definition in the JSR-94. We can think that the problem of "how" is fully defined and solved, and "what" is essentially solved. Maybe this is another philosophical debate about "having eggs first or having chickens first. In the future, the definition and launch of standard rules and the formulation of relevant standards should end such problems and disputes. In this article, we will talk about what the rule engine is. The rule engine evolved from the inference engine and is a component embedded in applications, this module separates business decisions from application code and uses predefined semantic modules to write business decisions. Accept data input, interpret business rules, and make business decisions based on rules.

  2.3 rules engine usage

Because the rule engine is a software component, only developers can use and control it through program interfaces. The rule engine program interfaces include at least the following APIs: API for loading and detaching rule sets, APIs for data operations, and APIs executed by the engine. To use the rule engine in a program, developers follow the following five typical steps: create a rule engine object, load the rule set to the engine, or replace the rule set; submit the data object set to be processed by the rule set to the engine, execute the command engine, export the engine execution result, and withdraw the processed data from the engine. After the rule engine is used, many program codes that involve business logic are basically replaced by these five typical steps.

An open business rule engine can be "embedded" anywhere in the application. Different rule engines can use different rule sets to process different data objects. In addition, there is no limit on the number of engines used.

  2.4 rule engine architecture and reasoning

The rule engine architecture is shown in:

Figure 2. Business Rule Engine Architecture

The reasoning steps of the rule engine are as follows: a. input the initial data (fact) to the Working Memory (Working Memory ). B. Use Pattern Matcher to compare the rule (rule) and data (fact) in the rule repository (Rules repository. C. If a conflict exists in the execution rule (conflict), that is, multiple rules are activated at the same time, and conflicting rules are put into the conflict set. D. resolve the conflict and add the activated rules to Agenda in sequence. E. Execute the rules in Agenda. Repeat steps B to e until all rules in Agenda are executed.

Any rule engine needs to solve the problem of the Efficiency of Rule Inference Mechanism and rule condition matching.

When the engine is executed, the instance is executed according to the priority in the rule execution queue. Because the execution part of the rule may change the data object in the workspace, in this way, some rule execution instances in the queue become invalid due to changes in conditions. They must be removed from the queue, or they may activate rules that do not meet the conditions and generate new rule execution instances to enter the queue. Therefore, a "dynamic" rule execution chain is created to form a rule reasoning mechanism. The "chain" reaction of such rules is entirely driven by the data in the workspace.

The efficiency of Rule condition matching determines the performance of the engine. The engine needs to quickly test the data objects in the workspace, find matching rules from the loaded rules, and generate rule execution instances. In 1982, Charles L. Forgy from Carnegie Mellon University in the United States invented a Rete algorithm, which effectively solved this problem. Currently, Rete algorithms are basically used in the world's top commercial business rule engine products.

  2.5 rules engine algorithms
 
The algorithms of most Rule Engine products are basically from Dr. charles Forgy proposed the RETE algorithm and its variants in 1979. The Rete algorithm is currently the most efficient Forward-Chaining inference algorithm. The Drools project is an object-oriented Java Implementation of the Rete algorithm, the core idea of the Rete algorithm is to dynamically construct a matching tree based on the separated matching items to significantly reduce the computing workload. For details, see CIS587: The RETE Algorithm, The Rete Algorithm, RETE Algorithm, Chapter 1 in expert system principle and programming.

  3. Java Rule Engine
 
Currently, most of the mainstream rule engine components are based on Java and C ++ Programming Language Environments. There are already various Java Rule Engine commercial products and open-source projects, some of which already support JSR94, some are making efforts in this direction, as shown below:

  3.1 Java Rule Engine Commercial Products

Java Rule Engine commercial products are mainly available (Jess is not an open source project, it can be used for academic research for free, but it is charged for commercial purposes ):


  3.2 Java Rule Engine open source project

The implementation of open-source projects mainly includes:

The Drools-Drools Rule Engine applies the improved Rete-II algorithm of the Rete algorithm. In terms of internal mechanisms, it uses the same concepts and methods as the Forgy algorithm, but adds a node type that can be seamlessly connected to the object-oriented language.

Mandarax is based on reverse inference (induction ). It is easy to integrate multiple data sources. For example, database records can be easily integrated into fact sets, and reflection is used to integrate functions in the object model. JSR 94 is not supported currently

OFBiz Rule Engine-supports Backward chaining. The initial code was based on Steven John Metsker's "Building Parsers in Java" and does not support JSR 94

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.