-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathLoad_comp_KG(hierarhy_3_2-2)_generate_script.py
210 lines (186 loc) · 8.17 KB
/
Load_comp_KG(hierarhy_3_2-2)_generate_script.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
import xml.etree.ElementTree as xml
import random
from random import randrange
from datetime import datetime
from datetime import timedelta
#Knowledge Graph Structure
Max_Level_2 = 833333
Max_Level_3 = 833333
Max_Objects_1 = 833333 #Model 1 lowest level
Max_Objects_2 = 833333 #Model 2 lowest level
#Constants
Max_Step_1 = 100000 #Maximum number of model elements in each rdf file
SPARQL_path = "C:/Blazegraph/1" #The path is used for UPDATE RDF DB creation script
Model_path = "Hierarchy_model/" #The path for output files
def createXML(filename):
#Open SPARQL file
spql = open(Model_path + "sparql_script.spql", "wt")
# Add header
header = str("<?xml version='1.0' encoding='UTF-8'?>\n<rdf:RDF\nxmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'\nxmlns:vCard='http://www.w3.org/2001/vcard-rdf/3.0#'\nxmlns:my='http://127.0.0.1/bg/ont/test1#'\n>")
f = open(Model_path + filename + "_static.nq", "wt")
f.write(header)
# Models core definition
f.write("\n<!--Model 1 Core definitions-->\n<rdf:Description rdf:about='http://127.0.0.1/Core_1/'>\n<my:has_id>Core_1</my:has_id>\n</rdf:Description>")
f.write("\n<!--Model 2 Core definitions-->\n<rdf:Description rdf:about='http://127.0.0.1/Core_2/'>\n<my:has_id>Core_2</my:has_id>\n</rdf:Description>")
f.write("\n</rdf:RDF>\n")
f.close()
# Model 1 hierarchy definition
# Add Level 2 definitions
FileNum = 0
i = 1
k = 1
while i <= Max_Level_2:
FileNum = FileNum + 1
f = open(Model_path + filename + "_Model1_level2_" + str(FileNum) + "_.nq", "at")
f.write(header)
f.write("\n<!--Objects definitions-->\n")
while k <= Max_Step_1:
body = str("<rdf:Description rdf:about='http://127.0.0.1/Core_1_Level_2_") + str(i) + str(
"/'>\n<my:has_id>Core_1_Level_2_") + str(i) + str(
"</my:has_id>\n<my:has_parent_id><rdf:Description rdf:about='http://127.0.0.1/Core_1/' /></my:has_parent_id>\n</rdf:Description>\n")
f.write(body)
i = i + 1
k = k + 1
if i > Max_Level_2:
break
f.write("\n</rdf:RDF>\n")
f.close()
spql.write("\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_Model1_level2_" + str(FileNum) + "_.nq>;\n")
k = 1
# Add Level 3 definitions
FileNum = 0
i = 1
k = 1
while i <= Max_Level_3:
FileNum = FileNum + 1
f = open(Model_path + filename + "_Model1_level3_" + str(FileNum) + "_.nq", "at")
f.write(header)
f.write("\n<!--Objects definitions-->\n")
while k <= Max_Step_1:
body = str("<rdf:Description rdf:about='http://127.0.0.1/Core_1_Level_3_") + str(i) + str(
"/'>\n<my:has_id>Core_1_Level_3_") + str(i) + str(
"</my:has_id>\n<my:has_parent_id><rdf:Description rdf:about='http://127.0.0.1/Core_1_Level_2_") + str(random.randint(1, Max_Level_2)) + str(
"/' /></my:has_parent_id>\n</rdf:Description>\n")
f.write(body)
i = i + 1
k = k + 1
if i > Max_Level_3:
break
f.write("\n</rdf:RDF>\n")
f.close()
spql.write(
"\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_Model1_level3_" + str(FileNum) + "_.nq>;\n")
k = 1
# Model 2 hierarchy definition
# Add Level 2 definitions
FileNum = 0
i = 1
k = 1
while i <= Max_Level_2:
FileNum = FileNum + 1
f = open(Model_path + filename + "_Model2_level2_" + str(FileNum) + "_.nq", "at")
f.write(header)
f.write("\n<!--Objects definitions-->\n")
while k <= Max_Step_1:
body = str("<rdf:Description rdf:about='http://127.0.0.1/Core_2_Level_2_") + str(i) + str(
"/'>\n<my:has_id>Core_2_Level_2_") + str(i) + str(
"</my:has_id>\n<my:has_parent_id><rdf:Description rdf:about='http://127.0.0.1/Core_2/' /></my:has_parent_id>\n</rdf:Description>\n")
f.write(body)
i = i + 1
k = k + 1
if i > Max_Level_2:
break
f.write("\n</rdf:RDF>\n")
f.close()
spql.write(
"\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_Model2_level2_" + str(FileNum) + "_.nq>;\n")
k = 1
# Add Level 3 definitions
FileNum = 0
i = 1
k = 1
while i <= Max_Level_3:
FileNum = FileNum + 1
f = open(Model_path + filename + "_Model2_level3_" + str(FileNum) + "_.nq", "at")
f.write(header)
f.write("\n<!--Objects definitions-->\n")
while k <= Max_Step_1:
body = str("<rdf:Description rdf:about='http://127.0.0.1/Core_2_Level_3_") + str(i) + str(
"/'>\n<my:has_id>Core_2_Level_3_") + str(i) + str(
"</my:has_id>\n<my:has_parent_id><rdf:Description rdf:about='http://127.0.0.1/Core_2_Level_2_") + str(random.randint(1, Max_Level_2)) + str(
"/' /></my:has_parent_id>\n</rdf:Description>\n")
f.write(body)
i = i + 1
k = k + 1
if i > Max_Level_3:
break
f.write("\n</rdf:RDF>\n")
f.close()
spql.write(
"\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_Model2_level3_" + str(FileNum) + "_.nq>;\n")
k = 1
spql.write("\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_static.nq>;\n")
# Add Object definitions
FileNum = 0
i = 1
k = 1
while i <= Max_Objects_1:
FileNum = FileNum + 1
f = open(Model_path + filename + "_object_" + str(FileNum) + "_.nq", "at")
f.write(header)
f.write("\n<!--Objects definitions-->\n")
while k <= Max_Step_1:
body = str("<rdf:Description rdf:about='http://127.0.0.1/Object_") + str(i) + str("/'>\n<my:has_id>Object_") + str(i) + str("</my:has_id>\n<my:has_parent_id><rdf:Description rdf:about='http://127.0.0.1/Core_1_Level_3_") + str(random.randint(1, Max_Level_3)) + str("/' /></my:has_parent_id>\n</rdf:Description>\n")
f.write(body)
i = i + 1
k = k + 1
if i > Max_Objects_1:
break
f.write("\n</rdf:RDF>\n")
f.close()
spql.write("\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_object_" + str(FileNum) + "_.nq>;\n")
k = 1
# Add Options definitions
FileNum = 0
i = 1
k = 1
while i <= Max_Objects_2:
FileNum = FileNum + 1
f = open(Model_path + filename + "_option_" + str(FileNum) + "_.nq", "at")
f.write(header)
f.write("\n<!--Options definitions-->\n")
while k <= Max_Step_1:
body = str("<rdf:Description rdf:about='http://127.0.0.1/Option_") + str(i) + str(
"/'>\n<my:has_id>Option_") + str(i) + str("</my:has_id>\n<my:has_parent_id><rdf:Description rdf:about='http://127.0.0.1/Core_2_Level_3_") + str(random.randint(1, Max_Level_3)) + str("/' /></my:has_parent_id>\n</rdf:Description>\n")
f.write(body)
i = i + 1
k = k + 1
if i > Max_Objects_2:
break
f.write("\n</rdf:RDF>\n")
f.close()
spql.write("\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_option_" + str(FileNum) + "_.nq>;\n")
k = 1
# Add Object-option links links Type-2 definitions
FileNum = 0
i = 1
k = 1
while i <= Max_Level_2:
FileNum = FileNum + 1
f = open(Model_path + filename + "_links_2_" + str(FileNum) + "_.nq", "at")
f.write(header)
f.write("\n<!--Add Object-option links Type-Hierarchy-->\n")
while k <= Max_Step_1:
body = str("<rdf:Description rdf:about='http://127.0.0.1/Core_1_Level_2_") + str(i) + str(
"/'>\n<my:linked_to><rdf:Description rdf:about='http://127.0.0.1/Core_2_Level_2_") + str(i) + str("/' /></my:linked_to>\n</rdf:Description>\n")
f.write(body)
i = i + 1
k = k + 1
if i > Max_Level_2: break
f.write("\n</rdf:RDF>\n")
f.close()
spql.write("\nLOAD <file:///" + str(SPARQL_path) + "/" + filename + "_links_2_" + str(FileNum) + "_.nq>;\n")
k = 1
spql.close()
if __name__ == "__main__":
createXML("KG_telecom")