-
Notifications
You must be signed in to change notification settings - Fork 0
/
trc_print.py
209 lines (179 loc) · 7.48 KB
/
trc_print.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from trc import Formula, ConditionalFormula, TypeAtom, TupleAttributeOrConstant
from trc import FormulaType
from trc import ConditionalFormula
from trc import AndFormula
from trc import OrFormula
from trc import ImplyFormula
from trc import ParenthesesFormula
from trc import SingleCondition
from trc import SingleConditionType
from trc import CompareAtom
from trc import TypeAtom
from trc import TupleAttributeOrConstant
from trc import TupleAttributeOrConstantType
from trc import Tuple
from trc import TupleAttribute
from trc import Constant
from trc import Trc
from trc import parseTrc
from normalisation import NormaliseFormula
import trc
import lex
# 1.Conditional Formula
def conFormulaToStr(inputConFormula: Formula) -> str: #DONE
if inputConFormula.type != FormulaType.ConditionalFormula:
assert ("ERROR: conFormulaToStr - input Formula type is not FormulaType.ConditionalFormula")
return ""
actualConFor: ConditionalFormula = inputConFormula.actualFormula
conditionsStr = ""
singCon: SingleCondition
for singCon in actualConFor.condition:
conTypeStr = ""
if singCon.type == SingleConditionType.EveryCondition:
conTypeStr = "∀"
elif singCon.type == SingleConditionType.ExistCondition:
conTypeStr = "∃"
elif singCon.type == SingleConditionType.NotExistConditon:
conTypeStr = "¬∃"
conditionsStr = conditionsStr + conTypeStr + singCon.tuple.value + ' '
formulaStr = FormulaToStr(actualConFor.formula)
return conditionsStr + formulaStr
# 2. And Formula
def andFormulaToStr(inputAndFormula: Formula) -> str: #DONE
if inputAndFormula.type != FormulaType.AndFormula:
assert ("ERROR: andFormulaToStr - input Formula type is not FormulaType.AndFormula")
return ""
outputStr = ""
actualAndFor: AndFormula = inputAndFormula.actualFormula
left: Formula = actualAndFor.formulaLeft
right: Formula = actualAndFor.formulaRight
leftStr = '(' + FormulaToStr(left) + ')'
rightStr = '(' + FormulaToStr(right) + ')'
outputStr = leftStr + ' ' + '∧' + ' ' + rightStr
return outputStr
# 3. Or Formula
def orFormulaToStr(inputOrFormula: Formula) -> str: #DONE
if inputOrFormula.type != FormulaType.OrFormula:
assert ("ERROR: orFormulaToStr - input Formula type is not FormulaType.OrFormula")
return ""
outputStr = ""
actualOrFor: OrFormula = inputOrFormula.actualFormula
left: Formula = actualOrFor.formulaLeft
right: Formula = actualOrFor.formulaRight
leftStr = '(' + FormulaToStr(left) + ')'
rightStr = '(' + FormulaToStr(right) + ')'
outputStr = leftStr + ' ' + '∨' + ' ' + rightStr
return outputStr
# 4. Neg Formula
def negFormulaToStr(inputNegFormula: Formula)->str: #DONE
if inputNegFormula.type != FormulaType.NegFormula:
assert ("ERROR: negFormulaToStr - input Formula type is not FormulaType.OrFormula")
return ""
outputStr = ""
innerFormulaStr = '(' + FormulaToStr(inputNegFormula.actualFormula) + ')'
outputStr = '¬' + innerFormulaStr
return outputStr
# 5. Imply Formula
def implyFormulaToStr(inputImplyFormula: Formula)->str: #DONE
if inputImplyFormula.type != FormulaType.ImplyFormula:
assert ("ERROR: implyFormulaToSt - input Formula type is not FormulaType.ImplyFormula")
return ""
outputStr = ""
actualImpyFor:ImplyFormula = inputImplyFormula.actualFormula
leftStr = FormulaToStr(actualImpyFor.formulaLeft)
rightStr = FormulaToStr(actualImpyFor.formulaRight)
outputStr = leftStr + ' ' + "->" + ' ' + rightStr
return outputStr
# 6. Parenthese Formula
def parentheseFormulaToStr(inputParentheseFormula: Formula)->str: #DONE
if inputParentheseFormula.type != FormulaType.ParenthesesFormula:
assert ("ERROR: parentheseFormulaToStr - input Formula type is not FormulaType.ParenthesesFormula")
return ""
actualParentheseFor: ParenthesesFormula = inputParentheseFormula.actualFormula
outputStr = '(' + FormulaToStr(actualParentheseFor.formula) + ')'
return outputStr
# 7.Atom
def atomToStr(inputAtomFormula: Formula) -> str: #DONE
if inputAtomFormula.type != FormulaType.Atom:
assert ("ERROR: atomToStr - input Formula type is not FormulaType.Atom")
return ""
actualAtom = inputAtomFormula.actualFormula
outputStr = ""
# 1. compare atom
'''
<Atom> -> <CompareAtom>
<CompareAtom> -> <TupleAttributeOrConstant> <Bop> <TupleAttributeOrConstant>
<TupleAttributeOrConstant> -> <TupleAttribute>
<TupleAttribute> -> <Tuple>.<AttributeName>
<AttributeName> -> <Identifier>
<TupleAttributeOrConstant> -> <Constant>
<Constant> -> int
<Constant> -> string
'''
if isinstance(actualAtom, CompareAtom):
actualCompareAtom = actualAtom
left: TupleAttributeOrConstant = actualCompareAtom.left
right: TupleAttributeOrConstant = actualCompareAtom.right
leftStr = tupleAttributeOrConstantToStr(left)
rightStr = tupleAttributeOrConstantToStr(right)
bopStr = actualCompareAtom.bop.value
outputStr = leftStr + ' ' + bopStr + ' ' + rightStr
# 2. type atom
'''
<Atom> -> <TypeAtom>
<TypeAtom> -> <Tuple> ∈ <TableName>
<TableName> -> <Identifier>
<Identifier> -> [a-z][a-zA-Z]+
'''
if isinstance(actualAtom, TypeAtom):
actualTypeAtom: TypeAtom = actualAtom
tupleStr = actualTypeAtom.tuple.value
tableNameStr = actualTypeAtom.tableName.value
outputStr = tupleStr + ' ' + '∈' + ' ' + tableNameStr
return outputStr
def tupleAttributeOrConstantToStr(inpuAttrOrCons: TupleAttributeOrConstant) -> str:
if inpuAttrOrCons.type == TupleAttributeOrConstantType.TupleAttribute:
inputTupleAttr: TupleAttribute = inpuAttrOrCons.tupleAttribute
return inputTupleAttr.tuple.value + '.' + inputTupleAttr.attribute.value
elif inpuAttrOrCons.type == TupleAttributeOrConstantType.Constant:
inputConstant: Constant = inpuAttrOrCons.constant
return str(inputConstant.value)
else:
assert ("ERROR: tupleAttributeOrConstantToStr - input type is not TupleAttribute or Constant")
return ""
formulaToStrSwitch = {
FormulaType.ConditionalFormula: conFormulaToStr,
FormulaType.AndFormula: andFormulaToStr,
FormulaType.OrFormula: orFormulaToStr,
FormulaType.NegFormula: negFormulaToStr,
FormulaType.ImplyFormula: implyFormulaToStr,
FormulaType.ParenthesesFormula: parentheseFormulaToStr,
FormulaType.Atom: atomToStr
}
def FormulaToStr(inputFormula: Formula) -> str:
return formulaToStrSwitch[inputFormula.type](inputFormula)
def TupleToStr(inputTuple: Tuple) -> str:
return inputTuple.value
def TrcToStr(inputTrc: Trc) -> str:
outputStr = ""
tupleStr = TupleToStr(inputTrc.tuple)
formulaStr = FormulaToStr(inputTrc.formula)
outputStr = '{ ' + tupleStr + ' | ' + formulaStr + ' }'
return outputStr
def printTrc(inputTrc: Trc):
print(TrcToStr(inputTrc))
return
if __name__ == "__main__":
# f = open("input1.txt", encoding='UTF-8')
f = open("normalisation_test/test1_atom_parenthese_and.txt", encoding='UTF-8')
source = (f.read())
tokens = lex.getTokens(source)
print("Finish tokenizing:")
for token in tokens:
print(token)
print("Start parsing")
originalTrc = parseTrc(tokens) # TODO:trc.py处理implication 好像有点问题
print("Finish parsing")
print("start print trc formula")
printTrc(originalTrc)
print("Finish print trc formula")