如何在Drools中获得最小值的最小值?

时间:2014-02-11 09:58:35

标签: drools

我想使用Drools管理表单中的一些问题。根据问题的答案,问题分数将更新为值。然后我想获得一个类别中回答的所有问题的最小值。

一个例子:

Category "Replicant or not?":
You’re in a desert walking along in the sand when all of the sudden you look down, and you see a tortoise, crawling toward you. You reach down, you flip the tortoise over on its back. Why is that? 
1. it was an error. (5 points).
2. It is the funniest think to do at a dessert. (3 points).
3. You want to kill the tortoise in the most painful way (1 point). 
//More other questions.

在测试结束时,每个类别的最小值将是使用过的。

然后我定义了一些drools规则来定义每个规则的分数(在电子表格中,我把规则翻译):

rule "Question_1"
    when
            $qs: Questions(checked == false);
            $q: Question(category == 'cat1', question == "q1", answer == "a") from $qs.getQuestions();
    then
            $q.setValue(5);
            $qs.setChecked(true);
            update($qs);
end

checked值用于避免在更新时重用规则。 categoryquestionanswer用于对问题进行分类。

然后,我计算最小值的规则是:

rule "Min value"
when
    $qs: Questions(checked == true);
    not q:Question($v: value; value < $v) from $qs.getQuestions();
then
    $qs.setMinValue($v);
    System.out.println("The smallest value is: "+ $v);
end

获得的错误是:

$v cannot be resolved to a variable

然后,问题是:我如何获得先前规则设定的值的最小值?

2 个答案:

答案 0 :(得分:1)

您正试图从不存在的事实中获取$ v:not q:Question($v: value; value < $v) from $qs.getQuestions(); $ v的期望值是什么,没有与该模式匹配的问题!?

第二种模式基本上是说:“来自Questions.getQuestions()的问题没有值($ v),并且该值小于相同的值”

你要做的是将$ v绑定到正模式。像这样:

rule "Min value"
when
    $qs: Questions(checked == true);
    Question($v: value) from $qs.getQuestions()
    not Question(value < $v) from $qs.getQuestions();
then
    $qs.setMinValue($v);
    System.out.println("The smallest value is: "+ $v);
end

希望它有所帮助,

---编辑:添加缺失问题积极模式

答案 1 :(得分:1)

使用累积CE可以避免重复“from”,速度提高约25%。

rule "getmin"
when
    Questions( $qs: questions )
    accumulate( Question( $v: value ) from $qs; $min: min( $v ) )
then
    System.out.println( "minimum is " + $min );
end