• No results found

2.5.1 Analyzing a regional rail system

Consider the rail network of a small city. There are two lines, the airport line,al, between the city and the airport, and the suburban line,sl, between the city and the suburbs. The managers of the network want to determine the average time for a traveler in the suburbs to get to the airport.

The data is a sequence of event tuples of the formpline,station,timeq, wherelineP tal,sluindicates the railway line on which the event occurs,stationP tair,city,suburbu

indicates the station at which the train is stopped, andtimePRindicates the event time. We will first write the expressiontscwhich calculates the time needed to travel from the suburbs to the city. We define the predicatesϕsspdq “ pd.line“sl^d.station“suburbq andϕscpdq “ pd.line“sl^d.station“cityqwhich indicate that the suburban line train is at the suburban station and at the city station respectively. The first two expressions,

tsc-pickup“splitpp ϕssq˚ ÞÑ0,ϕssÞÑtime,p ϕscq˚¨ϕsc ÞÑ0,`q, and tsc-drop “splitpp ϕssq˚¨ϕss¨ p ϕscq˚ ÞÑ0,ϕscÞÑtime,`q

are both defined for streams of the formp ϕssq˚¨ϕss¨ p ϕscq˚¨ϕsc. The expressions return

the time at which the traveler was picked up from the suburbs and dropped off at the city centre respectively. Our goal was to express the commute time from the suburbs to the city:

tsc“tsc-drop´tsc-pickup.

We can similarly express the querytca for the commute time from the city to the airport. The total travel time from the suburbs to the airport is given by

tsa“splitptsc,tca,`q.

Finally, the expression:

tavg“iterptsa,avgq

2.5.2 Rolling data telephone plans

Now consider a simple telephone plan, where the customer has a 5 GB monthly download limit. She can potentially use more than this amount, for an additional fee at the end of the month. Otherwise, the unused quota is added to her next month’s limit, up to a maximum of 20 GB.

The data domainDtel“RY tMu, wheredPRindicates a download ofdgigabytes and

Mindicates the end of the billing cycle and payment of the telephone bill. Given the entire

browsing history of the customer,wPJpR

˚

¨Mq˚K, we wish to compute the download limit

for the current month.

The first expression, totalDown “ iterpd PR ÞÑ d,`q maps a sequence of downloads

wPR˚to the total data downloaded. The browsing history of a single month is given by the regular expressionrm“R˚¨M, and the expression

monthDown“splitptotalDown,MÞÑ0,`q

maps data streamswPJrmKto the total quantity of data downloaded.

The following expression gives the unused allowance available at the end of the month, in terms ofl0, the download limit at the beginning of the month:

unusedpl0q “ prm ÞÑl0q ´monthDown.

The value returned by unused may be negative, or larger than 20 GB, so the expression: rolloverpl0q “minpmaxpunusedpl0q,rm ÞÑ0q,rm ÞÑ20q

caps it to between 0 GB and 20 GB, and gives the download quota to be added to the next month’s limit.

The expression nextQuotapl0q “rolloverpl0q `5provides the download limit for the next month in terms ofl0and the current month’s browsing history. Finally, the expression

quota“ piterpnextQuotaÑl0qqrl0 :“ pr˚mÞÑ5qs

maps the entire browsing history of the customer to the download limit of the current month.

2.5.3 Aggregating weather reports

Our final example deals with a stream of weather reports. Let the data domain Dwth “

RY tautEqx,sprEqx,newYear, . . .u, where the symbols autEqxand sprEqx represent the autumn and spring equinoxes. A measurementdPRindicates a temperature ofd0C. We wish to compute the average winter temperature. For this query, let winter be defined as the time between an autumn equinox and the subsequent spring equinox.

Observe that the regular expressions rsumm “ R˚ ¨autEqx and rwinter “ R˚ ¨sprEqx respectively capture a sequence of temperature readings made before the start of winter and

the start of summer. Given a sequence of temperature readings for a year,wPJR

˚

¨autEqx¨ R˚¨sprEqxK, the expression

y“splitps,w,Yq, where s“rsummÞÑ H, and

w“splitpiterpdPRÞÑ tdu,Yq,sprEqxÞÑ H,Yq

returns the set of temperature measurements made in winter. The expression avgWinter“avgpiterpy,Yqq

encodes the ultimate query of interest: the average winter-time temperature reading.