Example #1
0
def funcWrite2File(file_name):

    with open(file_name) as fileSmt:
        smt_file_content = fileSmt.readlines()

    smt_file_content = [x.strip() for x in smt_file_content]

    smt_file_lines = util.file_len(file_name)
    #print(smt_file_lines)

    fileTogFeSmt = open('ToggleFeatureSmt.smt2', 'w')

    for i in range(smt_file_lines):

        fileTogFeSmt.write(smt_file_content[i])
        fileTogFeSmt.write("\n")

    fileTogFeSmt.close()
Example #2
0
def funcPrunBranch(dfOrig, tree_model):

    noPathCond = 0
    #data set to hold set of candidate counter examples, refer to cand-set of prunBranch algorithm
    with open('CandidateSetBranch.csv', 'w', newline='') as csvfile:
        fieldnames = dfOrig.columns.values
        writer = cv.writer(csvfile)
        writer.writerow(fieldnames)

    with open('param_dict.csv') as csv_file:
        reader = cv.reader(csv_file)
        paramDict = dict(reader)

    dfRead = pd.read_csv('TestDataSMTMain.csv')

    for row in range(0, dfRead.shape[0]):
        if (paramDict['multi_label'] == 'True'):
            funcgetPath4multiLbl(tree_model, dfOrig, row,
                                 int(paramDict['no_of_params']))
        else:
            funcgetPath(tree_model, dfOrig, row)
        fileCond = open('TreeOutput.txt', 'r')
        first = fileCond.read(1)

        if not first:
            print('No Branch')
        else:
            noPathCond = util.file_len('ConditionFile.txt')
            if (noPathCond == 'empty'):
                return
            for i in range(noPathCond):
                funcAddCond2File(i)
                os.system(r"z3 ToggleBranchSmt.smt2 > FinalOutput.txt")
                satFlag = ReadZ3Output.funcConvZ3OutToData(dfOrig)

                if (satFlag == True):
                    dfSmt = pd.read_csv('TestDataSMT.csv')
                    dataAppend = dfSmt.values
                    with open('CandidateSetBranch.csv', 'a',
                              newline='') as csvfile:
                        writer = cv.writer(csvfile)
                        writer.writerows(dataAppend)
Example #3
0
def funcAddCond2File(index):

    temp_cond_content = ''
    with open('ConditionFile.txt') as fileCond:
        condition_file_content = fileCond.readlines()
    condition_file_content = [x.strip() for x in condition_file_content]

    with open('DecSmt.smt2') as fileSmt:
        smt_file_content = fileSmt.readlines()

    smt_file_content = [x.strip() for x in smt_file_content]
    smt_file_lines = util.file_len('DecSmt.smt2')
    fileCondSmt = open('ToggleBranchSmt.smt2', 'w')

    for i in range(smt_file_lines):
        fileCondSmt.write(smt_file_content[i])
        fileCondSmt.write("\n")
    fileCondSmt.close()

    with open('ToggleBranchSmt.smt2', 'r') as fileCondSmt:
        text = fileCondSmt.read()
        text = text.replace("(check-sat)", '')
        text = text.replace("(get-model)", '')

        with open('ToggleBranchSmt.smt2', 'w') as fileCondSmt:
            fileCondSmt.write(text)

    fileCondSmt = open('ToggleBranchSmt.smt2', 'a')

    temp_cond_content = condition_file_content[index]
    #print(temp_cond_content)

    fileCondSmt.write("(assert (not " + temp_cond_content + "))")
    fileCondSmt.write("\n")

    fileCondSmt.write("(check-sat) \n")
    fileCondSmt.write("(get-model) \n")

    fileCondSmt.close()
Example #4
0
def funcConvZ3OutToData(df):
    fe_flag = False

    with open('param_dict.csv') as csv_file:
        reader = cv.reader(csv_file)
        paramDict = dict(reader)
    no_of_params = int(paramDict['no_of_params'])
    testMatrix = np.zeros(((no_of_params), df.shape[1]))

    if (os.stat('FinalOutput.txt').st_size > 0):
        with open('FinalOutput.txt') as f1:
            file_content = f1.readlines()

        file_content = [x.strip() for x in file_content]
        noOfLines = util.file_len('FinalOutput.txt')

        with open('TestDataSMT.csv', 'w', newline='') as csvfile:
            fieldnames = df.columns.values
            writer = cv.writer(csvfile)
            writer.writerow(fieldnames)
            writer.writerows(testMatrix)

        dfAgain = pd.read_csv('TestDataSMT.csv')
        nums = re.compile(r"[+-]?\d+(?:\.\d+)?")
        if ('unknown' in file_content[0]):
            raise Exception('Encoding problem')
            sys.exit(1)
        if ('model is not available' in file_content[1]):
            return False
        else:
            i = 1
            while (i < noOfLines):
                minus_flag = False
                fe_flag = False
                if ("(model" == file_content[i]):
                    i = i + 1
                elif (")" == file_content[i]):
                    i = i + 1
                else:
                    for j in range(0, df.columns.values.shape[0]):
                        for param_no in range(0, no_of_params):
                            if (paramDict['multi_label'] == 'True'
                                    and no_of_params == 1
                                    and paramDict['white_box_model']
                                    == 'Decision tree'):
                                fe_add = ' '
                            else:
                                fe_add = str(param_no)
                            if (df.columns.values[j] + fe_add
                                    in file_content[i]):
                                feature_name = df.columns.values[j]
                                fe_flag = True
                                if ('Int' in file_content[i]):
                                    i = i + 1
                                    digit = int(
                                        re.search(r'\d+',
                                                  file_content[i]).group(0))
                                    if ('-' in file_content[i]):
                                        digit = 0 - digit
                                elif ('Real' in file_content[i]):
                                    i = i + 1
                                    if ("(/" in file_content[i]):
                                        if ('-' in file_content[i]):
                                            minus_flag = True
                                        multi_digits = re.findall(
                                            '\d*?\.\d+', file_content[i])
                                        if (len(multi_digits) == 1):
                                            i = i + 1
                                            multi_digits.append(
                                                float(
                                                    re.search(
                                                        r'\d+',
                                                        file_content[i]).group(
                                                            0)))
                                        digit = float(multi_digits[0]) / float(
                                            multi_digits[1])
                                        if (minus_flag == True):
                                            digit = 0 - digit
                                    else:
                                        #digit = round(float(re.search(r'\d+', file_content[i]).group(0)), 2)
                                        digit = float(
                                            re.search(
                                                r'\d+',
                                                file_content[i]).group(0))
                                        if ('-' in file_content[i]):
                                            digit = 0 - digit
                                dfAgain.loc[param_no, feature_name] = digit
                                i = i + 1
                    if (fe_flag == False):
                        i = i + 2
            dfAgain.to_csv('TestDataSMT.csv', index=False, header=True)
            return True

    else:
        raise Exception("There is no solver installed in your system")