Using Decision Table in JBoss Drools

Rules. We have known this word since childhood. First, parents teach us how to do the right thing, then we come to school, where teachers dictate their own rules. At the university, we are again faced with the rules that teachers and the dean's office set for us. As we grow up, we begin to follow the laws that are developed by the state. What do all these rules have in common? The fact that they have been verified over the years, decades, and some even generations. Imagine what our life would be like if the rules were changed often: every week or every day!





, Mediascope, : , . . -.  , , . 





?

, . ( ) - . BRMS Business Rule Management System. ,

- , -. , - , - .





, , «», . . : , , , , . , !





BRMS . : IBM, Red Hat, Agiloft, SAS Bosch. – , . JBoss Drools. , , , , - DRL, Excel-. UI- . , UI Excel-.





- ?

- , . , , , . .





,   (gender).   (SEX).  ,  gender = male, «»  1.  2. DRL :





rule "Rule 1 Example 1"
      when
         $s: Respondent($s.gender == "male")
      then
         $s.addResult("SEX", "1");
end

 rule "Rule 2 Example 2"
      when
         $s: Respondent($s.gender == "female")
      then
         $s.addResult("SEX", "2");
 end
      
      



, ,  Gherkin. , (  when) , (  then).   Drools   Left Hand Side ( LHS), - Right Hand Side ( RHS). : Respondent fact,   Drools, .  gender  result. , , framework  .  $.





 Excel-:





Example rule in Excel
Excel

?

, , , , . ,  Drools  ,  Miscrosoft Excel OpenOffice,  CSV.  DRL-.





: .  RuleSet, –  RuleTable. , .





Rule setting area

 DRL- . - . . : 





  • RuleSet β€“  DRL.





  • Import β€“ ,  Java-, , , java.lang.Math





  • Functions β€“ ,  RuleSet’.  DRL-.





,  RuleTable, , ,  Drools   DRL-. .  nameforRuleTable.





Rule description area

:





  • NAME β€“ . .





  • DESCRIPTION β€“ . . , . 





  • CONDITION β€“  Left Hand Side  ,  ACTION. .





  • ACTION β€“ , .  addResult,  Map  . .  ACTION  Java-, . .





    ,  CONDITION  ACTION  . 





,  $s  Respondent. , , ,  Import.  CONDITION  , .  CONDITION-: ,  Import.





, ,  CONDITION   ACTION, . , . . (Text-Parameter-Result  ). . /, , Drools . .





?

, , . .





,  Respondent. – , . (, , ). , ,  Drools,  Respondent. ,  Map<String, String> result, . ,  Respondent  :





public class Respondent {

    public int id;
    public String gender;
    public Boolean isActive;
    public Integer age;
    public List<String> pets;
    public String city;
    public List<String> devices;
    public Map<String, String> properties;
    public Car car;
    public MobileBrand mobileBrand;
    public Household household;

    public Map<String, String> result = new HashMap<>();

    public void addResult(String key, String value) {
        result.put(key, value);
    }

//  getters, setters, constructor

}
      
      



, , . , , ?  isActive.  true β€“ , false β€“ .





Rules for determining the gender of the respondent

, : . , , ,  .  Respondent.  DRL:





rule "name_for_RuleTable_20"
	when
		$s: Respondent(isActive == true, gender == "male")
	then
		$s.addResult("SEX", "M");
end

rule "name_for_RuleTable_21"
	when
		$s: Respondent(isActive == true, gender == "female")
	then
		$s.addResult("SEX", "F");
end
      
      



 $param -  .  isActive == $param   isActive == true. .  Drools , . ,  gender.





. :





Rules for determining the sex and age attribute

, - 17 18 . , .





, – . , . , ,  PHONE_SALES_PER  2019 . ,  $param . ,  MobileBrand   Import  .





Rules for determining the percentage of sales

, . ,  ACTION   $param, . .  in:





Rules for determining the segment of the smartphone market





:  SAMSUNG  APPLE,  PREMIUM. ,  HUAWEI  XIAOMI,  NOT_PREMIUM. ( ,    :) ) , . 





, ?  contains.  java.util.Collection.  Respondent   pets.  , . , , . Β« Β» – ,  $param .





The rule for determining a pet

forall(<>){<>}.  . , . ,  ,  . . , , ,  param. , – ($s). , ,  Drools , - (city devices),  CONDITION  . 





County Determination and Internet Activity Guidelines
-

, – .  size:





Rules for determining the number of devices owned

- , , . Β«-Β». Drools  :





Education definition rules

, . . ,  Car c  : , , . , , . , -, .  from:





Rules for determining the age of the car

, . , ,  Car,   Respondent,  Map<String, String> properties .  POWER.  Drools getValue.  Functions :





GetValue function description
getValue

:





Rules for determining the power of the car

, . : ,  PlayStation. ,  Household  Respondent  TVDevices   PlayStation. :





PlayStation rule
PlayStation

?

.  ACTION? ,  Java. . , ,  ACTION  .





 Drools, :





  • set β€“  ,  Drools  ;





  • update β€“  ,  set;





  • modify β€“  , , ;





  • insert β€“  .





 modify.





 RuleTable  , - 99 0. ,  $r,  Respondent’a   false.   RuleTable   isActive   REJECTED   TRUE.





 CONDITION. , . , , . . Drools . , , , . 





 Drools  (https://github.com/sxexesx/drools-decision-table).





Thanks for attention! I hope this article will be useful for those who want to dive into the wonderful world of BRMS!








All Articles