Logo BCDDLogo CSC
BCDDBehavioural Contract-Driven Design
version 1.0-beta
ITEN

Chapter 22 — Time in formulas

The detail g2a states that the light switches off 90 seconds after the last press, with a 5-second tolerance. The keywords of chapter 21 are not enough to express it: they establish in which steps a condition holds, but do not measure the time that passes between two steps. This chapter introduces the time limit, which measures the time that follows a step. It then introduces the forms that concern what has already occurred in the trace: the elapsed time, the count and the previous value. Finally it introduces repeating scenarios.

Within a certain time: WITHIN

The time limit is written with WITHIN followed by a duration, and is added to EVENTUALLY or to WHENEVER:

Form Meaning
EVENTUALLY p WITHIN d p holds in some step within d of the start of the scope
WHENEVER p THEN q WITHIN d in every step in which p holds, q must hold within d of the trigger step, the step itself included

The duration always has a unit, s or ms. With WITHIN the guarantee has a deadline: if the consequence does not hold within the duration, the guarantee is violated. WITHIN and AT ONCE do not go in the same guarantee (error E27).

For a formula without WHILE the start of the scope is the first step of the trace. With WHILE the scope may consist of several separate stretches of the trace: each stretch is a scope of its own, and the duration runs from the start of each stretch. WHILE modo = in_pulizia: EVENTUALLY avviso WITHIN 5 s therefore requires a warning in every cleaning stretch, within 5 s of its start. A stretch that ends without a warning before 5 s have elapsed violates the guarantee.

In the staircase light CSC a detail of guarantee g4 is added: a cleaning request receives a response within one second.

  GUARANTEE g4a DETAILS g4: WHENEVER requested(Pulizie) THEN accepted(Pulizie) OR rejected(Pulizie) WITHIN 1 s

The elapsed time: TIME SINCE

TIME SINCE e measures the elapsed time since the last step in which the event e occurred. TIME SINCE premuto <= 95 s holds in the steps in which the last press dates back no more than 95 seconds.

In the very step in which e occurs, TIME SINCE e is 0. Before e has occurred at least once its value is infinite: TIME SINCE premuto > 90 s is true even if nobody has ever pressed, and TIME SINCE premuto <= 95 s is false. Infinity is used only in comparisons, not in calculations. To exclude the case in which the event has never occurred, a condition such as TIME SINCE premuto >= 85 s is combined with the count COUNT(premuto) > 0, which holds only if the event has already occurred.

The count: COUNT

The count COUNT(e) is the number of steps in which the event e has occurred, from the start of the trace up to and including the step considered. COUNT(premuto) > 0 holds from the step of the first press onwards.

COUNT(e SINCE f) counts only the steps with e that follow the last step in which the event f occurred. COUNT(premuto SINCE avviso) = 0 holds in the steps in which nobody has pressed since the last warning.

COUNT(e) IN LAST d counts only the steps in which e occurred in the interval of duration d that ends with the step considered. The step considered is included; the step exactly d away is not. In the CSC Comando of chapter 13 the button provides the command accendi. Its guarantee c1 establishes that at least 0.2 seconds pass between two commands. Written as a formula, the guarantee establishes that in every interval of 0.2 seconds no more than one command is issued:

  GUARANTEE c1a DETAILS c1: ALWAYS COUNT(accendi) IN LAST 0.2 s <= 1

In formulas decimal numbers are written with a point. Two commands exactly 0.2 seconds apart satisfy the guarantee: when the second arrives, the first is already outside the interval.

The duration of IN LAST consists only of the first value that follows the keyword: a number with a unit, a name, a call or an expression in parentheses. No operator of the table in chapter 21 extends it. The formula c1a therefore reads (COUNT(accendi) IN LAST 0.2 s) <= 1. A calculated duration is written in parentheses: COUNT(e) IN LAST (2 * d).

In the report the line c1a is formal:

1. ERRORS: 0

2. DEGREE OF THE CONTRACTUAL LINES
…
  line 11: GUARANTEE c1a formal

