`
MMSCAU
  • 浏览: 112350 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

基于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

 

 

  • 大小: 8.6 KB
分享到:
评论
2 楼 chenlinux 2012-10-18  
博主,我们现在也要用drools,能否提供一个完整的例子(包括java文件、规则文件、dsl文件等)供我们学习,万分感谢。email:chenlinux0768@sina.com
1 楼 benbenbaozai 2011-08-02  
你这个规则流图是怎么生成的?

相关推荐

Global site tag (gtag.js) - Google Analytics