Drools:stateless vs stateful knowledge session

前端 未结 7 1874
生来不讨喜
生来不讨喜 2020-12-14 11:24

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

相关标签:
7条回答
  • 2020-12-14 11:39

    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

    0 讨论(0)
  • 2020-12-14 11:41

    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.

    0 讨论(0)
  • 2020-12-14 11:43

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

    0 讨论(0)
  • 2020-12-14 11:53

    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

    0 讨论(0)
  • 2020-12-14 11:57

    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!

    0 讨论(0)
  • 2020-12-14 11:57

    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 讨论(0)
提交回复
热议问题