8. Comprehensive Code Examples & Case Studies
8.1 Example 1: Flight Duty Period (FDP) Limits
This module models European Aviation Safety Agency (EASA) / FAA Flight Duty Period limits based on departure time and number of sectors (flight legs).
// ====================================================================
// MODULE: EASA Flight Duty Period (FDP) Regulations
// ====================================================================
CONSTANTS:
%fdp_base_limit% = 13:00;
%fdp_night_limit% = 11:00;
ENDCONSTANTS
// Determine if the duty starts during the circadian low (02:00 - 05:59)
%is_circadian_low_start% =
duty.%start_utc% >= 02:00 and duty.%start_utc% <= 05:59;
// Calculate allowable FDP reduction based on sector count
%sector_penalty% =
if count(leg(duty)) > 2 then
(count(leg(duty)) - 2) * 00:30
else
00:00;
// Calculate net max FDP allowed for the duty
%max_allowed_fdp% =
if %is_circadian_low_start% then
%fdp_night_limit% - %sector_penalty%
else
%fdp_base_limit% - %sector_penalty%;
// Hard Constraint: Actual FDP must not exceed max allowed FDP
CONSTRAINT easa_fdp_limit OF Duty
COMMENT: "Duty elapsed time must not exceed max allowable FDP"
STATUS: ON;
RULE:
duty.%elapsed_time% <= %max_allowed_fdp%;
ENDCONSTRAINT
8.2 Example 2: Minimum Sit Time & Rest Requirements
This module enforces sit times between consecutive legs within a duty and rest periods between consecutive duties.
// ====================================================================
// MODULE: Rest and Sit Time Legality
// ====================================================================
CONSTANTS:
%min_sit_time% = 00:45;
%min_rest_period% = 12:00;
ENDCONSTANTS
// Constraint 1: Minimum sit time between consecutive legs in a duty
CONSTRAINT min_sit_time_check OF Duty
COMMENT: "Connect time between consecutive flights must be at least 45 mins"
STATUS: ON;
RULE:
for each f1 -> f2 in elements
f2.departure_time - f1.arrival_time >= %min_sit_time%;
ENDCONSTRAINT
// Constraint 2: Minimum rest period between consecutive duties in a trip
CONSTRAINT min_inter_duty_rest OF Trip
COMMENT: "Rest period between consecutive duties must meet regulatory minimum"
STATUS: ON;
RULE:
all(duty(trip),
prev(duty(trip), duty.%end_time%) = 00:00 or
(duty.%start_time% - prev(duty(trip), duty.%end_time%)) >= %min_rest_period%
);
ENDCONSTRAINT
8.3 Example 3: Complex Multi-Duty Crew Pairing Cost Model
This example demonstrates a complete commercial crew pairing specification, incorporating multi-duty guarantees, TAFB (Time Away From Base) credits, soft penalties, and home-base closure checks.
// ====================================================================
// MODULE: Comprehensive Crew Pairing Specification
// ====================================================================
CONSTANTS:
%hourly_rate% = 75.00;
%base_airport% = "SAW";
%min_daily_guarantee% = 05:00;
ENDCONSTANTS
// --------------------------------------------------------------------
// 1. Structural Base Closure Rule (Illegal Subchain / Valid Conditional)
// --------------------------------------------------------------------
CONSTRAINT trip_must_start_and_end_at_base OF Trip
COMMENT: "Pairings must originate and terminate at home base SAW"
STATUS: ON;
RULE:
valid is_closed;
first(leg(trip), leg.%dep_airport%) = %base_airport% and
last(leg(trip), leg.%arr_airport%) = %base_airport%;
ENDCONSTRAINT
// --------------------------------------------------------------------
// 2. Duty Level Cost Computation
// --------------------------------------------------------------------
%duty_flight_time% = sum(leg(duty), leg.%block_time%);
%duty_elapsed_time% = duty.%end_time% - duty.%start_time%;
PROPERTY cost OF Duty
RULE:
max(
%duty_flight_time% * %hourly_rate%,
%min_daily_guarantee% * %hourly_rate%
);
ENDPROPERTY
// --------------------------------------------------------------------
// 3. Trip Level Cost Computation (TAFB vs. Sum of Duties)
// --------------------------------------------------------------------
%tafb_duration% = last(leg(trip), leg.%arr_time%) - first(leg(trip), leg.%dep_time%);
%tafb_credit_pay% = (%tafb_duration% / 3.5) * %hourly_rate%;
%sum_duty_costs% = sum(duty(trip), duty.cost);
// Soft penalty for 4-leg duties
%soft_penalty% =
sum(duty(trip), if count(leg(duty)) >= 4 then 150.00 else 0.00);
PROPERTY cost OF Trip
RULE:
max(%sum_duty_costs%, %tafb_credit_pay%) + %soft_penalty%;
ENDPROPERTY