The filter on events: WITH

An event with parameters can be filtered on the value of its parameters: e WITH c occurs in the steps in which e occurs and its parameters satisfy the condition c. The same applies to the phases of a request, with the parameters of the request. Guarantee g4 states that a cleaning request longer than two hours is rejected; the requested duration is the parameter durata of the request Pulizie:

  GUARANTEE g4b DETAILS g4: WHENEVER (requested(Pulizie) WITH durata > 7200 s) THEN rejected(Pulizie) WITHIN 1 s

Two hours are written 7200 s, because the only units are s and ms. The condition of WITH extends to the end of the expression: for this reason the filtered event is written in parentheses when something else follows it, as in TIME SINCE (premuto WITH dove = primo) > 5 s.

The previous value: PREV

The previous value PREV(x) is the value of the state x in the previous step. In the first step it is the initial value declared with INITIAL.

PREV is needed because in a step a state has the value it takes after the events of the step (chapter 21). Guarantee g7 states that during cleaning a press does not change the mode. The condition “during cleaning” concerns the mode before the press:

  GUARANTEE g7a DETAILS g7: WHENEVER premuto AND PREV(modo) = in_pulizia THEN modo = in_pulizia AT ONCE

Whenever premuto occurs and in the previous step the mode was in_pulizia, in the same step the mode is still in_pulizia.

PREV also identifies the step in which a state changes value: PREV(modo) = accesa AND modo = spenta holds in the step in which the light switches off.

The switch-off and the warning as formulas

The detail g2a becomes:

  GUARANTEE g2a DETAILS g2 STATUS HYPOTHESIS: ALWAYS ((PREV(modo) = accesa AND modo = spenta) IMPLIES (COUNT(premuto) > 0 AND TIME SINCE premuto >= 85 s AND TIME SINCE premuto <= 95 s))

It reads: in every step in which the light passes from on to off, there has been at least one press and since the last one between 85 and 95 seconds have elapsed. The 5-second tolerance is expressed by the two limits.

Three observations on the formula:

In scenario s2 the light switches off at instant 30: in step @30 s the formula requires at least 85 seconds since the last press, and 30 have elapsed. The scenario violates g2a, and is therefore rejected, as its line EXPECT REJECTED declares.

Guarantee g3, “shortly before switching off, the light gives a warning”, receives a formal detail:

  GUARANTEE g3a DETAILS g3: ALWAYS ((PREV(modo) = accesa AND modo = spenta) IMPLIES (COUNT(avviso) > 0 AND COUNT(premuto SINCE avviso) = 0))

In every step in which the light switches off there has been at least one warning, and nobody has pressed after the last warning. A press after the warning, therefore, prevents the announced switch-off. How long before the switch-off the warning must arrive is not expressed by the detail and remains in the text in words of g3.

Repeating scenarios: REPEAT

A scenario can end with steps that repeat. The REPEAT line stands among the steps and applies until the end of the scenario: the steps that follow it repeat at intervals of duration d.

Form Meaning
REPEAT m..n EVERY d the steps that follow repeat from m to n times
REPEAT FOREVER EVERY d the steps that follow repeat without end

Each repetition begins d after the previous one, and the times @t of the repeated steps are measured from the start of the repetition. In the following scenario someone climbing slowly presses the button every 60 seconds, from one to five times, and the light is on after every press:

  SCENARIO s5 EXPECT ADMITTED
    REPEAT 1..5 EVERY 60 s
    @0 s     premuto    modo = accesa

The scenario is equivalent to five scenarios, with one, two, three, four and five presses 60 seconds apart. bcdd checks the form of the repetition and reports error E30 in the following cases:

The order of the times (error E12, chapter 9) is checked among the repeated steps, with the times measured from the start of the repetition.

The CSC and the report

The header comment becomes:

-- VERSION 0.3: g1, g2 and g2a written as formulas; g3a, g4a, g4b, g7a and g8 added. Pending approval.

The report shows eight formal lines:

