Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the stateful Session and stateless Session of drools

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the drools stateful Session and stateless Session is what the related knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that everyone after reading this drools stateful Session and stateless Session is what the article will have a harvest, let's take a look.

1. Understanding

There are two kinds of session in drools, one is stateful Session (Stateful Session), the other is stateless Session (Stateless Session).

1. So what's the difference between them?

2. When should I use stateful Session and when should I use stateless Session?

Here is a brief account of my understanding.

1. Stateful Session

A stateful session is a session that uses reasoning to iterate changes to fact objects over time. In stateful session, data from previous calls to session (previous session state) is retained between session calls, while in stateless session, the data is discarded.

2. Stateless Session

A stateless session is a session that does not use reasoning to iterate changes to fact objects over time. Data previously invoked by stateless Session is not retained between sessions.

Explanation:

For the understanding that stateless session is a session that does not use reasoning to iterate fact objects over time, my understanding is for Java Api

1. In a stateless Session, there is only the execute method. The execute method is called many times. The last execute method will not affect the execution of the next execute method.

2. When using methods such as insert or update or modify or delete in the drl file, it will cause the object in the working memory to be updated and the rules to be rematched.

3. When should I use a different Session?

1. If we are just validating the rules, then use stateless Session.

For example:

Verify whether the user has the conditions to open a bank card. Calculate the discount for the order amount.

That is, it can be done in one step.

2. If our rules need to be completed in multiple steps, we can use stateful Session.

For example:

Insert Fact object An into Session, and then trigger the rule. Execute a piece of Java code to insert Fact object B into the Session, and then trigger the rule, which depends on the data of the previous step.

That is, the associated multiple steps are required to complete.

II. Demand

We have our own Count object, which has cnt and name2 properties.

The following two rules exist in the rules file

Rule 1:

If the Count object exists in working memory, add 1 to the cnt property of the Count object

Rule 2:

If there are two Count objects in working memory, the name=count-01 of one object and the name=count-02 of the other object outputs an ok string.

See the difference between stateful Session and stateless Session.

Third, implementation step 1, project structure description

2 、 Introduction of jar package org.drools drools-bom pom 7.69.0.Final import org.drools drools-compiler org.drools drools-mvel ch.qos.logback logback-classic 1.2.11 Org.projectlombok lombok 1.18.22 3 、 Write the count class @ Data@AllArgsConstructorpublic class Counter {/ * name * / private String name / * * count * / private Integer cnt;}

Is a normal java object.

4. Write kmodule.xml files

Note here that the value of type in ksession is different between stateless Session and stateful Session. If type is not written, the default is stateful Session.

5. Write a rule file package rules.statefulimport com.huan.drools.Counter// to increment the value of cnt in counter by rule "stateful_rule_count_increment" when $counter: Counter () then $counter.setCnt ($counter.getCnt () + 1); System.out.println ("rule_count_increment: count name: [" + $counter.getName () + "], cnt= [" + $counter.getCnt () + "]") End// outputs okrule "stateful_rule_count_exists" when Counter (name = = "count-01") and Counter (name = = "count-02") then System.out.println ("ok") if both count-01 and counter-02 exist in working memory; end6 and Stateful session run results

1. Because it is a stateful Session, the last object inserted into the working memory still exists when there are multiple fireAllRules. That is, the data of Session is retained.

2. After the stateful Session is executed, the dispose method must be called to avoid memory leaks.

7. Stateless Session running result

Stateless Session, because the Session data will be lost, so the ok is not output.

IV. matters needing attention

1. In the drl file, using methods such as insert\ update\ modify\ delete all cause rules to be rematched.

2. Whether stateful session or stateless session can be obtained in Java code is determined by the value of type in ksession.

3. Stateless session clears the data in working memory after execute execution, while stateful session is not cleared in fireAllRules unless the dispose method is called.

4. Personal understanding of statelessness and statelessness is better understood from the api level.

This is the end of the article on "what are the stateful Session and stateless Session of drools?" Thank you for reading! I believe you all have a certain understanding of "what is drools's stateful Session and stateless Session". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report