skip to main content
Corticon Studio: Rule Modeling Guide : Rule dependency: Chaining and looping : Looping examples : Given a date, determine the next working day

Try Corticon Now
Given a date, determine the next working day


For any given date, determine the next working day. Take into consideration weekends and holidays.


Implemented correctly in Corticon Studio, these rules should start with a given input date, and increment as necessary until the next workday is identified (workday defined here as any day not Saturday, Sunday, or a national holiday). A simple Vocabulary that supports these rules is shown in Example of a Finite Single-Rule Loop.
Figure 159. Sample Vocabulary for Holiday Rules
Next, the rules are implemented in the Rulesheet shown in the following figure:
Figure 160. Sample Rulesheet for Determining Next Workday
Let's step through this Rulesheet.
1. First, notice that the Scope section is not used. We are using a very simple Vocabulary with short entity names and no associations, so aliases are not necessary. Furthermore, none of our rules use collection operations, so aliases representing collections are not required either.
2. The first rule executed is the Nonconditional equation (in Condition/Action column 0) setting nextWorkDay equal to currentDate plus one day.
3. Rule 1 (in column 1) checks to see if the DateTime of the nextWorkDay matches any of the holidays defined in one or more Holiday entities. If it does, then the Action row B increments nextWorkDay by one day and posts a warning message.
4. Rule 3 checks to see if the nextWorkDay falls on a Sunday. Notice that this rule uses the .dayOfWeek operator, which is described in full detail in the Rule Language Guide. If the day of the week is Sunday (in other words, .dayOfWeek returns a value of 1), then the Action increments nextWorkDay by one day and posts a Warning message.
5. Rule 4 checks to see if the nextWorkDay falls on a Saturday. If the day of the week is Saturday (in other words, .dayOfWeek returns a value of 7), then the Action row C increments nextWorkDay by two days and posts a Warning message. By incrementing 2 days, we skip an extra iteration because we know Sunday is also a non-workday!
Do not forget to check for conflicts – they do exist in this Rulesheet. However, we will make the assumption that a holiday never falls on a weekend. 
Note: Resolution of the conflicts is straightforward, so we won't address that in detail here. One conflict – that between rules 1 and 4 - is left unresolved because we have assumed that a holiday never falls on a weekend. See Logical Analysis chapter more a complete discussion of conflict and other logical problems.
A modified Rulesheet displays the overrides added to resolve the conflicts in the following figure:
Figure 161. Holiday Rules with Ambiguities Resolved by Overrides
Using the same rules as before, let's click the Logical Loop Checker icon in the Corticon Studio toolbar. The following window opens:
Figure 162. Results of Logical Loop Check
This window first identifies exactly which rules are involved in loops. Secondly, the window outlines the specific attribute interactions that create the loops.
Now that we fully understand the looping logic present in our Rulesheet, let's create a Ruletest to verify that the loops operate as intended and produce the correct business results.
Figure 163. Ruletest for Holiday Rules
Given that July 4th, 2014 falls on a Friday, we expect nextWorkDay to contain a final value of July 7th, 2014 – a Monday – when the loops terminate. When we run the Ruletest, we see the following:
Figure 164. Ruletest for Holiday Rules
As you can see, we got the result we wanted...a three-day weekend!