예제 #1
0
def main():
    flightGraph = Graph()
    with open('map_data.json') as map_data_file:    
        map_data = json.load(map_data_file)
    for city_data in map_data["metros"]:
        flightGraph.addNode(city_data)
    for route_data in map_data["routes"]:
        success = flightGraph.addDualEdge(route_data)
        if success == False:
            print("There is no such cities in our route network: " + route_data["ports"][0] + " , " + route_data["ports"][1])
    keep_running = True
    while keep_running:
        print("Welcome to CSAir! What can I do for you?")
        print("1. City Info")
        print("2. City Connection")
        print("3. Info about CSAir")
        print("4. View Routes on Map")
        print("5. Edition")
        print("6. Save info into json")
        print("7. Design your own routes")
        print("8. Find shortest Route between two cities")
        print("9. Expanding route network")
        print("10. Exit")
        response = raw_input()
        if response == "1":
            cityInfo(flightGraph)
        elif response == "2":
            cityConnection(flightGraph)
        elif response == "3":
            csAirInfo(flightGraph)
        elif response == "4":
            visualizeMap(flightGraph)
        elif response == "5":
            mapEdition(flightGraph)
        elif response == "6":
            logToJson(flightGraph)
        elif response == "7":
            routesInfo(flightGraph)
        elif response == "8":
            findShortestPath(flightGraph)
        elif response == "9":
            expansionCsAirRoute(flightGraph)
        elif response == "10":
            exit()
        else:
            print("Incorrect Input, Please try again")
예제 #2
0
    def testAll(self):
        graph = Graph()
        self.assertEqual(graph.continent, [], "Incorrect Continent Initialization")
        self.assertEqual(graph.nodes, [], "Incorrect Node Initialization")
        self.assertEqual(graph.edges, [], "Incorrect Edge Initialization")
        self.assertFalse(graph.addDualEdge(flight1Data), "Build route between inexistent city")
        graph.addNode(city1Data)
        graph.addNode(city2Data)
        graph.addNode(city3Data)
        graph.addNode(city4Data)
        graph.addNode(city5Data)
        graph.addNode(city6Data)
        graph.addNode(city7Data)
        graph.addNode(city8Data)
        self.assertEqual(len(graph.continent), 4, "Unmatched number of continents")
        self.assertEqual(len(graph.nodes), 8, "Unsuccessful node addition")
        self.assertTrue(graph.addDualEdge(flight1Data))
        self.assertTrue(graph.addDualEdge(flight2Data))
        self.assertTrue(graph.addDualEdge(flight3Data))
        self.assertEqual(graph.routeInfo(graph.edges[0], 1), [1227, 368.09999999999997, 2.936])
        self.assertEqual(graph.shortestPath("IST", "BGW").distanceFromStartVertex, 1612, "Incorrect Shortest Path method, simplest test")

        self.assertTrue(graph.removeRoute(flight2Data["ports"][0], flight2Data["ports"][1]))
        self.assertTrue(graph.addSingleEdge(flight2Data))
        self.assertFalse(graph.addSingleEdge(flight2Data))
        self.assertEqual(len(graph.edges), 6, "Unsuccessful edge addition")
        self.assertEqual(len(graph.nodes[0].get_edgeIn()), 2, "Unequal edgeIn count")           
        self.assertEqual(len(graph.nodes[0].get_edgeOut()), 2, "Unequal edgeOut count")
        self.assertEqual(graph.findRoute("ABC", "CDE"), None)
        self.assertEqual(graph.nodes[1].get_edgeIn(), [graph.findRoute(city1Data["code"], city2Data["code"]), graph.findRoute(city3Data["code"], city2Data["code"])], "Different outgoing cities")
        
        graph.addDualEdge(flight4Data)
        graph.addDualEdge(flight5Data)
        graph.addDualEdge(flight6Data)
        graph.addDualEdge(flight7Data)
        graph.addDualEdge(flight8Data)
        graph.addDualEdge(flight9Data)
        graph.addDualEdge(flight10Data)
        self.assertEqual(graph.flightDistance(), ["\nSAO - MAD: distance 8373\nMAD - SAO: distance 8373", "\nALG - MAD: distance 726\nMAD - ALG: distance 726", "2858.8"], "Incorrect output from flightDistance method")
        self.assertEqual(graph.cityPopulation(), ["SAO", "ALG", "10206250"], "Incorrect output from cityPopulation method")
        self.assertEqual(graph.hubCity(), "CAI", "Incorrect output from hugCity method")
        self.assertFalse(graph.removeCity("ABC"), "cityCode does not exist")
        self.assertEqual(graph.shortestPath("BGW", "SAO").distanceFromStartVertex, 12065, "Incorrect Shortest Path method, second test")
        
        self.assertTrue(graph.removeCity("SAO"), "Fail on city removal")
        self.assertEqual(len(graph.edges), 16, "removeCity without removing edges")   
        self.assertEqual(len(graph.continent), 3, "continent should not be removed")

        graph.addNode(city9Data)
        graph.addNode(city10Data)
        graph.addDualEdge(flight11Data)
        print(graph.routeInfo(graph.edges[17], 1))
        self.assertEqual(graph.routeInfo(graph.edges[17], 1), [343, 102.89999999999999, 2.0])
        self.assertTrue(graph.removeCity("LOS"), "Fail on city removal")
        self.assertEqual(len(graph.continent), 3, "removeCity without removing continent") 

        self.assertTrue(graph.removeCity("ALG"), "Fail on city removal")
        self.assertEqual(graph.shortestPath("BGW", "SAO"), Null, "disconnected city")