本文介绍了将对象映射的值求和会给我带来错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个 SubjectTeacherPeriod 具有 num_attribute_map ,这是一个映射某些属性(例如无聊)的映射与他们各自的分数。我使用以下代码对一周中的每一天的属性(例如无聊)求和。

A SubjectTeacherPeriod has a num_attribute_map, which is a map that maps certain attributes (such as "boringness") with their respective scores. I use the following code to sum attributes (such as "boringness") over each day of the week.

但是某些行会导致错误。

But a certain line causes an error.

rule "insertAttributeDayTotal"
        //salience 1 // Do these rules first (optional, for performance)
    when
    $sum_regression_constraint : SumRegressionConstraint(
                    $class : class_,
                    $attribute : attribute//,
                    //$weight : weight;
                   )
    $day_of_week : DayOfWeek()
    $attribute_day_total : Number() from accumulate(
        SubjectTeacherPeriod(
            //period != null,
                period.class_ == $class,
            period.dayOfWeek == $day_of_week,
                $total : num_attribute_map[$attribute] //PROBLEM LINE
            ),
            sum($total)
        )

    then
    //System.out.println("BUCKET TOTAL "+$id+" "+$bucket_total.intValue());
        insertLogical(new AttributeDaySum($class, $attribute, $day_of_week, $attribute_day_total.intValue()));
end

错误是:

jesvin@Jesvin-Technovia:~/dev/drools/timetabler$ java -server in.co.technovia.timetabler.TimeTableApp
Exception in thread "main" java.lang.IllegalStateException: There are errors in the scoreDrl's:
Variables can not be used inside bindings. Variable [$attribute] is being used in binding 'num_attribute_map[$attribute]' : [Rule name='insertAttributeDayTotal']

Rule Compilation error : [Rule name='insertAttributeDayTotal']
    in/co/technovia/timetabler/domain/Rule_insertAttributeDayTotal_bb39fd28b3c8457cb8d86fc15b34a0e7.java (7:905) : Syntax error on token "null", invalid Type
    in/co/technovia/timetabler/domain/Rule_insertAttributeDayTotal_bb39fd28b3c8457cb8d86fc15b34a0e7.java (9:1050) : $total cannot be resolved






SubjectTeacherPeriod 具有奇怪的 num_attribute_map ,以便我可以在运行时定义属性。如果我想要 SubjectTeacherPeriod 无聊(int)属性,则可以执行 num_attribute_map.put (无聊,1)而不是向 SubjectTeacherPeriod 添加新属性。


SubjectTeacherPeriod has the curious num_attribute_map so that I can define attributes at runtime. If I wanted a boringness (int) attribute for SubjectTeacherPeriod, I can do num_attribute_map.put("boringness",1) instead of adding a new attribute to SubjectTeacherPeriod.

A SumRegressionConstraint 关心特定的 $ attribute 。该属性的值存储在 SubjectTeacherPeriod num_attribute_map 中。我想访问 num_attribute_map [$ attribute] ,但出现此问题。

A SumRegressionConstraint cares about a particular $attribute. That attribute's value is stored in num_attribute_map of SubjectTeacherPeriod. I want to access num_attribute_map[$attribute] but this problem shows up.

我在做什么错了?

还有其他方法可以使动态属性起作用吗?

Is there any other way to get dynamic attributes to work?

推荐答案

目前,您不能将变量绑定到表达式,而只能绑定到字段名称。因此,与其将其绑定到:

At the moment, you can't bind a variable to expressions, only to field names. So instead of binding it to:

$total : num_attribute_map[$attribute]

将其绑定到:

$total : num_attribute_map

然后,您可以在函数上使用表达式。如果您使用的是MVEL方言:

Then, you can use the expression on the function. If you are using the MVEL dialect:

sum( $total[$attribute] )

或者如果您使用的是Java方言:

Or if you are using the java dialect:

sum( $total.get($attribute) )

这篇关于将对象映射的值求和会给我带来错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 04:50