mercredi 22 mars 2017

How to invoke some method on demand

This is a loan business, when a user come to borrow money , should execute a batch rule by drools to generate a final decision: Refuse / Manually Review / Pass.

For example there are these rules :

- rule1 count(userId, refuse) > 1 ? Manually Review : Pass
- rule2 count(userId, manuallyReview) > 2 ? Manually Review : Pass
- rule3 count(userId, distinct institution between 1 year) > 5 ? Manually Review : Pass
- rule4 count(userId,distinct institution count between 1 month) > 3 ? Manually Review : Pass   
- rule5 count(userId,distinct institution count between 7 days) > 1 ? Manually Review : Pass    
- rule6 count(userId) > 0 ?  Manually Review : Pass  
- rule7 sum(notYetSettledAmount) > 0 ? Refuse : Pass 

and so on.

Above each rule corresponding a service interface. Because different scenario has different rules to execute, e.g. scene 1 should execute rule: 1 3 5 , scene 2 should execute rule: 2 4 6.

Before fire rule engine should get all the data it need. For example in scene 1 one user come to borrow money, and this scene should execute rule 1 3 5, so at first I should get all the data . Now I have below method to implement this -- that is call method on demand

List<RuleData> getRuleDataList(String userId,List<String> ruleNoList){
    List<RuleData> result = ...        
    if(ruleNoList.contains("RN001")){
        result.add(getRule1Data(userId)); //get data of rule 1 e.g. count(userId, refuse)
    }
    else if(ruleNoList.contains("RN002")){
        result.add(getRule2Data(userId)); // get data of rule 2 e.g. count(userId, manuallyReview)
    }
    ...

    return result;
}

Obviously this manner is not very clever, so I want to know how to implement it more elegantly.

Aucun commentaire:

Enregistrer un commentaire