Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. '''
  2. Created on Mar 14, 2017
  3.  
  4. @author: Denis
  5. '''
  6.  
  7. from domain.Graph import GraphException
  8. import time
  9.  
  10. from copy import deepcopy
  11.  
  12.  
  13. class UI:
  14.     '''
  15.    classdocs
  16.    '''
  17.  
  18.     def __init__(self, graph):
  19.         self._graph = graph
  20.  
  21.     @staticmethod
  22.     def readUserCommand():
  23.         return input("Enter the command : ")
  24.  
  25.     @staticmethod
  26.     def getVertices(graph):
  27.         s = "The number of vertices is "
  28.         s += str(graph.numberOfVertices())
  29.         return s
  30.  
  31.     @staticmethod
  32.     def edgeXY(graph):
  33.         x = int(input("x = "))
  34.         y = int(input("y = "))
  35.         if graph.isEdge(x, y) == True:
  36.             return "There is edge between {} and {}".format(x, y)
  37.         else:
  38.             return "There is no edge between {} and {}".format(x, y)
  39.  
  40.     @staticmethod
  41.     def in_out_degree(graph):
  42.         x = int(input("Enter the vertex : "))
  43.         s = "The in-degree for node {} is {}\n".format(x, graph.inDegree(x))
  44.         s += "The out-degree for node {} is {}".format(x, graph.outDegree(x))
  45.         return s
  46.  
  47.     @staticmethod
  48.     def outboundEdges(graph):
  49.         x = int(input("x = "))
  50.         s = graph.parseNout(x)
  51.         return "The outbound edges for {} are:\n {}".format(x, s)
  52.  
  53.     @staticmethod
  54.     def inboundEdges(graph):
  55.         x = int(input("x = "))
  56.         s = graph.parseNin(x)
  57.         return "The inbound edges for {} are:\n {}".format(x, s)
  58.  
  59.     @staticmethod
  60.     def costEdges(graph):
  61.         x = int(input("Enter x : "))
  62.         y = int(input("Enter y : "))
  63.         return "The cost between {} and {} is {}".format(x, y, graph.getTheCost(x, y))
  64.  
  65.     @staticmethod
  66.     def removeEdge(graph):
  67.         x = int(input("Enter x : "))
  68.         y = int(input("Enter y : "))
  69.         graph.removeEdge(x, y)
  70.         return "Done!"
  71.  
  72.     @staticmethod
  73.     def addEdge(graph):
  74.         x = int(input("Enter x : "))
  75.         y = int(input("Enter y : "))
  76.         cost = int(input("Enter cost : "))
  77.         graph.addEdge(x, y, cost)
  78.         return "Done!"
  79.  
  80.     @staticmethod
  81.     def modifyCost(graph):
  82.         x = int(input("Enter x : "))
  83.         y = int(input("Enter y : "))
  84.         newCost = int(input("Enter the new cost : "))
  85.         graph.modifyTheCost(x, y, newCost)
  86.         return "Done!"
  87.  
  88.     @staticmethod
  89.     def removeVertex(graph):
  90.         x = int(input("Enter the vertex to be deleted : "))
  91.         graph.removeVertex(x)
  92.         return "Done!"
  93.  
  94.     @staticmethod
  95.     def addVertex(graph):
  96.         x = int(input("Enter the vertex to be added : "))
  97.         graph.addVertex(x)
  98.         return "Done!"
  99.  
  100.     @staticmethod
  101.     def makeCopy(graph):
  102.         graphS = deepcopy(graph)
  103.         graphS._edgeProperty = deepcopy(graph._edgeProperty)
  104.         print("Copy made!")
  105.         return graphS
  106.  
  107.     def mainMenu(self):
  108.         commands = {
  109.             "1": UI.getVertices, "2": UI.edgeXY, "3": UI.in_out_degree,
  110.             "4": UI.outboundEdges, "5": UI.inboundEdges, "6": UI.costEdges,
  111.             "7": UI.modifyCost, "8": UI.addEdge, "9": UI.removeEdge,
  112.             "10": UI.addVertex, "11": UI.removeVertex}
  113.         menu = UI.getMenu()
  114.  
  115.         while True:
  116.             print(menu)
  117.             command = UI.readUserCommand()
  118.             if command == "0":
  119.                 break
  120.             if command == "12":
  121.                 b = UI.makeCopy(self._graph)
  122.             else:
  123.                 try:
  124.                     if command in commands:
  125.                         print(commands[command](self._graph))
  126.                     else:
  127.                         print("Invalid command!")
  128.                 except GraphException as msg:
  129.                     print(msg)
  130.                 except:
  131.                     print("Erro")
  132.  
  133.     @staticmethod
  134.     def getMenu():
  135.         s = "Available commands:\n"
  136.         s += "\t 1 - get the number of vertices\n"
  137.         s += "\t 2 - search if there is any edge between x and y\n"
  138.         s += "\t 3 - get the in-degree and out-degree of a vertex\n"
  139.         s += "\t 4 - outbound edges of a vertex\n"
  140.         s += "\t 5 - inbound edges of a vertex\n"
  141.         s += "\t 6 - get the cost between two vertices\n"
  142.         s += "\t 7 - modify the cost between two vertices\n"
  143.         s += "\t 8 - add an edge\n"
  144.         s += "\t 9 - remove an edge\n"
  145.         s += "\t 10 - add an vertex\n"
  146.         s += "\t 11 - remove an vertex\n"
  147.         s += "\t 12 - make a copy of the graph\n"
  148.         s += "\t 0 - exit"
  149.         return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement