-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_rq.py
120 lines (112 loc) · 3.27 KB
/
generate_rq.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
import argparse, json
level_2 = """
UNION
{{
SELECT DISTINCT ?s
WHERE {{
GRAPH <http://reasoner.renci.org/nonredundant> {{
?s ?p1 ?o1 .
?o1 ?p2 node: .
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p1_p {{ {properties} rdfs:subClassOf }}
?p1 rdfs:subPropertyOf* ?p1_p .
}}
}}
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p2_p {{ {properties} rdfs:subClassOf }}
?p2 rdfs:subPropertyOf* ?p2_p .
}}
}}
}}
}}
}}
"""
level_3 = """
UNION
{{
SELECT DISTINCT ?s
WHERE {{
GRAPH <http://reasoner.renci.org/nonredundant> {{
?s ?p1 ?o1 .
?o1 ?p2 ?o2 .
?o2 ?p3 node: .
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p1_p {{ {properties} rdfs:subClassOf }}
?p1 rdfs:subPropertyOf* ?p1_p .
}}
}}
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p2_p {{ {properties} rdfs:subClassOf }}
?p2 rdfs:subPropertyOf* ?p2_p .
}}
}}
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p3_p {{ {properties} rdfs:subClassOf }}
?p3 rdfs:subPropertyOf* ?p3_p .
}}
}}
}}
}}
}}
"""
level_4 = """
UNION
{{
SELECT DISTINCT ?s
WHERE {{
GRAPH <http://reasoner.renci.org/nonredundant> {{
?s ?p1 ?o1 .
?o1 ?p2 ?o2 .
?o2 ?p3 ?o3 .
?o3 ?p4 node: .
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p1_p {{ {properties} rdfs:subClassOf }}
?p1 rdfs:subPropertyOf* ?p1_p .
}}
}}
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p2_p {{ {properties} rdfs:subClassOf }}
?p2 rdfs:subPropertyOf* ?p2_p .
}}
}}
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p3_p {{ {properties} rdfs:subClassOf }}
?p3 rdfs:subPropertyOf* ?p3_p .
}}
}}
FILTER EXISTS {{
GRAPH <http://reasoner.renci.org/ontology> {{
VALUES ?p4_p {{ {properties} rdfs:subClassOf }}
?p1 rdfs:subPropertyOf* ?p4_p .
}}
}}
}}
}}
}}
"""
parser = argparse.ArgumentParser()
parser.add_argument('variables', help='set up variables to build the graph')
# parser.add_argument('properties', help='properties to search link')
parser.add_argument('output_file', help='output file path')
args = parser.parse_args()
variables = json.loads(args.variables)
if variables["depth"] == 2:
variables["depths"] = level_2.format(**variables)
elif variables["depth"] == 3:
variables["depths"] = (level_2 + level_3).format(**variables)
elif variables["depth"] == 4:
variables["depths"] = (level_2 + level_3 + level_4).format(**variables)
else:
variables["depths"] = ""
with open('sparql/base.rq') as f:
query = f.read().format(**variables)
with open(f'{args.output_file}', 'w') as f:
f.write(query)