2012-02-25 31 views
1

A SubjectTeacherPeriod有一個num_attribute_map,它是一個映射某些屬性(如「無聊」)與其各自分數的映射。我使用以下代碼在一週中的每一天對屬性進行求和(例如「無聊」)。對象地圖的總結值給我帶來的錯誤

但某一行會導致錯誤。

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 

錯誤是:

[email protected]:~/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,這樣我可以定義在運行時屬性。如果我想SubjectTeacherPeriodboringness(int)屬性,我可以做num_attribute_map.put("boringness",1)而不是將新屬性添加到SubjectTeacherPeriod

A SumRegressionConstraint關心一個特定的$attribute。該屬性的值存儲在SubjectTeacherPeriodnum_attribute_map中。我想訪問num_attribute_map[$attribute]但這個問題出現了。

我在做什麼錯?


是否有任何其他方式來獲取動態屬性的工作?

回答

1

此刻,您不能將變量綁定到表達式,只能綁定到字段名稱。因此,而不是將它綁定到:

$total : num_attribute_map[$attribute] 

將其綁定到:

$total : num_attribute_map 

然後,您可以使用該函數的表達式。如果您使用的是MVEL方言:

sum($total[$attribute]) 

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

sum($total.get($attribute)) 
+0

請澄清:它總結'num_attribute_map [$屬性]'從** **不同' SubjectTeacherPeriod'對象? – aitchnyu 2012-02-25 16:19:54

+0

是的,行爲是一樣的。該函數將爲每個SubjectTeacherPeriod執行,您可以在其中編寫任何表達式。例如:sum($ x [$ y] + 2 * $ foo.bar)。通常人們會使用簡單的表達式,如sum($ x)。 – 2012-02-25 16:44:07