将 Drools 与 Glassfish 服务器结合使用

发布于 2024-10-21 21:13:20 字数 3831 浏览 5 评论 0原文

我正在尝试在我的动态网络应用程序中使用简单的流口水规则。我创建了我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-10-28 21:13:20

我建议在您的 Web 应用程序中使用规则引擎作为单一 EJB,以避免每个 Web 会话创建自己的规则引擎实例(对于上面的简单演示并不重要)。

您可以尝试使用当前的库并重试吗? (也许当前这个词用错了,但还是有区别的!)
查看当前 drools 发行版中的示例或以下简单示例: http://javadude.wordpress.com/2012/03/01/drools-expert-read-rules-from-string-and-database/

..
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
..

(如果我我错了)

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/

..
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
..

(Experts might correct if I am wrong)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文