Operator - Faculty of Computer Science

An Algebraic
Representation of
Calendars
*
X. Sean Wang
Department of Computer Science
The University of Vermont
*Joint work with Ning Peng (North Carolina State U.)
and S. Jajodia (George Mason U.)
From where?!
Vermont
Viridis Montis
Univ. of VM
Or
UVM
1
Motivation

Calendar units, or time granularities, are used in
many systems



Time granularities should be defined formally, and
symbolically, in order to facilitate formal and
automatic reasoning



Day, week, year,…
Academic year, holidays, fiscal year, business days
All granularities are related to each other
The relationship is what we need to encode
Time granularities should be defined intuitively in
order to encourage users to defined them directly

Many time granularities are specific to specific users, and a
general system cannot accommodate all these possibilities.
Goal

A simple set of algebraic operators that work
on granularities



Expressive
Intuitive
Yield efficient implementation
2
Outline





Motivation and goals
Basic definitions
Algebraic operators
Computational considerations
Time granule conversion



A demo
Related work
Conclusion
Basic definition


Linear time flow
Time granularity
Each non-empty G(i) is called a granule.
3
Operator - grouping

Group(G, startindex, num_of_grans)


Start at an granule, group every num_of_grans of
granules into one granule.
Week=Group (Day, 0, 7)
Operator - altering ticks



Periodically expand/shrink by a few “sub-ticks”
from a “tick”
Useful: add a day to February for leap years
G=Alter(day, week, 2, -1, 3): in 2nd week, shrink
out 1 day, and do this for every 3 weeks.
4
More on altering tick operation

Group(G, 1, k) = Alter(G, G, 1, k-1, 1)


add k-1 ticks for every tick
Alter(second, day, i, 1, ∞)

add 1 second to a particular day i (the idea of leap
seconds)
Shifting operation

Shift(G, m)

Semantics: shift the index set

EastTime_Hour = Shift (GMT_Hour, 5).
5
Combine operator

Combine(G1, G2): combine all the granules of G1
into one granule if they are contained within one
granule of G2

Business_month=Combine(Business_day,
Month)
Anchored grouping operator

The anchored grouping operation Anchoredgroup(G1,G2) generates a new granularity G’ by
combining all the granules of G1 that are
between two granules of G2 into one granule of
G’.

Example:

FiscalYear = Anchored-group(month, October)
6
Subset operator

G" = Subset(G, m, n) generates a new
granularity G" by taking all the granules of G
whose labels are between m and n.

FutureYears = Subset(year, 2009, ∞)
Select operators

Select-down: Select-down(G1,G2,k,l) selects
granules of G1 by picking up l granules starting
from the kth one in each set of granules of G1
contained in one granule of G2.

Sunday = Select-down (day, week, 1, 1)
7
Select operators


The select-up operation Select-up(G1,G2)
generates a new granularity G" by selecting the
granules of G1 that contain one or more
granules of G2.
FirstWeekOfMonth = Select-up(FirstDayOfMonth, week)
Select operators

Select-by-intersect operation: For each granule G2(i),
there may exist a set of granules of G1, each granule
intersecting G2(i).

The operation Select-by-intersect (G1,G2, k, l) selects
granules of G1 by selecting l granules starting from the
kth one in all such sets, generating a new granularity G".
(If k is negative, count from the end of the set.)

LastWeekOfSemester =
Select-by-intersect (week, Semester, -1, 1)
8
Set operations

A granularity is a set of granules

The new granularity is the union, difference,
intersection of the input granules

Condition: If two granules from the two operands,
respectively, are non-disjoint (considering the
underlying time), then they must be the same
Example
9
create calendar cal with day;
add gran week into cal with
group (day, 1, 7);
add gran year into cal with
group (month, 1, 12);
add gran ps_month into cal with
alter ( day,
alter ( day,
alter ( day,
alter ( day,
alter ( day, group (day, 1, 31), 2, -3, 12),
4, -1, 12),
6, -1, 12),
9, -1, 12),
11, -1, 12);
/* leap year not considered yet */
add gran month into cal with
alter ( day,
alter ( day,
alter ( day, ps_month, 2+12*3, 1, 12 * 4),
2+12*99, -1, 12 * 100),
2 + 12 * 399, 1, 12 * 400);
add gran monday into cal with
select_down (day, week, 1, 1);
add gran tuesday into cal with
select_down (day, week, 2, 1);
…
add gran weekday into cal with
select_down (day, week, 2, 5);
add gran weekend into cal with
union (sunday, saturday);
Day 1 starts a year and a week… which was true in Year 1.
Note…Unix “cal” program tells differently…
which we believe is due to different leap year accounting…
Calendar

