Repository navigation
Expand file tree
/
Copy pathparseLogic.py
More file actions
145 lines (139 loc) · 5.73 KB
/
Copy pathparseLogic.py
File metadata and controls
145 lines (139 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys
import json
import os
import io
import re
import boolean
import itertools
from logicUtils import parseFunctions, compressItems, testReplacements
from convertLogic import writeFaultTree
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 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 parseLogicFT(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)
region_map = []
for region in region_json:
print(region['region_name'])
pregion = region
if ('locations' in region):
for loc,rule in region['locations'].items():
ft = writeFaultTree(file_path,rule,'./')
def parseLocLogicFT(file_path,loc):
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)
region_map = []
for region in region_json:
print(region['region_name'])
pregion = region
if ('locations' in region):
for l,rule in region['locations'].items():
if l == loc:
ft = writeFaultTree(file_path,loc,rule,'./')
logicDir = '../OoTR-5.1/data/World/'
file_path = logicDir + 'Deku Tree.json'
#parseLogicBool(file_path)
#parseLogicFT(file_path)
parseLocLogicFT(file_path,'GS Deku Tree Basement Back Room')
#testLogic(file_path)