Chapter 6. Create rules for employee rostering using guided rules
You can create rules that define hard and soft constraints for employee rostering using the guided rules designer in Decision Central.
6.1. Guided rules
Guided rules are business rules that you create in a UI-based guided rules designer in Decision Central that leads you through the rule-creation process. The guided rules designer provides fields and options for acceptable input based on the data objects for the rule being defined. The guided rules that you define are compiled into Drools Rule Language (DRL) rules as with all other rule assets.
All data objects related to a guided rule must be in the same project package as the guided rule. Assets in the same package are imported by default. After the necessary data objects and the guided rule are created, you can use the Data Objects tab of the guided rules designer to verify that all required data objects are listed or to import other existing data objects by adding a New item.
6.2. Create a guided rule to balance employee shift numbers
The BalanceEmployeesShiftNumber
guided rule creates a soft constraint that ensures shifts are assigned to employees in a way that is balanced as evenly as possible. It does this by creating a score penalty that increases when shift distribution is less even. The score formula, implemented by the rule, incentivizes the Solver to distribute shifts in a more balanced way.

Procedure
-
Go to Menu
Design Projects and click the project name. -
Click Create New Asset
Guided Rule. -
Enter
BalanceEmployeesShiftNumber
as the Guided Rule name and select theemployeerostering.employeerostering
Package. - Click Ok to create the rule asset.
-
Add a WHEN condition by clicking the
in the WHEN field.
-
Select
Employee
in the Add a condition to the rule window. Click +Ok. -
Click on the
Employee
condition to modify the constraints and add the variable name$employee
. Add the WHEN condition
From Accumulate
.-
Above the
From Accumulate
condition, click click to add pattern and selectNumber
as the fact type from the drop-down list. -
Add the variable name
$shiftCount
to theNumber
condition. -
Below the
From Accumulate
condition, click click to add pattern and select theShiftAssignment
fact type from the drop-down list. -
Add the variable name
$shiftAssignment
to theShiftAssignment
fact type. -
Click on the
ShiftAssignment
condition again and from the Add a restriction on a field drop-down list, selectemployee
. -
Select
equal to
from the drop-down list next to theemployee
constraint. -
Click the
icon next to the drop-down button to add a variable, and click Bound variable in the Field value window.
-
Select
$employee
from the drop-down list. -
In the Function box type
count($shiftAssignment)
.
-
Above the
-
Add the THEN condition by clicking the
in the THEN field.
Select
Modify Soft Score
in the Add a new action window. Click +Ok.-
Type the following expression into the box:
-($shiftCount.intValue()*$shiftCount.intValue())
-
Type the following expression into the box:
- Click Validate in the upper-right corner to check all rule conditions are valid. If the rule validation fails, address any problems described in the error message, review all components in the rule, and try again to validate the rule until the rule passes.
- Click Save to save the rule.
For more information about creating guided rules, see Designing a decision service using guided rules.
6.3. Create a guided rule for no more than one shift per day
The OneEmployeeShiftPerDay
guided rule creates a hard constraint that employees are not assigned more than one shift per day. In the employee rostering example, this constraint is created using the guided rule designer.

Procedure
-
Go to Menu
Design Projects and click the project name. -
Click Create New Asset
Guided Rule. -
Enter
OneEmployeeShiftPerDay
as the Guided Rule name and select theemployeerostering.employeerostering
Package. - Click Ok to create the rule asset.
-
Add a WHEN condition by clicking the
in the WHEN field.
- Select Free form DRL from the Add a condition to the rule window.
In the free form DRL box, type the following condition:
$shiftAssignment : ShiftAssignment( employee != null ) ShiftAssignment( this != $shiftAssignment , employee == $shiftAssignment.employee , shift.timeslot.startTime.toLocalDate() == $shiftAssignment.shift.timeslot.startTime.toLocalDate() )
$shiftAssignment : ShiftAssignment( employee != null ) ShiftAssignment( this != $shiftAssignment , employee == $shiftAssignment.employee , shift.timeslot.startTime.toLocalDate() == $shiftAssignment.shift.timeslot.startTime.toLocalDate() )
Copy to Clipboard Copied! This condition states that a shift cannot be assigned to an employee that already has another shift assignment on the same day.
-
Add the THEN condition by clicking the
in the THEN field.
- Select Add free form DRL from the Add a new action window.
In the free form DRL box, type the following condition:
scoreHolder.addHardConstraintMatch(kcontext, -1);
scoreHolder.addHardConstraintMatch(kcontext, -1);
Copy to Clipboard Copied! - Click Validate in the upper-right corner to check all rule conditions are valid. If the rule validation fails, address any problems described in the error message, review all components in the rule, and try again to validate the rule until the rule passes.
- Click Save to save the rule.
For more information about creating guided rules, see Designing a decision service using guided rules.
6.4. Create a guided rule to match skills to shift requirements
The ShiftReqiredSkillsAreMet
guided rule creates a hard constraint that ensures all shifts are assigned an employee with the correct set of skills. In the employee rostering example, this constraint is created using the guided rule designer.

