In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points of drools entry case analysis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
I. background
Recently in the learning rules engine drools, here is a brief record of how to get started with drools.
1.Drools introduction
Drools is an open source rule engine based on Java language provided by JBoss organization, which can liberate complex and changeable business rules from hard coding and store them in files or specific storage media (such as in database) in the form of rule scripts, so that changes to business rules do not need to modify the project code or restart the server to take effect immediately on the online environment.
Why do you want to learn drools
Suppose we have the following scenarios:
When we go to the store to buy clothes, it often happens that there is no discount for one item, 0.98 discount for two items, and 0.85 discount for more than three items.
So if we want to achieve the above functions in the code, do we need to write if. Else statement, assuming that the rules change later, do you need to modify these if. Else statement, and then redeploy the program. This is achievable, but not elegant enough. So can we write these business rules to the rule file, and then modify the rule file directly after the rule change? And drools can achieve this function.
Third, to achieve the above simple discount case 1. Introduce jar package org.drools drools-bom pom 7.69.0.Final import org.drools drools-compiler org.drools drools-mvel ch.qos.logback logback-classic 1.2.11 2, write kmodule.xml configuration file
This configuration file needs to be placed in the resources/META-INF directory.
Here we need to pay attention to the value of package under kbase, which needs to be consistent with the package value in the rule file, otherwise the rule will not be found, as shown below.
3. Write the rule file 1. The syntax package name of the rule file must be placed on the first line of the class introduced by package// into Java. You need some fully qualified name import// definition function. Optional function / / Optional// definition query Optional query / / Optionaldeclare / / Optionalglobal / / Optional// rule keyword "rule name" the name of the rule rule "rule name" / / Attributes attribute optional when / / keyword / / Conditions condition, can be empty then / / Actions / / match the result end / / keyword 2, write the rule file
The name of the rule file doesn't matter, for example: book-discount.drl
/ / package name, which must be prevented from going to the first line. This name needs to be consistent with the value of the package attribute in kbase * inverted class * / import com.huan.drools.CustomerOrder// definition rules rule "shop-rule-01" when / / pattern match: look for CustomerOrder in working memory, and the value of this object needs to be 1, / / if the condition is true $order is the name of the binding variable, which usually starts with $and is distinguished from the fact object by $order:CustomerOrder (purchaseQuantity = = 1) then System.out.println ("matching rule shop-rule-01") / / assignment. After the assignment here, get the assigned value $order.setDiscount (1D) in the Java code; endrule "shop-rule-02" when $order:CustomerOrder (purchaseQuantity = = 2) then System.out.println ("matching rule shop-rule-02"); $order.setDiscount (0.98) Endrule "shop-rule-03" when $order:CustomerOrder (purchaseQuantity > = 3) then System.out.println ("matching Rule shop-rule-03"); $order.setDiscount; end3, explain the package name
If the package name of shop-discount.drl is changed to com.huan.shop1, the following warning will be prompted:
12 File 43 com/huan/shop/shop-discount.drl' is in folder 01.589 [main] WARN org.drools.compiler.kie.builder.impl.KieBuilderImpl-File 'com/huan/shop/shop-discount.drl' is in folder' com/huan/shop' but declares package 'com.huan.shop1'. It is advised to have a correspondance between package and folder names.
4. Write Java code 1. Write an order object
This object saves several clothes purchased by the user and the corresponding discount.
/ * * customer orders for clothing purchases, omitting the getter and setter methods * * @ author huan.fu * @ date 2022-5-12-11:27 * / public class CustomerOrder {/ * purchased several clothes * / private Integer purchaseQuantity; / * how much discount is the final * / private Double discount; public CustomerOrder (Integer purchaseQuantity) {this.purchaseQuantity = purchaseQuantity 2. Write test code
1. Stateless testing method statelessSessionTest Rule 2, that is, the final discount is 0.98%.
2. Stateful testing method statefulSessionTest Rule 3, that is, the final discount is 85%.
Package com.huan.drools;import org.kie.api.KieServices;import org.kie.api.event.rule.DebugRuleRuntimeEventListener;import org.kie.api.runtime.KieContainer;import org.kie.api.runtime.KieSession;import org.kie.api.runtime.StatelessKieSession;/** * drools test class * / public class DroolsApplication {public static void main (String [] args) throws InterruptedException {/ / stateless session test statelessSessionTest () / / stateful session test statefulSessionTest ();} private static void statelessSessionTest () {/ / get kie services KieServices kieServices = KieServices.get (); / / get kie container object KieContainer kieContainer = kieServices.getKieClasspathContainer () / / get kie session, what you get here is stateless session, because / / type= "stateless" in / / is stateless session StatelessKieSession kieSession = kieContainer.newStatelessKieSession ("shop-ksession"); / / create an object, which can be understood as Fact object, that is, fact object CustomerOrder customerOrder = new CustomerOrder (2) / / add a listener to make it easy to observe the log kieSession.addEventListener (new DebugRuleRuntimeEventListener ()); / / stateless session only needs to execute the execute method. KieSession.execute (customerOrder); System.err.println ("the discount obtained after passing the rule is:" + customerOrder.getDiscount ();} private static void statefulSessionTest () {/ / get kie services KieServices kieServices = KieServices.get (); / / get kie container object KieContainer kieContainer = kieServices.getKieClasspathContainer () / / get kie session, where you get the stateful session KieSession kieSession = kieContainer.newKieSession ("shop-ksession-stateful"); / / create an object that can be understood as a Fact object, that is, the fact object CustomerOrder customerOrder = new CustomerOrder (3); / / add a listener to make it easy to observe the log kieSession.addEventListener (new DebugRuleRuntimeEventListener ()) / / add the customerOrder object to the working memory kieSession.insert (customerOrder); / / trigger all the rules. If you only want to trigger the specified rules, use the fireAllRules (AgendaFilter agendaFilter) method kieSession.fireAllRules (); / / the stateful session must call the dispose method kieSession.dispose () System.err.println ("after passing the rule, the discount obtained is:" + customerOrder.getDiscount ());}}
Note the difference between stateful session and stateless session writing here.
5. Test results
At this point, a simple example of our implementation using drools is implemented.
VI. Basic components of drools engine
1. Rules: business rules that we define ourselves, such as rules files we write. All rules must contain at least the conditions that trigger the rules and the actions specified by the rules.
2. Production memory: where the rules are stored in the Drools engine.
3. Facts: data entered or changed into the Drools engine, and the Drools engine matches the rule conditions to execute the applicable rules. If the value of the Fact object is modified in the rule, the real JavaBean data will also change.
For example, when we call ksession.insert (object), the inserted object can be understood as a Facts object.
4. The location where Working memory:facts is stored in the Drools engine.
5. Pattern matcher: a matcher that matches all the rules in Rule Base with the Fact object in Working memory, and the rules that match successfully will be activated and put into Agenda.
6. Agenda: agenda, implementing the sorted rules that are activated in Agenda.
These are all the contents of the article "case study of getting started with drools". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.