-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule_converter.py
412 lines (317 loc) · 12.7 KB
/
rule_converter.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# rule_converter.py
import re
### PROLOG ###
RE_q2upper = re.compile(r'\?([\w\d]+)', flags=re.I)
def q2upper_replace(m):
s = m[1].upper()
if s.startswith('_'):
s = 'Tmp' + s
return s
def convert_varnames(s):
return RE_q2upper.sub(q2upper_replace, s)
RE_predicate = re.compile(r'([\w\d]+)\(((?:[^)]|"\)|\)")+)\)') # 1: name, 2: args in braces
def lower_first_char(s):
return f"{s[0].lower()}{s[1:]}" if s else s
def getting_datatype_property(varname):
# ^^(IB,_)
return f'^^({varname},_)'
RE_DatatypeProp = re.compile(r'id|\w*index|exec_time|\w*iteration_n|text_line|step|text|precedence')
def using_boolean_literal(literal):
if literal in ('true', 'false'):
return f'"{literal}"^^xsd:boolean'
return literal
def getting_property(propname, varname):
if RE_DatatypeProp.match(propname):
return getting_datatype_property(varname)
else:
return (varname)
def predicate2lowerfirst_replace(pred_match):
name, args_str = pred_match[1], pred_match[2]
args = [s.strip() for s in args_str.split(", ")]
if len(args) == 2:
args[1] = getting_property(name, args[1])
args_str = ", ".join(args)
return f"{lower_first_char(name)}({args_str})"
def convert_predicate_calls_prolog(s):
return RE_predicate.sub(predicate2lowerfirst_replace, s)
IRI_type = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
IRI_prefix = 'http://vstu.ru/poas/ctrl_structs_2020-05_v1#'
# rdf_assert(B, 'http://vstu.ru/poas/ctrl_structs_2020-05_v1#index', IB) ,
# rdf_assert(B, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://vstu.ru/poas/ctrl_structs_2020-05_v1#SequenceBegin') ,
def convert_rulehead(s):
body, head = list(s.split(" -> "))
body = body.rstrip(' \t\n,')
new_head = []
head_predicates = RE_predicate.finditer(head)
for pred_match in head_predicates:
# new_head.append(f"% -> {pred_match[0].strip()}")
name, args_str = pred_match[1], pred_match[2]
args = [s.strip() for s in args_str.split(",")]
if len(args) == 2:
args[1] = using_boolean_literal(args[1])
prolog_call = f"rdf_assert({args[0]}, '{IRI_prefix}{name}', {args[1]})"
elif len(args) == 1:
prolog_call = f"rdf_assert({args[0]}, '{IRI_type}', '{IRI_prefix}{name}')"
else:
raise pred_match
new_head.append(prolog_call)
rule = "\t" + (',\n\t'.join([body, *new_head, 'fail.'])) + '\n'
return rule
def swrl2prolog(swrl, name=None):
rule = convert_predicate_calls_prolog(convert_rulehead(convert_varnames(swrl))).replace('# ', '% ')
if not name:
title = '% Rule\n'
else:
title = f'% Rule: {name}\n'
debug_print_rulename = f"writeln('\t{name},')," if 0 else ""
return f'''{title}swrl_rule() :- {debug_print_rulename}\n{rule}'''
def to_prolog(rules, out_path='from_swrl.pl', iri_prefix=None):
if iri_prefix:
set_IRI_prefix(iri_prefix)
with open(out_path, 'w') as file:
for rule in rules:
# swrl = rule._original_swrl
swrl = rule.swrl
prolog = swrl2prolog(swrl, f'{rule.name} [{" & ".join(rule.tags)}]')
file.write('\n')
file.write(prolog)
def set_IRI_prefix(iri_prefix):
global IRI_prefix
IRI_prefix = iri_prefix
### JENA ###
LOCAL_PREFIX = "my:"
RDF_TYPE = "rdf:type"
RE_SWRL_builtins = re.compile(r'lessThan|greaterThan|notEqual|equal|add|matches')
RE_boolean_value = re.compile(r'^(?:true|false)$')
# varnames conflicting with classnames
RE_conflicting_varnames = re.compile(r'\b(?:loop|cond|body)\b')
def convert_builtin(name: str, args: list):
if name == 'add':
# change the order (result is first in SWRL but last in Jena)
args = [args[1], args[2], args[0]]
name = 'sum'
if name == 'matches':
name = 'regex'
args_str = ", ".join(args)
return f"{name}({args_str})"
def convert_argument(plain: str):
plain = fix_conflicting_variable(plain)
plain = convert_literal(plain)
return plain
def convert_literal(plain: str):
if RE_boolean_value.match(plain):
# "true"^^xsd:boolean
return f'"{plain}"^^xsd:boolean'
return plain
def fix_conflicting_variable(plain: str):
if RE_conflicting_varnames.search(plain):
# add a couple of underscores
return plain + '__'
return plain
def predicate2triple_replace(pred_match):
name, args_str = pred_match[1], pred_match[2]
args = [convert_argument(s.strip()) for s in args_str.split(", ")]
if len(args) == 1:
# type checking
return f"({args[0]} {RDF_TYPE} {LOCAL_PREFIX}{name})"
# args[1:] = map(convert_argument, args[1:])
if RE_SWRL_builtins.match(name):
return convert_builtin(name, args)
else:
assert len(args) == 2, f"{len(args)}, in {name}"
# args[1] = args[1].replace('"', "'") # " -> ' (Jena accepts single-quoted strings by docs, both types actually)
return f"({args[0]} {LOCAL_PREFIX}{name} {args[1]})"
def convert_predicate_calls_jena(s):
return RE_predicate.sub(predicate2triple_replace, s)
def swrl2jena(swrl, name=None):
rule = convert_predicate_calls_jena(swrl)
if not name:
title = '# Rule'
else:
title = f'# Rule: {name}'
return f'''{title}\n[{rule}]'''
def to_jena(rules, out_path='from_swrl.jena_rules'):
with open(out_path, 'w') as file:
for rule in rules:
# swrl = rule._original_swrl
swrl = rule.swrl
jena = swrl2jena(swrl, f'{rule.name} [{" & ".join(sorted(rule.tags))}]')
file.write('\n' * 2)
file.write(jena)
### SPARQL ###
RULE_END_PUNCT = " . "
RE_predicate_with_comma = re.compile(r'([\w\d]+)\(((?:[^)]|"\)|\)")+)\)\s*,?\s*') # 1: name, 2: args in braces
def convert_builtin2sparql(name: str, args: list):
if name == 'add':
# BIND(?ia + 1 as ?ib)
return f"BIND( {args[1]} + {args[2]} as {args[0]} )"
if name == 'matches':
# FILTER (regex(?var, 'regex-patern'))
return f"FILTER (regex ( {args[0]}, {args[1]} ))"
op = {
'lessThan' : '<',
'greaterThan' : '>',
'notEqual' : '!=',
'equal' : '=',
}.get(name, "==!No op!==")
return f"FILTER ( {args[0]} {op} {args[1]} )"
def predicate2sparql_triple_replace(pred_match):
name, args_str = pred_match[1], pred_match[2]
args = [s.strip() for s in args_str.split(", ")]
if len(args) == 1:
# type checking
return f"{args[0]} {RDF_TYPE} {LOCAL_PREFIX}{name}" + RULE_END_PUNCT
if RE_SWRL_builtins.match(name):
return convert_builtin2sparql(name, args) + RULE_END_PUNCT
else:
assert len(args) == 2, len(args)
return f"{args[0]} {LOCAL_PREFIX}{name} {args[1]}" + RULE_END_PUNCT
def convert_predicate_calls_sparql(s):
return RE_predicate_with_comma.sub(predicate2sparql_triple_replace, s)
def swrl2sparql(swrl, name=None):
s = convert_predicate_calls_sparql(swrl)
body, head = list(s.split(" -> "))
if not name:
title = '# Rule'
else:
title = f'# Rule: {name}'
return f'''{title}
INSERT
{{ {head.strip()} }}
WHERE
{{
{body.strip()}
}}'''
def to_sparql(rules, out_path='sparql_from_swrl.ru', heading_path='sparql/rdfs4core.ru', base_iri=None):
with open(out_path, 'w') as file:
with open(heading_path) as heading_file:
heading_text = heading_file.read()
if base_iri:
# ensure it ends with '#'
base_iri = base_iri.rstrip('#') + '#'
# replace PREFIX my: <IRI>
heading_text = re.sub(r'PREFIX my: .+?\n', 'PREFIX my: <%s>\n' % base_iri, heading_text)
file.write(heading_text)
for rule in rules:
# swrl = rule._original_swrl
swrl = rule.swrl
sparql = swrl2sparql(swrl, f'{rule.name} [{" & ".join(sorted(rule.tags))}]')
file.write(sparql)
file.write(' ;\n\n') # ';' is a query separator
### ASP: Clingo, DLV ###
def convert_builtin_asp(name: str, args: list):
if name == 'add':
# Note the order (result is first in SWRL)
# return f'{args[1]} + {args[2]} = {args[0]}'
return f'{args[0]} = {args[1]} + {args[2]}'
if name == 'matches':
# 1 = @matches(X, "[a-zA-Z_0-9]+")
return f'1 = @matches({args[0]}, {args[1]})'
op = {
'lessThan' : '<',
'greaterThan' : '>',
'notEqual' : '!=',
'equal' : '=',
}.get(name, "==!No op!==")
return f"{args[0]} {op} {args[1]}"
def predicate2lowerfirst_replace_asp(pred_match):
name, args_str = pred_match[1], pred_match[2]
name = lower_first_char(name)
args = [s.strip() for s in args_str.split(", ")]
if RE_SWRL_builtins.match(name):
return convert_builtin_asp(name, args)
if len(args) == 2:
args = (args[0], name, args[1])
elif len(args) == 1:
args = (args[0], 'type', name) # use 'type' in place of 'rdf:type'
args_str = ", ".join(args)
return f"t({args_str})"
def convert_predicate_calls_clingo(s):
return RE_predicate.sub(predicate2lowerfirst_replace_asp, s)
def convert_rulehead(s):
body, head = list(s.split(" -> "))
body = body.rstrip(' \t\n,')
new_rules = []
head_predicates = RE_predicate.finditer(head)
# Размножаем правило, оставляя в head по одному утверждению за раз
for pred in head_predicates:
rule = ("\t" + pred[0] + ':-' + body + '.\n')
new_rules.append(rule)
return ''.join(new_rules)
def swrl2clingo(swrl, name=None):
rule = convert_predicate_calls_clingo(convert_rulehead(convert_varnames(swrl))).replace('# ', '% ')
if not name:
title = '% Rule\n'
else:
title = f'% Rule: {name}\n'
debug_print_rulename = f"writeln('\t{name},')," if 0 else ""
return f'''{title}{rule}'''
def to_clingo(rules, out_path='from_swrl.asp', iri_prefix=None, heading_path='asp/clingo_polyfill.lp'):
if iri_prefix:
set_IRI_prefix(iri_prefix)
with open(out_path, 'w') as file:
with open(heading_path) as heading_file:
heading_text = heading_file.read()
file.write(heading_text)
for rule in rules:
# swrl = rule._original_swrl
swrl = rule.swrl
clingo = swrl2clingo(swrl, f'{rule.name} [{" & ".join(sorted(rule.tags))}]')
file.write('\n')
file.write(clingo)
def main():
import ctrlstrct_swrl
RULES = ctrlstrct_swrl.RULES
# to_prolog(RULES)
to_jena(RULES)
# to_sparql(RULES)
# to_clingo(RULES)
def convert_swrl_json(file_in=None, target='jena', backend_name=None):
import json
import trace_gen.txt2algntr
find_by_keyval_in = trace_gen.txt2algntr.find_by_keyval_in
if not file_in:
file_in = r'C:\D\Work\YDev\CompPr\CompPrehension\src\main\resources\com\example\demo\models\businesslogic\domains\programming-language-expression-domain-laws.json'
with open(file_in) as file:
data = json.load(file)
if isinstance(target, str):
# func_name = 'swrl2' + target
func_name = 'convert_predicate_calls_' + target
converter_func = globals()[func_name]
else:
converter_func = target
target = converter_func.__name__
assert hasattr(converter_func, '__call__'), func_name
if not backend_name:
backend_name = target.capitalize()
# modify "formulation" field in each SWRL-type law dict
rule_field = "formulation"
rule_backend = "backend"
for formulation_dict in find_by_keyval_in("backend", "SWRL", data):
swrl = formulation_dict.get(rule_field, None)
if swrl:
swrl = swrl.replace(" ^ ", ", ")
swrl = swrl.replace('swrlb:', '')
rule = converter_func(swrl) + '.'
formulation_dict[rule_field] = rule
formulation_dict[rule_backend] = backend_name
# append -target to file name
file_out = re.sub(r'(?=\.[^.]*)', "-" + target, file_in)
with open(file_out, 'w') as file:
json.dump(data, file, indent=2)
def convert_swrl_list_to_jena(L):
for swrl in L:
swrl = swrl.replace(" ^ ", ", ")
swrl = swrl.replace('swrlb:', '')
rule = convert_predicate_calls_jena(swrl) + '.'
print(rule)
SWRL_raw = [
(
"describe_error",
"student_pos_less(?b, ?a) ^ before_direct(?a, ?b) -> describe_error(?a, ?b)"
),
]
if __name__ == '__main__':
# convert_swrl_json()
main()
# convert_swrl_list_to_jena([e[1] for e in SWRL_raw])