12

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
  • 847
  • 4
  • 19
  • 33

7 Answers7

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
  • 739
  • 6
  • 14
6

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,898
  • 1
  • 13
  • 5
5

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
  • 4,116
  • 2
  • 32
  • 24
3

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,084
  • 2
  • 12
  • 27
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
  • 7,318
  • 1
  • 31
  • 65
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
  • 945
  • 1
  • 13
  • 24
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".