如何在Drools 5中测试属性是否为Null

时间:2017-11-03 01:40:10

标签: drools rules mvel

流口水5,方言mvel

我希望此规则仅在存在任何MappingFile对象时匹配,并且仅当具有Reason代码的BLuReport对象为NULL时才匹配

我怎么能写出那个比较表达式......我尝试了几件事但没有成功:

rule "Mapping.Control Group to BlueReport"
    when 
        $internalAccount : MappingFile( $mappingControlGroup : controlGroup )
        $blueReport : BlueReport( **$Reason is null**  : Reason )
    then
        System.out.println("Updating BlueReport.controlGroup from MappingFile.controlGroup" + " Rule=" + drools.getRule().getName());
        modify ( $blueReport ) {
            setControl_group( $mappingControlGroup ),
            setReason( "Found mapping in the Internal Account object with account_id=" + $internalAccount.getExternalKey() );
        }
        retract ( $blueReport )
    end

另外,我不得不添加retract(BR),因为我在更新后不断进行无限循环...我正在进行更新吗?!

1 个答案:

答案 0 :(得分:0)

如果BlueReport字段确实名为Reason(与Java命名约定相反),那么您只需编写:

rule "Mapping.Control Group to BlueReport"
when 
    $internalAccount : MappingFile( $mappingControlGroup : controlGroup )
    $blueReport : BlueReport( Reason == null ) # or reason?
then
    System.out.println("Updating BlueReport.controlGroup from MappingFile.controlGroup" + " Rule=" + drools.getRule().getName());
    modify ( $blueReport ) {
        setControl_group( $mappingControlGroup ),
        setReason( "Found mapping in the Internal Account object with account_id=" + $internalAccount.getExternalKey() );
    }
    retract ( $blueReport );
end

也许你也应该有一个约束control_group != $mappingControlGroup

使用这些约束中的任何一个也可以避免循环。