def WritePollutionToFile(self): pop_size = 6 dR = DataReader() ContextListPop = dR.getContextFileWithPath("D:\docu\KL\Variant\listPop") ListPop = ContextListPop.split('\n') # print(listPop) ArrPop, ArrWeightPath = self.CreatePollution("D:\docu\KL\Data\mid.py") ArrPop = ArrPop[1:pop_size] dictPathPop = dict(zip(ListPop, ArrPop)) print(list(dictPathPop.values())[0]) for i in range(1,pop_size): variantPath = "D:\docu\KL\Variant\\" + list(dictPathPop.keys())[i-1] dR.writeToFile(variantPath,list(dictPathPop.values())[i-1])
def CrossOver(self,fnameA,funNameA,fnameB,funNameB,importStringA,importStringB): dR = DataReader() # cutpointA = self.GetCutOffPoint(fnameA,funNameA,importStringA) # cutpointB = self.GetCutOffPoint(fnameB,funNameB,importStringB) cutpointA = 5 cutpointB = 4 contextA = dR.getContextFileWithPath(fnameA) contextB = dR.getContextFileWithPath(fnameB) treeA = ast.parse(contextA) treeB = ast.parse(contextB) helper = AstHelper() nodeCutA = helper.GetNodeByLineNo(cutpointA,treeA) nodeCutB = helper.GetNodeByLineNo(cutpointB,treeB) lineNodeCutA = helper.GetFullStatementNode(nodeCutA) lineNodeCutB = helper.GetFullStatementNode(nodeCutB) # print(lineNodeCutA) # print(lineNodeCutB) indexA = contextA.find(lineNodeCutA) indexB = contextB.find(lineNodeCutB) print(indexA) print(contextA[:indexA]) print(contextB[indexA:]) # print(contextA[:indexA] + contextB[indexA:]) # print(contextB[:indexA] + contextA[indexA:]) # elif indexB > indexA: # print(contextB[:indexA] + contextA[indexA:indexB] + contextB[indexB:]) # print("dòng lệnh bị cắt trong A : " + str(cutpointA)) # print("full nội dung dòng lệnh trong A : " + str(lineNodeCutA)) # print(contextA[indexA:]) # print(contextA[:indexA]) # print("dòng lệnh bị cắt trong B : " + str(cutpointB)) # print("full nội dung dòng lệnh trong B : " + str(lineNodeCutB)) # print(contextB[indexB:]) # print(contextB[:indexB]) # print("Đoạn giữa : ") print(contextA[indexA:indexB]) # print(indexA) # print(indexB) # if indexA <= indexB: # print(contextA[indexA:indexB]) # else: # print(contextA[indexB:indexA]) return cutpointA, cutpointB
def FitnessFunction(self,funName, importString): exec (importString) global a fitnessPoint = 0.0 WPRate = 0.2 WFRate = 0.8 countTestPass = 0 # pass testcase pass countTestFail = 0 # pass testcase fail currentResult = [] dT = DataReader() testReader = ReadFile() ctx = dT.getContextFileWithPath("D:\\docu\\KL\\Data\\unitTest1.py") results, testCaseResuts, tests, totalPassed, totalFailed = testReader.importResultsFile() TestCaseResult,resultsTestCaseMap = testReader.ImportResultTestCase() # print(TestCaseResult) # print(resultsTestCaseMap) # print(results) # print(testCaseResuts) # print(tests) for i in range(len(tests)): # print(tests[i]) try: exec('global a; a = %s(*(tests[i]))' % funName) except: print("something wrong") countTestFail = countTestFail - 1 # magic code here currentResult.append(a) for i in range(len(currentResult)): if currentResult[i] == list(resultsTestCaseMap.keys())[i][0]: if resultsTestCaseMap[list(resultsTestCaseMap.keys())[i][0], list(resultsTestCaseMap.keys())[i][1]] == True: countTestPass = countTestPass + 1 elif resultsTestCaseMap[list(resultsTestCaseMap.keys())[i][0], list(resultsTestCaseMap.keys())[i][1]] == False: countTestFail = countTestFail + 1 print("Test pass : "******"Test fail : " + str(countTestFail)) fitnessPoint = WPRate * countTestPass + WFRate * countTestFail # for res in currentResult: # if res in resultsTestCaseMap.keys()[1]: # print(res) return fitnessPoint
def CreatePollution(self,fname): PolutionArr = [] tracer = Tracer() dR = DataReader() weightpathMap,lineArr = tracer.getLineInfor(fname,"mid","from Data.mid import mid") lineArrWeightPath = [] context = dR.getContextFileWithPath(fname) tree = ast.parse(context) astHelper = AstHelper() node3 = astHelper.GetNodeByLineNo(3,tree) for i in weightpathMap: if i.weight > 0: lineArrWeightPath.append(i) for line in lineArrWeightPath: node = astHelper.GetNodeByLineNo(line.lineNo,tree) newContext = astHelper.DeleteNode(context,node) # print(newContext) PolutionArr.append(newContext) # print(line.lineNo) # print(astor.to_source(node)) return PolutionArr,lineArrWeightPath
def getLineToTest2(self,funName,fname): global current dataReader = DataReader() testReader = ReadFile() results, testCaseResuts, tests, totalPassed, totalFailed = testReader.importResultsFile() dataProg = dataReader.file_len(fname) numOfLine, arrLines = dataProg # init lineToTest for i in range(1, numOfLine): if i not in lineToTest.keys(): lineToTest[i] = [] for i in list(lineToTest): lineToTest[i] = [] for i in list(testToLines): testToLines[i] = [] for i in range(len(tests)): current = tests[i] testToLines[tests[i]] = [] sys.settrace(self.traceit) exec('%s(*(tests[i]))' % funName) # print(lineToTest) first2pairs = {k: lineToTest[k] for k in list(lineToTest)[:15]} print(first2pairs)
def getLineToTest(self, fname, funName, x): exec(x) global current global results global totalPassed global totalFailed global numOfLine global arrLines weightPathMap = {} dataReader = DataReader() testReader = ReadFile() results, testCaseResuts, tests, totalPassed, totalFailed = testReader.importResultsFile( ) dataProg = dataReader.file_len(fname) numOfLine, arrLines = dataProg # funName = os.path.splitext('mid5.py')[0] # print(funName) # print(len(tests)) for i in range(1, numOfLine): if i not in lineToTest.keys(): lineToTest[i] = [] for i in list(lineToTest): lineToTest[i] = [] for i in list(testToLines): testToLines[i] = [] for i in range(len(tests)): current = tests[i] testToLines[tests[i]] = [] tracer = Tracer() sys.settrace(tracer.traceit) try: exec('%s(*(tests[i]))' % funName) except: print("something wrong") # print(numOfLine) # for i in range(1, numOfLine): # if i not in lineToTest.keys(): # lineToTest[i] = [] # print(tests) # print(results) # print(lineToTest) # print(testToLines) # for key in testToLines.keys(): # if results[key] == True: # for line in testToLines[key]: # weightPathMap[line] = 0 # elif results[key] == False: # for line in testToLines[key]: # if line in weightPathMap: # weightPathMap[line] = 0.5 # else: # weightPathMap[line] = 1 # print(results) firstLineToTestpairs = { k: lineToTest[k] for k in list(lineToTest)[:numOfLine] } firstTestToLinespairs = {} for i in range(len(tests)): firstTestToLinespairs[tests[i]] = [] for key, val in firstLineToTestpairs.items(): for r in results.keys(): if results[r] == False and r in val: weightPathMap[key] = 1 elif results[r] == True and r in val: weightPathMap[key] = 0 for key, val in firstLineToTestpairs.items(): for r in results.keys(): if results[r] == True and r in val and weightPathMap[key] == 1: weightPathMap[key] = 0.5 return weightPathMap, firstLineToTestpairs
def getProg(self): dataReader = DataReader() context = dataReader.getContextFile() return context
if __name__ == '__main__': # tracedr = Tracer() # helper = Helper() # # h = Tranformer() # arr = h.ModifyProgByDeleteStmt(tracedr,"D:\docu\KL\Data\mid.py") # for iline in range(len(arr)): # print(arr[iline].rank) # ct = helper.UpdateVariant(arr) # print(ct) # helper.WriteToFile(ct) # flag = Flag(0) dR = DataReader() context = dR.getContextFileWithPath("D:\docu\KL\Variant\Pop3.py") tree = ast.parse(context) helper = AstHelper() node5 = helper.GetNodeByLineNo(5, tree) # print(astor.to_source(node9)) node4 = helper.GetNodeByLineNo(4, tree) # print(astor.to_source(node4)) stringNode4 = helper.GetFullStatementNode(node4) stringNode5 = helper.GetFullStatementNode(node5) # print(stringNode4) print(stringNode5) # newContext = helper.InsertAfter(context,node8,node4)