A calendar is a set of granularities that are
defined over a single granularity using
algebraic operations

“Bottom” granularity
10
Expressiveness - preliminary

Group into:


G groups into G’ if each granule of G’ is a union of a set of
granules of G.
Periodical group into:

G groups periodically into H if G groups into H and there
exist positive integers R and P, where R is less than the
number of granules of H, such that


(1) for index i of H, i+R is a label of H if there is an index of H
that is greater than or equal to i+R, and
(2) for each index i of H,


if H(i) = union of G(jr) r=1, .., k, and H(i+R) is a non-empty granule
of H
then H(i+R) = union of G(jr+P) r=1, .., k
Expressiveness - preliminary

Example: G groups periodically into H (with
P=7, R=2)
11
Expressiveness - preliminary

Finite granularity:


Given a bottom B granularity
A granularity G defined over B is finite if the
number of granules in G is finite.
Expressiveness

The calendar algebra can represent all the
granularities that are either periodical or finite
w.r.t. the bottom granularity.

Can express a bit more?


Yes, if we allow ∞ in the altering tick operation
In this case, a notion of semi-periodic may be
needed to allow finite arbitrary “beginning
segment” for a granularity followed by a periodic
infinite segment (in terms of time line).
12
Computational considerations

Two aspects:

Well-definedness of operators: operands for some
operations must be well-behaved



Example: in Alter(G1, G2, …), G1 and G2 should satisfy
some restrictions between them.
Otherwise… Alter(week, month, 1, 1, 4): add a week to
the 1st month of every 4 months. What does this mean?
Maintain index to granule mapping

For conversion operations
Syntactic restrictions

The numbers appearing in the operators can be
tricky


Simple example: group(day, 1) gives back day.
Slightly more complicated:


alter(group(day, 2), 1, -1, 1) gives back day, too
More desirable

Figure out if the operand granularities are ok by just looking
at the operators used to define the operand granularities
(from the bottom granularity)
13
Some preliminary notions

“Group into” relationship


“Finer than” relationship




“G1 finer than G2” if each granule of G1 is fully contained
(taken as a set of time values) within a granule of G2
E.g. Weekday (union of Monday, Tuesday, …, Friday) is finer
than Day, but doesn’t group into Day
E.g. Day group into weekdays, but not finer.
Partition = “group into” + “finer than”


As defined earlier
If G’=Group(G, i, k), then G partitions G’
Sub-granularity

“G1 is a sub-granularity of G2”: if the granules of G1 is a
subset of the granules of G2
Full-integer labeled granularity

Slightly change the granularity definition

Original:



New:



Index set = the integers
If i<j both “point” to non-empty granules, then every integer
i<=k<=j should too.
Label set = any subset of the integers
If labels i<j both “point” to non-empty granules, then label k,
i<=k<=j, should too.
Full-integer labeled: the label set is the integers

This is the original definition
14
Label aligned sub-granularity


Given: G1 is a sub-granularity of G2
G1 is a label aligned sub-granularity of G2 if



The label set L1 for G1 is a subset of the label set L2 for
G2, and
Each label i in L1∩L2 points to the same granule in G1 and
G2.
Example:


firstDayofMonth=Select_down(day, month, 1, 1)
FirstDayofMonth is a label aligned sub-granularity of day (if
we simply keep the labels of day granules)
Examining each operator

Group: no requirement


Altering tick: the first operand granularity
must partition the second operand


If second operand is full-integer labeled, then output
is too.
Shift: no requirement


Input partitions output
Resulting granularity partitions operand, and vice
versa
Combine: no requirements.

The first operand partitions the resulting granularity
15
Examining each operator

Anchored-group



Subset: no requirement


Example: Anchored-group(month, October)
Requirement: second granularity is a label aligned
sub-granularity of the first
Result is a label aligned sub-granularity of the input
Select operators (down, up, intersect): no
requirements

Result is a label aligned sub-granularity of the first
input
Examining each operator

