Engine

3.5. Preconditions🔗

A precondition says when an event may happen: on picks the events it applies to, requires is a condition on the history before the event. This one allows a minibar charge only after check-in:

    - on: ChargePosted where charge_type = 'Minibar'
      requires: ever ReservationCheckedIn

A minibar charge on a folio that has only been opened is rejected: A minibar charge on a folio that is not checked in is rejected.

🔗structure
Spec.Pre : Type
Spec.Pre : Type

on: S requires: C

Spec.Pre.mk
on : Selector

the events it applies to; it sees only the incoming event

requires : CondExpr

checked on the history before the incoming event

3.5.1. The incoming event🔗

A precondition can read the event being appended: $field is its field, now() its time, and X(field) picks the earlier events of type X whose field equals the incoming one. The account's $amount > 0 and the shift's EmployeeAssigned(employee_id) use these; a person is assigned to a shift once because the count of earlier assignments of that employee_id must equal the count of earlier unassignments:

      - on: EmployeeAssigned
        requires: count(EmployeeAssigned(employee_id)) = count(EmployeeUnassigned(employee_id)) and count(EmployeeAssigned) - count(EmployeeUnassigned) < (last of ShiftCreated).staff_needed

Assigning Anna twice is rejected (Assigning the same person twice is rejected. ), and so is assigning Ben to a shift that needs one person and has Anna (Assigning a second person to a full shift is rejected. ).

X(f1, ..., fn) correlates on several fields at once: the events of type X that agree with the incoming event on every listed field. Each field must exist on X and on the incoming event, with one type, and be listed once. A person is assigned to a shift at most once:

    - on: PersonAssigned
      requires: never PersonAssigned(shift_id, person_id)

3.5.2. What a precondition guarantees🔗

What holds of the stored events: a precondition is satisfied by every picked event when each event its on picks had its requires on the history before it. A store's preconditions held when each was accepted by every event in its lifetime. On the shift, every EmployeeUnassigned in the store had a matching earlier EmployeeAssigned, because rule 5 accepted it:

Unassigning someone who is not on the shift is rejected.

3.5.3. Examples🔗

A charge can only be voided once it was posted and not yet voided:

    - on: ChargeVoided
      requires: ever ChargePosted(charge_id) and never ChargeVoided(charge_id)
chargeVoidedText · checked against the spec ·

Charge voided precondition loads without errors.

Nothing but a reopening is allowed on a closed folio:

    - on: any except FolioReopened
      requires: not (last of [FolioClosed, FolioReopened] is FolioClosed)
folioReopenedText · checked against the spec ·

Closed folio precondition loads without errors.

At most 3 failed logins an hour: the fourth is allowed, the fifth rejected (The fourth failed login in an hour is allowed and the fifth is rejected. ):

    - on: LoginFailed
      requires: at most 3 LoginFailed within 1 hour

A session may start only after recent activity:

    - on: SessionStarted
      requires: (last of (any except ReminderSent)).at > now() - 90 days
sessionStartedText · checked against the spec ·

Recent activity precondition loads without errors.

A charge needs a reservation that is not cancelled, which is another entity's events (in):

    - on: ChargePosted
      requires: never ReservationCancelled in reservation((last of FolioOpened).reservation_id)
reservationCancelledText · checked against the spec ·

Cancelled reservation precondition loads without errors.