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.

BalanceEmployeesShiftNumber Guided Rule

Procedure

  1. Go to Menu Design Projects and click the project name.
  2. Click Create New Asset Guided Rule.
  3. Enter BalanceEmployeesShiftNumber as the Guided Rule name and select the employeerostering.employeerostering Package.
  4. Click Ok to create the rule asset.
  5. Add a WHEN condition by clicking the 5686 in the WHEN field.
  6. Select Employee in the Add a condition to the rule window. Click +Ok.
  7. Click on the Employee condition to modify the constraints and add the variable name $employee.
  8. Add the WHEN condition From Accumulate.

    1. Above the From Accumulate condition, click click to add pattern and select Number as the fact type from the drop-down list.
    2. Add the variable name $shiftCount to the Number condition.
    3. Below the From Accumulate condition, click click to add pattern and select the ShiftAssignment fact type from the drop-down list.
    4. Add the variable name $shiftAssignment to the ShiftAssignment fact type.
    5. Click on the ShiftAssignment condition again and from the Add a restriction on a field drop-down list, select employee.
    6. Select equal to from the drop-down list next to the employee constraint.
    7. Click the edit icon next to the drop-down button to add a variable, and click Bound variable in the Field value window.
    8. Select $employee from the drop-down list.
    9. In the Function box type count($shiftAssignment).
  9. Add the THEN condition by clicking the 5686 in the THEN field.
  10. Select Modify Soft Score in the Add a new action window. Click +Ok.

    1. Type the following expression into the box: -($shiftCount.intValue()*$shiftCount.intValue())
  11. 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.
  12. 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.

OneEmployeeShiftPerDay Guided Rule

Procedure

  1. Go to Menu Design Projects and click the project name.
  2. Click Create New Asset Guided Rule.
  3. Enter OneEmployeeShiftPerDay as the Guided Rule name and select the employeerostering.employeerostering Package.
  4. Click Ok to create the rule asset.
  5. Add a WHEN condition by clicking the 5686 in the WHEN field.
  6. Select Free form DRL from the Add a condition to the rule window.
  7. 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() )
    Copy to Clipboard

    This condition states that a shift cannot be assigned to an employee that already has another shift assignment on the same day.

  8. Add the THEN condition by clicking the 5686 in the THEN field.
  9. Select Add free form DRL from the Add a new action window.
  10. In the free form DRL box, type the following condition:

    scoreHolder.addHardConstraintMatch(kcontext, -1);
    Copy to Clipboard
  11. 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.
  12. 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.

ShiftRequiredSkillsAreMet Guided Rule

Procedure

  1. Go to Menu Design Projects and click the project name.
  2. Click Create New Asset Guided Rule.
  3. Enter ShiftReqiredSkillsAreMet as the Guided Rule name and select the employeerostering.employeerostering Package.
  4. Click Ok to create the rule asset.
  5. Add a WHEN condition by clicking the 5686 in the WHEN field.
  6. Select ShiftAssignment in the Add a condition to the rule window. Click +Ok.
  7. Click on the ShiftAssignment condition, and select employee from the Add a restriction on a field drop-down list.
  8. In the designer, click the drop-down list next to employee and select is not null.
  9. Click on the ShiftAssignment condition, and click Expression editor.

    1. In the designer, click [not bound] to open the Expression editor, and bind the expression to the variable $requiredSkill. Click Set.
    2. In the designer, next to $requiredSkill, select shift from the first drop-down list, then requiredSkill from the next drop-down list.
  10. Click on the ShiftAssignment condition, and click Expression editor.

    1. In the designer, next to [not bound], select employee from the first drop-down list, then skills from the next drop-down list.
    2. Leave the next drop-down list as Choose.
    3. In the next drop-down box, change please choose to excludes.
    4. Click the edit icon next to excludes, and in the Field value window, click the New formula button.
    5. Type $requiredSkill into the formula box.
  11. Add the THEN condition by clicking the 5686 in the THEN field.
  12. Select Modify Hard Score in the Add a new action window. Click +Ok.
  13. Type -1 into the score actions box.
  14. 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.
  15. 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.

DayOffRequest Guided Rule

Procedure

  1. Go to Menu Design Projects and click the project name.
  2. Click Create New Asset Guided Rule.
  3. Enter DayOffRequest as the Guided Rule name and select the employeerostering.employeerostering Package.
  4. Click Ok to create the rule asset.
  5. Add a WHEN condition by clicking the 5686 in the WHEN field.
  6. Select Free form DRL from the Add a condition to the rule window.
  7. In the free form DRL box, type the following condition:

    $dayOffRequest : DayOffRequest( )
    		ShiftAssignment( employee == $dayOffRequest.employee , shift.timeslot.startTime.toLocalDate() == $dayOffRequest.date )
    Copy to Clipboard

    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.

  8. Add the THEN condition by clicking the 5686 in the THEN field.
  9. Select Add free form DRL from the Add a new action window.
  10. In the free form DRL box, type the following condition:

    scoreHolder.addSoftConstraintMatch(kcontext, -100);
    Copy to Clipboard
  11. 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.
  12. Click Save to save the rule.

For more information about creating guided rules, see Designing a decision service using guided rules.

Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2025 Red Hat