-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswrl_builder.py
111 lines (93 loc) · 4.06 KB
/
swrl_builder.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
# Copyright 2018 Bram Steenwinckel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
failures = None
observations = None
rules = None
def build_rule(rule):
if '>=' in rule:
s = rule.split('>=')
return "BuiltInAtom(<http://www.w3.org/2003/11/swrlb#greaterThanOrEqual> Variable(:"+s[0]+') "'+s[1]+'"^^xsd:decimal) DataPropertyAtom(:hasValue Variable(:result) Variable(:'+s[0]+'))'
if '=>' in rule:
s = rule.split('=>')
return "BuiltInAtom(<http://www.w3.org/2003/11/swrlb#greaterThanOrEqual> Variable(:"+s[0]+') "'+s[1]+'"^^xsd:decimal) DataPropertyAtom(:hasValue Variable(:result) Variable(:'+s[0]+'))'
if '<=' in rule:
s = rule.split('<=')
return "BuiltInAtom(<http://www.w3.org/2003/11/swrlb#lessThanOrEqual> Variable(:"+s[0]+') "'+s[1]+'"^^xsd:decimal) DataPropertyAtom(:hasValue Variable(:result) Variable(:'+s[0]+'))'
if '=<' in rule:
s = rule.split('=<')
return "BuiltInAtom(<http://www.w3.org/2003/11/swrlb#lessThanOrEqual> Variable(:"+s[0]+') "'+s[1]+'"^^xsd:decimal) DataPropertyAtom(:hasValue Variable(:result) Variable(:'+s[0]+'))'
if '<' in rule:
s = rule.split('<')
return "BuiltInAtom(<http://www.w3.org/2003/11/swrlb#lessThan> Variable(:"+s[0]+') "'+s[1]+'"^^xsd:decimal) DataPropertyAtom(:hasValue Variable(:result) Variable(:'+s[0]+'))'
if '>' in rule:
s = rule.split('>')
return "BuiltInAtom(<http://www.w3.org/2003/11/swrlb#greaterThan> Variable(:"+s[0]+') "'+s[1]+'"^^xsd:decimal) DataPropertyAtom(:hasValue Variable(:result) Variable(:'+s[0]+'))'
def build_observation(obs):
return "ClassAtom(:"+obs+" Variable(:o)) ObjectPropertyAtom(<http://www.w3.org/ns/sosa/hasResult> Variable(:o) Variable(:result))"
def build_failure(fail):
return "Head(ClassAtom(:"+fail+" Variable(:o)))"
def prefix(number):
return 'DLSafeRule(Annotation(<http://swrl.stanford.edu/ontologies/3.3/swrla.owl#isRuleEnabled> "true"^^xsd:boolean) Annotation(rdfs:comment ""^^xsd:string) Annotation(rdfs:label "S'+str(number)+'"^^xsd:string) Body('
def get_paths(f,number, s):
if len(f) == 1:
if f[0]['head'] in observations:
s+=build_observation(f[0]['head'])
if f[0]['rule']:
s+=build_rule(f[0]['rule'])
s+=')'
if f[0]['tail'] in failures:
s+=build_failure(f[0]['tail'])
#s+=f[0]['head']+'-'+f[0]['rule']+'->'+f[0]['tail']
global ruled
ruled = prefix(number)+s+')'
else:
if f[0]['head'] in observations:
s+=build_observation(f[0]['head'])
if f[0]['rule']:
s+=build_rule(f[0]['rule'])
#print(build_rule(f[0]['rule']))
#s+=f[0]['head']+'-'+f[0]['rule']+'->'+f[0]['tail']+'^'
f.pop(0)
get_paths(f[0],number, s)
ruled = ''
def produce_swrl_rules(file_name):
global ruled
with open(file_name) as f:
data = json.load(f)
global failures
failures = data['Failures']
global observations
observations = data['Observations']
global rules
rules = data['Rules']
elements = []
for f in failures:
for d in rules:
if d['tail'] == f:
elements.append([d])
finished = []
for e in elements:
if e[0]['head'] in observations:
finished.append(e)
else:
for d in rules:
if d['tail'] == e[0]['head']:
elements.append([d, e])
final_rules = ''
for f in range(0, len(finished)):
get_paths(finished[f],f, '')
final_rules += ruled+'\n'
ruled = ''
return final_rules