drtools规则使用快速入门之一

1、前言

Drools(JBoss Rules )具有一个易于访问企业策略、易于调整以及易于管理的开源业务规则引擎,符合业内标准,速度快、效率高。本文一个简单的例子讲解从java规则转化为规则引擎调用。

2、drtools规则

drtools规则使用快速入门之一_第1张图片

rule:定义一个规则。rule "ruleName"。一个规则可以包含三个部分:

属性部分:定义当前规则执行的一些属性等,比如是否可被重复执行、过期时间、生效时间等。

条件部分,即LHS,定义当前规则的条件,如  when Message(); 判断当前workingMemory中是否存在Message对象。

结果部分,即RHS,这里可以写普通java代码,即当前规则条件满足后执行的操作,可以直接调用Fact对象的方法来操作应用。

规则事例:


详细文法见:http://www.360doc.com/content/07/0705/18/7147_596025.shtml

3、使用例子

3.1需要转化的java代码

public int statusProcess(int faceStatus, double faceProbability, int tuStatus, double tuProbability) {
        if (faceStatus == 0 || tuStatus == 0) {
            return 0;
        }


        if (faceStatus == 1 || tuStatus == 1) {
            return 0;
        }


        if (faceStatus == 2 && tuStatus == 2) {
            double probability = tuProbability + faceProbability;


            if (probability>0.85 && tuProbability >= 0.85) {
                return 1;
            } else if (tuProbability > 0.4 && tuProbability < 0.85 && probability >= 1.3) {
                return 1;
            } else if (probability >= 0.8 && probability < 1.3) {
                return 2;
            } else if (probability < 0.8) {
                return 0;
            }
        }


        return 2;
    }


3.2 对应的规则文件

package com.feinno.drools
import com.feinno.drools.HelloWorldExample.ResultRate;


rule "1"
    dialect "mvel"
when
   m : ResultRate((faceStatus == -1 && tuStatus== -1 && status == -1 ), resultStatus : resultStatus )
then
	System.out.println( resultStatus ); 
     modify ( m ) {
	      status = 1,
	      resultStatus = 0
      };
end

rule "2"
    dialect "mvel"
when
   m : ResultRate((faceStatus == 0 && tuStatus== 0 && status == -1 ), resultStatus : resultStatus )
then
	System.out.println( resultStatus ); 
     modify ( m ) {
	      status = 1,
	      resultStatus = 0
      };
end

rule "3"
    dialect "mvel"
when
   m : ResultRate((faceStatus == 2 && tuStatus== 2&&tuProbability >= 0.85 && status == -1 ), resultStatus : resultStatus )
then
	System.out.println( resultStatus ); 
     modify ( m ) {
      status = 1,
      resultStatus =1
      };
end



rule "4"
    dialect "mvel"
when
   m : ResultRate((faceStatus == 2 && tuStatus== 2&& tuProbability > 0.4 && tuProbability < 0.85 && probability>= 1.3 && status == -1 ), resultStatus : resultStatus )
then
	System.out.println( resultStatus ); 
     modify ( m ) {
      status = 1,
      resultStatus =1
      };
end

rule "5"
    dialect "mvel"
when
   m : ResultRate((faceStatus == 2 && tuStatus== 2&& probability>= 0.8 && probability< 1.3 && status == -1 ), resultStatus : resultStatus )
then
	System.out.println( resultStatus ); 
     modify ( m ) {
      	status = 9,
      	resultStatus = 2
      };
end

rule "51"
    dialect "mvel"
when
   m : ResultRate((faceStatus == 2 && tuStatus== 2&&tuProbability >= 0.85&& status == 9 ), resultStatus : resultStatus )
then
	System.out.println( resultStatus ); 
     modify ( m ) {
      status = 1,
      resultStatus = 1
      };
end

rule "6"
    dialect "mvel"
when
   m : ResultRate((faceStatus == 2 && tuStatus== 2&& probability< 0.8 && status == -1 ), resultStatus : resultStatus )
then
	System.out.println( resultStatus ); 
     modify ( m ) {
      	status = 1,
      	resultStatus = 0
      };
end



注意:

1、规则不能嵌套

2、规则不支持a+b,增加一个中间变量probability

3.3 对应的java对象

	public static class ResultRate{
		int faceStatus;
		double faceProbability;
		int tuStatus;
		double tuProbability;
		int status;
		int resultStatus;
		double probability;
		
		public ResultRate(){
			status = -1;
		}
		
		public int getFaceStatus() {
			return faceStatus;
		}
		public void setFaceStatus(int faceStatus) {
			this.faceStatus = faceStatus;
		}
		public double getFaceProbability() {
			return faceProbability;
		}
		public void setFaceProbability(double faceProbability) {
			this.faceProbability = faceProbability;
		}
		public int getTuStatus() {
			return tuStatus;
		}
		public void setTuStatus(int tuStatus) {
			this.tuStatus = tuStatus;
		}
		public double getTuProbability() {
			return tuProbability;
		}
		public void setTuProbability(double tuProbability) {
			this.tuProbability = tuProbability;
		}
		public int getStatus() {
			return status;
		}
		public void setStatus(int status) {
			this.status = status;
		}
		public int getResultStatus() {
			return resultStatus;
		}
		public void setResultStatus(int resultStatus) {
			this.resultStatus = resultStatus;
		}
		
		public double getProbability() {
			return probability;
		}

		public void setProbability(double probability) {
			this.probability = probability;
		}

		public void computeProbability(){
			probability = faceProbability+tuProbability;
		}
		
	}


 3.4 测试程序

public static void testRate()throws Exception {
		String filepath = "C:\\目标路径\\ResultRate.drl";
		final Reader source = new FileReader(filepath);
		final PackageBuilder builder = new PackageBuilder();

		//新建规则
		builder.addPackageFromDrl(source);
		if (builder.hasErrors()) {
			System.out.println(builder.getErrors().toString());
			throw new RuntimeException("Unable to compile ResultRate.drl");
		}

		// get the compiled package (which is serializable)
		final Package pkg = builder.getPackage();
		// add the package to a rulebase (deploy the rule package).
		final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
		ruleBase.addPackage(pkg);
		final StatefulSession session = ruleBase.newStatefulSession();
		session.addEventListener(new DebugAgendaEventListener());
		session.addEventListener(new DebugWorkingMemoryEventListener());
		
		//测试
		ResultRate rusultRate = new ResultRate();
		rusultRate.setFaceStatus(2);
		rusultRate.setFaceProbability(0.3);
		rusultRate.setTuStatus(2);
		rusultRate.setTuProbability(0.90);
		rusultRate.computeProbability();
		
		session.insert(rusultRate);
		session.fireAllRules();
		System.out.println("======================================");
		
		//规则库后测试
		Package[] packs = ruleBase.getPackages();
		for(int i = 0;i

4、参考文档

http://blog.csdn.net/kevin_darkelf/article/details/50728137

http://www.360doc.com/content/07/0705/18/7147_596025.shtml


你可能感兴趣的:(java,大数据架构与服务程序设计)