In Rainbird, concepts can be ‘date’ concepts. Date concepts should be used when a date will be either input into or output by Rainbird. Dates in Rainbird are formatted as YYYY-MM-DD (e.g. 1st of March 2021 would be formatted as 2021-03-01).
Rainbird can use date expressions to transform and calculate dates. Date expressions can be combined with other date, transformative and comparative expressions to ‘retrieve’, ‘add’, ‘subtract’ or ‘compare’ time.
If you want to use the outcome of a date expression’s calculation in further calculations, you will need to create the initial date expression on the relationship between the date concept that holds the date concept instances that will be used in the expression, and a number concept which the outcome of the expression will be assigned to.
The expressions discussed in this article are used by Rainbird to retrieve a required value from a date instance that has been created at runtime or that exists in the map. The expressions discussed in this article are:- dayOfWeek: determines the day of the week as a numerical value (1-7, with Monday = 1, Tuesday = 2 etc)
- dayOfMonth: extracts the day value from a date (e.g. Rainbird would extract 12 if the date instance was 12th of February 2021)
- dayOfYear: counts the number of days a date is into a year (e.g. Rainbird would count 43 days if the date instance was 12th of February 2021)
- monthsOfYear: determines the month of the year as a numerical value (1-12, with January = 1, February =2 etc)
- year: extracts the year from a date (e.g. Rainbird would extract 2021 if the date instance was 12th of February 2021)
dayOfWeek: Rainbird uses the dayOfWeek function to determine the weekday of a date entered at runtime or that exists in the map, as a number (e.g. dayOfWeek(0000-12-24) would return a 7, for Sunday).
The next couple of tabs will demonstrate how to create a map that will use a dayOfWeek function to determine what day of the week a person’s birthday was on.
Figure 1: Example rule using the dayOfWeek expression
First, create the following concepts:
- 2 string concepts – ‘Person’ and ‘Day of week’
- 1 date concept – ‘Date’
- 1 number concept – ‘Day number
Now, link the concepts with the relationships as shown in Figure 2:
Figure 2: Concepts linked with relationships
- The main rule of this map exists on the ‘wants to know day of week‘ relationship (see Build the Rule: Figures 4 & 5).
- The ‘what day was my birthday‘ relationship will trigger the end-user to input a date.
- A rule will be created on the relationship ‘has day number’ which will assign a weekday to the number (1 -7) generated by the dayOfWeek expression (See Creating the Facts: Figure 3).
Next, we need to create facts, which will tell Rainbird what number equals what day of the week (1 for Monday, 2 for Tuesday etc).
Open the concept ‘Day of week’, and click ‘Add Instance’. Create a concept for each day of the week, (Monday, Tuesday etc), and under the ‘Facts’ subheading, in the ‘object’ box next to the ‘has day number’ relationship, type in the corresponding number for each day of the week (1 for Monday, 2 for Tuesday etc).
Now, Rainbird will know that if a ‘1’ value is returned, that means a person’s birthday will have been on a Monday (if ‘2’, Tuesday; 3, Wednesday; etc).
Figure 3: Creating facts that will assign the days of the week to the numbers 1-7
We can now create the rule which will determine what day of the week a person was born on.
Now that we have set up the map’s concepts and relationships, and have built facts, we can build the rule on the relationship ‘wants to know day of week‘. The rule will be built with two conditions and two expressions.
The first condition to add to the rule is the relationship ‘what day was my birthday‘, will make Rainbird ask the user to enter a Date at runtime. The object, %DATE, created by the ‘what day was my birthday’ relationship, will then be used in the dayOfWeek expression.
Add the expression by clicking ‘Add Expression‘ and typing in ‘dayOfWeek(%DATE)‘ into the ‘Expression’ box. The expression tells Rainbird to extract the day of the week from the %DATE object that was created by the previous condition.
Rainbird will need to save the extracted number to be used in the next expression, so the object of the expression is assigned to the variable %DATE_NUMBER.
Figure 4: Creating the first half of the rule
Note: The Rule will not save until we have used the new variable %DATE_NUMBER twice in the rule
The last part of the rule will use the facts that were previously created (see figure 2).
Rainbird needs to know that the object of the relationship (the date of the week) will have a corresponding ‘day number’, so we add the condition ‘%O has day number %DAY_NUMBER’.
We then use a comparison expression, ‘%DATE_NUMBER is equal to %DAY_NUMBER‘ to match the %DATE_NUMBER extracted by the expression with the %DAY_NUMBER.
As facts exist between %DAY_NUMBER and %DAY_OF_WEEK concepts, Rainbird will return ‘Monday‘ as the object of the relationship if a ‘1‘ value is extracted from the date entered at runtime (%DATE) by the ‘dayOfWeek(%DATE)‘ expression.
Figure 5: Matching the day of the week against the day number facts with the ‘is equal to’ expression
Now, if we were to run the query on the ‘wants to know day of the week’ relationship, and input 1993-09-20 as our birthday, Rainbird would extract the value ‘1’ from the date as the date fell on a Monday.
As the outcome of the rule is the Object (Day of Week), Rainbird returns the fact for 1, Monday:
Figure 6: Outcome of query with 1993-09-20 as the input date
In the knowledge map generated by the RBLang at the bottom of the article, please run the query on the ‘wants to know day of week’ for an example of a query that uses the ‘dayOfWeek’ expression.
dayOfMonth: Rainbird uses the dayOfMonth function to extract the day number within the month from a date instance which is either created at runtime or that exists as an instance in the map. For example, if the date instance was 2000-12-24, Rainbird would extract the number 24 from the date using the dayOfMonth function.
The next couple of tabs will demonstrate how to create a map that will use a dayOfMonth function to determine what day of the month an event happened on.
Figure 7: Example rule using the dayOfMonth expression
First, creating the following concepts:
- 1 string concept – ‘Person’
- 1 number concept – ‘Day of Month’ (a String concept would also return the same result, but won’t allow the outcome of the expression to be used in further calculations)
- 1 date concept – ‘Date’
Now, link the concepts with the relationships as shown in figure 8.
Figure 8: Concepts linked with relationships
- The main rule of this map exists on the ‘wants to know day in month‘ relationship (see Build the Rule: Figure 9).
- The ‘when did that happen‘ relationship will trigger the end-user to input a date which will be the date used in the expression.
Now that we have set up the map’s concepts and relationships, we can build the rule which the dayOfMonth expression will sit in. The rule will be built on the ‘wants to know day in month‘ relationship and is made up of two components: one condition, and one expression.
The condition will be the ‘when did that happen‘ relationship, as this relationship will ask the user to enter a date. The object of the ‘when did that happen‘, ‘%DAY_OF_MONTH‘ will then be used in the dayOfMonth expression.
Next, add the expression by clicking ‘Add Expression‘ and adding ‘dayOfMonth(%DAY_OF_MONTH)‘ to the ‘Expression’ box. Assign the variable of the expression (the day of the month that will be extracted) to %O, to display the extracted date as the query result (see Figure 10).
Figure 9: Building the rule
Now, if we were to run the query on the ‘wants to know day in month’ relationship, and input 2021-12-04, Rainbird would return the result ‘4’:
Figure 10: Outcome of query with 2021-12-04 as the input date
In the knowledge map generated by the RBLang at the bottom of the article, please run the query on the ‘wants to know day in month’ for an example of a query that uses the ‘dayOfMonth’ expression.
dayOfYear: Rainbird uses the dayOfYear function to calculate how many days a date instance, either created by the end-user at runtime or that exists in the map, is from the beginning of the year. For example, if the input date is 2021-02-04, the dayOfYear function would return the number 35 .
The next couple of tabs will demonstrate how to create a map that will use a dayOfYear function to determine how many days into a year a date is.
Figure 11: Example rule using the dayOfYear expression
First, create the following concepts:
- 1 string concepts – ‘Person’
- 1 date concept – ‘Date’
- 1 number concept – ‘Day of year’ (choosing a String would return the same result, but won’t allow further calculations with the outcome of the expression).
Next, create relationships between the concepts, as shown in figure 12:
Figure 12: Concepts linked with relationships
- The main rule of this map exists on the ‘wants to know number of days into year‘ relationship (see Build the Rule: Figure 13).
- The ‘when did that happen‘ relationship will trigger the end-user to input a date.
Now that we have set up the map’s concepts and relationships, we can build the rule which the dayOfYear expression will sit in. The rule will be built on the ‘wants to know number of days into the year’ relationship and is made up of two components: one condition, and one expression.
The first condition to add to the rule, the relationship ‘when did that happen‘, will make Rainbird ask the user to enter a date at runtime. The object, %DAY_INTO_YEAR, created by the ‘when did that happen‘ relationship, will then be used in the dayOfYear expression.
Next, add the expression by clicking ‘Add Expression‘, and then adding ‘dayOfYEAR(%DAY_INTO_YEAR)‘ to the ‘Expression’ box. Assign the variable of the expression (the number of days into the year that will be calculated by the expression) to %O, to display the extracted date as the query result (see Figure 14).
Figure 13: Creating the rule that uses the dayOfYear expression
Now, if we were to run the query on the ‘wants to know number of days into the year’ relationship, and input 2021-02-04, Rainbird would return the result ’35’:
Figure 14: Outcome of query with 2021-02-04 as the input date
In the knowledge map generated by the RBLang at the bottom of the article, please run the query on the ‘wants to know number of days into a year’ for an example of a query that uses the ‘dayOfYear’ expression.
monthOfYear: Rainbird uses the monthOfYear function to extract the month of the year from a date instance, either created by the end-user at runtime or that exists as an instance in the map. For example, if the input date is 2000-12-24, the monthOfYear function would extract the month value, 12.
The next couple of tabs will demonstrate how to create a map that will use a monthOfYear function to determine which month of the year a date falls on.
Figure 15: Example using the monthOfYear expression
First, create the following concepts:
- 2 string concepts – ‘Person’ and ‘Month of Year’
- 1 date concept – ‘Date’
- 1 number concept – ‘Month number.
Next, create relationships between the concepts, as shown in figure 16:
Figure 16: Concepts linked with relationships
- The main rule of the map will be on the ‘wants to know month of year‘ (Build the Rule: Figure 18).
- The ‘when did that happen‘ relationship will trigger the end-user to input a date.
- Facts which will assign a month of the year a number (January = 1, February = 2 etc) will be created between the month of year and month number concepts. Rainbird will match the number instance generated by the monthOfYear expression with the corresponding fact and display a month, rather than a number, as the output to the query. (See Creating the Facts: Figure 17).
Next, we need to create facts, which will tell Rainbird what number equals what month of the year (1 for January, 2 for February etc).
Open the concept ‘Month of year’, and click ‘Add Instance’. Create a concept for each month of the year, (January, February etc), and under the ‘Facts’ subheading, in the ‘object’ box next to the ‘has month number’ relationship, type in the corresponding number for each month of the year (1 for January, 2 for February etc).
Now, Rainbird will know that if a ‘1’ value is returned by the monthOfYear expression, that means an event happened in January (if ‘2’, February; 3, March; etc).
Figure 17: Assigning the numbers 1-12 to the months of the year
Now that we have set up the map’s concepts and relationships, and have built facts, we can build the rule on the relationship ‘wants to know month of year‘. The rule will be built with two conditions and two expressions.
The first condition to add to the rule, the relationship ‘when did that happen‘, will make Rainbird ask the user to enter a date at runtime. The object, %DATE, created by the ‘when did that happen‘ relationship, will then be used in the monthOfYear expression.
Next, add the expression by clicking ‘Add Expression‘ and adding ‘monthOfYear(%DATE)‘ to the ‘Expression’ box. The expression tells Rainbird to extract the month of the year from the %DATE object that was created by the previous condition. Rainbird will need to save the extracted number to be used in the next expression, so the object of the expression is assigned to the variable %DATE_MONTH.
Figure 18: Example using the monthOfYear expression
Note: The Rule will not save until we have used the new variable %DATE_MONTH twice in the rule
The last part of the rule will tell Rainbird to match the facts that were previously created with the extracted month value (see figure 17).
The rule will need to match the object of the relationship (the month of the year) with a corresponding ‘month number’, so we add the condition ‘%O has month number %MONTH_NUMBER’.
We then use a comparison expression, ‘%DATE_MONTH is equal to %MONTH_NUMBER‘ to match the %DATE_MONTH extracted by the expression with the %MONTH_NUMBER.
As facts exist which match %MONTH_NUMBER concept instances with %MONTH_OF_YEAR concept instances, Rainbird will return ‘January‘ as the object of the relationship if a ‘1‘ value is extracted from the date entered at runtime (%DATE) by the ‘monthOfYear(%DATE)‘ expression.
Figure 19: Matching the month of the year against the Month number facts with the ‘is equal to’ expression
Now, if we were to run the query on the ‘wants to know month of the year’ relationship, and input 2007-01-04 as a date birthday, Rainbird would extract the value ‘1’ from the date as the date fell in January.
As the outcome of the rule is the object (month of year), Rainbird returns the fact for 1, January:
Figure 20: Outcome of query with 2007-01-04 as the input date
In the knowledge map generated by the RBLang at the bottom of the article, please run the query on the ‘wants to know month of the year’ for an example of a query that uses the ‘monthOfYear’ expression.
year: Rainbird uses the year function to extract the year from a date instance, either created by the end-user at runtime or that exists as an instance in the map. For example, if the input date is 1984-12-09, the year function would extract the year value, 1984.
The next couple of tabs will demonstrate how to create a map that will use the year function to determine what year an event happened.
Figure 21: Example rule using the year expression
First, create the following concepts:
- 1 string concept – ‘Person’
- 1 date concept – ‘Date’
- 1 number concept – ‘Year’ (you could also use a string concept to return the same result, but a string concept won’t allow the outcome of the expression to be used in further calculations).
Next, create relationships between the concepts, as shown in figure 22:
Figure 22: Concepts linked with relationships
- The main rule of the map sits on the ‘wants to know year‘ relationship (see Build the Rule: Figure 23).
- The ‘when did that happen‘ relationship will trigger the end-user to input a date.
Now that we have set up the map’s concepts and relationships, we can build the rule which the year expression will sit in. The rule will be built on the ‘wants to know year’ relationship and is made up of two components: one condition, and one expression.
The first condition to add to the rule, the relationship ‘when did that happen‘, will make Rainbird ask the user to enter a date at runtime. The object, %DATE, created by the ‘when did that happen‘ relationship, will then be used in the year expression.
Next, add the expression by clicking ‘Add Expression‘, and then adding ‘year(%DATE)‘ to the ‘Expression’ box. The expression tells Rainbird to extract the year from the %DATE object that was created by the previous condition. Assign the variable of the expression (the year value that will be extracted from %DATE) to %O, to display the extracted date as the query result (see Figure 24).
Figure 23: Creating the rule that uses the year expression
Now, if we were to run the query on the ‘wants to know year’ relationship, and input 2007-01-04 a Rainbird would return the result ‘2007’:
Figure 24: Outcome of query with 2007-01-04 as the input date
In the knowledge map generated by the RBLang at the bottom of the article, please run the query on the ‘wants to know year’ for an example of a query that uses the ‘year’ expression.
The RBLang below will generate the a map that uses the different expressions mentioned in the article. Click on ‘Export .rbird’ to download the knowledge map, or ‘copy RBLang’ and paste the code directly into Rainbird.
Query and Results
Each expression can be demoed in the knowledge map by running the query on the relationship mentioned at the end of each individual expression section in the article above.