Drools无国籍与有状态会话

时间:2017-03-10 22:53:52

标签: java drools

需要一些帮助!! 我对Drools的有状态和无状态会话没有明确的理解。 我试图理解这一点,所以尝试了一个例子。

我已经在drools6.5版本上测试了以下drl和有状态和无状态会话,并且在两种情况下都得到了相同的输出。从我对无状态会话的理解,它应该只执行第一个规则,第二个规则不应该被激活在第一个规则中修改Application对象时("是有效年龄")。附加源代码。感谢任何帮助。

package com.company.license

import com.sample.dto.Applicant;
import com.sample.dto.Application;
import java.util.Date;


rule "Is of valid age"
no-loop
    when 
        Applicant( age < 18 )
        $a : Application()
    then
        $a.setValid( false );
        modify($a){setDateApplied(new Date())};
        System.out.println("from rule Is of Valid age --  " +$a.isValid() );
    end

rule "Application was made this year"
    when 
        $a : Application( dateApplied > "01-jan-2009" )
    then 
    $a.setValid( true);
    System.out.println("from rule made this year --  " +$a.isValid() );
end


package com.sample.dto;

import java.util.Date;

public class Application {
    public Date dateApplied;
    public boolean valid;

    // getter and setter methods here
    public Date getDateApplied() {
        return dateApplied;
    }
    public void setDateApplied(Date dateApplied) {
        this.dateApplied = dateApplied;
    }
    public boolean isValid() {
        return valid;
    }
    public void setValid(boolean valid) {
        this.valid = valid;
    }
}


package com.sample.dto;
public class Applicant {
    public String name;
    public int age;

    public Applicant(String name,int age){
        this.name =  name;
        this.age=age;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public int getAge() {
        return age;
    }


    public void setAge(int age) {
        this.age = age;
    }
}

package com.sample;

import java.util.Arrays;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;

import com.sample.dto.Applicant;
import com.sample.dto.Application;

/**
 * This is a sample class to launch a rule.
 */
public class LicenseTestStateful {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            KieSession ksession = kContainer.newKieSession("ksession-rules");
            Applicant applicant = new Applicant( "Mr John Smith", 16);
            Application application = new Application();
            ksession.execute(ks.getCommands().newInsertElements(Arrays.asList(new Object[]{application,applicant})));
            ksession.fireAllRules();
            ksession.dispose();

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}


package com.sample;

import java.util.Arrays;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;

import com.sample.dto.Applicant;
import com.sample.dto.Application;

/**
 * This is a sample class to launch a rule.
 */
public class LicenseTestStateless {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            StatelessKieSession ksession = kContainer.newStatelessKieSession();
            Applicant applicant = new Applicant( "Mr John Smith", 16);
            Application application = new Application();
            ksession.execute(ks.getCommands().newInsertElements(Arrays.asList(new Object[]{application,applicant})));       
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
        <ksession name="statelesssession" type="stateless" default="true"/>
    </kbase>
    <kbase name="dtables" packages="dtables">
        <ksession name="ksession-dtables"/>
    </kbase>
    <kbase name="process" packages="process">
        <ksession name="ksession-process"/>
    </kbase>
</kmodule>


Output:
from rule Is of Valid age --  false
from rule made this year --  true

2 个答案:

答案 0 :(得分:0)

无状态和有状态的KieSession之间的区别在于使用会话的API。使用execute方法之一激活无状态会话,其中将一组命令传递给要执行的会话,并在最后调用dispose以返回所有资源;因此,会话无法重复使用,其状态也会丢失。

使用状态会话,您可以选择以您喜欢的任何顺序执行各种API调用,调用一些&#34; fire&#34;方法经常根据需要等。

StatelessKieSession只是KieSession的包装。

答案 1 :(得分:0)

我想我做对了。如果事实中的更改发生在同一请求中,那么与该更改相关联的所有其他规则将触发,而不管任何无状态/有状态会话。