Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools:stateless vs stateful knowledge session

Tags:

drools

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.

like image 813
tech2504 Avatar asked Jun 18 '13 17:06

tech2504


5 Answers

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.

like image 136
Prakhyat Avatar answered Oct 23 '22 12:10

Prakhyat


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).

like image 22
Kris Verlaenen Avatar answered Oct 23 '22 10:10

Kris Verlaenen


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!

like image 27
Kingz Avatar answered Oct 23 '22 11:10

Kingz


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

like image 4
Haijin Avatar answered Oct 23 '22 10:10

Haijin


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 ).

like image 1
Anmol Singh Jaggi Avatar answered Oct 23 '22 11:10

Anmol Singh Jaggi