% user-initiated events need_car(Who,Where) :- print(Who, " is requesting a reservation in ", Where), make_reservation(Who,Where). ready_for_trip(Who,Where) :- print(Who, " is ready to pick up a car in ", Where), pick_up_car(Who,Where). trip_cancelled(Who,Where) :- print(Who, " is requesting reservation cancellation in ", Where), cancel_reservation(Who,Where). trip_completed(Who,Where) :- print(Who, " is returning car to ", Where), return_car(Who,Where). % rental agency events order_cars(Where,How_many) :- print(How_many," cars ordered for ",Where), deliver_cars(Where,How_many),!. transfer_cars(From,To,Requested_number) :- print("Request for ",Requested_number," car transfer from ",From," to ",To), car_inventory(From,On_lot), car_reservations(From,Reserved), Available_for_transfer is On_lot - Reserved, Available_for_transfer >= Requested_number, adjust_car_inventory(From,-Requested_number), adjust_car_inventory(To,Requested_number), print(Requested_number," car(s) transferred from ",From," to ",To),!. transfer_cars(From,To,Requested_number) :- print(From," - requested number of cars are not available"). % car manufacturer events deliver_cars(Where,How_many) :- print(How_many," cars shipped for ",Where), receive_cars(Where,How_many),!. % rental agency events make_reservation(Who,Where) :- car_available(Where), assert(reservation(Who,Where)), print("A car is reserved for ",Who," in ", Where), adjust_car_reservations(Where, 1),!. make_reservation(Who,Where) :- print(Who, " - Sorry, a car is not available in ",Where). cancel_reservation(Who,Where) :- reservation(Who,Where), retract(reservation(Who,Where)), print("Car reservation is cancelled for ",Who," in ",Where), adjust_car_reservations(Where, -1),!. cancel_reservation(Who,Where) :- print(Who, "you did not make a reservation in ",Where). pick_up_car(Who,Where) :- reservation(Who,Where), retract(reservation(Who,Where)), assert(car_out(Who,Where)), adjust_car_inventory(Where, -1), adjust_car_reservations(Where, -1), print(Who," - Your car is ready."),!. pick_up_car(Who,Where) :- print(Who, " - you do not have a reservation in ",Where). return_car(Who,Where) :- car_out(Who,Where), retract(car_out(Who,Where)), adjust_car_inventory(Where,1), print(Who," - Return OK, please pay the invoice"),!. return_car(Who,Where) :- print(Who, " - The car you are returning to ",Where, " is not ours"). % car rental service events car_available(Where) :- car_inventory(Where,Cars_available), car_reservations(Where,Cars_reserved), Cars_available > Cars_reserved,!. receive_cars(Where,Qty_received) :- print(Qty_received," cars received at ",Where), adjust_car_inventory(Where,Qty_received),!. adjust_car_inventory(Where, Adjustment) :- car_inventory(Where,Old_inventory), New_inventory is Old_inventory + Adjustment, retract(car_inventory(Where,Old_inventory)), assert(car_inventory(Where,New_inventory)),!. adjust_car_reservations(Where, Adjustment) :- car_reservations(Where,Old_reservations), New_reservations is Old_reservations + Adjustment, retract(car_reservations(Where,Old_reservations)), assert(car_reservations(Where,New_reservations)),!. % sample facts for testing car_inventory(atlanta,2). car_inventory(chicago,3). car_reservations(atlanta,0). car_reservations(chicago,0). % sample test test:- need_car(jim,chicago), need_car(julie,atlanta), ready_for_trip(jim,chicago), ready_for_trip(julie,atlanta), trip_completed(julie,atlanta), ready_for_trip(bill,chicago), trip_completed(jim,chicago), % pp car_inventory, order_cars(atlanta,2), % pp car_inventory, transfer_cars(atlanta,chicago,1).