1. Understanding

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

1. What is the difference between the two?
2. When to use a stateful session and when to use a stateless session?
此处简单说一下我的理解 .

1. Stateful Session

A stateful session is one that uses inference to make iterative changes to fact objects over time. In a stateful session, data from previous invocations of the session (previous session state) is preserved between session invocations, while in a stateless session, this data is discarded.

2. Stateless Session

A stateless session is one that does not use inference to make iterative changes to fact objects over time. Data from previous invocations of a stateless session is not persisted between sessions.

explain:

For the understanding of 无状态 session 是不会使用推理对fact 对象随时间进行迭代更改的会话 , my understanding is to use it for Java Api ,

1. In a stateless Session, only the execute method is called, and the execute method is called multiple times. The last execute method will not affect the next time execute the execution of the method.
2、 drl文件中insert update modify delete时,会Causes an object update in working memory, resulting in a rematch of the rules.

3. So when to use different sessions?

1. If we just verify the rules, then use a stateless Session.
for example:

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

即一步就可以完成。

2. If our rules require multiple steps to complete, we can use a stateful Session.
for example:

  1. Insert Fact object A into the Session, and then trigger the rule.
  2. Execute a piece of Java code
  3. Insert Fact object B into the Session, and then trigger the rule. At this time, the rule needs to depend on the data of the previous rule.

即需要关联的多步来完成。

2. Demand

We ourselves have a Count object, which has 2 attributes cnt and name .
The following 2 rules exist in the rules file

Rule one:
If there is a Count object in working memory, add 1 to the Count object's cnt attribute
Rule two:
2Count ,一个对象的name=count-01 name=count-02 ok .

For stateful sessions and stateless sessions, see what the results are.

3. Implementation steps

1. Project structure description

项目结构

2. Import jar package

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-bom</artifactId>
            <type>pom</type>
            <version>7.69.0.Final</version>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-mvel</artifactId>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.11</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.22</version>
    </dependency>
</dependencies>

3. Write the Count class

 @Data
@AllArgsConstructor
public class Counter {
    /**
     * 名称
     */
    private String name;
    /**
     * 计数
     */
    private Integer cnt;
}

Just an ordinary java object.

4. Write the kmodule.xml file

 <kmodule xmlns="http://www.drools.org/xsd/kmodule">

    <kbase name="kabse-01" packages="rules.stateful" default="false">
        <!--
            type="stateful" 表示有状态的session
        -->
        <ksession name="stateful-session" default="false" type="stateful"/>
    </kbase>
    <kbase name="kabse-02" packages="rules.stateless" default="false">
        <!--
            type="stateless" 表示无状态的session
        -->
        <ksession name="stateless-session" default="false" type="stateless"/>
    </kbase>
</kmodule>

It should be noted here that the value of ksession in type , the value of stateless session and stateful session are inconsistent, do not write type, the default is stateful session.

5. Write rule files

 package rules.stateful

import com.huan.drools.Counter

// 将counter中的cnt的值递增一下
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

// 如果工作内存中同时存在 count-01 和 counter-02 则输出ok
rule "stateful_rule_count_exists"
    when
        Counter(name == "count-01") and Counter(name == "count-02")
    then
        System.out.println("ok");
end

6. Stateful session running results

Stateful session运行结果
1. Because it is a stateful Session, the object inserted into the working memory last time still exists after many times fireAllRules . 即Session的数据保留了 .
2. After the stateful Session is executed, the dispose method must be called to avoid memory leaks.

7. Stateless Session running results

Stateless Session运行结果
For a stateless session, because the data of the session will be lost, ok does not output.

Fourth, matters needing attention

1. In the drl file, when using insert\update\modify\delete and other methods, the rules are rematched.
2、 Java代码中是否可以获取stateful session stateless session是有ksession中的type of.
3. stateless session After the execution of execute , the data in the working memory will be cleared, while stateful session fireAllRules will not be cleared in ---8350a24d8986c3f5adb0ae8d. Unless the dispose method is called.
4. Personal understanding of stateful and stateless is better understood from the API level.

5. Complete code

https://gitee.com/huan1993/spring-cloud-parent/tree/master/drools/drools-session

6. Reference documents

1. https://docs.drools.org/7.69.0.Final/drools-docs/html_single/index.html#kie-sessions-con_decision-engine
2. https://www.javainuse.com/drools_states
3. https://groups.google.com/g/drools-usage/c/qYbqiS1ht4g


huan1993
218 声望34 粉丝

java工程师