基于Drools 规则引擎的信用卡申请系统<4> 之 Drools 实现业务规则

设计规则流如下:

先执行:check,然后执行payment

规则流设计图

 

对应的规则流rmf文件如下: 【使用IDE生成,Drools4.0.7 的可读性,不如Drools 5】

 

<org.drools.ruleflow.core.impl.RuleFlowProcessImpl id="1">
  <nodes id="2">
    <entry>
      <long>1</long>
      <org.drools.ruleflow.core.impl.StartNodeImpl id="3">
        <id>1</id>
        <name>Start</name>
        <incomingConnections id="4"/>
        <outgoingConnections id="5">
          <org.drools.ruleflow.core.impl.ConnectionImpl id="6">
            <type>1</type>
            <from class="org.drools.ruleflow.core.impl.StartNodeImpl" reference="3"/>
            <to class="org.drools.ruleflow.core.impl.RuleSetNodeImpl" id="7">
              <ruleFlowGroup>check</ruleFlowGroup>
              <id>3</id>
              <name>check</name>
              <incomingConnections id="8">
                <org.drools.ruleflow.core.impl.ConnectionImpl reference="6"/>
              </incomingConnections>
              <outgoingConnections id="9">
                <org.drools.ruleflow.core.impl.ConnectionImpl id="10">
                  <type>1</type>
                  <from class="org.drools.ruleflow.core.impl.RuleSetNodeImpl" reference="7"/>
                  <to class="org.drools.ruleflow.core.impl.RuleSetNodeImpl" id="11">
                    <ruleFlowGroup>payment</ruleFlowGroup>
                    <id>4</id>
                    <name>payment</name>
                    <incomingConnections id="12">
                      <org.drools.ruleflow.core.impl.ConnectionImpl reference="10"/>
                    </incomingConnections>
                    <outgoingConnections id="13">
                      <org.drools.ruleflow.core.impl.ConnectionImpl id="14">
                        <type>1</type>
                        <from class="org.drools.ruleflow.core.impl.RuleSetNodeImpl" reference="11"/>
                        <to class="org.drools.ruleflow.core.impl.EndNodeImpl" id="15">
                          <id>2</id>
                          <name>End</name>
                          <incomingConnections id="16">
                            <org.drools.ruleflow.core.impl.ConnectionImpl reference="14"/>
                          </incomingConnections>
                          <outgoingConnections id="17"/>
                        </to>
                      </org.drools.ruleflow.core.impl.ConnectionImpl>
                    </outgoingConnections>
                  </to>
                </org.drools.ruleflow.core.impl.ConnectionImpl>
              </outgoingConnections>
            </to>
          </org.drools.ruleflow.core.impl.ConnectionImpl>
        </outgoingConnections>
      </org.drools.ruleflow.core.impl.StartNodeImpl>
    </entry>
    <entry>
      <long>2</long>
      <org.drools.ruleflow.core.impl.EndNodeImpl reference="15"/>
    </entry>
    <entry>
      <long>3</long>
      <org.drools.ruleflow.core.impl.RuleSetNodeImpl reference="7"/>
    </entry>
    <entry>
      <long>4</long>
      <org.drools.ruleflow.core.impl.RuleSetNodeImpl reference="11"/>
    </entry>
  </nodes>
  <variables id="18"/>
  <lastNodeId>4</lastNodeId>
  <id>myflow</id>
  <name>MyFlow</name>
  <version>1.0</version>
  <type>RuleFlow</type>
  <packageName>com.chen.drools</packageName>
</org.drools.ruleflow.core.impl.RuleFlowProcessImpl>

 

 

规则文件如下:

 

清单一:rules.drl


package com.chen.drools

#list any import classes here.
import com.chen.beans.User;


#declare any global variables here

 

#如果申请人既没房也没车,同时学历为大专及以下,并且月薪少于5000,那么不通过。
rule "No1"
 ruleflow-group "check"
 when
  #conditions
  $user: User(hasHouse =="无", hasCar =="无", degree =="大专以下", salary <5000)
 then
  #actions
  $user.setApprove(false);
end

#如果申请人既没房也没车,同时学历为大专或本科,并且月薪少于3000,那么不通过。

rule "No2"
 #include attributes such as "salience" here...
 ruleflow-group "check"
 when
  #conditions
  $user: User( hasHouse =="无", hasCar =="无", salary <3000,( degree =="大专" || degree =="本科"))
 then
  #actions
  $user.setApprove(false);
  
end

#如果申请人既没房也没车,同时学历为本科以上,并且月薪少于2000,同时之前没有信用卡的,那么不通过。

rule "No3"
    ruleflow-group "check"
 when
  $user:User( hasHouse =="无", hasCar =="无", degree =="本科以上", salary <2000)
 then
  $user.setApprove(false);
end

 

 

清单二: payment.drl
package com.chen.drools

#list any import classes here.
import com.chen.beans.CardInfo;
import com.chen.beans.User;
//如果申请人有房有车,或者月收入在20000以上,那么发放的信用卡信用额度为15000

rule "R1"
 ruleflow-group "payment"
 when
  $user: User( salary >20000 || (hasCar =="有" && hasHouse =="有") )
  $card: CardInfo(  )
 then
  
  $user.setCardInfo($card);
  $card.setValue(15000);
  $card.setBankName("中银广州");
  $card.setUser($user);
  #creatPayment(15000,$user);
  #输出测试
end

//如果申请人没房没车,但月收入在10000~20000之间,那么发放的信用卡信用额度为6000

rule "R2"
 #include attributes such as "salience" here...
 ruleflow-group"payment"
 when
  #conditions
  $user: User( hasHouse =="无" , hasCar =="无" , salary >10000 , salary <20000 )
  $card: CardInfo(  )
 then
  #actions
  $user.setCardInfo($card);
  $card.setValue(6000);
  $card.setBankName("中银广州2");
  $card.setUser($user);
  #creatPayment(6000,$user);
end

//如果申请人没房没车,月收入在10000以下,那么发放的信用额度为3000。

rule "R3"
    ruleflow-group"payment"
 when
  $user : User( hasHouse =="无", hasCar =="无" , salary <10000 )
  $card: CardInfo(  )
 then
     $user.setCardInfo($card);
  $card.setValue(3000);
  $card.setBankName("中银广州3");
  $card.setUser($user);
end

//如果申请人有房没车或者是没房但有车,月收入在10000以下,那么发放的信用额度为5000。

rule "R4"
    ruleflow-group"payment"
 when
    $user : User( ( hasHouse =="有" && hasCar =="无") || (hasHouse =="无" && hasCar =="有") && salary <10000 )
    $card: CardInfo(  )
 then
  $user.setCardInfo($card);
  $card.setValue(5000);
  $card.setBankName("中银广州4");
  $card.setUser($user);
end

// 如果申请人有房没车或者是没房但有车,月收入在10000~2000之间,那么发放的信用额度为8000。

rule "R5"
    ruleflow-group"payment"
 when
  $user: User( (hasHouse =="有" && hasCar =="无") || (hasHouse =="无" && hasCar =="有") && salary <= 20000 , salary >=10000 )
  $card: CardInfo(  )
 then
  $user.setCardInfo($card);
  $card.setValue(8000);
  $card.setBankName("中银广州5");
  $card.setUser($user);
end

 

 

你可能感兴趣的:(ide)