PHP Rules
Rule Example: Determine whether an Airline Passenger is Suitable for Upgrade
In this example, we determine whether a given airline passenger is eligible to be upgraded from their
coach seat to a first-class seat. To be eligible, a passenger must:
- be in economy class now and either
- hold a Gold card or
- hold a Silver card
- and their carry-on luggage must be less than or equal to 15.0 pounds.
To determine this, we must compare a passenger's facts with our rule.
The Rule
We fetch the rule from our RuleBase, which, for simplicity's sake, is merely a text file with a .rul extension.
Its contents are in Reverse Polish Notation (to handle precedence):
# Rule establishing when an airline passenger may be
# allowed to upgrade his/her seat.
passengerIsEconomy IS true
passengerIsGoldCardHolder IS true
passengerIsSilverCardHolder IS true
OR
AND
passengerCarryOnBaggageAllowance EQUALS 15.0
passengerCarryOnBaggageWeight EQUALS 10.0
LESSTHANOREQUALTO
AND
The Rule Context:
We get facts pertaining to a Rule from our FactBase, which again is simply a text file. Note the difference between
a sql.con file and a txt.con file.
# Format of this file is:
# Rule_Element_Type|Rule_Element_Name|SQL_Statement|Expected_Return_Data_Type_from_SQL_Query
p|passengerIsEconomy|SELECT if(((SELECT p.class FROM swindlen_airlines.passengers p WHERE p.id = ? ) = 'economy'), 1, 0);|boolean
p|passengerIsGoldCardHolder|SELECT if(((SELECT p.card_type FROM swindlen_airlines.passengers p WHERE p.id = ? ) = 'gold'), 1, 0);|boolean
p|passengerIsSilverCardHolder|SELECT if(((SELECT p.card_type FROM swindlen_airlines.passengers p WHERE p.id = ? ) = 'silver'), 1, 0);|boolean
v|passengerCarryOnBaggageWeight|SELECT b.weight FROM swindlen_airlines.passengers p, swindlen_airlines.baggage b WHERE b.passenger_id = p.id and p.id = ?;|double
v|passengerCarryOnBaggageAllowance|SELECT v.value FROM swindlen_airlines.variables v WHERE v.name = 'passengerCarryOnBaggageAllowance';|double
The Resulting Proposition:
Proposition statement = ( ( passengerCarryOnBaggageWeight <= passengerCarryOnBaggageAllowance ) AND ( ( passengerIsSilverCardHolder OR passengerIsGoldCardHolder ) AND passengerIsEconomy ) ), value = FALSE
Page executed in 0.1272 seconds.