In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to understand the java rule engine", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the java rules engine.
WHAT
The rule engine, developed from the reasoning engine, is a component embedded in the application, which separates business decisions from the application code and uses predefined semantic modules to write business decisions. Accept data input. Interpret business rules and make business decisions based on business rules. In Java, most rule engines implement JSR94. Think of the rule engine as a system with data and rules as input. He said that these rule definitions provide us with output.
If x then y
WHY
Our business code is full of if/else, and of course, we can use policy pattern + template pattern to extract if/else into various subclasses. But it also increases the complexity of the class.
Hard coding
If you need to modify the logic in the Java class, it will be a lengthy process before the change enters the production environment.
Hard-coded development
Test code
Audit code
Deployment code
Introduce rule engine
The service interface is simple and simple, handing over the complex and changeable business logic to rules closer to the natural language than the high-level language, allowing users to configure and publish themselves on the web side or the cs side, decoupling the technology and business logic.
Common frame drools
Google's rule engine framework is also the first choice for many companies. Charles Forgy's-based RETE algorithm, easy to adjust and easy to manage open source business rules engine, in line with industry standards, high speed and high efficiency. A business analyst or auditor can easily view the business rules for him and verify that the encoded rules implement the required business rules. In general, enterprises will do secondary development, and there is a web-side visual interface.
Demo
No spring version, maven introduced drools
Org.kie kie-api 6.3.0.Final org.drools drools-core 6.3.0.Final org.drools drools-compiler 6.3.0.Final org.drools drools-decisiontables 6.3.0.Final org.drools drools-templates 6.3.0.Final
You need to add a META-INF folder under resources and create a relationship between kmodule.xml declaration rule files
Write a rule file and create a rules1 folder under resources to ensure that it is consistent with the packages in the xml file. Create a new rule.drl file with a syntax similar to java, but finally convert it to java.
Pacage com.ruleengine.drools// imports custom entity class import com.ruleengine.drools.Message// rule name rule 'VIP' when / / entry condition is that status is VIP and the message attribute is given to printMsg, otherwise m: Message (status = = Message.VIP, printMsg: message) then System.out.println (printMsg) cannot be used externally M.setMessage ("no discount for ordinary users"); m.setStatus (Message.COMMON_USER); / / update entity update (m); second end// rule rule "commonUser" when Message (status = = Message.COMMON_USER, printMsg: message) then System.out.println (printMsg) End
Call
Public class DroolsMain {public static void main (String [] args) {KieServices kieServices = KieServices.Factory.get (); KieContainer kieContainer = kieServices.getKieClasspathContainer (); KieSession kSession = kieContainer.newKieSession ("ksession-rules"); Message m = new Message (); m.setMessage ("VIP discount"); m.setStatus (Message.VIP) / / insert entity class into execution rule kSession.insert (m); kSession.fireAllRules ();}}
Custom CLA
Public class Message {public static final int VIP-1; public staitc final int COMMON_USER = 0; private String message; private String status; public String getMessage () {return this.message;} public int getStatus () {return this.status;} public void setMessage (String message) {this.message = message;} public void setStatus (int status) {this.status = status;}} characteristic representative
Youzan finally chose drools rule engine to do the risk control logic. Used to protect against the following risks:
Stealing cards. For example, embezzling users' bank cards and spending money in shops with likes.
Fraud. Ex.: to trick consumers into buying by publishing low-cost goods
Cash out. Ex.: carry out false transactions in the store you have created to cash out credit cards
Spam. Example: publishing false news, pornography and other illegal goods and pages
Stealing accounts. For example, hackers use account passwords obtained by other platforms to illegally steal users' accounts on Zan platform by hitting the library.
From https://tech.youzan.com/rules-engine/
Easyrule
Https://github.com/j-easy/easy-rules
Characteristics
Lightweight framework and easy-to-learn API
Programming Model of Development and Annotation based on POJO
Support the ability to create composite rules from simple rules
Support for expression languages such as MVEL and SpEL, and the latest version supports JEXL
Concept
Facts rules engine input
Rule rules rules rule collection
Condition matching condition
Action executes the action
There are two ways to define rules
Define declaratively by adding comments to the POJO
Define programmatically through RuleBuilder API
For more cases, you can see https://github.com/j-easy/easy-rules/tree/master/easy-rules-tutorials here.
Introduction of maven, the latest version of org.jeasy easy-rules-core 4.1.0POJOpackage com.lrq.wechatdemo.rules; import org.jeasy.rules.annotation.Action;import org.jeasy.rules.annotation.Condition;import org.jeasy.rules.annotation.Fact;import org.jeasy.rules.annotation.Rule;import org.jeasy.rules.support.UnitRuleGroup Public class RuleClass {@ Rule (priority = 1) / / Rule setting priority public static class FizzRule {@ Condition / / the return value is boolean public boolean isFizz (@ Fact ("number") Integer number) {return number% 5 = = 0;} @ Action public void printFizz () {System.out.print ("fizz\ n") } @ Rule (priority = 2) public static class BuzzRule {@ Condition public boolean isBuzz (@ Fact ("number") Integer number) {return number% 7 = = 0;} @ Action public void printBuzz () {System.out.print ("buzz\ n");}} public static class FizzBuzzRule extends UnitRuleGroup {public FizzBuzzRule (Object...) Rules) {for (Object rule: rules) {addRule (rule);}} @ Override public int getPriority () {return 0 } @ Rule (priority = 3) public static class NonFizzBuzzRule {@ Condition public boolean isNotFizzNorBuzz (@ Fact ("number") Integer number) {/ / can return true, because this is the latest rule to trigger according to / / assigned priorities / / and in which case, the number is not fizz nor buzz return number% 5! = 0 | | number% 7! = 0 } @ Action public void printInput (@ Fact ("number") Integer number) {System.out.print (number+ "\ n");}
Call
Import org.jeasy.rules.api.Facts;import org.jeasy.rules.api.Rules;import org.jeasy.rules.api.RulesEngine;import org.jeasy.rules.core.DefaultRulesEngine;import org.jeasy.rules.core.RulesEngineParameters; public class RuleJavaClient {public static void main (String [] args) {/ / create rule engine RulesEngineParameters parameters = new RulesEngineParameters () .skipOnFirstAppliedRule (true); RulesEngine fizzBuzzEngine = new DefaultRulesEngine (parameters) / / create a rule set and register the rule Rules rules = new Rules (); rules.register (new RuleClass.FizzRule ()); rules.register (new RuleClass.BuzzRule ()); rules.register (new RuleClass.FizzBuzzRule (new RuleClass.FizzRule (), new RuleClass.BuzzRule (); rules.register (new RuleClass.NonFizzBuzzRule ()); / / execute the rule Facts facts = new Facts () For (int I = 1; I
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.