Drools -rule语法组成

Drools -Rule syntax


    Our rule file can contain the  following elements:

1. package: 

Package-name is the name of the folder in which a rule file lives.This is useful for organising our rules 

(especially when you build up  hundreds of them).

    与java语言的包一样,命名空间。

2. import: 

This pulls in any Java class files (including fact models) that we use in our rules.
与java一样的功能。

3. globals: 

This defines the global variables that we use. Remember variables (boxes that hold values)? Earlier, they were
 emptied as soon as our rule had fired and only changes to the facts lived on. Compared to 'normal' variables,
global variables live longer, and allow us to pass information into and out of  our rules.
     利用全局变量,可以向规则传递变量(不仅仅值值变量,还可以传递接口实现,利用它调用方法)。最好不要用全集变量让许多规则传递信息,而是利用fact。

Note:
Passing information into and out of our rules via a global variable is almost the same as passing a fact into the
 rules. The difference is that the rule engine does not match (or fire) against global variables. variables.
      This makes the rule engine more suitable for passing in items that  change slowly, such as the current date,
 counter, and so on, and giving rules access to external resources (such as log files).

4. functions: 

Sometimes you may need to perform the same calculation in many rules. Defining a function allows you to
 perform the same calculation again and again. Note that it's often easier to call a normal Java function to carry
out the same task.
   与java中的函数一样,不过,我们可以利用3中的全局变量传递类实例,从而使用类实例中相关的方法。

5.queries




6.attributes

   no-loop
   default value: false


type: Boolean


    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.
    要明白什么情况下会导致此规则重新激活。

ruleflow-group
default value: N/A


type: String


Ruleflow is a Drools feature that lets you exercise control over the firing of rules. Rules that are assembled by the same ruleflow-group identifier fire only when their group is active.


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. It's ideal for calculation rules where you have a number of rules that modify a fact and you don't want any rule re-matching and firing again. Only when the ruleflow-group is no longer active or the agenda-group loses the focus those rules with lock-on-active set to true become eligible again for their activations to be placed onto the agenda.


salience
default value: 0


type: integer


Each rule has an integer salience attribute which defaults to zero and 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.


Drools also supports dynamic salience where you can use an expression involving bound variables.


Example 8.48. Dynamic Salience


rule "Fire in rank order 1,2,.."
        salience( -$rank )
    when
        Element( $rank : rank,... )
    then
        ...
end


agenda-group
default value: MAIN


type: String


Agenda groups allow the user to partition the Agenda providing more execution control. Only rules in the agenda group that has acquired the focus are allowed to fire.


auto-focus
default value: false


type: Boolean


When a rule is activated where the auto-focus value is true and the rule's agenda group does not have focus yet, then it is given focus, allowing the rule to potentially fire.


activation-group
default value: N/A


type: String


Rules that belong to the same activation-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.


Note: This used to be called Xor group, but technically it's not quite an Xor. You may still hear people mention Xor group; just swap that term in your mind with activation-group.


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. Currently two dialects are available, Java and MVEL. While the dialect can be specified at the package level, this attribute allows the package definition to be overridden for a rule.


date-effective
default value: N/A


type: String, containing a date and time definition


A rule can only activate if the current date and time is after date-effective attribute.


date-expires
default value: N/A


type: String, containing a date and time definition


A rule cannot activate if the current date and time is after the date-expires attribute.


duration
default value: no default value


type: long


The duration dictates that the rule will fire after a specified duration, if it is still true.

7. rule: 

This is the 'when…then' structure that we've spent most of time write.

8. comments: 

These are pieces of text ignored by the rule engine, which explain what is going on with us. They can be on a 
single line (anything after '//' until the end of the line) or split over multiple lines (everything between the
/* and */ Comments split over many lines)


你可能感兴趣的:(Drools,Drools,JBoss,Rules,规则引擎)