标签:object 实例 网络 编写测试用例 交互 连接 manage 语言转换 int
String implName = "org.jcp.jsr94.ri.RuleServiceProvider"; Class.forName(implName); RuleServiceProvider serviceProvider = RuleServiceProviderManager.getRuleServiceProvider(implName);
RuleAdministrator admin = serviceProvider.getRuleAdministrator(); HashMap properties = new HashMap(); properties.put("name", "My Rules"); properties.put("description", "A trivial rulebase"); FileReader reader = new FileReader("rules.xml"); RuleExecutionSet ruleSet = null; try { LocalRuleExecutionSetProvider lresp = admin.getLocalRuleExecutionSetProvider(properties); ruleSet = lresp.createRuleExecutionSet(reader, properties); } finally { reader.close(); }
public class RuleEngineFacade { private RuleAdministrator ruleAdministrator; private RuleServiceProvider ruleServiceProvider; private LocalRuleExecutionSetProvider ruleSetProvider; private RuleRuntime ruleRuntime; // configuration parameters private String ruleServiceProviderUrl; private Class ruleServiceProviderImpl; public void setRuleServiceProviderUrl(String url) { this.ruleServiceProviderUrl = url; } public void setRuleServiceProviderImpl(Class impl) { this.ruleServiceProviderImpl = impl; } public void init() throws Exception { RuleServiceProviderManager.registerRuleServiceProvider( ruleServiceProviderUrl, ruleServiceProviderImpl); ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider(ruleServiceProviderUrl); ruleAdministrator = ruleServiceProvider.getRuleAdministrator(); ruleSetProvider = ruleAdministrator.getLocalRuleExecutionSetProvider(null); } public void addRuleExecutionSet(String bindUri,InputStream resourceAsStream) throws Exception { Reader ruleReader = new InputStreamReader(resourceAsStream); RuleExecutionSet ruleExecutionSet = ruleSetProvider.createRuleExecutionSet(ruleReader, null); ruleAdministrator.registerRuleExecutionSet(bindUri,ruleExecutionSet,null); } public StatelessRuleSession getStatelessRuleSession(String key) throws Exception { ruleRuntime = ruleServiceProvider.getRuleRuntime(); return (StatelessRuleSession) ruleRuntime.createRuleSession(key, null, RuleRuntime.STATELESS_SESSION_TYPE); } public StatefulRuleSession getStatefulRuleSession(String key) throws Exception { ruleRuntime = ruleServiceProvider.getRuleRuntime(); return (StatefulRuleSession) ruleRuntime.createRuleSession( key, null, RuleRuntime.STATEFUL_SESSION_TYPE); } public RuleServiceProvider getRuleServiceProvider() { return this.ruleServiceProvider; } }
public class Rule { private String ruleName; private RuleEngineFacade engineFacade; public void init() throws Exception { InputStream is = Rule.class.getResourceAsStream(ruleName); engineFacade.addRuleExecutionSet(ruleName, is); is.close(); } public void setRuleName(String name) { this.ruleName = name; } public void setEngineFacade(RuleEngineFacade engine) { this.engineFacade = engine; } public StatelessRuleSession getStatelessRuleSession() throws Exception { return engineFacade.getStatelessRuleSession(ruleName); } public StatefulRuleSession getStatefuleRuleSession() throws Exception { return engineFacade.getStatefulRuleSession(ruleName); } }
<bean id="ruleEngine" class="spring.RuleEngineFacade" init-method="init" singleton="false"> <property name="ruleServiceProviderUrl"> <value>http://drools.org/</value> </property> <property name="ruleServiceProviderImpl"> <value>org.drools.jsr94.rules.RuleServiceProviderImpl</value> </property> </bean> <bean id="fibonacci" class="spring.Rule" init-method="init"> <property name="ruleName"> <value>/test/fibonacci.drl</value> </property> <property name="engineFacade"> <ref local="ruleEngine"/> </property> </bean>
public class JSRTest extends TestCase { ApplicationContext ctx = null; protected void setUp() throws Exception { super.setUp(); ctx = new FileSystemXmlApplicationContext("testrule.xml"); } public void testGetRuleSession() throws Exception { Rule rule = (Rule)ctx.getBean("fibonacci"); assertNotNull(rule.getStatefuleRuleSession()); assertNotNull(rule.getStatelessRuleSession()); } public void testStatelessRule() throws Exception { Rule rule = (Rule)ctx.getBean("fibonacci"); Fibonacci fibonacci = new Fibonacci(50); List list = new ArrayList(); list.add(fibonacci); StatelessRuleSession session = rule.getStatelessRuleSession(); session.executeRules(list); session.release(); } public void testStatefulRule() throws Exception { Rule rule = (Rule)ctx.getBean("fibonacci"); Fibonacci fibonacci = new Fibonacci(50); StatefulRuleSession session = rule.getStatefuleRuleSession(); session.addObject(fibonacci); session.executeRules(); session.release(); } }
标签:object 实例 网络 编写测试用例 交互 连接 manage 语言转换 int
原文地址:http://www.cnblogs.com/gsyun/p/7476573.html