简体   繁体   English

我如何在Java中制定杰西规则

[英]How i could make a jess rule in java

I am a begginer at jess rules so i can't understand how i could use it. 我是jess规则的初学者,所以我不明白如何使用它。 I had read a lot of tutorials but i am confused. 我读了很多教程,但我很困惑。

So i have this code : 所以我有这个代码:

Date choosendate = "2013-05-05";
Date date1 = "2013-05-10";
Date date2 = "2013-05-25";
Date date3 = "2013-05-05";
int var = 0;

    if (choosendate.compareTo(date1)==0)
                {
                    var = 1;
                }
                else if (choosendate.compareTo(date2)==0)
                {
                    var = 2;
                }
                else if (choosendate.compareTo(date3)==0)
                {
                    var = 3;
                }

How i could do it with jess rules? 我如何用杰西规则做到这一点? I would like to make a jess rules who takes the dates , compare them and give me back in java the variable var. 我想制定一个jess规则来获取日期,比较日期,然后在java中将变量var给我。 Could you make me a simple example to understand it? 您能给我一个简单的例子来理解它吗?

This problem isn't a good fit for Jess as written (the Java code is short and efficient as-is) but I can show you a solution that could be adapted to other more complex situations. 这个问题对Jess来说并不是一个很好的选择(Java代码是简短而高效的),但我可以向您展示一个可以适应其他更复杂情况的解决方案。 First, you would need to define a template to hold Date , int pairs: 首先,您需要定义一个模板来保存Dateint对:

(deftemplate pair (slot date) (slot score))

Then you could create some facts using the template. 然后,您可以使用模板创建一些事实。 These are somewhat equivalent to your date1 , date2 , etc, except they associate each date with the corresponding var value: 这些与您的date1date2等类似,除了它们将每个日期与相应的var值相关联之外:

(import java.util.Date)
(assert (pair (date (new Date 113 4 10)) (score 1)))
(assert (pair (date (new Date 113 4 25)) (score 2)))
(assert (pair (date (new Date 113 4 5))  (score 3)))

We can define a global variable to hold the final, computed score (makes it easier to get from Java.) This is the equivalent of your var variable: 我们可以定义一个全局变量来保存最终的计算出的分数(使从Java中获得分数更加容易。)这与var变量等效:

(defglobal ?*var* = 0)

Assuming that the "chosen date" is going to be in an ordered fact chosendate , we could write a rule like the following. 假设“选择日期”将在有序事实chosendate ,我们可以编写如下规则。 It replaces your chain of if statements, and will compare your chosen date to all the dates in working memory until it finds a match, then stop: 它替换了if语句链,并将您选择的日期与工作内存中的所有日期进行比较,直到找到匹配项,然后停止:

(defrule score-date
    (chosendate ?d)
    (pair (date ?d) (score ?s))
    =>
    (bind ?*var* ?s)
    (halt))

OK, now, all the code above goes in a file called dates.clp . 好的,现在,以上所有代码都放在名为dates.clp的文件中。 The following Java code will make use of it (the call to Rete.watchAll() is included so you can see some interesting trace output; you'd leave that out in a real program): 以下Java代码将利用它(包括对Rete.watchAll()的调用,因此您可以看到一些有趣的跟踪输出;您可以将其保留在实际程序中):

    import jess.*;
    // ...

    // Get Jess ready
    Rete engine = new Rete();
    engine.batch("dates.clp");
    engine.watchAll();

    // Plug in the "chosen date"
    Date chosenDate = new Date(113, 4, 5);
    Fact fact = new Fact("chosendate", engine);
    fact.setSlotValue("__data", new Value(new ValueVector().add(chosenDate), RU.LIST));
    engine.assertFact(fact);

    // Run the rule and report the result
    int count = engine.run();
    if (count > 0) {
        int score = engine.getGlobalContext().getVariable("*var*").intValue(null);
        System.out.println("Score = " + score);
    } else {
        System.out.println("No matching date found.");
    }

As I said, this isn't a great fit, because the resulting code is larger and more complex than your original. 正如我说的那样,这不合适,因为生成的代码比原始代码更大,更复杂。 Where using a rule engine makes sense is if you've got multiple rules that interact; 如果您有多个交互的规则,那么使用规则引擎就有意义。 such a Jess program has no more overhead than this, and so fairly quickly starts to look like a simplification compared to equivalent Java code. 这样的Jess程序没有比这更多的开销,因此与等效的Java代码相比,它很快就开始看起来像是一种简化。 Good luck with Jess! 杰西祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM