def get_information(file_path): # using target_MDG and a benchmark, create result """ Execute Each algorithm for given file and return TurboMQ, cohesion, and coupling :param file_path: A path of dot file :return: Clustering result - value of TurboMQ, A list of [Cohesion, Coupling], A list of result clusters """ targetMDG = make_target_MDG(file_path) methods = ['WCA', 'HC', 'WCA_HC', 'SA', 'WCA_SA', 'PSO', 'WCA_PSO'] clusters_set = [] TMQ = [] cohe_coup = [] print("====WCA start====\n") clusters_set.append(WCA(targetMDG)) print("====WCA end====\n\n") print("====HC start====\n") clusters_set.append(HC.HC(targetMDG)) print("====HC end====\n\n") print("====WCA_HC start====\n") clusters_set.append(HC.WCA_HC(targetMDG, WCA(targetMDG))) print("====WCA_HC end====\n\n") print("====SA start====\n") clusters_set.append(SA.SA(targetMDG)) print("====SA end====\n\n") print("====WCA_SA start====\n") clusters_set.append(SA.WCA_SA(targetMDG, WCA(targetMDG))) print("====WCA_SA end====\n\n") print("====PSO start====\n\n") clusters_set.append(PSO.PSO(targetMDG)) print("====PSO end====\n\n") print("====WCA_PSO start====\n\n") clusters_set.append(PSO.WCA_PSO(targetMDG, WCA(targetMDG))) print("====WCA_PSO end====\n\n") # get TMQ data for clusters in clusters_set: TMQ.append(TurboMQ.calculate_fitness(clusters, targetMDG)) cohe_coup.append(TurboMQ.get_cohesion_coupling(clusters, targetMDG)) # write result files for i in range(len(methods)): DotParser.write_file(file_path, methods[i], clusters_set[i], targetMDG) return TMQ, cohe_coup, clusters_set
def make_target_MDG(file_path): """ Make MDG based on given file :param file_path: A path of dot file :return: MDG graph of given dot file """ dot_file = DotParser.read_and_render('test/' + file_path) # print(dot_file.source) # get edge information and parse it edges = DotParser.parser(dot_file, "->") # second argument should be "--" or "->" (depends on .dot file format) # make dependency graph and set feature vector targetMDG = MDG.MDG(edges) # print(targetMDG.edges) targetMDG.set_feature_vector() return targetMDG
def main(): parser = argparse.ArgumentParser(description='Modularize given dot file') parser.add_argument('file_path', metavar='F', type=str, nargs='+', help='File path for dot file') parser.add_argument( '-a', help= 'Algorithm for modularization. All, WCA, HC, WCA_HC, SA, WCA_SA, PSO, WCA_PSO' ) args = parser.parse_args() file_path = args.file_path if args.a: modularizeMethod = args.a else: modularizeMethod = 'All' for single_file in file_path: targetMDG = MakeResult.make_target_MDG(single_file) clusters = None if modularizeMethod == 'WCA': clusters = WCA(targetMDG) elif modularizeMethod == 'HC': clusters = HC.HC(targetMDG) elif modularizeMethod == 'WCA_HC': clusters = HC.WCA_HC(targetMDG, WCA(targetMDG)) elif modularizeMethod == 'SA': clusters = SA.SA(targetMDG) elif modularizeMethod == 'WCA_SA': clusters = SA.WCA_SA(targetMDG, WCA(targetMDG)) elif modularizeMethod == 'PSO': clusters = PSO.PSO(targetMDG) elif modularizeMethod == 'WCA_PSO': clusters = PSO.WCA_PSO(targetMDG, WCA(targetMDG)) elif modularizeMethod == 'All': MakeResult.print_result(single_file) if modularizeMethod != 'All': DotParser.write_file(single_file, modularizeMethod, clusters, targetMDG)
def __init__(self, infile): self.Parser = DotParser() self.Graph = self.Parser.readFile(infile) self.Analysis = GraphAnalysis(self.Graph) self.State = NBODY
class JumanG: def __init__(self, infile): self.Parser = DotParser() self.Graph = self.Parser.readFile(infile) self.Analysis = GraphAnalysis(self.Graph) self.State = NBODY def outputToTikz(self, graph, outfile): TW.tikGraph(graph, outfile) def runRadial(self): return RD.radialAssign(self.Graph) def runNBody(self, useRadialSeed=True): if useRadialSeed: return NB.reposition(self.runRadial(), False) else: return NB.reposition(self.Graph, True) def runCirco(self): outputFile = "a.png" call(["circo", "-Tpng", self.Parser.FileName, "-o", outputFile]) call(["eog", outputFile]) return self.Graph def runTopDown(self): return TD.arrange(self.Graph) def chooseSolver(self): (numberOfNodes, numberOfEdges, numberOfLeaves) = self.Analysis.numberOfNodesEdgesAndLeaves() rootNodes = self.Analysis.getRootNodes() numberOfRootNodes = len(rootNodes) hasCycles = True #If it is an acyclic graph, we can run topdown if self.Graph.Type == 1 and rootNodes: hasCycles = self.Analysis.BFS(rootNodes[0], True) if not hasCycles: topoList = self.Analysis.topologicalSort(True) treeDepth = topoList[-1][1] print "numberOfNodes", numberOfNodes, "treeDepth", treeDepth if numberOfNodes > pow(3, treeDepth) and numberOfRootNodes == 1: self.State = RADIAL return self.State else: self.State = TOPDOWN return self.State #If we have an acyclic graph that has no roots. Its some form of ring. elif self.Graph.Type == 1: self.State = CIRCO return self.State else: # undirected graph # Metric for graph: connectedness: #nodes/#edges, < 3 connectedness = self.Graph.getNumEdges() / float(numberOfNodes) print connectedness if connectedness < 3: #radial -> nbody self.State = NBODY else: #nbody random self.State = NBODY_JITTER return self.State def runChosenSolver(self, choice=None): if choice == None: choice = self.State #print "TEST",choice, CIRCO if choice == NBODY: return self.runNBody() if choice == NBODY_RADIAL: return self.runNBody(True) if choice == NBODY_JITTER: return self.runNBody(False) if choice == TOPDOWN: return self.runTopDown() if choice == RADIAL: return self.runRadial() if choice == CIRCO: return self.runCirco() return self.runNBody() '''return { NBODY:self.runNBody(), NBODY_RADIAL:self.runNBody(True), NBODY_JITTER:self.runNBody(False), TOPDOWN:self.runTopDown(), RADIAL:self.runRadial(), CIRCO:self.runCirco(), #NBODYJ:self.runNBody(False), }.get(choice,self.runNBody()) ''' def printChoice(self): return { NBODY: "NBODY", NBODY_RADIAL: "NBODY_RADIAL", NBODY_JITTER: "NBODY_JITTER", TOPDOWN: "TOPDOWN", RADIAL: "RADIAL", CIRCO: "CIRCO", }.get(self.State, "???")
return count def numberOfNodesEdgesAndLeaves(self): nodes = len(self.Graph.AdjacencyList) edges = 0 leafCount = 0 for node in self.Graph.AdjacencyList: edgeList = self.Graph.AdjacencyList[node] if not edgeList: leafCount += 1 else: edges += len(edgeList) return (nodes, edges, leafCount) if __name__ == "__main__": parser = DotParser() filename = argv[1] parser.readFile(filename) graph = parser.Graph analysis = GraphAnalysis(graph) print graph arbitraryRoot = graph.getNodeList()[0] analysis.BFS(arbitraryRoot) analysis.printDependencyTable print "ROOTS:"
class JumanG: def __init__(self, infile): self.Parser = DotParser() self.Graph = self.Parser.readFile(infile) self.Analysis = GraphAnalysis(self.Graph) self.State = NBODY def outputToTikz(self, graph, outfile): TW.tikGraph(graph, outfile) def runRadial(self): return RD.radialAssign(self.Graph) def runNBody(self, useRadialSeed = True): if useRadialSeed: return NB.reposition(self.runRadial(),False) else: return NB.reposition(self.Graph,True) def runCirco(self): outputFile = "a.png" call(["circo","-Tpng",self.Parser.FileName,"-o",outputFile]) call(["eog",outputFile]) return self.Graph def runTopDown(self): return TD.arrange(self.Graph) def chooseSolver(self): (numberOfNodes,numberOfEdges,numberOfLeaves) = self.Analysis.numberOfNodesEdgesAndLeaves() rootNodes = self.Analysis.getRootNodes() numberOfRootNodes = len(rootNodes) hasCycles = True #If it is an acyclic graph, we can run topdown if self.Graph.Type==1 and rootNodes: hasCycles = self.Analysis.BFS(rootNodes[0],True) if not hasCycles: topoList = self.Analysis.topologicalSort(True) treeDepth = topoList[-1][1] print "numberOfNodes",numberOfNodes,"treeDepth",treeDepth if numberOfNodes > pow(3,treeDepth) and numberOfRootNodes==1: self.State = RADIAL return self.State else: self.State = TOPDOWN return self.State #If we have an acyclic graph that has no roots. Its some form of ring. elif self.Graph.Type == 1: self.State = CIRCO return self.State else: # undirected graph # Metric for graph: connectedness: #nodes/#edges, < 3 connectedness = self.Graph.getNumEdges()/float(numberOfNodes) print connectedness if connectedness < 3: #radial -> nbody self.State = NBODY else: #nbody random self.State = NBODY_JITTER return self.State def runChosenSolver(self, choice = None): if choice == None: choice = self.State #print "TEST",choice, CIRCO if choice == NBODY: return self.runNBody() if choice == NBODY_RADIAL: return self.runNBody(True) if choice == NBODY_JITTER: return self.runNBody(False) if choice == TOPDOWN: return self.runTopDown() if choice == RADIAL: return self.runRadial() if choice == CIRCO: return self.runCirco() return self.runNBody() '''return { NBODY:self.runNBody(), NBODY_RADIAL:self.runNBody(True), NBODY_JITTER:self.runNBody(False), TOPDOWN:self.runTopDown(), RADIAL:self.runRadial(), CIRCO:self.runCirco(), #NBODYJ:self.runNBody(False), }.get(choice,self.runNBody()) ''' def printChoice(self): return { NBODY:"NBODY", NBODY_RADIAL:"NBODY_RADIAL", NBODY_JITTER:"NBODY_JITTER", TOPDOWN:"TOPDOWN", RADIAL:"RADIAL", CIRCO:"CIRCO", }.get(self.State,"???")