2013-06-25 42 views
0

我在理解在OptaPlanner演示示例(NurseRostering應用程序)之一中實現的Drools規則時遇到問題。誰能解釋下面的規則是如何工作的:NurseRostering應用程序中Drools規則的說明

// a nurse can only work one shift per day, i.e. no two shift can be assigned to the same nurse on a day. 
rule "oneShiftPerDay" 
when 
    $leftAssignment : ShiftAssignment($leftId : id, $employee : employee, $shiftDate : shiftDate) 
    $rightAssignment : ShiftAssignment(employee == $employee, shiftDate == $shiftDate, id > $leftId) 
then 
    insertLogical(new IntConstraintOccurrence("oneShiftPerDay", ConstraintType.NEGATIVE_HARD, 
      1, 
      $leftAssignment, $rightAssignment)); 
end 

是否有大約規則的解釋和方式詳細其中規定任何資源得到實施?當我在網上和某些書中查看一些示例時,我發現它很容易理解,但是當我檢查Drools中提供的示例時,我無法得到一個想法。

回答

0
when 
    // When a specific shift with id $leftId is assigned to employee $employee and that shift is on date $shiftDate 
    $leftAssignment : ShiftAssignment($leftId : id, $employee : employee, $shiftDate : shiftDate) 
    // AND there is another shift assigned to the same for the same date and with a higher id 
    $rightAssignment : ShiftAssignment(employee == $employee, shiftDate == $shiftDate, id > $leftId) 
then 
    // Then this solution is penalized: it gets -1 hard score point 
    scoreHolder.addHardConstraintMatch(kcontext, -1); 

注意:在隨後的一面,我用了OptaPlanner 6語法(而不是過時的計劃5.x的語法),因爲它是faster and easier

關於id < $leftId部分:爲了確保Drools只與ShiftAssignment A和ShiftAssignment B匹配(它給出了A-B)而不是A-A,B-B和B-A,以避免懲罰太多。