Drools 7.57 中的模式匹配问题。0.Final

Problem with pattern matching in Drools 7.57.0.Final

我正在尝试 运行 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 == Validation.FAILED 都被触发了!这是错误的,它发生在 Drools 7.57.0.Final 中,而不会发生在 Drools 7.39.0.Final used由原来的项目。是 Drools 错误还是什么?我是 Drools 的新手,所以这让我很困惑。

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

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 方言

顺便说一句,我想向您推荐 drools 测试 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 方言)测试的可视化弹出窗口。

与 7.57 相同。0.Final(使用 mvel 方言)