⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 localcontrolleragent.java.svn-base

📁 這是一個JAVA語言寫的多代理人程式用來模擬飛機起飛或是降落的程式
💻 SVN-BASE
字号:
package agents;

import java.util.*;
import mock.*;

import world.*;
import base.*;
import interfaces.*;
import agents.PilotAgent;

public class LocalControllerAgent extends Agent implements LocalControllerInterface {
		
		List<Strip> strips;
		public Tower tower;
		public String name;
		public MockGroundController groundcontroller;
		
		public LocalControllerAgent(String newname, Tower newtower) {
			super();
			name  = newname; 
			tower = newtower;
			strips = new ArrayList<Strip>();
			tower.ctrlAssumeLocalControllerRole(this);
		}
		public LocalControllerAgent(String newname, Tower newtower, MockGroundController gc) {
			super();
			name  = newname; 
			tower = newtower;
			strips = new ArrayList<Strip>();
			tower.ctrlAssumeLocalControllerRole(this);
			groundcontroller = gc;
		}
		
		/* Messages
		 * Takeoff / Landing Scenario
		 * 
		 */
		
		// Take off scenario
		public void msgNewDepartureStrip(Strip departure) {
			print("Received new takeoff strip");
			strips.add(departure);
			stateChanged();
		}
		
		public void msgReadyForTakeoff(String callNumber, Runway currentPosition) {
			print("Received msgReadyForTakeoff");
			Strip temp_strip;
			if ((temp_strip = get_strip(callNumber)) != null)
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_Ready_For_Takeoff;
			stateChanged();
		}
		
		public void msgConfirmPositionAndHold(String callNumber) {
			// print("Received msgConfirmPositionAndHold");
			Strip temp_strip;
			if ((temp_strip = get_strip(callNumber)) != null)
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_Confirm_Position_And_Hold;
			stateChanged();
		}
		
		public void msgConfirmClearedForTakeoff(String callNumber,
				Runway takeoffRunway) {
			print("Received msgConfirmClearedForTakeoff");
			Strip temp_strip;
			if ((temp_strip = get_strip(callNumber)) != null)
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_CONFIRMED_CLEARED_TAKEOFF;
			stateChanged();
		}

		public void msgConfirmDepartureControl(String callNumber) {
			print("Received msgConfirmDepartureControl");
			Strip temp_strip;
			if ((temp_strip = get_strip(callNumber)) != null) {
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_Confirm_Departure_Control;
			}
			stateChanged();
		}

		// Landing scenario
		public void msgNewArrivingFlight(String callnumber, Runway runway, Gate gate) {
			// print("Received msgNewArrivingFlight " + callnumber);
			Strip strip    = new Strip();
			strip.callNum  = callnumber;
			strip.runway   = runway;
			strip.gate     = gate;
			strip.lc_flight_state = Strip.LCFlightState.LC_New_Arriving_Flight;
			strips.add(strip);
		}		
		

		public void msgNewArrivingFlight(Plane plane, String callNumber,
				Runway runway, Gate gate) {
			
			print("Received msgNewArrivingFlight " + callNumber);
			Strip strip    = new Strip();
			strip.callNum  = callNumber;
			strip.runway   = runway;
			strip.gate     = gate;
			strip.lc_flight_state = Strip.LCFlightState.LC_New_Arriving_Flight;
			System.out.println("Adding");
			strips.add(strip);
			System.out.println("Size of strips " + strips.size());
			stateChanged();
		}
		
		public void msgRequestToLand(String callNumber, String approach, Runway runway) {
			print("Received msgRequestToLand " + callNumber);
			Strip temp_strip;
			temp_strip = get_strip(callNumber);
			if(temp_strip == null) {
				System.out.println("Strip is null");
				System.out.println("Size of strips " + strips.size());
			} else {
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_Request_To_Land;
			}
			stateChanged();
		}
		
		public void msgConfirmClearanceToLand(String callNumber, Runway runway) {
			print("Received msgConfirmClearanceToLand");
			Strip temp_strip;
			if ((temp_strip = get_strip(callNumber)) != null)
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_CONFIRMED_CLEARANCE_TO_LAND;			
			stateChanged();
		}		

		public void msgConfirmLandingPath(String callNumber,
				List<Taxiway> path, GroundControllerInterface gc) {
			print("Received msgConfirmLandingPath");
			Strip temp_strip;		
			if ((temp_strip = get_strip(callNumber)) != null)
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_Confirm_Landing_Path;
			stateChanged();
		}

