gpt4 book ai didi

drools - Drools 7.57.0.Final 中的模式匹配问题

转载 作者:行者123 更新时间:2023-12-04 14:53:22 25 4
gpt4 key购买 nike

我正在尝试运行 this GitHub project使用 Drools 7.57.0.Final 而不是原始项目中使用的 7.39.0.Final。我发现了一些问题。最让我触动的问题是第 6 节第 5 步中的问题。问题出在 Drools 文件 VisaApplicationValidationWithAgendaAndSalience.drl 中。这是我添加的带有“调试”语句的内容:

package io.github.aasaru.drools.section06.step5

import io.github.aasaru.drools.domain.Passport;
import io.github.aasaru.drools.domain.Validation;
import io.github.aasaru.drools.domain.VisaApplication;

import java.time.LocalDate;

rule "Invalidate visa application with invalid passport"
dialect "mvel"
agenda-group "validate-application"
salience 20
when
$passport: Passport( validation == Validation.FAILED )
$visaApplication: VisaApplication( passportNumber == $passport.passportNumber,
validation != Validation.FAILED )
then
System.out.println( "Set " + $visaApplication + " invalid as " + $passport + " hasn't passed validation");
modify($visaApplication) {
setValidation( Validation.FAILED )
}
end

rule "Set application without validation info as passed"
dialect "mvel"
agenda-group "validate-application"
salience 10
when
$visaApplication: VisaApplication( validation == Validation.UNKNOWN )
then
System.out.println("debug >>>>>>>>>>>>> " + $visaApplication.validation);
System.out.println( $visaApplication + " is without validation info, consider OK for now" );
modify($visaApplication) {
setValidation( Validation.PASSED )
}
end

rule "Invalidate visa application where passport expires earlier than 6 months after visit end date"
dialect "mvel"
agenda-group "validate-application"
salience 20
when
$passport: Passport( validation != Validation.FAILED )
$visaApplication: VisaApplication( passportNumber == $passport.passportNumber,
$passport.expiresOn.isBefore(visitEndDate.plusMonths(6)),
validation != Validation.FAILED )
then
System.out.println( "Set " + $visaApplication + " invalid as " + $passport + " not valid 6 months after visit");
modify($visaApplication) {
setValidation( Validation.FAILED )
}
end

这里是关于之前规则的控制台输出部分:

Set VisaApplication(#1, pass:CA-SARAH-1) invalid as Passport[no:CA-SARAH-1, name:Sarah Murphy] hasn't passed validation
Set VisaApplication(#4, pass:AU-JAMES-4) invalid as Passport[no:AU-JAMES-4, name:James Brown] not valid 6 months after visit
debug >>>>>>>>>>>>> FAILED
VisaApplication(#1, pass:CA-SARAH-1) is without validation info, consider OK for now
Set VisaApplication(#1, pass:CA-SARAH-1) invalid as Passport[no:CA-SARAH-1, name:Sarah Murphy] hasn't passed validation
debug >>>>>>>>>>>>> UNKNOWN
VisaApplication(#2, pass:CA-SIMON-2) is without validation info, consider OK for now
debug >>>>>>>>>>>>> UNKNOWN
VisaApplication(#3, pass:AU-EMILY-3) is without validation info, consider OK for now

很明显,中间规则,即带有salience 10 的规则,不仅针对模式validation == Validation.UNKNOWN 被触发,而且还针对模式验证== Validation.FAILED!这是错误的,它发生在 Drools 7.57.0.Final 中,而不会发生在原始项目使用的 Drools 7.39.0.Final 中。是 Drools 错误还是什么?我对 Drools 还很陌生,所以这让我很困惑。

其次,为什么我的控制台输出 Set VisaApplication(#1, pass:CA-SARAH-1) ... 两次?这似乎是重新评估第一条规则的结果,但这不应该发生,因为这是有状态 session 而不是无状态 session 。

If you want to reproduce the console log, download the project, change Drools version to the latest one 7.57.0.Final, run the app io.github.aasaru.drools.section06.VisaIssue, and when asked,enter 5 for the step and no when asked whether all passports should be considered as expired.

最佳答案

恭喜,您发现了 drools 错误 DROOLS-6542 - 在 7.60.0.Final 中修复
有一个解决方法 - 为 rule "Invalidate visa application with invalid passport" 删除 mvel dialect。

顺便说一句,我想建议你进行流口水测试 library这可以为您节省大量时间来考虑复杂的规则并简化编写测试场景。下面是测试的样子。

@DroolsSession(
resources = "classpath*:**/section06/step5/*",
showStateTransitionPopup = true)
public class Section6Step5Test {

@RegisterExtension
public DroolsAssert drools = new DroolsAssert();

@Test
public void test() {

ApplicationRepository.getPassports().forEach(drools::insert);
ApplicationRepository.getVisaApplications().forEach(drools::insert);

drools.getSession().getAgenda().getAgendaGroup("issue-visa").setFocus();
drools.getSession().getAgenda().getAgendaGroup("validate-application").setFocus();
drools.getSession().getAgenda().getAgendaGroup("validate-passport").setFocus();

drools.fireAllRules();

assertEquals(1, drools.getObjects(Visa.class).size());
}
}

这是 7.39.0.Final(或 7.57.0.Final 没有 mvel 方言)测试的可视化弹出窗口。

enter image description here

与 7.57.0.Final 相同(使用 mvel 方言)

enter image description here

关于drools - Drools 7.57.0.Final 中的模式匹配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68625889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com