Procedure
-
Go to Menu
Design Projects and click the project name. -
Click Create New Asset
Guided Rule. -
Enter
ShiftReqiredSkillsAreMet
as the Guided Rule name and select theemployeerostering.employeerostering
Package. - Click Ok to create the rule asset.
-
Add a WHEN condition by clicking the
in the WHEN field.
-
Select
ShiftAssignment
in the Add a condition to the rule window. Click +Ok. -
Click on the
ShiftAssignment
condition, and selectemployee
from the Add a restriction on a field drop-down list. -
In the designer, click the drop-down list next to
employee
and selectis not null
. Click on the
ShiftAssignment
condition, and click Expression editor.-
In the designer, click
[not bound]
to open the Expression editor, and bind the expression to the variable$requiredSkill
. Click Set. -
In the designer, next to
$requiredSkill
, selectshift
from the first drop-down list, thenrequiredSkill
from the next drop-down list.
-
In the designer, click
Click on the
ShiftAssignment
condition, and click Expression editor.-
In the designer, next to
[not bound]
, selectemployee
from the first drop-down list, thenskills
from the next drop-down list. -
Leave the next drop-down list as
Choose
. -
In the next drop-down box, change
please choose
toexcludes
. -
Click the
icon next to
excludes
, and in the Field value window, click the New formula button. -
Type
$requiredSkill
into the formula box.
-
In the designer, next to
-
Add the THEN condition by clicking the
in the THEN field.
-
Select
Modify Hard Score
in the Add a new action window. Click +Ok. -
Type
-1
into the score actions box. - Click Validate in the upper-right corner to check all rule conditions are valid. If the rule validation fails, address any problems described in the error message, review all components in the rule, and try again to validate the rule until the rule passes.
- Click Save to save the rule.
For more information about creating guided rules, see Designing a decision service using guided rules.
6.5. Create a guided rule to manage day off requests
The DayOffRequest
guided rule creates a soft constraint that allows a shift to be reassigned to another employee in the event the employee who was originally assigned the shift is no longer able to work that day. In the employee rostering example, this constraint is created using the guided rule designer.

Procedure
-
Go to Menu
Design Projects and click the project name. -
Click Create New Asset
Guided Rule. -
Enter
DayOffRequest
as the Guided Rule name and select theemployeerostering.employeerostering
Package. - Click Ok to create the rule asset.
-
Add a WHEN condition by clicking the
in the WHEN field.
- Select Free form DRL from the Add a condition to the rule window.
In the free form DRL box, type the following condition:
$dayOffRequest : DayOffRequest( ) ShiftAssignment( employee == $dayOffRequest.employee , shift.timeslot.startTime.toLocalDate() == $dayOffRequest.date )
$dayOffRequest : DayOffRequest( ) ShiftAssignment( employee == $dayOffRequest.employee , shift.timeslot.startTime.toLocalDate() == $dayOffRequest.date )
Copy to Clipboard Copied! This condition states if a shift is assigned to an employee who has made a day off request, the employee can be unassigned the shift on that day.
-
Add the THEN condition by clicking the
in the THEN field.
- Select Add free form DRL from the Add a new action window.
In the free form DRL box, type the following condition:
scoreHolder.addSoftConstraintMatch(kcontext, -100);
scoreHolder.addSoftConstraintMatch(kcontext, -100);
Copy to Clipboard Copied! - Click Validate in the upper-right corner to check all rule conditions are valid. If the rule validation fails, address any problems described in the error message, review all components in the rule, and try again to validate the rule until the rule passes.
- Click Save to save the rule.
For more information about creating guided rules, see Designing a decision service using guided rules.