# BRMS-Rules-KJar **Repository Path**: mirrors_gspandy/BRMS-Rules-KJar ## Basic Information - **Project Name**: BRMS-Rules-KJar - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-24 - **Last Updated**: 2026-01-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

Sunil Samuel
web_github@sunilsamuel.com
http://www.sunilsamuel.com

**

[Code] BRMS :: Rules as KJar

** # Overview This project is a sample BRMS (Drools) project that can be used to deploy into the kie-server. ## Introduction This sample BRMS (Drools) project can be used as a starting point for your own requirements and to be extended to support your criteria. This application uses Cucumber to test the BRMS (Drools) engine and to run as JUnit or Maven tests. The `RulesEngineSupport.java` is a re-usable generic code that can help to test your DRL rules. The application is written with the assumption that you have some understanding of the Drools engine and the Kie Server. There is another Github application that is created to describe the deployment of this kjar into the kie server. See the following project: ``` KieServer-KJar-Deployment ``` ## Technology Stack 1. Drools (BRMS) 2. Cucumber # Project Description The application provides a scenario that uses a person's information to determine if this person is eligible for a loan and if they are, the amount of the loan. This information is determined by the input from the user. ## Installation The KJar should be installed into your maven .m2 repository directory so that it can be deployed into the Kie Server. In order to install into the maven repository, use maven as follows in the `BRMS-Rules-KJar` directory: ```sh mvn clean install ``` ## Rule This is a very simple application that uses the following rule to determine the loan amount for a person. This application can be extended to be more realistic and more robust. ``` Criteria 1 1. The user is 18 or over years old 2. The user is a full-time student 3. The user family income is less than $45,000 THEN : Loan Amount = 5000 ``` ``` Criteria: 2 1. The user is less than 18 years old 2. The user is a full-time student 3. The user family income is less than $45,000 THEN : Loan Amount = 8000 ``` ``` Criteria: 3 1. The user is 18 or over years old 2. The user is a part-time student 3. The user family income is less than $55,000 THEN : Loan Amount = 3000 ``` ``` Criteria: 4 Otherwise, the person is not eligible for a loan ``` ## Query The Drools Query can be used to retrieve the collection of all of the LoanAmount objects that were inserted into the knowledge session. ```sql query "Query LoanAmount" loanAmount : List() from collect(LoanAmount()) end ``` ## Kie-Server Request Once this is deployed into the kie server (see **KieServer-KJar-Deployment** project), the following JSON can be used to execute the rules against your data elements. ```http POST /kie-server/services/rest/server/containers/instances/brms-rules-kjar HTTP/1.1 Host: localhost:8080 content-type: application/json accept: application/json X-KIE-ContentType: JSON Accept: application/json Authorization: Basic [your authorization] Cache-Control: no-cache { "lookup":"defaultStatelessKieSession", "commands":[ { "insert":{ "object":{ "com.sunilsamuel.brms.model.UserInformation":{ "firstName":"Sunil", "lastName":"Samuel", "identifier":2342342, "age":18, "collegeStatus":"PartTime", "familyIncome":46000 } } } }, { "insert":{ "object":{ "com.sunilsamuel.brms.model.UserInformation":{ "firstName":"Joel", "lastName":"Samuel", "identifier":234234245, "age":18, "collegeStatus":"FullTime", "familyIncome":43000 } } } }, { "fire-all-rules":"" }, { "query":{ "name":"Query LoanAmount", "out-identifier":"loanAmount" } } ] } ``` ## Kie-Server Response The response from the Kie-Server will include every LoanAmount object that was inserted by the rules. Therefore, you can send multiple facts into the Drools engine and the `query` will provide the list of inserted objects. The Kie Server response is as follows: ```js { "type":"SUCCESS", "msg":"Container brms-rules-kjar successfully called.", "result":{ "execution-results":{ "results":[ { "key":"", "value":4 }, { "key":"loanAmount", "value":{ "org.drools.core.runtime.rule.impl.FlatQueryResults":{ "idFactHandleMaps":{ "type":"LIST", "componentType":null, "element":[ { "type":"MAP", "componentType":null, "element":[ { "key":"loanAmount", "value":{ "org.drools.core.common.DisconnectedFactHandle":{ "id":6, "identityHashCode":2069568795, "objectHashCode":-1629276672, "recency":6, "object":[ { "com.sunilsamuel.brms.model.LoanAmount":{ "userIdentifier":2342342, "loanAmount":3000, "eligible":true } }, { "com.sunilsamuel.brms.model.LoanAmount":{ "userIdentifier":234234245, "loanAmount":5000, "eligible":true } } ], "traitType":"NON_TRAIT", "external-form":"0:6:2069568795:-1629276672:6:null:NON_TRAIT:java.util.ArrayList" } } } ] } ] }, "idResultMaps":{ "type":"LIST", "componentType":null, "element":[ { "type":"MAP", "componentType":null, "element":[ { "key":"loanAmount", "value":[ { "com.sunilsamuel.brms.model.LoanAmount":{ "userIdentifier":2342342, "loanAmount":3000, "eligible":true } }, { "com.sunilsamuel.brms.model.LoanAmount":{ "userIdentifier":234234245, "loanAmount":5000, "eligible":true } } ] } ] } ] }, "identifiers":{ "type":"SET", "componentType":null, "element":[ "loanAmount" ] } } } } ], "facts":[ ] } } } ``` # Cucumber Testing Cucumber (cukes) is great for testing the results of the Drools execution given your facts. The `RulesEngineSupport.java` test support code can be used to extend the testing capabilities. Please look at the `RuleEngine.java` that extends `RulesEngineSupport.java`. These code can be used directly as is into your application to test your application. ## Feature File A sample cucumber feature file looks as follows: ```feature @BRMS @UserInformation @Complete Feature: Validate Loan Amount for User Validate the loan amount given the different values for the criteria including College Status, Age, and Family Income. Scenario Outline: Validate Loan Amount Given a user with the first name and the last name And is identified with And the user age is And the user college status is And the user family income is When I check the loan eligibility Then the loan amount is And the loan user identifier is And the loan eligibility is Examples: | first | last | identifier | age | status | income | amount | loanIdentifier | eligibility | | Sunil | Samuel | 123 | 14 | FullTime | 42000 | 8000 | 123 | true | | Person | Samuel | 421231 | 21 | PartTime | 54043 | 3000 | 421231 | true | | Person2 | Samuel | 23432 | 21 | FullTime | 32000 | 5000 | 23432 | true | | Person2 | Samuel | 23432 | 21 | FullTime | 32000 | 5000 | 23432 | true | | Person3 | Samuel | 123455 | 21 | FullTime | 76000 | 0 | 123455 | false | ```