17.2.3 Vehicles
The |
|---|
// VEHICLES
// The class that represents all self-propelled devices
define abstract class <vehicle> (<physical-object>)
// Every vehicle has a unique identification code
slot vehicle-id :: <string>, required-init-keyword: id:;
// The normal operating speed of this class of vehicle in miles per hour
each-subclass slot cruising-speed :: <positive-integer>;
// Allow individual differences in the size of particular aircraft,
// while providing a suitable default for each class of aircraft
each-subclass slot standard-size :: <size>;
end class <vehicle>;
define method initialize (vehicle :: <vehicle>, #key)
next-method();
unless (slot-initialized?(vehicle, physical-size))
vehicle.physical-size := vehicle.standard-size;
end unless;
end method initialize;
define method say (object :: <vehicle>) => ()
format-out("Vehicle %s", object.vehicle-id);
end method say;
// This class represents companies that fly commercial aircraft
define class <airline> (<object>)
slot name :: <string>, required-init-keyword: name:;
slot code :: <string>, required-init-keyword: code:;
end class <airline>;
define method say (object :: <airline>) => ()
format-out("Airline %s", object.name);
end method say;
// This class represents a regularly scheduled trip for a commercial
// airline
define class <flight> (<object>)
slot airline :: <airline>, required-init-keyword: airline:;
slot number :: <nonnegative-integer>,
required-init-keyword: number:;
end class <flight>;
define method say (object :: <flight>) => ()
format-out("Flight %s %d", object.airline.code, object.number);
end method say;
// This class represents vehicles that normally fly for a portion of
// their trip
define abstract class <aircraft> (<vehicle>)
slot altitude :: <integer>, init-keyword: altitude:;
// Direction here is either #"inbound" or #"outbound"
slot direction :: <symbol>;
// The next step this aircraft might be able to make
slot next-transition :: <aircraft-transition>,
required-init-keyword: transition:, setter: #f;
end class <aircraft>;
define method initialize (vehicle :: <aircraft>, #key)
next-method();
// There is a one-to-one correspondance between aircraft instances and
// transition instances
// An aircraft can only make one transition at a time
// Connect the aircraft to its transition
vehicle.next-transition.transition-aircraft := vehicle;
end method initialize;
// The next step an aircraft might be able to make
define class <aircraft-transition> (<object>)
slot transition-aircraft :: <aircraft>, init-keyword: aircraft:;
slot from-container :: <vehicle-storage>, init-keyword: from:;
slot to-container :: <vehicle-storage>, init-keyword: to:;
// The earliest possible time that the transition could take place
slot earliest-arrival :: <time-of-day>, init-keyword: arrival:;
// Has this transition already been entered in the sorted sequence?
// This flag saves searching the sorted sequence
slot pending? :: <boolean> = #f, init-keyword: pending?:;
end class <aircraft-transition>;
// Describes one step of an aircraft's movements
define method say (transition :: <aircraft-transition>) => ()
say(transition.earliest-arrival);
format-out(": ");
say(transition.transition-aircraft);
format-out(" at ");
say(transition.to-container);
end method say;
// Commercial aircraft are aircraft that may have a flight
// assigned to them
define abstract class <commercial-aircraft> (<aircraft>)
slot aircraft-flight :: false-or(<flight>) = #f, init-keyword: flight:;
end class <commercial-aircraft>;
define method say (object :: <commercial-aircraft>) => ()
let flight = object.aircraft-flight;
if (flight)
say(flight);
else
format-out("Unscheduled Aircraft %s", object.vehicle-id);
end if;
end method say;
// The class that represents all commericial Boeing 707 aircraft
define class <B707> (<commercial-aircraft>)
inherited slot cruising-speed, init-value: 368;
inherited slot standard-size,
init-value: make(<size>, length: 153, width: 146, height: 42);
end class <B707>;
define method say (aircraft :: <B707>) => ()
if (aircraft.aircraft-flight)
next-method();
else
format-out("Unscheduled B707 %s", aircraft.vehicle-id);
end if;
end method say;
|
In the preceding code, we model everything from the most general class of vehicle down to the specific class that represents the Boeing 707. We also model the transition steps that an aircraft may take as it travels throughout the airport, and the airlines and flights associated with commercial aircraft.




