-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_exprs.py
217 lines (159 loc) · 7.12 KB
/
run_exprs.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
210
211
212
213
214
215
216
217
from __eval.expression_makeup import *
from ctrlstrct_run import *
# main()
DATA = test_items_to_expressions(load_all_test_items())
PREV_COUNT = {}
def proccess_onto_with_reasoner(reasoning, count=30):
expr_chain = make_expr_chain(DATA, count)
if reasoning in PREV_COUNT and len(expr_chain) == PREV_COUNT[reasoning]:
print('Skip this iteration')
return None
PREV_COUNT[reasoning] = len(expr_chain)
if reasoning == "pellet":
onto = prepare_ontology(expr_chain, inject_swrl=True)
if True:
debug_rdf_fpath = 'exprs_dump.rdf' # +"_ext.rdf"
onto.save(file=debug_rdf_fpath, format='rdfxml')
debug_rdf_fpath = 'exprs_dump.n3'
onto.save(file=debug_rdf_fpath, format='ntriples')
print(f"Saved RDF file: {debug_rdf_fpath} !")
else:
debug_rdf_fpath = None
print(">_ running Pellet ...")
# if _eval_max_traces is not None:
measure_stats_for_pellet_running()
start = timer()
with onto:
# запуск Pellet
try:
sync_reasoner_pellet(infer_property_values=True, infer_data_property_values=True, debug=0)
except Exception as e:
print(e)
end = timer()
seconds = end - start
time_report = " Time elapsed: %.3f s." % seconds
print(">_ Pellet finished")
print(time_report)
# if _eval_max_traces is not None:
run_stats = get_process_run_stats()
run_stats.update({"wall_time": seconds})
run_stats.update({"count": len(expr_chain)})
if debug_rdf_fpath:
onto.save(file=debug_rdf_fpath+"_ext.rdf", format='rdfxml')
print(f"Saved RDF file: {debug_rdf_fpath}_ext.rdf !")
return run_stats
onto = prepare_ontology(expr_chain, inject_swrl=False)
if reasoning in ("clingo", "dlv"):
# raise RuntimeError("ASP solver does not support match(regex, str)!")
print(f">_ running {reasoning} ...")
import asp_helpers
measure_f, run_f = {
"clingo": (measure_stats_for_clingo_running, asp_helpers.run_clingo_on_ontology),
"dlv": (measure_stats_for_dlv_running, asp_helpers.run_DLV_on_ontology),
}.get(reasoning)
# if _eval_max_traces is not None:
measure_f()
start = timer()
# запуск Clingo / DLV
onto, elapsed_times = run_f(onto, rules_fpath='expr_penskoy.asp', stats=True) # ?
end = timer()
seconds = end - start
time_report = " Time elapsed: %.3f s." % seconds
print(f">_ {reasoning} finished")
print(time_report)
# if _eval_max_traces is not None:
run_stats = get_process_run_stats()
run_stats.update(elapsed_times) # add data from dict
run_stats.update({"count": len(expr_chain)})
return run_stats
# if debug_rdf_fpath:
# onto.save(file=debug_rdf_fpath+"_ext.rdf", format='rdfxml')
# print(f"Saved RDF file: {debug_rdf_fpath}_ext.rdf !")
if reasoning == "prolog":
name_in = "pl_in_expr.rdf"
name_out = "pl_out_expr.rdf"
onto.save(file=name_in, format='rdfxml')
eval_stats = run_swiprolog_reasoning(name_in, name_out, verbose=1, command_name="run_ontology")
# if _eval_max_traces is not None:
eval_stats.update({"count": len(expr_chain)})
return eval_stats
# clear_ontology(onto)
# onto = get_ontology("file://" + name_out).load()
# seconds = eval_stats['wall_time']
if reasoning in ('sparql', 'jena'):
name_in = f"{reasoning}_in_expr.n3"
name_out = f"{reasoning}_out_expr.n3"
onto.save(file=name_in, format='ntriples')
rules_path = {
'jena': "jena/all_for_exprs.rules",
'sparql': "expr_penskoy.ru",
}[reasoning]
eval_stats = run_jena_reasoning(name_in, name_out, reasoning_mode=reasoning, verbose=1, rules_path=rules_path)
# if _eval_max_traces is not None:
eval_stats.update({"count": len(expr_chain)})
return eval_stats
# clear_ontology(onto)
# onto = get_ontology("file://" + name_out).load()
# seconds = eval_stats['wall_time']
raise ValueError(reasoning)
def eval_expressions():
eval_results = []
# 46
for n in sorted({
17
# *range(50, 52 + 1, 6),
# *range(5, 20 + 1, 5),
# *range(25, 30 + 1, 5),
# *range(18, 29 + 1, 1),
# 29
}):
# reasoners = ("pellet", )
# reasoners = ("prolog", );
reasoners = ("sparql", )
# reasoners = ("jena", )
# reasoners = ("jena", "sparql")
# reasoners = ("prolog", "sparql")
# reasoners = ("clingo", "sparql")
# reasoners = ("jena", "prolog", "sparql")
# reasoners = ("clingo", "jena", "prolog", "sparql")
# reasoners = ("pellet", "clingo", "jena", "prolog", "sparql")
for reasoning_type in reasoners:
print(' > > > > > > > > > > > > > ')
print(f" Running {n} operands with {reasoning_type}")
print(' < < < < < < < < < < < < < ')
eval_result = proccess_onto_with_reasoner(count=n, reasoning=reasoning_type)
if eval_result is None:
continue
eval_item = {
'dataset': 'penskoy-expressions',
'reasoner': reasoning_type,
'count': n,
}
eval_item.update(eval_result)
# dump current result
with open('partial_eval_expr.txt', "a") as file:
file.write(str(eval_item))
file.write('\n')
eval_results.append(eval_item)
# break
# dump full result
with open('saved_eval_expr.txt', "a") as file:
for eval_item in eval_results:
file.write(str(eval_item))
file.write('\n')
print(' ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ')
print("Expr eval finished.")
# exit()
def convert_rules():
from rule_converter import to_prolog, to_jena, to_sparql, to_clingo
from __eval.expression_laws import get_owl_swrl_laws
_owl, swrl = get_owl_swrl_laws()
# to_prolog(swrl, out_path='expr_penskoy.pl', iri_prefix="http://penskoy.n/expressions#")
# to_jena(swrl, out_path='expr_penskoy.jena_rules')
# to_sparql(swrl, out_path='expr_penskoy.ru', base_iri="http://penskoy.n/expressions#")
to_clingo(swrl, out_path='expr_penskoy.asp') # matched() built-in was polyfilled.
def main():
# convert_rules()
eval_expressions()
if __name__ == '__main__':
main()