This is the 10th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

A, Drools – Group

There are three types of drools-groups: activation-group (agenda Group), ruleflow-group (ruleflow Group), and agenda-group (activation Group).

/** * If one rule with the same activation-group property is executed, no other rules will be executed. * You can prioritize rules with attributes such as salience */ activation-group /** * divide rules into groups, and then use the ruleflow-group value in the ruleflow. This attribute determines which rule to execute based on the direction of the process */ ruleflow-group /** *Agenda Group is used to regroup rules based on an Agenda. When the engine calls a rule with the agend-group * attribute set, the specified agenda group needs to be displayed to obtain focus; otherwise, the rules of this group will not be executed. */ agenda-groupCopy the code

Second, the Agenda – group

Agenda groups allow rules to be grouped and put on a stack with push/pop operations and push operations done by SetFocus.

kieSession.getAgenda().getAgendaGroup("group2").setFocus();
Copy the code

The agenda is always executed at the top of the stack, and when all the rules for the agenda group are executed, the agenda group is removed and the next group is executed.

Rules are called and executed by StatelessKieSession or KieSession. The general order is to create a StatelessKieSession or KieSession, and add various compiled rules to the session. We then insert the Global and Fact objects that might be used in the rule into the Session, and call the fireAllRules method to trigger and execute the rule.

Before the fireAllRules method is called, all rules and Fact objects are stored in an Agenda table object. Each rule in the Agenda table and the business data associated with it is called an Activation. The Activation is executed in sequence, and the order of execution is random if the relevant control order property (such as the salience property) is not set.

An Agenda Group is used to regroup rules based on an Agenda. You can add an Agenda – Group attribute to a rule. The value of the agend-group attribute is a string that divides rules into several agenda groups. When the engine calls a rule with the agend-group attribute set, the specified agenda group that needs to be displayed is focused. Otherwise, the rules in the agenda group are not executed.

public void excuteAgendaGroup(){ KieBase kieBase = KieStarter.newKieBase(getAgendaGroupDrl()); KieSession kieSession = kieBase.newKieSession(); // Group2 after execution kieSession.getAgenda().getAgenda ().getAgenda ().setFocus(); //group1 run kieSession.getAgenda().getAgenda ().setFocus(); kieSession.fireAllRules(); kieSession.dispose(); } private String getAgendaGroupDrl(){ return "rule "test-agenda-group1" " + "agenda-group "group1" " + "when then System.out.println(" test-agend-group1-1 is triggered "); " + "end " + "rule "test-agenda-group2" " + "agenda-group "group2" " + "when " + "then " + "System.out.println(" test-agend-group2 is triggered "); end\n" + "rule "test-agenda-group3" " + "agenda-group "group1" " + "when then System.out.println("test-agenda-group1-2 Trigger "); " + "end "; }Copy the code

Third, the Activation – Group

The activation group requires the same group to execute only one

This attribute groups several rules into a unified named group. At execution time, if only one rule with the same activation-group property is executed, none of the other rules will be executed. An attribute such as salience can be used to implement the execution priority of a rule.

public void executeActivationGroup(){ KieBase kieBase = KieStarter.newKieBase(getActivationGroupDrl()); KieSession kieSession = kieBase.newKieSession(); kieSession.fireAllRules(); } private String getActivationGroupDrl(){ return "rule "test-activation-group1" " + "salience 1"+ "activation-group "+ "when then system.out. println("test-activation-group1 was triggered "); " + "end " + "rule "test-activation-group2" " + "salience 2"+ "activation-group "group1" " + "when " + "then " + Println ("test-activation-group2 was triggered "); end\n"; }Copy the code

Fourth, Flow – Group

Streaming groups support service choreography, running rules in sequence like a workflow

The ruleflow-group attribute is used when using the ruleflow. The value of this attribute is a string, which is used to divide the rules into groups. Then, the corresponding rules are used in the ruleflow by using the value of the ruleflow-group attribute. This property determines which rule to execute by the direction of the process. This is explained in the ruleflow.

Rule "test-ruleflow-group1" ruleflow-group "group1" when then system.out. println("test-ruleflow-group1 is triggered "); End rule "test-ruleflow-group2" ruleflow-group "group1" when then system.out. println("test-ruleflow-group2 is triggered "); endCopy the code

\