$ bcdd check UsoLuce.csc
bcdd check UsoLuce.csc
lines: 57 (recognised 47, comments 5, blank 5, unrecognised 0)
blocks: 1
  line 4: CSC UsoLuce VERSION 0.3, STATUS HYPOTHESIS

1. ERRORS: 0

2. DEGREE OF THE CONTRACTUAL LINES
  verbal: 7
  formal: 8
  declared extension: 0
  unrecognised: 0
  line 16: GUARANTEE g1 formal
  line 18: GUARANTEE g2 formal
  line 20: GUARANTEE g2a formal
  line 21: GUARANTEE g2b verbal
  line 23: GUARANTEE g3 verbal
  line 25: GUARANTEE g3a formal
  line 26: GUARANTEE g4 verbal
  line 28: GUARANTEE g4a formal
  line 29: GUARANTEE g4b formal
  line 30: GUARANTEE g5 verbal
  line 32: GUARANTEE g6 verbal
  line 34: GUARANTEE g7 verbal
  line 36: GUARANTEE g7a formal
  line 37: GUARANTEE g8 formal
  line 38: ASSUME a1 verbal
…
14. TO BE JUDGED: 5
  line 21: GUARANTEE g2b DETAILS g2: consistency between the line and the line that details it, one of the two in words
  line 25: GUARANTEE g3a DETAILS g3: consistency between the line and the line that details it, one of the two in words
  line 28: GUARANTEE g4a DETAILS g4: consistency between the line and the line that details it, one of the two in words
  line 29: GUARANTEE g4b DETAILS g4: consistency between the line and the line that details it, one of the two in words
  line 36: GUARANTEE g7a DETAILS g7: consistency between the line and the line that details it, one of the two in words

15. CHECKS NOT EXECUTED: 4
  scenarios: lines 41, 46, 49, 51, 54
  coverage of the triggers: lines 16, 18, 20, 25, 28, 29, 36, 37
  consistency of assumptions and guarantees: lines 16, 18, 20, 25, 28, 29, 36, 37
  consistency of the details: line 20

outcome: no errors

Section 15 now lists five scenarios, including s5, and eight formal lines for trigger coverage and for consistency. The entry consistency of the details concerns g2a, which is a formal detail of a formula, g2. The consistency between the two lines is a check that bcdd does not execute. The formal details of a guarantee in words, such as g3a, g4a, g4b and g7a, are instead in section 14, because consistency with a line in words is judged.

Summary

Special cases

The value after the press. If in g7a modo = in_pulizia is written in place of PREV(modo) = in_pulizia, the formula becomes: whenever there is a press and, after the press, the mode is in_pulizia, the mode is in_pulizia. The formula is always satisfied and expresses no guarantee. Even a press that takes the mode from in_pulizia to accesa, forbidden by g7, satisfies the formula. The form, names and types are correct, and bcdd, which does not evaluate formulas, does not detect the defect:

1. ERRORS: 0

2. DEGREE OF THE CONTRACTUAL LINES
…
  line 36: GUARANTEE g7a formal
…

Light that never switches off. g2a is evaluated in the step of the switch-off. If in a trace the light stays on until the end, g2a has no step in which to apply. That the switch-off does not happen is a case covered by g2: “in a later step the light is off”.

Previous value in the first step. In the first step PREV(modo) is spenta, the value declared with INITIAL. A press in the first step therefore has PREV(modo) = spenta, and g7a does not concern it.

Minutes and durations without a unit. The units of durations are only s and ms, as in scenarios and in named constants: 90 seconds are written 90 s, and not 1.5 min. A number without a unit after WITHIN is not a duration, and bcdd reports error E25, inconsistent type:

1. ERRORS: 1
  line 28: [E25] inconsistent type: Duration needed, Integer found

A repeated step as long as the period. In scenario s5, a second repeated step @60 s would fall at the same instant as the next repetition:

1. ERRORS: 1
  line 57: [E30] the step @60 s is not shorter than the period of REPEAT: every repeated step has a time smaller than EVERY 60 s