16.9.2. Other rule action methods from drools and kcontext variables

In addition to the standard rule action methods, the decision engine supports methods in conjunction with the predefined drools and kcontext variables that you can also use in rule actions.

You can use the drools variable to call methods from the KnowledgeHelper class in your Red Hat Decision Manager distribution, which is also the class that the standard rule action methods are based on. For all drools rule action options, download the Red Hat Decision Manager 7.10.0 Source Distribution ZIP file from the Red Hat Customer Portal and navigate to ~/rhdm-7.10.0-sources/src/drools-$VERSION/drools-core/src/main/java/org/drools/core/spi/KnowledgeHelper.java.

The following examples are common methods that you can use with the drools variable:

  • drools.halt(): Terminates rule execution if a user or application has previously called fireUntilHalt(). When a user or application calls fireUntilHalt(), the decision engine starts in active mode and evaluates rules continually until the user or application explicitly calls halt(). Otherwise, by default, the decision engine runs in passive mode and evaluates rules only when a user or an application explicitly calls fireAllRules().
  • drools.getWorkingMemory(): Returns the WorkingMemory object.
  • drools.setFocus( "<agenda_group>" ): Sets the focus to a specified agenda group to which the rule belongs.
  • drools.getRule().getName(): Returns the name of the rule.
  • drools.getTuple(), drools.getActivation(): Returns the Tuple that matches the currently executing rule and then delivers the corresponding Activation. These calls are useful for logging and debugging purposes.

You can use the kcontext variable with the getKieRuntime() method to call other methods from the KieContext class and, by extension, the RuleContext class in your Red Hat Decision Manager distribution. The full Knowledge Runtime API is exposed through the kcontext variable and provides extensive rule action methods. For all kcontext rule action options, download the Red Hat Decision Manager 7.10.0 Source Distribution ZIP file from the Red Hat Customer Portal and navigate to ~/rhdm-7.10.0-sources/src/kie-api-parent-$VERSION/kie-api/src/main/java/org/kie/api/runtime/rule/RuleContext.java.

The following examples are common methods that you can use with the kcontext.getKieRuntime() variable-method combination:

  • kcontext.getKieRuntime().halt(): Terminates rule execution if a user or application has previously called fireUntilHalt(). This method is equivalent to the drools.halt() method. When a user or application calls fireUntilHalt(), the decision engine starts in active mode and evaluates rules continually until the user or application explicitly calls halt(). Otherwise, by default, the decision engine runs in passive mode and evaluates rules only when a user or an application explicitly calls fireAllRules().
  • kcontext.getKieRuntime().getAgenda(): Returns a reference to the KIE session Agenda, and in turn provides access to rule activation groups, rule agenda groups, and ruleflow groups.

    Example call to access agenda group "CleanUp" and set the focus

    kcontext.getKieRuntime().getAgenda().getAgendaGroup( "CleanUp" ).setFocus();

    This example is equivalent to drools.setFocus( "CleanUp" ).

  • kcontext.getKieRuntime().getQueryResults(<string> query): Runs a query and returns the results. This method is equivalent to drools.getKieRuntime().getQueryResults().
  • kcontext.getKieRuntime().getKieBase(): Returns the KieBase object. The KIE base is the source of all the knowledge in your rule system and the originator of the current KIE session.
  • kcontext.getKieRuntime().setGlobal(), ~.getGlobal(), ~.getGlobals(): Sets or retrieves global variables.
  • kcontext.getKieRuntime().getEnvironment(): Returns the runtime Environment, similar to your operating system environment.