def testLogic(file_path): json_string = "" with io.open(file_path, 'r') as file: for line in file.readlines(): json_string += line.split('#')[0].replace('\n', ' ') json_string = re.sub(' +', ' ', json_string) try: region_json = json.loads(json_string) except json.JSONDecodeError as error: raise Exception("JSON parse error around text:\n" + \ json_string[error.pos-35:error.pos+35] + "\n" + \ " ^^\n") #print(region_json) alg = boolean.BooleanAlgebra() t, f, n, a, o, s = alg.definition() for region in region_json: #print(region['region_name']) pregion = region if ('locations' in region): for loc, rule in region['locations'].items(): pregion['locations'][loc] = { 'logic': rule, 'items': [], 'paths': [] } #print("\t" + loc) rule = parseFunctions(rule) print("\t\t" + rule) boolrule = alg.parse(rule, simplify=True) syms = boolrule.symbols args = len(syms) if args > 0: testReplacements(syms, s, pregion['locations'][loc]['logic'])
def parseLogicBool(file_path): json_string = "" with io.open(file_path, 'r') as file: for line in file.readlines(): json_string += line.split('#')[0].replace('\n', ' ') json_string = re.sub(' +', ' ', json_string) try: region_json = json.loads(json_string) except json.JSONDecodeError as error: raise Exception("JSON parse error around text:\n" + \ json_string[error.pos-35:error.pos+35] + "\n" + \ " ^^\n") #print(region_json) alg = boolean.BooleanAlgebra() t, f, n, a, o, s = alg.definition() region_map = [] for region in region_json: print(region['region_name']) pregion = region if ('locations' in region): for loc,rule in region['locations'].items(): pregion['locations'][loc] = {'logic': rule, 'items':[], 'paths':[]} print("\t" + loc) rule = parseFunctions(rule) print("\t\t" + rule) boolrule = alg.parse(rule, simplify=True) syms = boolrule.symbols args = len(syms) if args > 0: ttable = list(itertools.product([f, t], repeat=args)) total = len(ttable) k = 0 for combo in ttable: k = k + 1 print(str(k) + ' of ' + str(total), end='\r', flush=True) tup = {} evalrule = boolrule for i,sym in enumerate(syms): tup[sym] = combo[i] r = evalrule.subs(tup, simplify=True) if r: pregion['locations'][loc]['items'].append(compressItems(syms, tup, s)) #print(pregion['locations'][loc]['items']) #print(json.dumps(pregion['locations'][loc])) #print(str(combo) + " " + str(r)) else: r = boolrule st = "N/A" if r: pregion['locations'][loc]['items'].append(compressItems(syms, [True], s)) #print(json.dumps(pregion['locations'][loc])) #print(st + " " + str(r)) region_map.append(pregion) print(json.dumps(region_map))
def writeFaultTree(location, loc, rule, outpath): logic_ft = outpath + loc + '.xml' #logic_string = region_json[1]['locations']['Ice Cavern Iron Boots Chest'] #logic_string = logic_string.replace('can_use(Dins_Fire)','(Dins_Fire and Magic_Meter)') logic_string = parseFunctions(rule) #print(logic_string) ft = buildGate(logic_string) with io.open(logic_ft, 'w') as f: f.write(ft) #print(ft) return logic_ft