def getGfc(fileName, arcPrimFile, showOutput): # Verifica se existe ou não a definição de arco primitivo, caso não, a cria if not util.pathExists(arcPrimFile): command = '{}/Others/calp_chusho {}'.format(getcwd(), fileName) subprocess.run(command, shell=True) # Busca as informações referente aos arcos primitivos daquele programa primitiveNodes = util.getContentFromFile(arcPrimFile) # Busca o conteúdo do arquivo .gfc gfc = util.getContentFromFile(fileName).replace('\t', '') gfc, numNodes = prepareGFC(gfc, getPrimitiveNode(primitiveNodes)) if showOutput: for node in gfc: print('Nó: {}\tOrigens: {}\tDestinos: {}\tDistância do início: {}\tDistância do fim: {}'.format(node, gfc[node][constants._iNSources], gfc[node][constants._iNTargets], gfc[node][constants._iNDistancesBegin], gfc[node][constants._iNDistancesEnd])) return gfc, numNodes
def getOffsetFromCode(codeFile, beginOffset, getOut): contentFile = util.getContentFromFile(codeFile) beginLine = contentFile.rfind('\n', 0, beginOffset) endOffset = beginOffset + getOut descriptor = (contentFile[beginOffset - 1: endOffset]) descriptor_line = (contentFile[beginLine : endOffset]) return descriptor, descriptor_line
def gfcUtils_main(baseFolder, gfcFileName): prot2PokeMain(gfcFileName) filesInFolder = [f for f in listdir(baseFolder) if isfile(join(baseFolder, f))] for file in filesInFolder: if not str(file).startswith('__') and not file.endswith('main.gfc'): file = '{}/{}'.format(baseFolder, file) gfc = prepareGFC_basic(util.getContentFromFile(file).replace('\t', '')) gfcToDot(gfc, file.replace('.gfc', '.dot')) dotToPng(file.replace('.gfc', '.dot'), file.replace('.gfc', '.png'))
def getProgramsInfo(): programsInfoFileName = '{}/Programs/ProgramsInfo.csv'.format(os.getcwd()) programsInfo = util.getContentFromFile(programsInfoFileName) programsInfo_dict = dict() columns = [ line.split(',') for line in programsInfo.splitlines() if len(line.split(',')[0]) > 0 and line.split(',')[0] != 'Program' ] for program in columns: programsInfo_dict[program[0]] = program[1:] return programsInfo_dict
def getMinimalMutants(baseFolder, sourceFile): fileName = "{}/log/minimal.txt".format(baseFolder) #fileName = "{}/arc_prim/minimal.txt".format(baseFolder) #Utilizar as informações dos minimais já passadas if (not util.pathExists(fileName)): computeMinimal( "{baseFolder}/{sourceFile}".format(baseFolder=baseFolder, sourceFile=sourceFile), False, "{baseFolder}/log".format(baseFolder=baseFolder)) contentFile = util.getContentFromFile(fileName) minimalMutants = [] linesFile = contentFile.splitlines() for line in linesFile: line = line.replace("\n", "") if len(line) > 0: minimalMutants.append(int(line.strip())) return minimalMutants
def executeProteum(baseFolder, sourceFile, sessionName, executableFile, executionType, directory, units): if not util.pathExists("{}/log".format(baseFolder)): util.createFolder("{}/log".format(baseFolder)) ##################### # Create Test Session ##################### print('\n##### \tCriando sessão de testes ' + util.formatNow() + '\t#####') # Inclui o arquivo driver.c na compilação caso ele exista na pasta driver = 'driver.c' if util.pathExists( '{}/driver.c'.format(baseFolder)) else '' proteum.createSession(baseFolder, sessionName, executionType, sourceFile, executableFile, directory, driver) ################### # Create Test Cases ################### print('\n##### \tCriando casos de teste ' + util.formatNow() + '\t#####') testCases = util.convertStringToArray( util.getContentFromFile("{}/testset.txt".format(baseFolder)), "\n") proteum.createTestCases(sessionName, directory, testCases) ################# # Show Test Cases ################# print('\n##### \tCasos de teste ' + util.formatNow() + '\t#####') print(proteum.showTestCases(sessionName, directory)) ################### # Delete Test Cases ################### #print ('\n##### \tCasos de teste deletados ' + formatNow() + '\t#####') #proteum.deleteTestCase(sessionName, directory) ################## # Generate mutants ################## print('\n##### \tGerando mutantes ' + util.formatNow() + '\t#####') proteum.createEmptySetMutants(sessionName, directory) for unitName in str(units).splitlines(): proteum.generateMutants(sessionName, directory, unitName) ################# # Execute mutants ################# print('\n##### \tExecutando mutantes ' + util.formatNow() + '\t#####') proteum.executeMutants(sessionName, directory) ############################### # Seta mutante como equivalente ############################### print('\n##### \tSetando mutantes como equivalentes ' + util.formatNow() + '\t#####') proteum.setEquivalent(sessionName, directory, "") ######################################### # Execute mutants considering equivalents ######################################### print('\n##### \tExecutando mutantes considerando os equivalentes ' + util.formatNow() + '\t#####') proteum.executeMutants(sessionName, directory) ######################### # Casos de teste efetivos ######################### print('\n##### \tExibindo casos de teste efetivos ' + util.formatNow() + '\t#####') proteum.listGood(sessionName, directory, "i") #################### # Gera os relatórios #################### print('\n##### \tGeração dos relatórios ' + util.formatNow() + '\t#####') proteum.showMutants(sessionName, directory, "") ################### # Exibe os mutantes ################### print('\n##### \tExibe mutantes ' + util.formatNow() + '\t#####') proteum.generateReport(sessionName, directory)
def main(_baseExperimentFolder, _baseFolder, executionMode): #executionMode | 1 - Run and analyze # 2 - Run # 3 - Analyze if int(executionMode) >= 1 and int(executionMode) <= 3: print( '####################################################################' ) print( '#\t Executing script to find minimal mutants properties\t #') print('#\t\t ' + util.formatNow() + '\t\t\t #') print( '####################################################################' ) #################### # Set main variables #################### baseExperimentFolder = _baseExperimentFolder baseFolder = _baseFolder sourceFile = baseFolder[baseFolder.rfind("/") + 1:] sessionName = sourceFile executableFile = sessionName executionType = "research" directory = baseFolder #unitName = util.getContentFromFile("{baseFolder}/unit.txt".format(baseFolder = baseFolder)) units = util.getContentFromFile( "{baseFolder}/unit.txt".format(baseFolder=baseFolder)) if int(executionMode) == 1 or int(executionMode) == 2: ################# # Execute proteum ################# executeProteum(baseFolder, sourceFile, sessionName, executableFile, executionType, directory, units) if int(executionMode) == 1 or int(executionMode) == 3: ##################### # Get minimal mutants ##################### print('\n##### \tBuscando mutantes minimais ' + util.formatNow() + '\t#####') minimalMutants = getMinimalMutants(baseFolder, sourceFile) ###################### # Simplifying GFC file ###################### # Desabilitado pois estou utilizando GFC já passados #print ('\n##### \tSimplificando arquivo GFC ' + util.formatNow() + '\t#####' ) #prot2PokeMain("{baseFolder}/__{sourceFile}.gfc".format( #baseFolder = baseFolder, sourceFile = sourceFile)) ################################ # Get basic mutants informations ################################ print('\n##### \tBuscando e calculando informações dos mutantes ' + util.formatNow() + '\t#####') mutantsHeader, mutantsInfo = getMutantsInfo( baseFolder, minimalMutants, sessionName, units) ################################################ # Write csv File with basic mutants informations ################################################ print('\n##### \tGerando arquivo com informações dos mutantes ' + util.formatNow() + '\t#####') fileNameResults = "{baseFolder}/log/{sessionName}_result.csv".format( sessionName=sessionName, baseFolder=baseFolder) util.writeInCsvFile(fileNameResults, mutantsInfo, mutantsHeader) ########################################################### # Write mutants info to compute machine learning algorithms ########################################################### datasetBaseFolder = '{}/ML/Dataset'.format( util.getPreviousFolder(baseExperimentFolder)) ######################## ### --- Minimals --- ### essentialInfo = computeEssencialInfo(mutantsInfo, minimal_Equivalent=0) # Gera apenas um arquivo com todos os mutantes essentialFileName = '{}/MINIMAL/mutants.csv'.format( datasetBaseFolder) util.writeDataFrameInCsvFile( essentialFileName, essentialInfo, sep=',', mode='a+', header=True if util.pathExists(essentialFileName) == False else False, index=False) # Gera um arquivo para cada programa com todos os seus mutantes essentialFileName = '{}/MINIMAL/Programs/{}.csv'.format( datasetBaseFolder, sessionName) util.writeDataFrameInCsvFile(essentialFileName, essentialInfo, sep=',', mode='w+', header=True, index=False) ########################### ### --- Equivalents --- ### essentialInfo = computeEssencialInfo(mutantsInfo, minimal_Equivalent=1) # Gera apenas um arquivo com todos os mutantes essentialFileName = '{}/EQUIVALENT/mutants.csv'.format( datasetBaseFolder) util.writeDataFrameInCsvFile( essentialFileName, essentialInfo, sep=',', mode='a+', header=True if util.pathExists(essentialFileName) == False else False, index=False) # Gera um arquivo para cada programa com todos os seus mutantes essentialFileName = '{}/EQUIVALENT/Programs/{}.csv'.format( datasetBaseFolder, sessionName) util.writeDataFrameInCsvFile(essentialFileName, essentialInfo, sep=',', mode='w+', header=True, index=False)
def getMutantsInfo(baseFolder, minimalMutants, sessionName, units): arrMutantsInfo = [] arrHeaderMutants = [] mutantsInfoFileName = "{baseFolder}/log/mutants.txt".format( baseFolder=baseFolder) if (not util.pathExists(mutantsInfoFileName)): proteum.showMutants(sessionName, baseFolder, "") mutantsInfoFile = util.getContentFromFile(mutantsInfoFileName) # Código que foi utilizado para gerar o mutante codeFile = '{baseFolder}/__{sessionName}.c'.format(baseFolder=baseFolder, sessionName=sessionName) # Coleta as informações para cada uma das funções executadas for unitName in units.splitlines(): gfcFileName = "{baseFolder}/arc_prim/{unitName}.gfc".format( baseFolder=baseFolder, unitName=unitName) #Utiliza as informações já passadas arcPrimFileName = "{baseFolder}/arc_prim/{unitName}.tes".format( baseFolder=baseFolder, unitName=unitName) gfc, numNodes = gfcUtils.gfcMain(gfcFileName, arcPrimFileName) # Array que conterá todas as complexidades dos mutantes complexities = [] # Propriedade responsável por contar o número de mutantes em cada nó do GFC mutantsOnNodes = [] # Divide o arquivo de mutantes pelo caracter # (cada um representa um mutante) mutants = mutantsInfoFile.split("#") for mutant in mutants: # Variável utilizada para identificar o último mutante analisado lastICount = 0 # Busca todas as informações relevantes do mutante que estão no arquivo mutantNumber, status, operator, programGraphNode, offSet, getOut, descriptorSize, callingFunction = getMutantInfosFromText( mutant) if mutantNumber == None: continue # Caso a função onde ocorreu a mutação for diferente da função analisada, ignora este mutante pois ele será analisado em outro momento functionName = str( gfcUtils.getOffsetFromCode(codeFile, callingFunction, descriptorSize)) functionName = functionName[2:functionName.find('(', 2)].strip() if functionName != unitName: continue # Define se o mutante é minimal ou não minimal = mutantNumber in minimalMutants # Define se o mutante é equivalente ou não equivalent = status.__contains__("Equivalent") # Verifica o tipo de declaração que houve mutação descriptor, descriptor_line = gfcUtils.getOffsetFromCode( codeFile, offSet, getOut) typeStatement = gfcUtils.getTypeStatementFromCode( descriptor, descriptor_line, sessionName) # Calcula o número de mutantes no nó da mutação mutantsOnNodes = gfcUtils.getMutantsOnNode(mutantsOnNodes, programGraphNode) # Busca os nós origens e destinos # Calcula as informações de distância do nó da mutação até os nós inicias \ # Calcula as informações de distância do nó da mutação até os nós finais \ # Calcula as informações de pertencimento ou não aos arcos primitivos sourceNode, targetNode, \ distanceBegin, distanceBegin_min, distanceBegin_max, distanceBegin_avg,\ distanceEnd, distanceEnd_min, distanceEnd_max, distanceEnd_avg,\ sourcePrimitiveArc, targetPrimitiveArc, sourcesNodeIsPrimitive, targetsNodeIsPrimitive = gfcUtils.getInfoNode(gfc, programGraphNode, numNodes) # Define se o mutante pertence aos arcos primitivos ou não primitiveArc = sourcePrimitiveArc or targetPrimitiveArc # Reune todas as informações dos mutantes num array arrMutantInfo = [] arrMutantInfo.append(sessionName) arrMutantInfo.append(mutantNumber) arrMutantInfo.append( status ) # Temporariamente a coluna _IM_RESULT vai conter o status arrMutantInfo.append(status) arrMutantInfo.append(operator) arrMutantInfo.append(programGraphNode) arrMutantInfo.append("1" if minimal else "0") arrMutantInfo.append("1" if primitiveArc else "0") arrMutantInfo.append("1" if sourcePrimitiveArc else "0") arrMutantInfo.append("1" if targetPrimitiveArc else "0") arrMutantInfo.append("1" if equivalent else "0") arrMutantInfo.append(distanceBegin) arrMutantInfo.append(distanceBegin_min) arrMutantInfo.append(distanceBegin_max) arrMutantInfo.append(distanceBegin_avg) arrMutantInfo.append(distanceEnd) arrMutantInfo.append(distanceEnd_min) arrMutantInfo.append(distanceEnd_max) arrMutantInfo.append(distanceEnd_avg) arrMutantInfo.append(sourceNode) arrMutantInfo.append(targetNode) arrMutantInfo.append(sourcesNodeIsPrimitive) arrMutantInfo.append(targetsNodeIsPrimitive) arrMutantInfo.append('[MutantsOnNode]') arrMutantInfo.append(typeStatement) # Adiciona as informações do mutante num outro array que contém todas as informações de todos os mutantes arrMutantsInfo.append(arrMutantInfo) # Atualiza as informações sobre os nós dos mutantes for iCount in range(lastICount, len(arrMutantsInfo), 1): arrMutantsInfo[iCount][ constants._IM_COMPLEXITY] = gfcUtils.getNumMutantsOnNode( mutantsOnNodes, arrMutantsInfo[iCount][constants._IM_PROGRAM_GRAPH_NODE]) complexities.append( arrMutantsInfo[iCount][constants._IM_COMPLEXITY]) lastICount = iCount # Normaliza os dados de complexidade dos mutantes #complexities = util.normalize(complexities) #for iCount in range(len(complexities)): # arrMutantsInfo[iCount][constants._IM_COMPLEXITY] = complexities[iCount] arrHeaderMutants.append("Program Name") arrHeaderMutants.append("#") arrHeaderMutants.append("Result") arrHeaderMutants.append("Status") arrHeaderMutants.append("Operator") arrHeaderMutants.append("Program Graph Node") arrHeaderMutants.append("Minimal?") arrHeaderMutants.append("Primitive Arc?") arrHeaderMutants.append("Source Primitive Arc?") arrHeaderMutants.append("Target Primitive Arc?") arrHeaderMutants.append("Equivalent?") arrHeaderMutants.append("Distance Begin") arrHeaderMutants.append("Distance Begin (min)") arrHeaderMutants.append("Distance Begin (max)") arrHeaderMutants.append("Distance Begin (avg)") arrHeaderMutants.append("Distance End") arrHeaderMutants.append("Distance End (min)") arrHeaderMutants.append("Distance End (max)") arrHeaderMutants.append("Distance End (avg)") arrHeaderMutants.append("Source Node") arrHeaderMutants.append("Target Node") arrHeaderMutants.append("Sources Nodes are primitive?") arrHeaderMutants.append("Targets Nodes are primitive?") arrHeaderMutants.append("Complexity") arrHeaderMutants.append("Type Statement") return arrHeaderMutants, arrMutantsInfo