日期:2014-05-16  浏览次数:20374 次

Drools的JSR94实现形式

最近在研究规则引擎的东东。网上找了下,单说drools的文章不少,单独讲JSR94的文章并不多见。现把drools的JSR写法整理发上来,供自己记录,供大家参考。

?

调用入口:JSR94Sample.java

?

package info.syang.stu1;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.HashMap;

import javax.rules.ConfigurationException;
import javax.rules.InvalidRuleSessionException;
import javax.rules.RuleExecutionSetNotFoundException;
import javax.rules.RuleRuntime;
import javax.rules.RuleServiceProvider;
import javax.rules.RuleServiceProviderManager;
import javax.rules.RuleSessionCreateException;
import javax.rules.RuleSessionTypeUnsupportedException;
import javax.rules.StatelessRuleSession;
import javax.rules.admin.LocalRuleExecutionSetProvider;
import javax.rules.admin.RuleAdministrator;
import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetCreateException;
import javax.rules.admin.RuleExecutionSetRegisterException;

import org.drools.jsr94.rules.RuleServiceProviderImpl;


/**
 * Drools的JSR94实现形式
 * @author yangsong
 *
 */
public class JSR94Sample {
    private RuleServiceProvider ruleProvider;
    public void run(){
        String uri = RuleServiceProviderImpl.RULE_SERVICE_PROVIDER;
        try{
            //1.注册ruleProvider,并且从RuleServiceProviderManager获取ruleProvider
            RuleServiceProviderManager.registerRuleServiceProvider(uri, RuleServiceProviderImpl.class);
            ruleProvider = RuleServiceProviderManager.getRuleServiceProvider(uri);
            
            HashMap<String,String> properties = new HashMap<String,String>();
            
            //2.获取RuleAdministrator实例,获取RuleExectuionSetProvider
            RuleAdministrator admin = ruleProvider.getRuleAdministrator();
            LocalRuleExecutionSetProvider ruleExecutionSetProvider = admin.getLocalRuleExecutionSetProvider(properties);
            
            //3.创建RuleExecutionSet
            Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("info/syang/stu1/addpoint.drl"));
            RuleExecutionSet reSet = ruleExecutionSetProvider.createRuleExecutionSet(reader, properties);
            
            //4.注册RuleExecutionSet
            admin.registerRuleExecutionSet("mysample",reSet,properties);
            
            
            //5.获取RuleRuntime, 创建会话
            RuleRuntime runtime = ruleProvider.getRuleRuntime();
            StatelessRuleSession ruleSession = (StatelessRuleSession)runtime.createRuleSession("mysample",null,RuleRuntime.STATELESS_SESSION_TYPE);

            //6.运行实例
            PointDomain pointDomain = new PointDomain();
            fillPointDomain(pointDomain);
            
            ruleSession.executeRules(Arrays.asList(pointDomain));
//            @SuppressWarnings("unchecked")
//			List<PointDomain> results = (List<PointDomain>)ruleSession.executeRules(Arrays.asList(pointDomain));
            
			System.out.println("执行完毕BillThisMonth:"+ pointDomain.getBillThisMonth());
			System.out.println("执行完毕BuyMoney:" + pointDomain.getBuyMoney());
			System.out.println("执行完毕BuyNums:" + pointDomain.getBuyNums());

			System.out.println("执行完毕规则引擎决定发送积分:" + pointDomain.getPoint());
            
        }catch(ConfigurationException e){
            e.printStackTrace();
        } catch (RemoteException e) {
			e.printStackTrace();
		} catch (RuleExecutionSetCreateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RuleExecutionSetRegisterException e) {
			e.printStackTrace();
		} catch (RuleSessionTypeUnsupportedException e) {
			e.printStackTrace();
		} catch (RuleSessionCreateException e) {
			e.printStackTrace();
		} catch (RuleExecutionSetNotFoundException e) {