-
Notifications
You must be signed in to change notification settings - Fork 3
/
GraphDBProcessor.py
229 lines (193 loc) · 8.7 KB
/
GraphDBProcessor.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
from QueryProcessor import *
from json import load
from sparql_dataframe import get
from rdflib import Graph, URIRef, RDF, RDFS, Literal
from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore
class CollectionProcessor(Processor):
def __init__(self):
super().__init__()
def uploadData(self, path):
try:
base_url = 'https://github.com/leonardozilli/res/'
new_graph = Graph()
with open(path, 'r', encoding='utf-8') as f:
json_file = load(f)
if isinstance(json_file, list):
for collection in json_file:
self.createGraph(collection, base_url, new_graph)
else:
self.createGraph(json_file, base_url, new_graph)
store = SPARQLUpdateStore()
endpoint = self.getDbPathOrUrl()
store.open((endpoint, endpoint))
for triple in new_graph.triples((None, None, None)):
store.add(triple)
store.close()
return True
except Exception as e:
print(e)
return False
def createGraph(self, json_file:dict, base_url, new_graph:Graph):
collection_type = URIRef(base_url + 'Collection')
manifest_type = URIRef(base_url + 'Manifest')
canvas_type = URIRef(base_url + 'Canvas')
has_id = URIRef('https://schema.org/identifier')
has_part = URIRef('https://www.w3.org/2002/07/owl#hasPart')
collection_subject = URIRef(json_file['id'])
new_graph.add((collection_subject, has_id, Literal(json_file['id'])))
new_graph.add((collection_subject, RDF.type, collection_type))
if json_file['label'] is not None:
collection_label_key:dict = json_file['label']
collection_label_value = list(collection_label_key.keys())[0]
new_graph.add((collection_subject, RDFS.label, Literal(json_file['label'][collection_label_value][0])))
for manifest in json_file['items']:
manifest_subject = URIRef(manifest['id'])
new_graph.add((collection_subject, has_part, manifest_subject))
new_graph.add((manifest_subject, has_id, Literal(manifest['id'])))
new_graph.add((manifest_subject, RDF.type, manifest_type))
if manifest['label'] is not None:
manifest_label_key:dict = manifest['label']
manifest_label_value = list(manifest_label_key.keys())[0]
new_graph.add((manifest_subject, RDFS.label, Literal(manifest['label'][manifest_label_value][0])))
for canvas in manifest['items']:
canvas_subject = URIRef(canvas['id'])
new_graph.add((manifest_subject, has_part, canvas_subject))
new_graph.add((canvas_subject, has_id, Literal(canvas['id'])))
new_graph.add((canvas_subject, RDF.type, canvas_type))
if canvas['label'] is not None:
canvas_label_key:dict = canvas['label']
canvas_label_value = list(canvas_label_key.keys())[0]
new_graph.add((canvas_subject, RDFS.label, Literal(canvas['label'][canvas_label_value][0])))
class TriplestoreQueryProcessor(QueryProcessor):
def __init__(self):
super().__init__()
def getAllCanvases(self):
endpoint = self.getDbPathOrUrl()
query ="""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX lz: <https://github.com/leonardozilli/res/>
PREFIX schema: <https://schema.org/>
SELECT ?id ?label (strafter(str(?class), 'res/') AS ?type)
WHERE{
?canvas rdf:type lz:Canvas;
rdfs:label ?label;
schema:identifier ?id;
rdf:type ?class.
}
"""
df_sparql = get(endpoint, query, True)
return self.cleaning_dataframe(df_sparql)
def getAllCollections(self):
endpoint = self.getDbPathOrUrl()
query ="""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX lz: <https://github.com/leonardozilli/res/>
PREFIX schema: <https://schema.org/>
SELECT ?id ?label (strafter(str(?class), 'res/') AS ?type)
WHERE{
?s rdf:type lz:Collection;
rdf:type ?class;
rdfs:label ?label;
schema:identifier ?id .
}
"""
df_sparql = get(endpoint, query, True)
return self.cleaning_dataframe(df_sparql)
def getAllManifests(self):
endpoint = self.getDbPathOrUrl()
query ="""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX lz: <https://github.com/leonardozilli/res/>
PREFIX schema: <https://schema.org/>
SELECT ?id ?label (strafter(str(?class), 'res/') AS ?type)
WHERE{
?manifest rdf:type lz:Manifest;
rdfs:label ?label;
schema:identifier ?id ;
rdf:type ?class.
}
"""
df_sparql = get(endpoint, query, True)
return self.cleaning_dataframe(df_sparql)
def getCanvasesInCollection(self, collectionId:str):
endpoint = self.getDbPathOrUrl()
query ="""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX lz: <https://github.com/leonardozilli/res/>
PREFIX schema: <https://schema.org/>
PREFIX owl: <https://www.w3.org/2002/07/owl#>
SELECT ?id ?label (strafter(str(?class), 'res/') AS ?type)
WHERE {
?collection schema:identifier "%s" ;
rdf:type lz:Collection ;
owl:hasPart ?manifest .
?manifest owl:hasPart ?canvas .
?canvas schema:identifier ?id ;
rdfs:label ?label ;
rdf:type ?class.
}
""" % collectionId
df_sparql = get(endpoint, query, True)
return self.cleaning_dataframe(df_sparql)
def getCanvasesInManifest(self, manifestId:str):
endpoint = self.getDbPathOrUrl()
query ="""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX lz: <https://github.com/leonardozilli/res/>
PREFIX schema: <https://schema.org/>
PREFIX owl: <https://www.w3.org/2002/07/owl#>
SELECT ?id ?label (strafter(str(?class), 'res/') AS ?type)
WHERE{
?manifest schema:identifier "%s" ;
rdf:type lz:Manifest ;
owl:hasPart ?canvas .
?canvas schema:identifier ?id ;
rdf:type ?class ;
rdfs:label ?label .
}
""" % manifestId
df_sparql = get(endpoint, query, True)
return self.cleaning_dataframe(df_sparql)
def getEntitiesWithLabel(self, label:str):
endpoint = self.getDbPathOrUrl()
query ="""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX schema: <https://schema.org/>
SELECT ?id ?label (strafter(str(?class), 'res/') AS ?type)
WHERE{
?entity rdfs:label "%s" .
?entity rdfs:label ?label .
?entity schema:identifier ?id .
?entity rdf:type ?class .
}
"""% self.escape_quotes(label)
df_sparql = get(endpoint, query, True)
return self.cleaning_dataframe(df_sparql)
def getManifestsInCollection(self, collectionId:str):
endpoint = self.getDbPathOrUrl()
query ="""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX lz: <https://github.com/leonardozilli/res/>
PREFIX schema: <https://schema.org/>
PREFIX owl: <https://www.w3.org/2002/07/owl#>
SELECT ?id ?label (strafter(str(?class), 'res/') AS ?type)
WHERE
{
?collection schema:identifier "%s" ;
rdf:type lz:Collection;
owl:hasPart ?manifest.
?manifest rdf:type lz:Manifest ;
rdf:type ?class;
rdfs:label ?label;
schema:identifier ?id
}
""" % collectionId
df_sparql = get(endpoint, query, True)
return self.cleaning_dataframe(df_sparql)