13

What is difference between stateless and stateful knowledge sessions.I read some documents both are maintained state.But when can i use stateless/stateful knowledge sessions.

tech2504
  • 947
  • 4
  • 19
  • 34

8 Answers8

18

Stateless: The facts/working memory is inserted to Knowledge base session before firing rules. These facts can be set by calling public methods on an object while executing rules and after setting these objects are returned back with changed values.

Any changes in the facts while executing rules, for example insert(xyz) or modify(xyz), is not made aware to the rule engine.

Stateful: The facts/working memory is inserted to Knowledge base session before firing rules and after the rules are fired dispose() has to be called to avoid memory leaks.

Any changes in the facts while executing rules, for example insert(xyz) or modify(xyz), is made aware to the rule engine.

Prakhyat
  • 989
  • 8
  • 17
7

Stateless means a new session is created for each request (so no state is maintained). Stateful means it will continue from whatever state the session was when the previous command ended (for example, all data that was inserted into the session will still be there).

Kris Verlaenen
  • 2,918
  • 1
  • 15
  • 5
6

The basic difference the way i see it, is the way the session is auto disposed in stateless. There are no performance gain to be had by choosing one vs. other. Actually, the stateless session uses a stateful session behind it. So go figure!

Kingz
  • 5,086
  • 3
  • 36
  • 25
4

I want to quote the drools documentation here which cleared my mind.

"StatelessKnowledgeSession provides a convenience API, wrapping StatefulKnowledgeSession. It avoids the need to call dispose(). Stateless sessions do not support iterative invocations, the act of calling execute(...) is a single shot method that will internally instantiate a StatefulKnowledgeSession, add all the user data and execute user commands, call fireAllRules, and then call dispose()."

So basically, stateless session is a stateful session used one time.

This then imply that stateless session can also do inference, unlike many documents and some answers here said! This should only depend on the "then" part of the rule, whether you use "modify" or not.

While I have not verify this myself, this post seems support my reasoning.

https://groups.google.com/forum/#!topic/drools-usage/qYbqiS1ht4g

Haijin
  • 2,561
  • 2
  • 17
  • 30
1

In stateful sessions, we can modify facts and reinsert them even after having the rules fired before.

In stateless sessions, on the other hand, once all the rules have been fired ( using execute() ), we cannot further modify the facts and reinsert them into the session ( as the session is unusable after execution() is called ).

Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
0

1) In case of Stateless Knowledge Session, while rules execution i.e. once fireRules method is called, modification in the inserted facts (in the then part) is not available to the rules engine. In case of Stateful Knowledge Session, any changes in the facts is available to the rule engine.

2) Once rules are fired, Stateful Knowledge Session object must call the method dispose() to release the session and avoid memory leaks.

3) In case of Stateful Knowledge Session, any changes to facts is available to the rule engine. So the rules are called iteratively. If Fact A is modified in last rule of DRL, then this change will re-activate all the rules and fire the rules that are build on Fact A. This is not the case with Stateless Knowledge Session.

The hidden fact is Stateless session uses a Stateful session behind it

chammu
  • 1,275
  • 1
  • 18
  • 26
0

This link is accurate : https://groups.google.com/forum/#!topic/drools-usage/qYbqiS1ht4g Drools should add in the official document.

Stateful : "Inserted data objects will be part of working memory & can be reused later for further rule execution."

Stateless : "Inserted data objects will not be stored in working memory after rules execution".

0

If I refer to the Drools documentation on Stateless Session, it says :

A stateless KIE session is a session that does not use inference to make iterative changes to facts over time. In a stateless KIE session, data from a previous invocation of the KIE session (the previous session state) is discarded between session invocations, whereas in a stateful KIE session, that data is retained

This may probably need clarification in the documentation and iterative be replaced by interactive.

This does NOT mean that :

Any changes in the facts while executing rules, for example insert(xyz) or modify(xyz), is not made aware to the rule engine.

as suggested by @Prakhyat in his comment.

You can actually tell Drools to ignore any updates (insert/modify) made to working memory for better performance using Sequential mode in Phreak to have better performance :

In sequential mode, the Drools engine ignores any insert, modify, or update statements in rules and executes rules in a single sequence. As a result, rule execution may be faster in sequential mode, but important updates may not be applied to your rules.

Sequential mode applies to only stateless KIE sessions

[...]

Sequential mode is disabled by default in the Drools engine.

guiliguili
  • 11
  • 1