Drools动态加载规则文件

 1 /** 
 2  * Tools for Drools 
 3  * @author kevin 
 4  * @version Date: 2009-7-7 Time: 10:34:58 
 5  */  
 6 public class DroolsUtil {  
 7     private KnowledgeAgent kagent;  
 8     private static DroolsUtil instance;  
 9   
10     /** 
11      *  return the default agent with name "agent" and interval "5" 
12      */  
13     private DroolsUtil(){  
14         this.kagent = getKnowledgeAgent("agent","5");  
15     }  
16   
17     /** 
18      * return the agent with the specified name and interval 
19      * @param agentName The name of the agent 
20      * @param interval The interval to scan resources 
21      */  
22     private DroolsUtil(String agentName,String interval){  
23         this.kagent = getKnowledgeAgent(agentName,interval);  
24     }  
25   
26     /** 
27      * return the instance with default agent 
28      * @return DroolsUtil's instance 
29      */  
30     public static DroolsUtil getInstance(){  
31         if(instance == null){  
32             instance = new DroolsUtil();  
33         }  
34         return instance;  
35     }  
36   
37     /** 
38      * return the instance with agent which is given specified name and  interval 
39      * @param agentName The name of the agent 
40      * @param interval The interval to scan resources 
41      * @return DroolsUtil's instance 
42      */  
43     public static DroolsUtil getInstance(String agentName,String interval){  
44         if(instance == null){  
45            instance = new DroolsUtil(agentName,interval);  
46         }  
47         return instance;  
48     }  
49   
50     /** 
51      * Get the KnowledgeAgent and make it scan the resources per 5 seconds 
52      * @param agentName The name of the agent 
53      * @param interval The interval to scan resources 
54      * @return KnowledgeAgent 
55      */  
56     public KnowledgeAgent getKnowledgeAgent(String agentName,String interval){  
57         ResourceChangeScannerConfiguration sconf = ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();  
58         sconf.setProperty("drools.resource.scanner.interval",interval);  
59         ResourceFactory.getResourceChangeScannerService().configure(sconf);  
60         ResourceFactory.getResourceChangeScannerService().start();  
61         ResourceFactory.getResourceChangeNotifierService().start();  
62         KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();  
63         aconf.setProperty("drools.agent.scanDirectories","true");  
64         aconf.setProperty("drools.agent.newInstance","true");  
65         KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent(agentName,aconf);  
66         kagent.applyChangeSet(ResourceFactory.newClassPathResource("resource.xml"));  
67         return kagent;  
68     }  
69   
70     /** 
71      * Get the KnowledgeBase 
72      * @return KnowledgeBase 
73      */  
74     public KnowledgeBase getKnowledgeBase(){  
75         return kagent.getKnowledgeBase();  
76     }  
77   
78     /** 
79      * Get the StatefulKnowledgeSession 
80      * @return StatefulKnowledgeSession 
81      */  
82     public StatefulKnowledgeSession getStatefulKnowledgeSession(){  
83         return getKnowledgeBase().newStatefulKnowledgeSession();  
84     }  
85   
86     /** 
87      * Get the StatelessKnowledgeSession 
88      * @return StatelessKnowledgeSession 
89      */  
90     public StatelessKnowledgeSession getStatelessKnowledgeSession(){  
91         return getKnowledgeBase().newStatelessKnowledgeSession();  
92     }  
93       
94 }  
原文地址:https://www.cnblogs.com/ibook360/p/2615706.html