[rules-users] setting global variables in session : the Command interface VS the RuleContext. What is the difference ?

Wolfgang Laun wolfgang.laun at gmail.com
Mon Apr 23 07:30:24 EDT 2012


If you have a StatelessKnowledgeSession (or StatefulKnowledgeSession)
object, you'd call its method setGlobal () to assign an object to a
global variable.

If you don't have the session object, you can acquire it in some other
way, e.g., in consequence (RHS) code. You'd use the predefined
variable kcontext (of type org.drools.runtime.KnowledgeContext - see
javadoc) to obtain KnowledgeRuntime and call ist setGlobal() method.

Using the execute() method on a KnowledgeSession object lets you call
a set of methods represented as Command objects, as is frequently done
in the context of a web service.

-W


On 23/04/2012, Ahmed Feki <feki.ahmed.job at gmail.com> wrote:
> hi ,
> i m new in drools, i m reading the Drools-JBoss-Rules-5.0-Developers-Guide
> book in which i found a validation rule example on its chapter 3. In this
> exemple, global variables were set into the session with two methods shown
> in this code :
>
> ...
> session = knowledgeBase.newStatelessKnowledgeSession();
>     session.setGlobal("reportFactory", reportFactory);
>     session.setGlobal("inquiryService", inquiryService);
> ...
>
> and these variables qualified as immutables, were called from the
> RuleContext
>
> public static void error(RuleContext drools,
>       Object... context) {
>  KnowledgeRuntime knowledgeRuntime = drools
>         .getKnowledgeRuntime();
>     ValidationReport validationReport = (ValidationReport)
>         knowledgeRuntime.getGlobal("validationReport");
>     ReportFactory reportFactory = (ReportFactory)
>         knowledgeRuntime.getGlobal("reportFactory");
> ...
>
> and for other variables(or facts ) the Command interface was used instead :
>
>  List<Command> commands = new ArrayList<Command>();
>     commands.add(CommandFactory.newSetGlobal(
>         "validationReport", report));
>     commands.add(CommandFactory
>         .newInsertElements(getFacts(customer)));
>     session.execute(CommandFactory
>         .newBatchExecution(commands));
>
> i m looking for accuracy in understanding when and which to use each
> method.
>


More information about the rules-users mailing list