参照了一些代码,自己对Drools的JSR94接口作了些封装
RuleEngineFacadeBean对Drools与JSR94的加载进行了封装
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import javax.rules.RuleRuntime;
import javax.rules.RuleServiceProvider;
import javax.rules.RuleServiceProviderManager;
import javax.rules.StatefulRuleSession;
import javax.rules.StatelessRuleSession;
import javax.rules.admin.LocalRuleExecutionSetProvider;
import javax.rules.admin.RuleAdministrator;
import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetRegisterException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Builds up the JSR94 object structure. It'll simplify the task of building a
* <code>RuleExecutionSet</code> and associated <code>RuntimeSession</code>
* objects from a given <code>InputStream</code>.
*
* @author N. Alex Rupp (n_alex <at>codehaus.org)
*/
public class RuleEngineFacadeBean {
private static final Log logger = LogFactory
.getLog(RuleEngineFacadeBean.class);
private static final Map<String, RuleSetWapper> RULESETMAP = new HashMap<String, RuleSetWapper>();
public String ruleServiceProviderName = "org.drools.jsr94.rules.RuleServiceProviderImpl";
public String ruleServiceProviderUri = "http://drools.org/";
private RuleAdministrator ruleAdministrator;
private RuleServiceProvider ruleServiceProvider;
private LocalRuleExecutionSetProvider ruleSetProvider;
private RuleRuntime ruleRuntime;
private boolean init = false;
//
private RuleSetBean ruleSet = new RuleSetBean();
public RuleEngineFacadeBean() {}
public RuleEngineFacadeBean(String ruleServiceProviderUri,
String ruleServiceProviderName) throws Exception {
setRuleServiceProviderUri(ruleServiceProviderUri);
setRuleServiceProviderName(ruleServiceProviderName);
init();
}
public void init() throws Exception {
if (init) {
return;
}
RuleServiceProviderManager
.registerRuleServiceProvider(getRuleServiceProviderUri(), Class
.forName(getRuleServiceProviderName()));
setRuleServiceProvider(RuleServiceProviderManager
.getRuleServiceProvider(getRuleServiceProviderUri()));
setRuleAdministrator(getRuleServiceProvider().getRuleAdministrator());
setRuleSetProvider(getRuleAdministrator()
.getLocalRuleExecutionSetProvider(null));
}
/**
* Returns a named <code>StatelessRuleSession</code>.
*
*
* @return StatelessRuleSession
* @throws Exception
*/
public StatelessRuleSession getStatelessRuleSession(final String key,
final java.util.Map properties) throws Exception {
setRuleRuntime(getRuleServiceProvider().getRuleRuntime());
String ruleSetName = getRuleSetName(key);
loadRuleSet(ruleSetName);
return (StatelessRuleSession) getRuleRuntime()
.createRuleSession(ruleSetName, properties,
RuleRuntime.STATELESS_SESSION_TYPE);
}
/**
* Returns a named <code>StatelessRuleSession</code>.
*
* @param key
* @return StatelessRuleSession
* @throws Exception
*/
public StatelessRuleSession getStatelessRuleSession(final String key)
throws Exception {
return this.getStatelessRuleSession(key, null);
}
public StatefulRuleSession getStatefulRuleSession(final String key)
throws Exception {
return this.getStatefulRuleSession(key, null);
}
public StatefulRuleSession getStatefulRuleSession(final String key,
final java.util.Map properties) throws Exception {
setRuleRuntime(getRuleServiceProvider().getRuleRuntime());
String ruleSetName = getRuleSetName(key);
loadRuleSet(ruleSetName);
return (StatefulRuleSession) getRuleRuntime()
.createRuleSession(ruleSetName, properties,
RuleRuntime.STATEFUL_SESSION_TYPE);
}
private String getRuleSetName(String ruleSetName) {
String ruleSetName2 = getRuleSet().getRuleSetName(ruleSetName);
if (ruleSetName2 == null) {
ruleSetName2 = ruleSetName;
}
return ruleSetName2;
}
private RuleSetWapper loadRuleSet(String ruleSetName) throws Exception {
RuleSetWapper ruleSetWapper = getRULESETMAP().get(ruleSetName);
String classStr = "class:";
String fileStr = "file:";
String dbStr = "db:";
String urlStr = "url:";
if (ruleSetName.startsWith(classStr)) {//////===========================
// ==class
try {
if (ruleSetWapper == null) {
String str = ruleSetName.substring(classStr.length());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = getClass().getResourceAsStream(str);
int data = is.read();
while (data > -1) {
baos.write(data);
data = is.read();
}
Map<String, Object> config = new HashMap<String, Object>();
RuleExecutionSet ruleExecutionSet;
byte[] byteArray = baos.toByteArray();
ruleExecutionSet = getRuleSetProvider()
.createRuleExecutionSet(
new InputStreamReader(
new ByteArrayInputStream(
byteArray),
"UTF-8"), config);
ruleSetWapper = new RuleSetWapper(byteArray.length, 0L,
ruleSetName, ruleExecutionSet);
getRULESETMAP().put(ruleSetName, ruleSetWapper);
getRuleAdministrator()
.registerRuleExecutionSet(ruleSetName,
ruleExecutionSet, null);
is.close();
}
} catch (Exception e) {
logger.error("Cann't not create rule set: " + ruleSetName, e);
throw e;
}
}// ////=============================class
else if (ruleSetName.startsWith(fileStr)) {//////=======================
// ======file
String str = ruleSetName.substring(fileStr.length());
try {
File file = new File(str);
if (!file.exists() || !file.isFile()) {
logger.error("Cann't not load rule set file: "
+ ruleSetName);
}
if (ruleSetWapper != null
&& ruleSetWapper.getUpdateTime() >= file.lastModified()) {
//do nothing
} else {
//
FileInputStream is = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int data = is.read();
while (data > -1) {
baos.write(data);
data = is.read();
}
Map<String, Object> config = new HashMap<String, Object>();
RuleExecutionSet ruleExecutionSet;
byte[] byteArray = baos.toByteArray();
ruleExecutionSet = getRuleSetProvider()
.createRuleExecutionSet(
new InputStreamReader(
new ByteArrayInputStream(
byteArray),
"UTF-8"), config);
ruleSetWapper = new RuleSetWapper(byteArray.length, file
.lastModified(), ruleSetName, ruleExecutionSet);
getRULESETMAP().put(ruleSetName, ruleSetWapper);
getRuleAdministrator()
.registerRuleExecutionSet(ruleSetName,
ruleExecutionSet, null);
is.close();
}
} catch (Exception e) {
logger.error("Cann't not create rule set: " + ruleSetName, e);
throw e;
}
}// ////=============================file
else if (ruleSetName.startsWith(dbStr)) {//////=========================
// ====db
logger.error("DB protocal not surport.");
}// ////=============================db
else if (ruleSetName.startsWith(urlStr)) {//////=========================
// ====url
logger.error("URL protocal not surport.");
}// ////=============================url
else {// ////=============================other
logger.error("Not surport protocal.");
}
getRuleAdministrator().registerRuleExecutionSet(
ruleSetName,
ruleSetWapper
.getRuleSet(),
null);
return ruleSetWapper;
}
public RuleServiceProvider getRuleServiceProvider() {
return this.ruleServiceProvider;
}
public String getRuleServiceProviderName() {
return ruleServiceProviderName;
}
public void setRuleServiceProviderName(String ruleServiceProviderName) {
this.ruleServiceProviderName = ruleServiceProviderName;
}
public String getRuleServiceProviderUri() {
return ruleServiceProviderUri;
}
public void setRuleServiceProviderUri(String ruleServiceProviderUri) {
this.ruleServiceProviderUri = ruleServiceProviderUri;
}
public RuleAdministrator getRuleAdministrator() {
return ruleAdministrator;
}
public void setRuleAdministrator(RuleAdministrator ruleAdministrator) {
this.ruleAdministrator = ruleAdministrator;
}
public LocalRuleExecutionSetProvider getRuleSetProvider() {
return ruleSetProvider;
}
public void setRuleSetProvider(LocalRuleExecutionSetProvider ruleSetProvider) {
this.ruleSetProvider = ruleSetProvider;
}
public RuleRuntime getRuleRuntime() {
return ruleRuntime;
}
public void setRuleRuntime(RuleRuntime ruleRuntime) {
this.ruleRuntime = ruleRuntime;
}
public void setRuleServiceProvider(RuleServiceProvider ruleServiceProvider) {
this.ruleServiceProvider = ruleServiceProvider;
}
public RuleSetBean getRuleSet() {
return ruleSet;
}
public void setRuleSet(RuleSetBean ruleSet) {
this.ruleSet = ruleSet;
}
public static Map<String, RuleSetWapper> getRULESETMAP() {
return RULESETMAP;
}
public class RuleSetWapper {
private long length;
private long updateTime;
private String fileName;
private RuleExecutionSet ruleSet;
public RuleSetWapper() {}
public RuleSetWapper(long length, long updateTime, String fileName,
RuleExecutionSet ruleSet) {
this.length = length;
this.updateTime = updateTime;
this.fileName = fileName;
this.ruleSet = ruleSet;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(long updateTime) {
this.updateTime = updateTime;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public RuleExecutionSet getRuleSet() {
return ruleSet;
}
public void setRuleSet(RuleExecutionSet ruleSet) {
this.ruleSet = ruleSet;
}
}
}
JSR94SessionBean是对
RuleEngineFacadeBean再一次封装以方便外部调用
package com.ce.project.mboss.rules;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.rules.StatefulRuleSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JSR94SessionBean {
private static final Log logger = LogFactory.getLog(JSR94SessionBean.class);
private RuleEngineFacadeBean ruleEngineFacade;
//
private StatefulRuleSession ruleSession = null;
public JSR94SessionBean() {
}
public JSR94SessionBean(RuleEngineFacadeBean ruleEngineFacade) throws Exception {
setRuleEngineFacade(ruleEngineFacade);
init();
}
public void init() throws Exception {
getRuleEngineFacade().init();
}
public void destroy() {
if (ruleSession != null) {
try {
ruleSession.release();
} catch (Exception e) {
}
}
}
public List<?> executeStatefulRules(String ruleSetName, List<?> factList, Map properties) throws Exception {
init();
if (ruleSetName == null) {
logger.error("Rule set is not specify!!!");
return Collections.EMPTY_LIST;
}
ruleSession = getRuleEngineFacade().getStatefulRuleSession(ruleSetName, properties);
ruleSession.addObjects(factList);
ruleSession.executeRules();
List<?> objectList = ruleSession.getObjects();
ruleSession.release();
return objectList;
}
public StatefulRuleSession getRuleSession() {
return ruleSession;
}
public void setRuleSession(StatefulRuleSession ruleSession) {
this.ruleSession = ruleSession;
}
public RuleEngineFacadeBean getRuleEngineFacade() {
return ruleEngineFacade;
}
public void setRuleEngineFacade(RuleEngineFacadeBean ruleEngineFacade) {
this.ruleEngineFacade = ruleEngineFacade;
}
}
RuleSetBean用于规则存放的类
package com.ce.project.mboss.rules;
import java.util.HashMap;
import java.util.Map;
public class RuleSetBean {
private Map<String, String> ruleSetMap = new HashMap<String, String>();
public RuleSetBean() {}
public String getRuleSetName(String ruleSetName){
return ruleSetMap.get(ruleSetName);
}
public void setRuleSetMap(Map<String, String> ruleSetMap) {
this.ruleSetMap = ruleSetMap;
}
}
GlobalData用于存放规则中的全局数据
package com.ce.project.mboss.rules;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GlobalData {
private String name;
private List list = new ArrayList();
private Map map = new HashMap();
public GlobalData() {}
public GlobalData(String name) {
this.name = name;
}
public List getList() {
return list;
}
public Map getMap() {
return map;
}
public void setListData(Object obj) {
list.add(obj);
}
public void setMapData(Object key, Object value) {
map.put(key, value);
}
public Object getMapData(Object key) {
return map.get(key);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}