這是一個JAVA語言寫的多代理人程式用來模擬飛機起飛或是降落的程式
源代码在线查看: clearancedeliveryagent.java.svn-base
package agents;
import java.util.*;
import world.*;
import base.*;
import interfaces.*;
public class ClearanceDeliveryAgent extends Agent implements ClearanceDeliveryInterface {
List strips;
Tower tower;
String name;
public ClearanceDeliveryAgent(String newname, Tower newtower) {
super();
name = newname;
tower = newtower;
strips = new ArrayList();
}
/* Messages
* Takeoff / Landing Scenario
*
*/
// Take off scenario
public void msgNewDepartingFlight(Plane plane, String callNumber, Runway runway, Gate gate, String destination, PilotInterface p) {
print("Received msgNewDepartingFlight");
Strip strip = new Strip(callNumber);
strip.runway = runway;
strip.gate = gate;
strip.cd_flight_state = Strip.CDFlightState.NO_ACTION;
strips.add(strip);
print("New strip: " + strip.toString());
stateChanged();
}
public void msgRequestingClearance(String callNumber, Gate gate) {
print("Received msgRequestingClearance");
Strip tempstrip;
tempstrip = get_strip(callNumber);
tempstrip.cd_flight_state = Strip.CDFlightState.REQUESTING_CLEARANCE;
stateChanged();
}
public void msgConfirmClearance(String callNumber, GroundControllerInterface groundController) {
print("Received msgConfirmClearance");
Strip tempstrip;
tempstrip = get_strip(callNumber);
tempstrip.cd_flight_state = Strip.CDFlightState.CONFIRM_CLEARANCE_GRANTED;
stateChanged();
}
public void msgConfirmWeather(String callNumber) {
print("Received msgConfirmWeather");
Strip tempstrip;
tempstrip = get_strip(callNumber);
tempstrip.cd_flight_state = Strip.CDFlightState.CONFIRM_WEATHER;
stateChanged();
}
/* Scheduler
*
*
*/
public boolean pickAndExecuteAnAction() {
Strip temp_strip;
if((temp_strip = get_strip(Strip.CDFlightState.NO_ACTION)) != null) {
}
// Take off schedules
if((temp_strip = get_strip(Strip.CDFlightState.REQUESTING_CLEARANCE)) != null) {
DoClearanceGranted(temp_strip.callNum);
return true;
}
if((temp_strip = get_strip(Strip.CDFlightState.CONFIRM_CLEARANCE_GRANTED)) != null) {
DoSendWeather(temp_strip.callNum, temp_strip);
return true;
}
if((temp_strip = get_strip(Strip.CDFlightState.CONFIRM_WEATHER)) != null) {
DoConfirmWeather(temp_strip);
return true;
}
return false;
}
/* Actions
*
*/
// Actions for Take off Scenario
public void DoClearanceGranted(String callnum) {
Strip temp_strip;
print("Sending pilot msgClearanceGranted");
List points = null; // hack to get waypoint to work
temp_strip = get_strip(callnum);
temp_strip.cd_flight_state = Strip.CDFlightState.NO_ACTION;
getPilot(callnum).msgClearanceGranted(points, World.get_ground_controller(tower));
stateChanged();
}
public void DoSendWeather(String callnum, Strip strip) {
print("Sending ground controller msgNewDepartureStrip");
Strip temp_strip;
Weather report = new Weather();
temp_strip = get_strip(callnum);
temp_strip.cd_flight_state = Strip.CDFlightState.NO_ACTION;
// world.get_ground_controller(tower).msgNewDepartureStrip(strip);
World.get_ground_controller(tower).msgNewDepartureStrip(strip);
getPilot(callnum).msgSendWeather(report);
stateChanged();
}
public void DoConfirmWeather(Strip strip) {
strip.cd_flight_state = Strip.CDFlightState.NO_ACTION;
stateChanged();
}
// Subroutines
public Strip get_strip(String cn) {
for (Strip strip : strips)
if (strip.callNum.equals(cn))
return strip;
return null;
}
public Strip get_strip(Strip.CDFlightState state) {
if(strips.size() > 0) {
for (Strip strip : strips)
if (strip.cd_flight_state == state)
return strip;
}
return null;
}
public PilotInterface getPilot(String callnumber) {
Plane plane;
plane = World.get_plane(callnumber);
return plane.radio();
}
}