Drools动态加载规则文件的解决方案

/**
 * Tools for Drools
 * @author kevin
 * @version Date: 2009-7-7 Time: 10:34:58
 */
public class DroolsUtil {
    private KnowledgeAgent kagent;
    private static DroolsUtil instance;

    /**
     *  return the default agent with name "agent" and interval "5"
     */
    private DroolsUtil(){
        this.kagent = getKnowledgeAgent("agent","5");
    }

    /**
     * return the agent with the specified name and interval
     * @param agentName The name of the agent
     * @param interval The interval to scan resources
     */
    private DroolsUtil(String agentName,String interval){
        this.kagent = getKnowledgeAgent(agentName,interval);
    }

    /**
     * return the instance with default agent
     * @return DroolsUtil's instance
     */
    public static DroolsUtil getInstance(){
        if(instance == null){
            instance = new DroolsUtil();
        }
        return instance;
    }

    /**
     * return the instance with agent which is given specified name and  interval
     * @param agentName The name of the agent
     * @param interval The interval to scan resources
     * @return DroolsUtil's instance
     */
    public static DroolsUtil getInstance(String agentName,String interval){
        if(instance == null){
           instance = new DroolsUtil(agentName,interval);
        }
        return instance;
    }

    /**
     * Get the KnowledgeAgent and make it scan the resources per 5 seconds
     * @param agentName The name of the agent
     * @param interval The interval to scan resources
     * @return KnowledgeAgent
     */
    public KnowledgeAgent getKnowledgeAgent(String agentName,String interval){
        ResourceChangeScannerConfiguration sconf = ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();
        sconf.setProperty("drools.resource.scanner.interval",interval);
        ResourceFactory.getResourceChangeScannerService().configure(sconf);
        ResourceFactory.getResourceChangeScannerService().start();
        ResourceFactory.getResourceChangeNotifierService().start();
        KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
        aconf.setProperty("drools.agent.scanDirectories","true");
        aconf.setProperty("drools.agent.newInstance","true");
        KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent(agentName,aconf);
        kagent.applyChangeSet(ResourceFactory.newClassPathResource("resource.xml"));
        return kagent;
    }

    /**
     * Get the KnowledgeBase
     * @return KnowledgeBase
     */
    public KnowledgeBase getKnowledgeBase(){
        return kagent.getKnowledgeBase();
    }

    /**
     * Get the StatefulKnowledgeSession
     * @return StatefulKnowledgeSession
     */
    public StatefulKnowledgeSession getStatefulKnowledgeSession(){
        return getKnowledgeBase().newStatefulKnowledgeSession();
    }

    /**
     * Get the StatelessKnowledgeSession
     * @return StatelessKnowledgeSession
     */
    public StatelessKnowledgeSession getStatelessKnowledgeSession(){
        return getKnowledgeBase().newStatelessKnowledgeSession();
    }
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值