		/* Scheduler
		 * 
		 * 
		 */
		public boolean pickAndExecuteAnAction() {
			
			Strip temp_strip;
			if((temp_strip = get_strip(Strip.LCFlightState.NO_ACTION)) != null) {
					
			}
			// Take off schedules
			if((temp_strip = get_strip(Strip.LCFlightState.LC_Ready_For_Takeoff)) != null) {
				DoPositionAndHold(temp_strip.callNum);
				return true;
			}
			if((temp_strip = get_strip(Strip.LCFlightState.LC_Confirm_Position_And_Hold)) != null) {
				DoClearedForTakeOff(temp_strip.callNum);
				return true;
			}
			if((temp_strip = get_strip(Strip.LCFlightState.LC_CONFIRMED_CLEARED_TAKEOFF)) != null) {
				DoContactDepartureControl(temp_strip.callNum);
				return true;
			}			
			
			// Landing schedules
			if((temp_strip = get_strip(Strip.LCFlightState.LC_New_Arriving_Flight)) != null) {
				
				return true;
			}
			if((temp_strip = get_strip(Strip.LCFlightState.LC_Request_To_Land)) != null) {
				// System.out.println("doclearancetoland");
				DoClearanceToLand(temp_strip.callNum);
				return true;
			}
			if((temp_strip = get_strip(Strip.LCFlightState.LC_CONFIRMED_CLEARANCE_TO_LAND)) != null) {
				DoLandingPath(temp_strip.callNum);
				return true;
			}			
			if((temp_strip = get_strip(Strip.LCFlightState.LC_Confirm_Landing_Path)) != null) {
				// DoContactDepartureControl(temp_strip.callNum);
				return true;
			}	
			return false;
		}
		
		/* Actions
		 * 
		 */
		
		// Actions for Take off Scenario 
		public void DoPositionAndHold(String callNumber) {
			Strip tempstrip;
			tempstrip = get_strip(callNumber);
			tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
			print("Sending pilot msgPositionAndHold");
			getPilot(callNumber).msgPositionAndHold();
			stateChanged();
		}
		
		public void DoClearedForTakeOff(String callnum) {
			/*
			Strip tempstrip;
			tempstrip = get_strip(callnum);
			tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
			Runway takeoffRunway = new Runway();
			print("Sending pilot msgClearedForTakeOff");
			
			getPilot(callnum).msgClearedForTakeoff(takeoffRunway);
			stateChanged();
			*/
			//print("doClearedForTakeoff");
			Runway takeoffRunway = new Runway();
			if(!World.runways_exist()) {
				Strip tempstrip;
				tempstrip = get_strip(callnum);
				tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
				Runway runway = new Runway();
				// print("Sending pilot msgNotClearedToLand");
				getPilot(callnum).msgPositionAndHold();
				stateChanged();
			} else {
				Strip tempstrip;
				
				tempstrip = get_strip(callnum);
				boolean runwayisoccupied = false;
				print("Size of strips: " + strips.size());
				/*
				for(Strip strip : strips) {
					// System.out.println("Strip 1: " + strip.callNum + " Strip 2: " + tempstrip.callNum);
					//System.out.println("Plane pos " + strip.callNum + " " + World.get_plane(strip.callNum).objectState.position.y + 
						//	"Runway pos: " + tempstrip.runway.objectState.position.y);
					print(strip.callNum);
					if((strip.callNum != tempstrip.callNum)) {
						// These pilots have the same runway
						System.out.println("Plane pos: " + World.get_plane(strip.callNum).objectState.position.y + 
								"Runway pos: " + tempstrip.runway.objectState.position.y);
						if(World.get_plane(strip.callNum).objectState.position.y == tempstrip.runway.objectState.position.y-20) {
							print("Sending pilot msgNotClearedToLand");
							getPilot(callnum).msgPositionAndHold();
							runwayisoccupied = true;
							break;
						}
					}
				}
				*/
				/*
				for(WorldObject worldobj : World.world_objects) {
					if(worldobj instanceof Plane) {
						if(worldobj != World.get_plane(callnum)) {
							if(worldobj.objectState.position.y == tempstrip.runway.objectState.position.y) {
								((PilotAgent)getPilot(callnum)).msgPositionAndHold();
								runwayisoccupied = true;
							}
						}
					}
				}
				*/
				
				if(!runwayisoccupied) {
					tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
					print("Sending pilot msgClearedForTakeoff");
					getPilot(callnum).msgClearedForTakeoff(takeoffRunway);
					stateChanged();
				}
			}
		}
		
