将 Drools 与 Glassfish 服务器结合使用
我正在尝试在我的动态网络应用程序中使用简单的流口水规则。我创建了我的 servlet,现在尝试集成我的规则。但它在这一行给了我错误
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
java.lang.NoSuchMethodError: org.drools.util.ChainedProperties.<init>(Ljava/lang/String;Ljava/lang/ClassLoader;Z)V
at org.drools.RuleBaseConfiguration.init(RuleBaseConfiguration.java:395)
at org.drools.RuleBaseConfiguration.<init>(RuleBaseConfiguration.java:251)
at org.drools.common.AbstractRuleBase.<init>(AbstractRuleBase.java:147)
at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:160)
at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:137)
at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:87)
at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:75)
at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:38)
at server.HelloServlet.runRules(HelloServlet.java:148)
at server.HelloServlet.NewRule(HelloServlet.java:181)
at server.HelloServlet.doGet(HelloServlet.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
,这里是我的 Helloservlet.java 类
@WebServlet("/HelloServlet") 公共类 HelloServlet 扩展 HttpServlet { 私有静态最终长serialVersionUID = 1L;
@EJB HelloBean bean;
@PersistenceUnit
EntityManagerFactory emf;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.print("<html><body><title>servlet page</title>");
out.print("<h1> " +request.getServletPath() + "</h1>");
out.println("<h2>" + bean.sayHello("nabiyin") + "</h2>");
MyUser user=(MyUser)emf.createEntityManager().createQuery("select u from MyUser u").getResultList().get(0);
try {
NewRule();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("HATAAAAA");
e.printStackTrace();
}
out.print("age: " + user.getAge());
if (user.getAge()<23) {
out.println("hello");
}
out.print("</html></body>");
}
public void runRules(String[] rules, Object... facts)
throws Exception
{
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
PackageBuilder builder = new PackageBuilder();
for(String ruleFile : rules)
{
System.out.println("Loading file: "+ruleFile);
builder.addPackageFromDrl(
new InputStreamReader(
this.getClass().getResourceAsStream(ruleFile)));
}
Package pkg = builder.getPackage();
ruleBase.addPackage(pkg);
WorkingMemory workingMemory
= ruleBase.newStatefulSession();
for(Object fact : facts)
{
System.out.println("Inserting fact: "+fact);
workingMemory.insert(fact);
}
workingMemory.fireAllRules();
}
public void NewRule()
throws Exception
{
System.out.println("New rule started");
Number n1=3 , n2=1, n3=4 , n4=1 ,n5=5;
new HelloServlet().runRules(
new String[]{"model/NewRule.drl"},
n1,n2,n3,n4,n5);
System.out.println("New rule finisheds");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
I am trying to use simple drools rule with my dynamic web app. I created my servlet and now trying to integrate my rule. But it gives me error at this line
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
java.lang.NoSuchMethodError: org.drools.util.ChainedProperties.<init>(Ljava/lang/String;Ljava/lang/ClassLoader;Z)V
at org.drools.RuleBaseConfiguration.init(RuleBaseConfiguration.java:395)
at org.drools.RuleBaseConfiguration.<init>(RuleBaseConfiguration.java:251)
at org.drools.common.AbstractRuleBase.<init>(AbstractRuleBase.java:147)
at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:160)
at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:137)
at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:87)
at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:75)
at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:38)
at server.HelloServlet.runRules(HelloServlet.java:148)
at server.HelloServlet.NewRule(HelloServlet.java:181)
at server.HelloServlet.doGet(HelloServlet.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
And Here my Helloservlet.java class
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB HelloBean bean;
@PersistenceUnit
EntityManagerFactory emf;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.print("<html><body><title>servlet page</title>");
out.print("<h1> " +request.getServletPath() + "</h1>");
out.println("<h2>" + bean.sayHello("nabiyin") + "</h2>");
MyUser user=(MyUser)emf.createEntityManager().createQuery("select u from MyUser u").getResultList().get(0);
try {
NewRule();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("HATAAAAA");
e.printStackTrace();
}
out.print("age: " + user.getAge());
if (user.getAge()<23) {
out.println("hello");
}
out.print("</html></body>");
}
public void runRules(String[] rules, Object... facts)
throws Exception
{
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
PackageBuilder builder = new PackageBuilder();
for(String ruleFile : rules)
{
System.out.println("Loading file: "+ruleFile);
builder.addPackageFromDrl(
new InputStreamReader(
this.getClass().getResourceAsStream(ruleFile)));
}
Package pkg = builder.getPackage();
ruleBase.addPackage(pkg);
WorkingMemory workingMemory
= ruleBase.newStatefulSession();
for(Object fact : facts)
{
System.out.println("Inserting fact: "+fact);
workingMemory.insert(fact);
}
workingMemory.fireAllRules();
}
public void NewRule()
throws Exception
{
System.out.println("New rule started");
Number n1=3 , n2=1, n3=4 , n4=1 ,n5=5;
new HelloServlet().runRules(
new String[]{"model/NewRule.drl"},
n1,n2,n3,n4,n5);
System.out.println("New rule finisheds");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议在您的 Web 应用程序中使用规则引擎作为单一 EJB,以避免每个 Web 会话创建自己的规则引擎实例(对于上面的简单演示并不重要)。
您可以尝试使用当前的库并重试吗? (也许当前这个词用错了,但还是有区别的!)
查看当前 drools 发行版中的示例或以下简单示例: http://javadude.wordpress.com/2012/03/01/drools-expert-read-rules-from-string-and-database/
(如果我我错了)
I recommend using the rule engine as a singelton EJB in your web application, to avoid every web session creates its own rule engine instance (for the simple demo above not important).
Can you try to work with the current libraries and try again. (maybe current is the wrong word, but there is a difference!)
Look at the samples in the current drools distribution or this simple sample: http://javadude.wordpress.com/2012/03/01/drools-expert-read-rules-from-string-and-database/
(Experts might correct if I am wrong)