drools rule (three) RHS syntax Detailed

1 Usage

The right hand side (RHS) is a consequence or action part of the common name of the rule; this section should contain a list of operations to be performed. Use imperative condition or type the code in the RHS of the rule is bad practice; as a rule should be atomic - "When this, then do this" rather than "when this might do it." RHS part of the rule should also be kept small, thereby maintaining its statement and readability. If you need to find the RHS imperative and / or condition code, then you should probably break down the rule into multiple rules. The main purpose of the RHS is to insert, delete, or modify working memory data. To help you, you can use some easy ways to modify working memory; without first referring to a working memory instance.

  • update(object, handle); will tell Drools engine of an object has changed (an already bound to something on the LHS) and rules may need to be reconsidered.
  • update(object); can also be used; here, Knowledge Helper will check the facts for you to pass the handle to find objects by identity. (Note that if the insertion Drools engine Java bean properties listening, you should avoid calling when an object changes update()). In fact after the field value is changed, you must change the fact that another update before calling, or you will cause Drools engine indexing problems. modify keywords can avoid this problem.
  • insert(newSomething()); new objects you create will be placed in the Working Memory.
  • insertLogical(newSomething()); similar to the insert, but when there are no more facts to support true triggered the current rules, the object will be automatically deleted.
  • delete(handle);从 Working Memory remove an object.

These convenient way to basically provide you with a quick use of KnowledgeHelperexamples (working memory can be accessed from the rule file) macros. Predefined KnowledgeHelper类型variables droolsallow you to call several other useful methods. ( KnowledgeHelperFor more advanced operations, please refer to the interface documentation).

  • Call drools.halt()termination rule immediately. When the current session is by fireUntilHalt()进入工作区时,必须调用drools.halt()returning control.

  • Method insert(Object o), update(Object o)and delete(Object o)it can be in drools上调用, but because they often use, so you can ignore the direct object reference is called.

  • drools.getWorkingMemory()Returns the WorkingMemoryobject.

  • drools.setFocus( String s) Set the focus to the agenda designated group.

  • drools.getRule().getName()From RHS rules call, returns the name of the rule.

  • drools.getTuple()Returns the currently executing rule matching Tuple, drools.getActivation()passing the appropriate activation. (These calls for records and for debugging.)

Complete Knowledge Runtime API by another predefined KieContexttypes of variables kcontextpublic. Its method getKieRuntime()provides an KieRuntimeobject type, which in turn provides access to a number of methods, many of which are very useful for encoding RHS logic.

  • Call kcontext.getKieRuntime().halt()termination rule immediately.

  • Visitors getAgenda()return Agenda of this session of reference, the session Agendaalso provides access to various sets of rules: activation groups, agenda groups, and rule flow groups. A fairly common example is the activation of some of the agenda of the group, this can be done through a lengthy call:

    // give focus to the agenda group CleanUp
    kcontext.getKieRuntime().getAgenda().getAgendaGroup( "CleanUp" ).setFocus();
    
    (你可以使用drools.setFocus( "CleanUp" )实现相同的功能)

     

  • To run the query, call getQueryResults(String query), and then the results can be processed, such as "  inquiry" in the section. Use kcontext.getKieRuntime().getQueryResults()or using drools.getKieRuntime().getQueryResults()the correct method to query the operating rules of the RHS, it is the only supported method.

  • A set of methods to handle events management allows you to add and remove event listeners for Working Memory and Agenda.

  • The method getKieBase()returns KieBasean object, the system backbone as well as the initiator of all Knowledge of the current session.

  • You can use setGlobal(…​), getGlobal(…​)and getGlobals()manage global.

  • Methods getEnvironment()return run Environment, it works and you know the operating system environment is very similar.

 

2 modify

This language extension provides a structured approach fact updates. It will update with many setter calls combine to change the field of the object. This is the modifysyntax of architecture statement:

modify ( <fact-expression> ) {
    <expression> [ , <expression> ]*
}

Parenthesized <fact-expression> must generate facts object reference. Expression block list should contain the given object setter calls, without the need for ordinary object references (compiler automatically added). This example demonstrates a simple fact changes.

rule "modify stilton"
when
    $stilton : Cheese(type == "stilton")
then
    modify( $stilton ){
        setPrice( 20 ),
        setAge( "overripe" )
    }
end

When changing the properties of fine-grained listener combination, the advantages of using the modified statement is particularly evident. For more information, see the appropriate section.

 

Guess you like

Origin blog.csdn.net/top_explore/article/details/93873534