Drools4.0之规则加载

/**
 * 加载规则
 * @return
 */
private static synchronized RuleBase loadRule(String drl){
		PackageBuilderConfiguration cfg = new PackageBuilderConfiguration();

		JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
		javaConf.setCompiler(JavaDialectConfiguration.JANINO);
		javaConf.setJavaLanguageLevel("1.5");

		cfg.setDialectConfiguration("java", javaConf);
		cfg.setClassLoader(Thread.currentThread().getContextClassLoader());

		PackageBuilder builder = new PackageBuilder(cfg);

		try {
			builder.addPackageFromDrl(new InputStreamReader((new ClassPathResource(drl))
					.getInputStream()));
		} catch (DroolsParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		RuleBase ruleBase = RuleBaseFactory.newRuleBase();
		try {
			ruleBase.addPackage(builder.getPackage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ruleBase;
}

private static ruleBase = null;
if(ruleBase==null){//让规则只加载一次,改了规则文件要重新启动
	ruleBase = loadRule("TransactPurview.drl");
}


WorkingMemory workingMemory=ruleBase.newStatefulSession();
workingMemory.setGlobal("vo", vo);//用于返回值

workingMemory.insert(condition);	//规则判断的条件
workingMemory.fireAllRules();

你可能感兴趣的:(java,thread)