以drools列出验证

时间:2014-09-08 03:18:33

标签: java drools

这是一个流口水的新手, 以下是java类结构。

public class Person {
 List<PersonAddress> personAddress;
}

public enum AddressType {
 CURRENT, PREVIOUS;
}
public class PersonAddress{    
  Address address;
  AddressType type
  Integer timeAtAddress;        
}

public class Address {
String city;
String country;
String street;
}

我必须编写一些代码来验证drools中的PersonAddress。 规则1.如果person具有PersonAddress实例列表,如果其中一个是AddressType == CURRENT和timeAtAddress&lt; 3,然后我想查找该列表是否包含AddressType == PREVIOUS的地址。

规则2.如果以上条件为真,那么我想获取AddressAddress实例,其中AddressType == PREVIOUS,

Drools版本5.5.0.Final,

Java 1.7

可以使用功能

这是我尝试过的,但它不起作用

  function boolean isPreviousAddressExist(java.util.List list) {
        if(list.isEmpty()) {
            return false;
        }
        boolean validRecordFound = false;
        for(int addressIndex = 0; addressIndex < list.size(); addressIndex++) {
            PersonAddress pa = (PersonAddress)list.get(addressIndex);
            if(AddressType.CURRENT.equals(pa.getAddressType()) && pa.getTimeAtAddress() != null && pa.getTimeAtAddress() < 3) {
                validRecordFound =  true;
                break;     
            }
        }
        boolean previousRecordFound = false;
        if(validRecordFound) {
           for(int addressIndex = 0; addressIndex < list.size(); addressIndex++) {
                PersonAddress pa = (PersonAddress)list.get(addressIndex);
                if(AddressType.PREVIOUS.equals(pa.getAddressType())) {
                    previousRecordFound =  true;
                    break;
                }
            }
        } else {
            previousRecordFound = true;
        }
        return previousRecordFound;
    }

rule "Previous-Physical-Home Address is required for Time at Current-Physical-Home"
when

    $quotation:Quotation()        
    eval(!isPreviousAddressExist($quotation.getApplicantList()))   
then   
    runningResults.addRunningResult(new BusinessRuleRunningResult(null,  " A Previous physical Home Address is required.", false));
end

1 个答案:

答案 0 :(得分:1)

以下是两条规则,使用extends来避免重复:

rule "brief CURRENT"
when
  Person( $name: name, $pa: personAddress )
  PersonAddress( type == AddressType.CURRENT, timeAtAddress < 3 ) from $pa
then
end
rule "no PREVIOUS"
extends "brief CURRENT"
when
  not PersonAddress( type == AddressType.PREVIOUS ) from $pa
then
  System.out.println( "invalid. " + $name );
end
rule "has PREVIOUS"
extends "brief CURRENT"
when
  $ppa: PersonAddress( type == AddressType.PREVIOUS ) from $pa
then
  System.out.println( "valid. " + $name +
                      " at " + $ppa.getAddress().getCity() );
end

这假设每个人只有一个CURRENT;如果没有,规则可能不止一次。使用exist来避免。

我没有尝试在您的代码中找到错误,因为&#34;没有工作&#34;太模糊了无论如何,如果你正在使用规则,编写程序代码进行检查的重点是什么?