		public void DoContactDepartureControl(String callnum) {
			Strip tempstrip;
			tempstrip = get_strip(callnum);
			tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
			TRACON departureControl = new TRACON();
			print("Sending pilot msgContactDepartureControl");
			getPilot(callnum).msgContactDepartureControl(departureControl);
			stateChanged();
		}
		
		// Actions for Landing Scenario
		public void DoClearanceToLand(String callnum) {
			
			if(!World.runways_exist()) {
				Strip tempstrip;
				tempstrip = get_strip(callnum);
				tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
				Runway runway = new Runway();
				// print("Sending pilot msgNotClearedToLand");
				getPilot(callnum).msgNotClearedToLand();
				stateChanged();
			} else {
				Strip tempstrip;
				boolean runwayisoccupied = false;
				tempstrip = get_strip(callnum);
				for(Strip strip : strips) {
					// System.out.println("Strip 1: " + strip.callNum + " Strip 2: " + tempstrip.callNum);
					if((strip.callNum != tempstrip.callNum)&&(strip.runway == tempstrip.runway)) {
						// These pilots have the same runway
						if(checkPlaneOnRunway(World.get_plane(strip.callNum),World.get_plane(tempstrip.callNum))) {
							// print("Sending pilot msgNotClearedToLand");
							getPilot(callnum).msgNotClearedToLand();
							runwayisoccupied = true;
						}
					}
				}
				if(!runwayisoccupied) {
					tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
					print("Sending pilot msgClearanceToLand");
					getPilot(callnum).msgClearanceToLand(tempstrip.runway);
					stateChanged();
				}
			}
		}
		
		public void DoLandingPath(String callnum) {
			Strip tempstrip;
			tempstrip = get_strip(callnum);
			tempstrip.lc_flight_state = Strip.LCFlightState.NO_ACTION;
			List<Taxiway> paths = null; // quick hack to get paths to work
			print("Sending pilot msgLandingPath");
			getPilot(callnum).msgLandingPath(paths, World.get_ground_controller(tower));
			print("Sending ground controller arrival strip");
			if(World.get_ground_controller(tower) == null ) {
				groundcontroller.msgNewArrivalStrip(tempstrip);
			} else {
				World.get_ground_controller(tower).msgNewArrivalStrip(tempstrip);
			}
			stateChanged();
		}
		
		public void DoNewLandingStrip(Strip newstrip) {
			print("Sending ground controller msgNewArrivalStrip");
			World.get_ground_controller(tower).msgNewArrivalStrip(newstrip);
			strips.remove(newstrip);
		}
		
		// 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.LCFlightState state) {
			synchronized(strips) {
			try {
			for (Strip strip : strips)
				if (strip.lc_flight_state == state)
					return strip;
			} catch(ConcurrentModificationException e) {
				// e.printStackTrace();
			}
			return null;
			}
		}
		
		public PilotInterface getPilot(String callnumber) {
			Plane plane; 
			plane = World.get_plane(callnumber);
			return plane.radio();
			
		}
		
		public boolean checkPlaneOnRunway(Plane plane, Runway runway) {
			System.out.println("Plane position: " + plane.getObjectState().get_position());
			System.out.println("Runway position: " + runway.getObjectState().get_position().x);
			// If the plane's x is less than the x position of the runway then 
			// the plane is still on the runway
			if(plane.getObjectState().get_position().x < runway.getObjectState().get_position().x) {
				return true;
			} else {
				return false;
			}
		}
		
		public boolean checkPlaneOnRunway(Plane plane, Plane checkplane) {
			// System.out.println("Plane 1: " + plane.getName() + " " + plane.getObjectState().get_position().y +
			//		" Plane 2: " + checkplane.getName() + " "+ checkplane.getObjectState().get_position().y);
			if(plane.getObjectState().get_position().y < checkplane.getObjectState().get_position().y) {
				return false;
			} else if (plane.getObjectState().get_position().y > checkplane.getObjectState().get_position().y) {
				return false;
			} else {
				return true;
			}
		}
		
		/* Non normative scneario Messages*/
		public void msgEmergencyLanding(String callNumber, String approach,
				Runway runway) {
			Strip temp_strip;
			if ((temp_strip = get_strip(callNumber)) != null)
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_Emergency_Landing;
			stateChanged();
		}


		public void msgNotReadyToRequestLanding(String callNumber) {
			Strip temp_strip;
			if ((temp_strip = get_strip(callNumber)) != null)
				temp_strip.lc_flight_state = Strip.LCFlightState.LC_Not_Ready_To_Request_Landing;
			stateChanged();
		}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -