-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_bppy.py
100 lines (79 loc) · 2.6 KB
/
main_bppy.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
# import openai
import argparse
import importlib
import os
parser = argparse.ArgumentParser()
parser.add_argument("parameters", nargs="*", default=["rs1"])
args = parser.parse_args()
example = args.parameters[0]
with open(os.path.dirname(os.path.realpath(__file__)) + "/tests/" + example + ".py", "r") as f:
exec(f.read())
# with open('secrets/openai_api_key', 'r') as f_api_key:
# openai.api_key = f_api_key.read()
with open('files/intro_bppy.txt', 'r') as f_intro:
intro = f_intro.read()
with open('files/hot_cold_bppy.py', 'r') as f_hot_cold:
hot_cold = f_hot_cold.read()
prompt = intro.format(hot_cold=hot_cold, requirements=prompt)
print(prompt)
# completion = openai.ChatCompletion.create(model="gpt-3.5-turbo",
# messages=[{"role": "user", "content": prompt}],
# temperature=0)
# answer = completion.choices[0].message.content
# print(answer)
# with open("answers/bppy/rs10", "w") as f:
# f.write(answer)
with open("answers/bppy/rs10", "r") as f:
answer = f.read()
print(answer)
def get_functions(answer):
f_dict = {}
try:
exec(answer, globals(), f_dict)
except Exception as e:
pass
#print(e)
d = dict([(k, v) for k, v in f_dict.items()])
f = []
for k, v in d.items():
if isfunction(v) and v.__name__ == "wrapper":
f.append(v)
return f
from bppy import *
bthreads = get_functions(answer)
#print(bthreads)
class EvaluatorListener(PrintBProgramRunnerListener):
def starting(self, b_program):
self.events = []
def ended(self, b_program):
pass
def event_selected(self, b_program, event):
self.events.append(event.name)
if len(self.events) == 20:
raise TimeoutError()
def init_bprogram():
eval = EvaluatorListener()
return BProgram(bthreads=[x() for x in bthreads],
event_selection_strategy=SimpleEventSelectionStrategy(),
listener=eval)
n = 100
results = [0] * Test(trace=[]).tests_num
for i in range(n):
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
bprog = init_bprogram()
bprog.run()
except TimeoutError:
pass
trace = bprog.listener.events
#print(trace)
test_obj = Test(trace=trace)
for j in range(len(results)):
try:
test_obj.__getattribute__("req_" + str(j + 1))()
results[j] += 1
except AssertionError:
pass
print("alignment of 100 sampled traces with requirements:")
print([x / n for x in results])