def convert_kcr_to_kmax(kcr_, kmax_, solset_): solset = list() kcrf, kcrc, kcrv = read_dimacs(kcr_) kmaxf, kamxc, kmaxv = read_dimacs(kmax_) _kcri = [i[0] for i in kcrf] # generate .config files from samples for sol in solset_: config = set() ksol = set() for sel in sol: val = int(sel) if abs(val) in _kcri: i = _kcri.index(abs(val)) feature = kcrf[i][1] # if not feature.startswith('CONFIG_'): # feature = 'CONFIG_' + feature if feature != 'MODULES' and feature != 'CONFIG_MODULES' and 'CHOICE_' not in feature: if val > 0: if '=' in feature: if '=' in feature: finfo = feature.split('=') if finfo[1] == 'n': config.add('-' + finfo[0]) else: config.add(finfo[0]) else: config.add(feature) elif val < 0: if '=' not in feature: config.add('-' + feature) for kf in kmaxf: if str(kf[1]) in config: ksol.add(kf[0]) elif ('-' + str(kf[1])) in config: ksol.add(-1 * kf[0]) else: r = random.random() if r < 0.5: ksol.add(kf[0]) else: ksol.add(-1 * kf[0]) solset.append(ksol) return solset
def get_rank(dimacs_, dir_, jsonfile_): # read dimacs file for feature list _features, _clauses, _vars = read_dimacs(dimacs_) if os.path.exists(jsonfile_): with open(jsonfile_, 'r') as file: data = file.read() importer = JsonImporter() _root = importer.import_(data) total = _root.count else: print("ERROR: tree file not found!") return _cdir = dir_ for file in os.listdir(_cdir): if file.endswith('.config'): # convert config file into variable list sol = read_config_kmax(_features, _cdir + "/" + file) # traverse tree based on solution _node = _root _precision = 0 _number = 0 while _precision == 0: _node, _precision, _number = traverse_cube(_node, sol, _number) if _precision > 0: print(str(_number / total) + "," + str(_precision / total)) else: print("ERROR: tree traverse failure")
def gen_configs_kmax(dimacs_, samples_, cdir_): # remove existing contents on the folder for f in os.listdir(cdir_): file_path = os.path.join(cdir_, f) try: if os.path.isfile(file_path): os.unlink(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: print(e) # get feature information features, clauses, vars = read_dimacs(dimacs_) # generate .config files from samples i = 0 for s in samples_: config = "" for sel in s: feature = features[abs(sel) - 1] if sel > 0: if feature[2] == 'nonbool': config = config + feature[1] + "=" + feature[3] + "\n" else: config = config + feature[1] + "=y\n" elif sel < 0: if feature[2] == 'nonbool': if is_int(feature[3]): config = config + feature[1] + "=0\n" else: config = config + feature[1] + "=\"\"\n" else: config = config + "# " + feature[1] + " is not set\n" with open(cdir_ + "/" + str(i) + ".config", 'w') as outfile: outfile.write(config) outfile.close() i += 1 print("Configs generated")
def test_randconfig_kmax(dimacs_): passed = 0 common = list() init = True # check if randconfig exists cdir = os.path.dirname(dimacs_) + "/correctness/randconfigs/" if not os.path.exists(cdir): print("randconfig not found") return # get features and clauses _features, _clauses, _vars = read_dimacs(dimacs_) _names = [i[1] for i in _features] # iterate over each randconfig configurations for file in os.listdir(cdir): if file.endswith('.config'): with open(cdir + "/" + file, 'r') as f: _existing = set() sol = list() for line in f: # line: # FEATURE is not set if line.startswith('#'): line = line[0:len(line) - 1] data = line.split() if len(data) > 4: if data[1] in _names: i = _names.index(data[1]) _existing.add(data[1]) if i != -1: sol.append('-' + _features[i][0]) # line: FEATURE=y or FEATURE="nonbool value" else: line = line[0:len(line) - 1] data = line.split('=') if len(data) > 1: if data[0] in _names: i = _names.index(data[0]) _existing.add(data[0]) if data[1] == 'y': sol.append(str(_features[i][0])) #print(_features[i][0]) elif data[1] == '\"\"' or data[1] == '0': if _features[i][3] != '\"\"' and _features[ i][3] != '0': sol.append('-' + _features[i][0]) #print('-' + _features[i][0]) # else: # #print(_features[i][1]) else: sol.append(str(_features[i][0])) #print(str(_features[i][0])) outfile = cdir + '/check.dimacs' gen_dimacs(_vars, _clauses, sol, outfile) res = getoutput(SHARPSAT + ' -q ' + outfile) print(line + ": " + str(res)) if not is_int(res): print(file + " failed") return # set all nonexistent variables to false # for f in _features: # if f[1] not in _existing: # sol.append('-' + str(f[0])) # sol.append('-55') # sol.append('-109') outfile = cdir + '/check.dimacs' gen_dimacs(_vars, _clauses, sol, outfile) res = getoutput(SHARPSAT + ' -q ' + outfile) if is_int(res): if res != '1': print(file + ": " + str(res)) else: passed += 1 else: print(file + ": " + str(res)) if init: common = sol init = False else: for s in common: if s not in sol: common.remove(s) print("Passed:" + str(passed)) print(common)
def test_randconfig_kcr(dimacs_, target_): # check if randconfig exists cdir = os.path.dirname(dimacs_) + "/randconfigs" if not os.path.exists(cdir): print("randconfig not found") return # get features and clauses _features, _clauses, _vars = read_dimacs(dimacs_) kf, kc, kv = read_dimacs("/home/jeho/kmax/kconfig_case_studies/cases/" + target_ + "/kconfig.dimacs") _names = list() kn = [i[1] for i in kf] prefix = False for f in _features: if len(f) > 2: name = f[1] for i in range(2, len(f)): name = name + ' ' + f[i] if not name.startswith('CONFIG_') and prefix: name = 'CONFIG_' + name _names.append(name) else: name = f[1] if not name.startswith('CONFIG_') and prefix: name = 'CONFIG_' + name _names.append(name) # iterate over each randconfig configurations for file in os.listdir(cdir): if file.endswith('.config'): with open(cdir + "/" + file, 'r') as f: sol = set() ambig = set() for line in f: if line.startswith('#'): line = line[0:len(line) - 1] data = line.split() if len(data) > 4: if data[1] in _names: i = _names.index(data[1]) if i != -1: sol.add('-' + _features[i][0]) # else: # print(line) else: line = line[0:len(line) - 1] data = line.split('=') if len(data) > 1: if data[1] == 'y': if data[0] in _names: i = _names.index(data[0]) sol.add(str(_features[i][0])) # else: # print('y: '+data[0]) elif data[1] == '\"\"' or data[1] == '0': if data[0] in kn: k = kn.index(data[0]) if kf[k][3] != '\"\"' and kf[k][3] != '0': feature = data[0] + '=n' if feature in _names: i = _names.index(feature) sol.add(str(_features[i][0])) # else: # print("Not in dimacs: " + feature) else: # print('n: ' + line) ambig.add(data[0]) # feature = data[0] + '=n' # if feature in _names: # i = _names.index(feature) # sol.add(str(_features[i][0])) else: temp = line.replace('\"', '') feature = temp.replace('\\\\', '\\') if feature in _names: i = _names.index(feature) sol.add(str(_features[i][0])) else: if data[0] in kn: k = kn.index(data[0]) dv = kf[k][3].replace('\"', '') dv = dv.replace('\\\\', '\\') feature = data[0] + '=' + dv if feature in _names: i = _names.index(feature) sol.add(str(_features[i][0])) # else: # print("Not in dimacs: " + feature) else: for ft in kn: print(ft) print('nb: ' + line + "," + feature) # outfile = cdir + '/check.dimacs' # gen_dimacs(_vars, _clauses, sol, outfile) # res = getoutput(SHARPSAT + ' -q ' + outfile) # print(line + ": " + str(res)) # print(sol) # if not is_int(res): # print(file + " failed") # return outfile = cdir + '/check.dimacs' gen_dimacs(_vars, _clauses, sol, outfile) res = getoutput(SHARPSAT + ' -q ' + outfile) if is_int(res): print(file + "," + str(len(_features) - len(sol)) + "," + str(len(ambig)) + "," + str(res) + "," + str(ambig)) else: print(file + "," + str(len(_features) - len(sol)) + "," + str(len(ambig)) + ",0")
print(file + "," + str(len(_features) - len(sol)) + "," + str(len(ambig)) + "," + str(res) + "," + str(ambig)) else: print(file + "," + str(len(_features) - len(sol)) + "," + str(len(ambig)) + ",0") # test Kclause models target = "axtls_2_1_4" dimacs = os.path.dirname( os.path.realpath(__file__)) + "/FM/" + target + ".dimacs" wdir = os.path.dirname(dimacs) + "/smarch" cdir = "/home/jeho/kmax/kconfig_case_studies/cases/" + target + "/data/kmax" # sample configurations features, clauses, vcount = read_dimacs(dimacs) const = [] #read_constraints(constfile, features) samples = sample(vcount, clauses, 1068, wdir, const, False, 1) # Test by Kbuild for Kmax gen_configs_kmax(dimacs, samples, cdir) test_kbuild(target, "data/kmax") # # Test by randconfig for Kmax # dimacs = "/home/jeho/kmax/kconfig_case_studies/cases/" + target + "/kconfig.dimacs" # test_randconfig_kmax(dimacs) # # # text kcr models # dimacs = os.path.dirname(os.path.realpath(__file__)) + "/FM/kcr/" + target + ".dimacs" # wdir = os.path.dirname(dimacs) + "/smarch" # cdir = home + "/kmax/kconfig_case_studies/cases/" + target + "/data/kcr"
def gen_configs_kcr(target_, dimacs_, samples_, cdir_): freevar = set() with open(os.path.dirname(dimacs_) + '/' + target_ + '.features') as file: for line in file: feature = line[0:len(line) - 1] if target_ not in ('axtls_2_1_4', 'uClibc-ng_1_0_29'): if not feature.startswith('CONFIG_'): feature = 'CONFIG_' + feature freevar.add(feature) features, clauses, vars = read_dimacs(dimacs_) _indexes = [i[0] for i in features] # generate .config files from samples n = 0 for s in samples_: config = "" for sel in s: if abs(sel) in _indexes: i = _indexes.index(abs(sel)) feature = features[i][1] if target_ not in ('axtls_2_1_4', 'uClibc-ng_1_0_29'): if not feature.startswith('CONFIG_'): feature = 'CONFIG_' + feature if feature in freevar: freevar.remove(feature) if feature != 'MODULES' and feature != 'CONFIG_MODULES' and 'CHOICE_' not in feature: if sel > 0: if '=' in feature: if '=' in feature: finfo = feature.split('=') if is_int(finfo[1]): config = config + feature + "\n" else: if finfo[1] == 'n': config = config + finfo[0] + '=0\n' else: config = config + finfo[0] + '=\"' + finfo[1] + "\"\n" else: config = config + feature + "=y\n" elif sel < 0: if '=' not in feature: config = config + "# " + feature + " is not set\n" else: if feature in freevar: freevar.remove(feature) for fv in freevar: r = random.random() if r < 0.5: config = config + "# " + fv + " is not set\n" else: config = config + fv + "=y\n" with open(cdir_ + "/" + str(n) + ".config", 'w') as outfile: outfile.write(config) outfile.close() n += 1 print("Configs generated")
def get_rank_unigen(dimacs_, sampleFile_): # read dimacs file for feature list _features, _clauses, _vars = read_dimacs(dimacs_) # read tree structure from file _treefile = os.path.dirname(dimacs_) + '/smarch/tree.json' # node = AnyNode(count=-1, cube=[]) if os.path.exists(_treefile): with open(_treefile, 'r') as file: data = file.read() importer = JsonImporter() _root = importer.import_(data) total = _root.count else: print("ERROR: tree file not found!") return with open(sampleFile_, 'r') as f: i = 1 for line in f: line = line[1:len(line)] raw = line.split() if len(raw) != 0: sol = raw[1:len(raw) - 1] #print(sol) # traverse tree based on solution _node = _root _precision = 0 _number = 0 _cdir = os.path.dirname(dimacs_) + '/' sdimacs = _cdir + "/sample.dimacs" ssol = _cdir + "/" + "sample.sol" gen_dimacs(_vars, _clauses, sol, sdimacs) getoutput("minisat " + sdimacs + " " + ssol) _outfile = os.path.dirname(sampleFile_) + '/validCheck.dimacs' gen_dimacs(_vars, _clauses, sol, _outfile) res = getoutput(SHARPSAT + ' -q ' + _outfile) fsol = list() with open(ssol, 'r') as f: fsol = f.read().split() del fsol[0] if is_int(res): print(res, end=',') while _precision == 0: _node, _precision, _number = traverse_cube( _node, fsol, _number) if _precision > 0: print( str(_number / total) + "," + str(_precision / total)) else: print("ERROR: tree traverse failure") else: print("ERROR: sample invalid") i += 1
def test_randconfig_kcr(dimacs_, target_, cdir_): # get features and clauses _features, _clauses, _vars = read_dimacs(dimacs_) kf, kc, kv = read_dimacs("/home/jeho/kmax/kconfig_case_studies/cases/" + target_ + "/kconfig.dimacs") _names = list() kn = [i[1] for i in kf] passed = 0 common = list() init = True prefix = False for f in _features: if len(f) > 2: name = f[1] for i in range(2, len(f)): name = name + ' ' + f[i] if not name.startswith('CONFIG_') and prefix: name = 'CONFIG_' + name _names.append(name) else: name = f[1] if not name.startswith('CONFIG_') and prefix: name = 'CONFIG_' + name _names.append(name) # iterate over each randconfig configurations for file in os.listdir(cdir_): if file.endswith('.config'): with open(cdir_ + "/" + file, 'r') as f: sol = set() ambig = set() for line in f: if line.startswith('#'): line = line[0:len(line) - 1] data = line.split() if len(data) > 4: if data[1] in _names: i = _names.index(data[1]) if i != -1: sol.add(-1 * _features[i][0]) # else: # print(line) else: line = line[0:len(line) - 1] data = line.split('=') if len(data) > 1: if data[1] == 'y': if data[0] in _names: i = _names.index(data[0]) sol.add(_features[i][0]) # else: # print('y: '+data[0]) elif data[1] == '\"\"' or data[1] == '0': if data[0] in kn: k = kn.index(data[0]) if kf[k][3] != '\"\"' and kf[k][3] != '0': feature = data[0] + '=n' if feature in _names: i = _names.index(feature) sol.add(_features[i][0]) # else: # print("Not in dimacs: " + feature) else: # print('n: ' + line) ambig.add(k) # feature = data[0] + '=n' # if feature in _names: # i = _names.index(feature) # sol.add(str(_features[i][0])) else: temp = line.replace('\"', '') feature = temp.replace('\\\\', '\\') if feature in _names: i = _names.index(feature) sol.add(_features[i][0]) else: if data[0] in kn: k = kn.index(data[0]) dv = kf[k][3].replace('\"', '') dv = dv.replace('\\\\', '\\') feature = data[0] + '=' + dv if feature in _names: i = _names.index(feature) sol.add(_features[i][0]) # else: # print("Not in dimacs: " + feature) else: for ft in kn: print(ft) print('nb: ' + line + "," + feature) # outfile = cdir + '/check.dimacs' # gen_dimacs(_vars, _clauses, sol, outfile) # res = getoutput(SHARPSAT + ' -q ' + outfile) # print(line + ": " + str(res)) # print(sol) # if not is_int(res): # print(file + " failed") # return outfile = cdir_ + '/check.dimacs' gen_dimacs(_vars, _clauses, sol, outfile) res = getoutput(SHARPSAT + ' -q ' + outfile) freecount = 0 for a in ambig: free = True test = sol.copy() test.add(a) gen_dimacs(_vars, _clauses, test, outfile) ares = getoutput(SHARPSAT + ' -q ' + outfile) if not is_int(ares): free = False test = sol.copy() test.add(-1 * a) gen_dimacs(_vars, _clauses, test, outfile) ares = getoutput(SHARPSAT + ' -q ' + outfile) if not is_int(ares): free = False if free: freecount += 1 # else: # print(file + ": " + str(res)) # if init: # common = sol # init = False # else: # for s in common: # if s not in sol: # common.remove(s) if is_int(res): val = int(res) / (2**freecount) print(file + ": " + str(val)) if val == 1.0: passed += 1 else: print(file + ": " + "INV") # if is_int(res): # print(file + ":" + str(len(_features) - len(sol)) + "," + str(len(ambig)) + "," + str(res) + "," + str(ambig)) # else: # print(file + ":" + str(len(_features) - len(sol)) + "," + str(len(ambig)) + ",0") print("Passed:" + str(passed)) print(common)