Backend Development 22 min read

Optimizing CRM Opportunity Flow with EasyRule and the Rete Algorithm

This article explains how to improve the efficiency of CRM opportunity handling by introducing rule‑engine concepts, detailing the Rete algorithm, comparing Drools and EasyRule, and providing practical EasyRule integration examples with Java code for backend developers.

Zhuanzhuan Tech
Zhuanzhuan Tech
Zhuanzhuan Tech
Optimizing CRM Opportunity Flow with EasyRule and the Rete Algorithm

The article begins with an overview of CRM systems, defining Customer Relationship Management and describing the complex business processes involved in opportunity (商机) management, including pools, statuses, and flow rules.

It then identifies key challenges in the current hard‑coded flow logic, such as high maintenance cost, difficulty adapting to frequent rule changes, and the need for code redeployment after rule updates.

To address these issues, the concept of a rule engine is introduced, explaining the distinction between inference engines and rule engines, and outlining the advantages of externalizing business logic into configurable rules.

The core of the discussion focuses on the Rete algorithm, the most efficient pattern‑matching algorithm for rule engines. It describes the algorithm’s workflow, including working memory, pattern matcher, agenda management, alpha and beta networks, and the steps for rule compilation and runtime execution. The article also presents a concrete basketball‑player selection example that illustrates how facts are matched against rules through a series of network nodes (A‑H).

Following the theory, the article compares Drools and EasyRule, ultimately selecting EasyRule for its simplicity. It details EasyRule’s components (name, description, priority, facts, conditions, actions) and shows how to define rules via POJO annotations, RuleBuilder API, and composite rule groups (UnitRuleGroup, ActivationRuleGroup, ConditionalRuleGroup). It also explains priority handling and execution modes.

Several Java code examples demonstrate EasyRule usage:

public class Student { private Integer grade; private String gender; private Integer age; private Boolean isStrong; private Integer height; private Boolean isGoodSeed = true; }

Rule rule1 = new MVELRule().name("grade rule").description("判断一个学生是否是一个篮球好苗子-年级").priority(1).when("student.getGrade() <= 3").then("System.out.println(\"年级-不是好苗子\");").then("student.setIsGoodSeed(false);");

Rule rule2 = new MVELRuleFactory(new YamlRuleDefinitionReader()).createRule(new FileReader(ResourceUtils.getFile("classpath:gender-rule.yml")));

Rule rule4 = new RuleBuilder().name("strong rule").description("判断一个学生是否是一个篮球好苗子-是否强壮").priority(4).when(condition).then(action).build();

It then shows how to register these rules, create a Facts object, and fire the engine:

RulesEngine rulesEngine = new DefaultRulesEngine(); rulesEngine.fire(rules, facts);

The integration steps for applying EasyRule to the CRM opportunity flow are outlined: configuring the engine, exposing a generic fire API, extracting existing flow rules into discrete criteria, and invoking the engine within the service layer. Configuration examples in YAML/JSON illustrate rule definitions and engine settings.

Finally, the article presents before‑and‑after performance comparisons, summarizes lessons learned (ensure rule‑engine suitability, involve business analysts, maintain rule governance, and retain referenced code), and concludes that introducing EasyRule significantly improved team efficiency and system maintainability.

Backendrule engineRete AlgorithmCRMEasyRule
Zhuanzhuan Tech
Written by

Zhuanzhuan Tech

A platform for Zhuanzhuan R&D and industry peers to learn and exchange technology, regularly sharing frontline experience and cutting‑edge topics. We welcome practical discussions and sharing; contact waterystone with any questions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.