我是新來的流口水。我試圖理解Drools提供的無狀態和有狀態會話之間的區別。無狀態VS流氓國家會話行爲
按我最初的理解,
在無狀態會話的情況下,如果事實是任何規則,那麼它不會被重新提交給推理引擎的操作執行過程中被修改,找出新的規則,匹配修改的事實。
在有狀態會話的情況下,如果在任何規則的動作執行期間修改了事實,那麼它將被重新提交給推理引擎以找出與修改後的事實相匹配的新規則,然後執行相應的動作。
所以,當我試圖通過編寫一個樣本規則,以驗證這種行爲,我發現,行爲是在兩種情況下完全相同。所以現在我對有狀態和無狀態會話之間的區別感到困惑。
我想請大家幫我理解狀態和無狀態會話的正確的行爲。
供您參考我貼有狀態我的示例代碼和無狀態會話與它們的輸出和採樣規則一起。
licenseApplication.drl (Rule file)
package com.idal.droolsapp
rule "Is of valid age"
no-loop
when
$a : Applicant(age < 18)
then
System.out.println("Not eligible for license");
System.out.println("Setting Valid to false");
modify($a) { setValid(false) };
end
rule "Is of valid false"
salience 100
when
$a : Applicant(valid == false)
then
System.out.println("Second rule fired");
end
輸入對象(事實)Applicant.java
package com.idal.droolsapp;
public class Applicant {
private String name;
private int age;
private boolean valid = true;
public Applicant(String name, int age) {
setName(name);
setAge(age);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public boolean isValid() {
return valid;
}
@Override
public String toString() {
return "Applicant [name=" + name + ", age=" + age + ", valid=" + valid
+ "]";
}
}
StatelessSessionExample.java(無狀態會話測試代碼)的無狀態會話測試代碼
package com.idal.droolsapp;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;
public class StatelessSessionExample {
/**
* @param args
*/
public static void main(String[] args) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource(
"licenseApplication.drl", StatelessSessionExample.class),
ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.err.println(kbuilder.getErrors().toString());
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
Applicant applicant = new Applicant("Mr John Smith", 16);
ksession.execute(applicant);
System.out.println("Updated Applicant = " + applicant);
}
}
輸出:
不有資格獲得許可 設置有效爲假 第二條規則解僱 更新申請人=申請人[名稱=約翰史密斯先生,年齡= 16,有效期=假]
StatefulSessionExample.java(無狀態會話測試代碼)的有狀態會話測試代碼
package com.idal.droolsapp;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
public class StatefulSessionExample {
/**
* @param args
*/
public static void main(String[] args) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource(
"licenseApplication.drl", StatefulSessionExample.class),
ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.err.println(kbuilder.getErrors().toString());
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
Applicant applicant = new Applicant("Mr John Smith", 16);
ksession.insert(applicant);
ksession.fireAllRules();
ksession.dispose();
System.out.println("Updated Applicant = " + applicant);
}
}
輸出:
不符合許可 設置有效期爲false 第二條規則解僱 更新申請人=申請人[名稱=約翰史密斯先生,年齡= 16,有效期=假]
再次要求大家幫助我理解有狀態和無狀態會話的正確行爲。
由於提前, 馬尼什·甘地