-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpq_reformat.py
39 lines (27 loc) · 1.07 KB
/
rpq_reformat.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
"""
File name: rpq_reformat.py
Author: Temur Malishava
"""
from rpq import *
# ((a+/b)+/c+)+ => ((a*/(a/b))*/((a*/(a/b))/c))+
def rpq_reformat(rpq):
if isinstance(rpq, RPQ_Plus):
rpq = RPQ_Plus(rpq_reformat(rpq.subRPQ))
if isinstance(rpq, RPQ_Concat):
l = rpq_reformat(rpq.left_subRPQ) # a+
r = rpq_reformat(rpq.right_subRPQ) # b
rpq = inner_reform(l, r)
return rpq
def inner_reform(left, right):
main = None
if isinstance(left, RPQ_Plus) and isinstance(right, RPQ_Plus): # a+/b+
main = RPQ_S(left.subRPQ, RPQ_Concat(left.subRPQ, right.subRPQ), right.subRPQ) # a, (a/b), b
if isinstance(left, RPQ_Plus) and not isinstance(right, RPQ_Plus): # a+/b
main = RPQ_S(left.subRPQ, RPQ_Concat(left.subRPQ, right), None) # a, (a/b), None
if not isinstance(left, RPQ_Plus) and isinstance(right, RPQ_Plus): # a/b+
main = RPQ_S(None, RPQ_Concat(left, right.subRPQ), right.subRPQ) # None, (a/b), b
if main is not None:
return main
# a/b
else:
return RPQ_Concat(left, right)