In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "simple use of Drools rule engine". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Easy to use
An example of setting different discounts for different goods
Pom.xml
Org.drools drools-compiler 7.23.0.Final
The kmodule.xml file is used to configure rule scripts, which can have multiple kbase
Files can be loaded automatically when placed in the project resources/META-INF directory
Product Bean entity
/ / lombok Note @ Datapublic class Product {public static final String DIAMOND = "DIAMOND"; public static final String GOLD = "GOLD"; private String type; private int discount;}
Resources/rules/Rules.drl rule script
Package rulesimport drools.Product// discounts 15%rule Offer4Diamond when productObject:Product (type = = Product.DIAMOND) then productObject.setDiscount (15) when the commodity type is DIAMOND; end// discounts 25%rule Offer4Gold when productObject:Product (type = = Product.GOLD) then productObject.setDiscount (25) when the commodity type is GOLD; end
Junit Test
@ Test public void droolsTest () {KieServices ks = KieServices.Factory.get (); KieContainer kieContainer = ks.getKieClasspathContainer (); KieSession kSession = kieContainer.newKieSession ("ksession-rule"); Product product = new Product (); product.setType (Product.GOLD); kSession.insert (product); int count = kSession.fireAllRules (); System.out.println ("hit" + count + "rule") System.out.println ("merchandise" + product.getType () + "discount is" + product.getDiscount () + "%");}
Running result
Hit 1 rule goods GOLD discount is 25%
Explain
Kmodule.xml
This file is used to configure the rule table. It can contain multiple kbase. Each kbase has a name attribute and cannot be repeated.
Kbase can contain multiple ksession. The packages attribute is the package path of the folder under the src/main/resources directory. Multiple packages can be defined, separated by commas, and all rule files under the packages directory will be loaded, except for subdirectories.
The name of ksession cannot be repeated. The name set by KieSession in Java code is the name of this configuration.
DroolsTest
KieServices
This interface provides many methods, through which you can access KIE-related objects about building and running, for example, you can obtain KieContainer, use KieContainer to access information such as KBase and KSession, and obtain KieRepository objects, and use KieRepository to manage KieModule.
KieServices is a center, through which various objects are obtained to complete the operation of rule building, management, and execution.
KieContainer
KieContainer can be understood as a container for KieBase.
KieBase
KieBase can be understood as a knowledge repository, including a number of rules, processes, methods, etc., mainly rules and methods in Drools, KieBase itself does not contain run-time data and so on. If you need to implement the rules in the rule KieBase, you need to create a KieSession according to KieBase.
KieSession
KieSession is a session that deals with the Drools engine, which is based on KieBase, contains runtime data, contains "fact Fact", and performs regular operations on the facts of the run-time data. It is convenient for us to create KieSession through KieContainer. In fact, it is essentially created from KieBase.
Dynamic rule
There are several ways to realize dynamic rules.
1. Using Workbench, dynamically generate configuration rules through the Web management page
two。 To store the rules in the database in the form of a string, we need to develop a set of rule management system.
Use Wrokbench
1.docker deployment workbench
Workbench
Docker run-p 8080-8080-p 8001-- 8001-d-- name drools-wb jboss/drools-workbench-showcase:7.5.0.Final
Kie-server
Docker run-p 8180 link drools-wb:kie_wb jboss/kie-server-showcase:7.5.0.Final 8180-d-name kie-server-
Access http://ip address: 8080/drools-wb
Default account / password admin/admin
two。 Create a project
3. Create Object
4. Create a DRL file
5. Deployment
6. Use deployed rule files
Remote rule Maven warehouse address acquisition
Local Maven repository settings.xml configuration. Here are only 3 points that need to be added. You need to add them accordingly.
Admin admin guvnor-m2-repo Guvnor M2 Repo http://10.211.55.7:8080/drools-wb/maven2/ default true always true always true guvnor-m2-repo
Create a standard SpringBoot project
Product entity. The package path should be the same as that in the rule file.
@ Datapublic class Product {private String type; private int discount;}
DroolsController.java
@ RestController@RequestMapping ("/ api/") @ Slf4jpublic class DroolsController {@ PostMapping ("getdiscount") public Product getDiscount (Product product) throws IOException {KieServices ks = KieServices.Factory.get (); / / RELEASE represents the latest official version of the jar package ReleaseIdImpl releaseId = new ReleaseIdImpl ("com.myteam", "test", "RELEASE"); KieContainer kieContainer = ks.newKieContainer (releaseId) / automatically scan the Maven repository. New versions will automatically download KieScanner kieScanner = ks.newKieScanner (kieContainer); kieScanner.start (3000); KieSession kieSession = kieContainer.newKieSession (); kieSession.insert (product); int count = kieSession.fireAllRules (); log.info ("Total {} rules executed", count) Log.info ("discount for goods {} is {}%", product.getType (), product.getDiscount ()); return product;}}
PostMan access interface
Consistent with the configuration in the DRL rules file
7. Dynamic rule
Modify the ppdrl.drl rules table, modify the GOLD discount to 50%, save the rules, upgrade the project version by one version, and build&deploy the project
When you access the interface again, the discount for GOLD has been changed to 50%, and the service has not been restarted.
This is the end of the content of "simple use of Drools Rule engine". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.