-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLTL2STL.py
180 lines (167 loc) · 6.88 KB
/
LTL2STL.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
import random
def infix_to_prefix(formula):
precedence = {'!': 3, 'X': 2, 'F': 2, 'G': 2, '&': 1, '|': 1, 'i': 0, 'e': 0}
stack = []
prefix_formula = []
def is_operator(token):
return token in precedence.keys()
def has_higher_precedence(op1, op2):
return precedence[op1] > precedence[op2]
def split_formula(formula):
return formula.replace('(', ' ( ').replace(')', ' ) ').split()
tokens = split_formula(formula)
for token in reversed(tokens):
if token == ')':
stack.append(token)
elif token == '(':
while stack and stack[-1] != ')':
prefix_formula.append(stack.pop())
stack.pop()
elif is_operator(token):
while (stack and is_operator(stack[-1]) and
has_higher_precedence(stack[-1], token)):
prefix_formula.append(stack.pop())
stack.append(token)
else:
prefix_formula.append(token)
while stack:
prefix_formula.append(stack.pop())
return ' '.join(reversed(prefix_formula))
def prefix_LTL_to_STL(formula):
if formula.startswith('!'):
##print("negation formula:", formula)
arg, rest = prefix_LTL_to_STL(formula[2:])
##print(f"neg: arg:{arg}, rest:{rest}")
return f"('neg',({arg}))", rest
elif formula.startswith('X'):
#print("next")
arg, rest = prefix_LTL_to_STL(formula[2:])
#print(f"next: arg:{arg}, rest:{rest}")
#TODO: check with (1,1)
return f"('evntually', (1,2), ({arg}))", rest
elif formula.startswith('F'):
#print("eventually")
arg, rest = prefix_LTL_to_STL(formula[2:])
#print(f"event: arg:{arg}, rest:{rest}")
return f"('eventually', (0, timeunits -1), ({arg}))", rest
elif formula.startswith('G'):
#print("globally")
arg, rest = prefix_LTL_to_STL(formula[2:])
#print(f"glob: arg:{arg}, rest:{rest}")
return f"('always', (0, timeunites-1), ({arg}))", rest
elif formula.startswith('&'):
#print("conjunction")
arg1, rest = prefix_LTL_to_STL(formula[2:])
arg2, rest = prefix_LTL_to_STL(rest)
#print(f"conjun: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"('and',({arg1}), ({arg2}))", rest
elif formula.startswith('|'):
#print("disjunction")
arg1, rest = prefix_LTL_to_STL(formula[2:])
arg2, rest = prefix_LTL_to_STL(rest)
#print(f"disjun: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"('or', ({arg1}), ({arg2}))", rest
elif formula.startswith('i'):
##print("implication")
arg1, rest = prefix_LTL_to_STL(formula[2:])
arg2, rest = prefix_LTL_to_STL(rest)
#print(f"implic: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"('implication', ({arg1}), ({arg2}))", rest
elif formula.startswith('e'):
#print("equivalence")
arg1, rest = prefix_LTL_to_STL(formula[2:])
arg2, rest = prefix_LTL_to_STL(rest)
#print(f"equiv: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"('equivalence', ({arg1}), ({arg2}))", rest
elif formula.startswith('U'):
#print("until")
arg1, rest = prefix_LTL_to_STL(formula[2:])
arg2, rest = prefix_LTL_to_STL(rest)
#print(f"until: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"('until', (0, timeunites-1), ({arg1}), ({arg2}))", rest
elif formula.startswith('c'):
#print("symbol")
if len(formula) > 2:
#print("symbol rest: ", formula[3:])
return formula[:2], formula[3:]
else:
#print("symbol rest: ")
return formula[:2], ""
def prefix_LTL_to_scarlet(formula):
if formula.startswith('!'):
##print("negation formula:", formula)
arg, rest = prefix_LTL_to_scarlet(formula[2:])
##print(f"neg: arg:{arg}, rest:{rest}")
return f"!({arg})", rest
elif formula.startswith('X'):
# print("next")
arg, rest = prefix_LTL_to_scarlet(formula[2:])
# print(f"next: arg:{arg}, rest:{rest}")
return f"X({arg})", rest
elif formula.startswith('F'):
# print("eventually")
arg, rest = prefix_LTL_to_scarlet(formula[2:])
# print(f"event: arg:{arg}, rest:{rest}")
return f"F({arg})", rest
elif formula.startswith('G'):
# print("globally")
arg, rest = prefix_LTL_to_scarlet(formula[2:])
# print(f"glob: arg:{arg}, rest:{rest}")
return f"G({arg})", rest
elif formula.startswith('&'):
# print("conjunction")
arg1, rest = prefix_LTL_to_scarlet(formula[2:])
arg2, rest = prefix_LTL_to_scarlet(rest)
# print(f"conjun: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"&({arg1}, {arg2})", rest
elif formula.startswith('|'):
# print("disjunction")
arg1, rest = prefix_LTL_to_scarlet(formula[2:])
arg2, rest = prefix_LTL_to_scarlet(rest)
# print(f"disjun: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"|({arg1}, {arg2})", rest
elif formula.startswith('i'):
##print("implication")
arg1, rest = prefix_LTL_to_scarlet(formula[2:])
arg2, rest = prefix_LTL_to_scarlet(rest)
# print(f"implic: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"->({arg1}, {arg2})", rest
elif formula.startswith('e'):
# print("equivalence")
arg1, rest = prefix_LTL_to_scarlet(formula[2:])
arg2, rest = prefix_LTL_to_scarlet(rest)
# print(f"equiv: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"<->({arg1}, {arg2})", rest
elif formula.startswith('U'):
# print("until")
arg1, rest = prefix_LTL_to_scarlet(formula[2:])
arg2, rest = prefix_LTL_to_scarlet(rest)
# print(f"until: arg1:{arg1}, arg2:{arg2}, rest:{rest})")
return f"U({arg1}, {arg2})", rest
elif formula.startswith('c'):
# print("symbol")
if len(formula) > 2:
# print("symbol rest: ", formula[3:])
return formula[:2], formula[3:]
else:
# print("symbol rest: ")
return formula[:2], ""
'''
stl_declare = []
from Declare_formulas import formulas, formulas_names
for i in range(len(formulas)):
fn = formulas_names[i]
f = formulas[i]
print("--------------------------{}-------------------------------".format(fn))
print(i+1)
print("Original formula: ",f)
pf = infix_to_prefix(f)
print("in prefix format: ", pf)
stl, _ = prefix_LTL_to_STL(pf)
stl_declare.append(stl)
print("translated in STL: ", stl)
print("\n\n")
'''
#formula dataset
#15 and 19 and 1
#existence(c0,2) and chain_succession(c0, c1) and init(c0)