Set operators: there should be a common
granularity G such that both operands are
label-aligned sub-granularities of G


Example: union(Saturday, Sunday)
The result is also a label-aligned sub-granularity
of G
16
Syntactic restrictions
Syntactic restrictions

Layer 1: start with full-integer labeled, bottom
granularity


Layer 2: select some granules from the first layer


Every granularity will be full-integer labeled
Every granularity will be a label-aligned sub-granularity of
the first layer granularities
Layer 3: combine some granules from the second
layer

Most flexible and difficult to deal with
17
Granule conversion

Examples:






Find the week day of a particular day
Find the the first Monday of a particular month
Find the last day of a particular fiscal year
Find the moon phase of a particular day
**with the algebra help, the conversions can be pretty
exotic…such as the first business day after Christmas in
2008
How to talk about a “particular” granule of a
granularity?


Its label
Relative labels


(year, month, day): (2008, 5, 26)
Year is absolute label, month and day are relative numbers
Granule conversion

Given a source granularity and a target granularity



Given their relative label schemes



Source: (2008, 5, 26)
Find the granule in the target


Source: (year, month, day)
Target: (week, day)
Given a granule in the source granularity in its relative label
scheme


Source: Day
Target: Day
Result: (n, 1)
Note… the relationship between the source and target
granules can be of different kinds:



Target contains source or the other way around
Or they may simply overlap with each other
Multiple result granules may be possible
18
Down and up conversions

There is always a common ancestor granularity



that both the source and target granularities can be defined
on with no other granularities needed (intermediary not
counted).
Bottom granularity is one but not necessary the
computationally efficient one to use
Granularity conversion is split into two steps

Convert the source granule to a set of granules in the
common ancestor granularity


Convert down
Then convert these granules to the target granule(s)

Convert up
Conversion computation




Basic idea: recursively keep track of the granules
The common ancestor granularity (CAG) must group
into both source and target.
When in first layer, we don’t need to keep track of all
the granules in the conversion process. Instead, we
derive a first and a last granules in the common
ancestor granularity that are “used”.
Basically, follow the algebra definition



Altering tick is a little tricky, but still easy enough
Layer 2 and 3 granularities are more complicated
The number of granules to keep track can be large
19
Example conversion


Convert (year:2008, month:5, day:26) to (week, day)
Step 1: down convert year:2008 to month granules



Pick out the 5th granule
Since year=group(month, 12) and the CAG is month, this is easy
Step 2, down convert the resulting month granule to day
granules.


Pick out the 26th granule
Day is the CAG, but month is involved, a little tricky.

Step 3, convert day granule to target granularity.

Step 4, up convert day to week



This is a no-operation as the target is the same as source
This is easy as week=group(day, 1, 7): output week label
Count day of the week: output the number
Demo


A java program by Peng Ning in 1998.
Published on sourceforge.org
20
Related work

Past

Definition of granularities




B. Leban, D. McDonald, and D. Foster. A representation for
collections of temporal intervals. In Proceedings of AAAI,
pages 367–371, 1986.
M. Niezette and J. Stevenne. An efficient symbolic
representation of periodic time. In Proceedings of CIKM,
pages 161–168, 1992.
Jef Wijsen. A string-based model for infinite granularities. In
AAAI-2000 Workshop on Spatial and Temporal Granularities,
pages 9–16, July 2000.
Conversion


TSQL 92 has “scale” and “cast” operations
Efficiency problem studied by the TSQL92 group
Related work

More recent

XML encoding of algebraic expressions



University of Arizona group
University of Vermont group
Conversion of algebra expressions (in XML form)
into:

Periodical enumeration of intervals (U. Milan)


Into Java programs (U. Arizona)


For logical reasoning of time in multiple granularities
For incorporating granularities into application programs
Automaton-based approach

University of Udini, Italy.
21
Conclusion – take aways

Algebraic definition sits between the user and the
system



Only a few operations are needed for “everyday”
use



Relatively intuitive, and
Efficient implementation exists
Altering tick is most complicated, but arguably intuitive
Syntactic restriction is a way to “tame” the algebra
expression to behave in a good way, and yield to
more efficient implementation
Various applications of calendar algebra should still
be interesting in temporal reasoning and other timerelated systems.
Contact information
[email protected]
http://www.cs.uvm.edu/~xywang
Thank you
22