2012-02-25 3 views
1

SubjectTeacherPeriodnum_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) 특성을 사용하려면 SubjectTeacherPeriod에 새 특성을 추가하는 대신 num_attribute_map.put("boringness",1)을 사용할 수 있습니다.

SumRegressionConstraint은 약 $attribute입니다. 이 속성의 값은 num_attribute_mapSubjectTeacherPeriod으로 저장됩니다. num_attribute_map[$attribute]에 액세스하고 싶지만이 문제가 나타납니다.

내가 뭘 잘못하고 있니?


동적 속성을 작동시키는 다른 방법이 있습니까?

답변

1

현재 필드 이름에만 표현식에 변수를 바인딩 할 수 없습니다. 그래서 그 대신에 바인딩의 :에 바인딩합니다

$total : num_attribute_map[$attribute] 

: 다음

$total : num_attribute_map 

, 당신은 기능에 식을 사용할 수 있습니다.

sum($total[$attribute]) 

아니면 자바 방언을 사용하는 경우 :

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

명확히하세요 : 당신은 MVEL 방언 사용하는 경우는'num_attribute_map 합계 않습니다 [$ 속성]'에서'를 ** 별개 ** SubjectTeacherPeriod' 오브젝트? – aitchnyu

+0

예, 동작은 동일합니다. 이 함수는 각 SubjectTeacherPeriod에 대해 실행될 것이고 당신은 그 안에 어떤 표현식이라도 쓸 수 있습니다. 예 : sum ($ x [$ y] + 2 * $ foo.bar). 일반적으로 사람들은 sum ($ x)과 같은 간단한 표현식을 사용합니다. –

관련 문제