Drools规则表达式:访问嵌套类数据成员

时间:2013-10-13 16:34:18

标签: java drools

以下是我班级的结构:

public class Address {
                private String street;
        private int phone;
        public void setStreet(String s){street = s;}
        public void setPhone(int p){phone = p;}
        public String getStreet(String s){return street;}
        public int getPhone(){  return phone;   }
}
public class Borrower {
    Address adr = new Address();
    public void setAddress(String s, int p) {
        adr.setStreet(s);
        adr.setPhone(p);
    }   
    public Address getAddress() {   return adr; }   
}

Drools表达我正在使用: -

rule "Nested Address"
    salience -10    
    when
        $bo : Borrower(Address.street.equals("USA") && Address.phone != 717)
        $loanApp : LoanApplication()        
    then 
        $loanApp.addFeedbackMessage(FeedbackMessages.LOCATION_NOT_US);  
        System.out.println("Nested Address  " + $bo.adr.street + ", Nested Phone " + $bo.adr.phone);
end

上面给我错误:

Unable to Analyse Expression Address.street.equals("USA"):

有人可以告诉我我做错了吗?

2 个答案:

答案 0 :(得分:3)

您应该使用属性名称“address”(注意小写字母a)而不是类Address。这样:

rule "Nested Address"
    salience -10    
    when
        $bo : Borrower(address.street.equals("USA") && address.phone != 717)
        $loanApp : LoanApplication()        
    then 
        $loanApp.addFeedbackMessage(FeedbackMessages.LOCATION_NOT_US);  
        System.out.println("Nested Address  " + $bo.adr.street + ", Nested Phone " + $bo.adr.phone);
end

答案 1 :(得分:1)

您应该使用“adr”(属性名称)而不是“地址”(类名):

rule "Nested Address"
    salience -10    
    when
        $bo : Borrower(adr.street.equals("USA") && adr.phone != 717)
        $loanApp : LoanApplication()        
    then 
        $loanApp.addFeedbackMessage(FeedbackMessages.LOCATION_NOT_US);  
        System.out.println("Nested Address  " + $bo.adr.street + ", Nested Phone " + $bo.adr.phone);
end