一个流口水的新手,
以下是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;
}


我必须编写一些代码来验证流口水中的PersonAddress。
规则1.如果某人具有PersonAddress实例列表,如果其中之一是AddressType == CURRENT并且timeAtAddress
规则2。如果上述条件成立,那么我想获取其中AddressType == PREVIOUS的PersonAddress实例,

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

最佳答案

这是两个规则,使用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


假设每人只有一个电流;否则,规则可能会触发多次。使用exist避免。

我没有尝试在您的代码中找到错误,因为“行不通”太模糊了。无论如何,如果您正在使用规则,那么编写程序代码进行检查的意义何在?

09-25 20:18