Drools Rule Attributes

From Training Material
Jump to navigation Jump to search


title
Drools Rule Attributes
author
Filip Stachecki (filip@nobleprog.pl) based on docs.jboss.org/drools

Drools Rule Attributes⌘

Rule attributes.png

Drools documentation, Figure 8.10. rule attributes

no-loop⌘

  • type: boolean
  • default value: false

When a rule's consequence modifies a fact it may cause the rule to activate again, causing an infinite loop. Setting no-loop to true will skip the creation of another Activation for the rule with the current set of facts.

enabled⌘

  • type: boolean
  • possible values: true or false, also calculated based on external value

If enabled is set to false, rule won't be executed (but will be evaluated!)

rule "Rule 1"
 enabled false
when 
 $e : Emp(sal > 1000)
then    
  System.out.println($e.name)
end

dialect⌘

  • default value: as specified by the package
  • type: String
  • possible values: "java" or "mvel"

The dialect species the language to be used for any code expressions in the LHS or the RHS code block. While the dialect can be specified at the package level, this attribute allows the package definition to be overridden for a rule.

package rules1
dialect "mvel" 

rule "Rule 1"
when 
 $e : Emp(sal > 1000)
then    
  System.out.println($e.name)
end

rule "Rule 2"
dialect "java" 
when 
 $e : Emp($e.getSal() > 1000)
then    
  System.out.println($e.getName());
end

salience⌘

  • default value: 0
  • type: integer

Each rule has an integer salience attribute which can be negative or positive. Salience is a form of priority where rules with higher salience values are given higher priority when ordered in the Activation queue.

rule "Rule 1"
when 
 exists Emp()
then    
  System.out.println("First rule")
end

rule "Rule 2"
 salience 10
when 
 exists Emp()
then    
  System.out.println("Second rule")
end

ruleflow-group⌘

  • default value: N/A
  • type: String

ClipCapIt-160926-114635.PNG

  • Drools uses ruleflow-group attributes which allows BPMN diagrams to declaratively specify when rules are allowed to fire.
  • The screenshot is taken from Eclipse using the Drools plugin.

agenda-group⌘

  • default value: MAIN
  • type: String

Agenda groups are a way to partition the Agenda into groups and to control which groups can execute. By default, all rules are in the agenda group "MAIN".

rule "Rule 1"
agenda-group "myGroup1"
when 
 exists Emp()
then    
  System.out.println("First")
end

rule "Rule 2"
agenda-group "myGroup2"
when 
 exists Emp()
then    
  System.out.println("Second")
end

rule "Rule 3"
when 
 exists Emp()
then    
  System.out.println("Third")
end

The "agenda-group" attribute lets you specify a different agenda group for the rule. Initially, a Working Memory has its focus on the Agenda group "MAIN". A group's rules will only fire when the group receives the focus. This can be achieved either ny using the method by setFocus() or the rule attribute auto-focus. "auto-focus" means that the rule automatically sets the focus to its agenda group when the rule is matched and activated.

ksession.getAgenda().getAgendaGroup( "myGroup1" ).setFocus();
ksession.getAgenda().getAgendaGroup( "myGroup2" ).setFocus();
ksession.fireAllRules();

The agenda always evaluates the top of the stack. When all the rules have fired for a group, it is poped from the stack and the next group is evaluated.

Result:

Answer >>

Second
First
Third

lock-on-active⌘

  • default value: false
  • type: Boolean

Whenever a ruleflow-group becomes active or an agenda-group receives the focus, any rule within that group that has lock-on-active set to true will not be activated any more; irrespective of the origin of the update, the activation of a matching rule is discarded.
This is a stronger version of no-loop, because the change could now be caused not only by the rule itself.

activation-group⌘

  • default value: N/A
  • type: String

Rules that belong to the same activation-group (a.k.a XOR group), identified by this attribute's string value, will only fire exclusively.
More precisely, the first rule in an activation-group to fire will cancel all pending activations of all rules in the group, i.e., stop them from firing.

rule "Rule 1"
activation-group "myGroup1"
when 
 exists Emp()
then    
  System.out.println("First")
end

rule "Rule 2"
activation-group "myGroup1"
salience 10
when 
 exists Emp()
then    
  System.out.println("Second")
end

rule "Rule 3"
when 
 exists Emp()
then    
  System.out.println("Third")
end

Result:

Answer >>

Second
Third

date-effective⌘

date-expires⌘

duration⌘