/
interface.py
1243 lines (899 loc) · 41.2 KB
/
interface.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
d1lod.interface
A high-level wrapper around a d1lod.graph that implements a variety
of d1lod-specific methods.
The most common method that will be called by will be addDataset(). This method
takes care of the work of adding the dataset, its digital objects, and its
people and organizations to the graph. All statements for a dataset are
accumulated into a temporary Redland's RDF.Model w/ an in-memory storage. When
all the triples for a dataset are accumulated, those triples are converted into
a SPARQL UPDATE query string and passed to the Graph to be inserted into
the graph database.
Aside from the basic methods (count, exists, etc), a general pattern is followed
for method naming of having separate methods such as addDataset and
addDatasetTriples (note the addition of the 'Triples' to the name). This pattern
is used to separate concerns, where the former is concerned with higher-level
issue such as whether or not a dataset should be added in the first place and
the latter is concerned with adding the triples for that dataset to the graph.
http://docs.s4.ontotext.com/display/S4docs/Fully+Managed+Database#FullyManagedDatabase-cURL%28dataupload%29
"""
import urllib
import uuid
import re
import RDF
import logging
import dataone, validator, util
from d1lod.people import processing
# Default namespaces
NAMESPACES = {
'owl': 'http://www.w3.org/2002/07/owl#',
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'xsd': 'http://www.w3.org/2001/XMLSchema#',
'foaf': 'http://xmlns.com/foaf/0.1/',
'dcterms': 'http://purl.org/dc/terms/',
'datacite': 'http://purl.org/spar/datacite/',
'prov': 'http://www.w3.org/ns/prov#',
'geolink': 'http://schema.geolink.org/1.0/base/main#',
'd1dataset': 'http://dataone.org/dataset/',
'd1person': 'http://dataone.org/person/',
'd1org': 'http://dataone.org/organization/',
'd1node': 'https://cn.dataone.org/cn/v1/node/'
}
class Interface:
def __init__(self, graph):
"""Initialize a graph with the given name.
Parameters:
-----------
graph : str
The name of the graph.
"""
self.graph = graph
# Load the formats map
self.formats = util.loadFormatsMap()
# Set up the temporary model which accumulates triples when addDataset
# is called
self.model = None
# Synchronize the newly added namespaces to the Graph object
# for faster referencing
self.graph.ns = NAMESPACES
# Add fixed statements
#
# Note: These are inserted regardless of whether or not they already
# exist
prov = self.graph.ns['prov']
owl = self.graph.ns['owl']
self.graph.insert(RDF.Uri(prov+'wasRevisionOf'), RDF.Uri(owl+'inverseOf'), RDF.Uri(prov+'hadRevision'))
def __str__(self):
return "Interface to Graph: '%s'." % self.graph.name
def prepareTerm(self, term):
"""Prepare an RDF term to be added to an RDF Model.
A term is either:
- An RDF.Node
- An RDF.Uri
- A string, which is either:
- A binding string (e.g., '?s')
- A URI reference (e.g., 'rdf:type')
- A URI (e.g., http://...)
- A literal
If the term is a str with a namespace prefix that the Interface knows
about then that namespace will be interpolated prior to making the term
into an RDF.Uri.
Arguments:
term : str | RDF.Node | RDF.Uri
The RDF term (subject, predicate, or object) to be prepared.
Returns:
str | RDF.Node | RDF.Uri
"""
if isinstance(term, RDF.Uri) or isinstance(term, RDF.Node):
return term
elif isinstance(term, str) or isinstance(term, unicode):
# Binding?: Do nothing
if term.startswith('?'):
return term
# Conver 'http...' strings to RDF.Uri
if term.startswith('http'):
return RDF.Uri(term)
parts = term.split(':')
# URI
if len(parts) > 1 and parts[0] in self.graph.ns:
prefix = self.graph.ns[parts[0]]
other_parts = parts[1:]
term = RDF.Uri(prefix + ':'.join(other_parts))
else:
# Literal
term = RDF.Node(term)
else:
raise Exception("Invalid term sent can't be prepared: (type is %s) Term is `%s`." % (type(term), term))
return term
def createModel(self):
"""Creates a Redland RDF Model.
Returns:
RDF.Model
"""
storage = RDF.HashStorage("temp", options="new='yes',hash-type='memory'")
if storage is None:
raise Exception("new RDF.Storage failed")
model = RDF.Model(storage)
if model is None:
raise Exception("new RDF.model failed")
self.model = model
def insertModel(self):
"""Inserts the current RDF Model (if it exists) into the graph and
deletes it if successful.
Returns: None
"""
if self.model is None:
logging.info("Attempted to insert a model that was None.")
return
# checking for all the statements in the current model
# if either subject / object / predicate is a blank node - then indicate that the payload contains a blank node
blank_node = False
for s in self.model:
blank_node = self.tripleHasBlankNode(s.subject, s.predicate, s.object)
if blank_node == True:
break
sparql_data = " .\n ".join([str(s) for s in self.model])
# Log model size
logging.info('Inserting model of size %d.', self.model.size())
self.graph.insert_data(payload=sparql_data, blank_node=blank_node)
return
def add(self, s, p, o):
"""Adds a triple to the current model.
Performs pre-processing on the subject, predicate, and objects
convert each one into an RDF.Node or RDF.Uri if it isn't already.
Pre-processing will perform namespace interpolation, e.g. if
s=='foo:Bar' and the namespace 'foo' exists and is http://foo.com/, the pre-processing step will convert s to
RDF.Uri('<http://foo.com/Bar').
Parameters:
-----------
s : RDF.Node | str
The subject of the triple pattern.
p : RDF.Node | str
The predicate of the triple pattern.
o : RDF.Node | str
The object of the triple pattern.
Examples:
---------
add(RDF.Uri('http://example.com/#me'), 'rdfs:label', 'Myself')
add(RDF.Uri('http://example.com/#me'), 'rdfs:label', RDF.Node('Myself'))
add(RDF.Uri('http://example.com/#me'), 'rdfs:label', RDF.Node('Myself'))
Returns: None
"""
if self.model is None:
logging.info("Failed to add triple to model because there was no current model.")
return
# - Converts strings to Nodes or Uris, whichever is appropriate
s = self.prepareTerm(s)
p = self.prepareTerm(p)
o = self.prepareTerm(o)
st = ""
try:
st = RDF.Statement(s, p, o)
except:
logging.info("Failed to create statement.")
try:
self.model.append(st)
except RDF.RedlandError:
logging.info("Failed to add statement: %s" % st)
return
def exists(self, s='?s', p='?p', o='?o'):
"""Determine whether any triples matching the given pattern exist in
the graph.
Parameters:
-----------
s : str
The subject of the triple pattern.
p : str
The predicate of the triple pattern.
o : str
The object of the triple pattern.
Returns:
--------
bool
Whether or not any triples with the pattern exist in the Graph.
"""
result = self.find(s=s, p=p, o=o, limit=1)
if result is None:
return False
if len(result) > 0 and 'error-message' in result[0]:
logging.error(result[0]['error-message'])
return False
if len(result) > 0:
return True
return False
def find(self, s='?s', p='?p', o='?o', limit=100):
"""Finds triples in the graph matching the given pattern.
Parameters:
-----------
s : RDF.Node | str
The subject of the triple pattern.
p : RDF.Node | str
The predicate of the triple pattern.
o : RDF.Node | str
The object of the triple pattern.
Returns:
--------
List
A list of Dicts with names s, p, and o.
"""
s = self.prepareTerm(s)
p = self.prepareTerm(p)
o = self.prepareTerm(o)
# checks if the payload contains a blank node or not
blank_node = self.tripleHasBlankNode(s, p, o)
if isinstance(s, RDF.Uri):
s = '<' + str(s) + '>'
if isinstance(p, RDF.Uri):
p = '<' + str(p) + '>'
if isinstance(o, RDF.Uri):
o = '<' + str(o) + '>'
query = u"""
SELECT * WHERE { %s %s %s } LIMIT %d
""" % (s, p, o, limit)
return self.graph.query(query, blank_node=blank_node)
def delete(self, s='?s', p='?p', o='?o'):
"""Delete all triples matching the given pattern from the graph.
Parameters:
-----------
s : str
The subject of the triple pattern.
p : str
The predicate of the triple pattern.
o : str
The object of the triple pattern.
Returns: dictionary object of the SPARQL response
"""
s = self.prepareTerm(s)
p = self.prepareTerm(p)
o = self.prepareTerm(o)
# checks if the payload contains a blank node or not
blank_node = self.tripleHasBlankNode(s, p, o)
if isinstance(s, RDF.Uri):
s = '<' + str(s) + '>'
if isinstance(p, RDF.Uri):
p = '<' + str(p) + '>'
if isinstance(o, RDF.Uri):
o = '<' + str(o) + '>'
payload_data = u"%s %s %s" % (s, p, o)
return self.graph.delete_data(payload=payload_data, blank_node=blank_node)
def datasetExists(self, identifier):
"""Determines whether a dataset exists in the graph.
The criterion used for existence is whether or not *any* triples with
the given identifier exist in the graph.
Parameters:
-----------
identifier : str
Non-urlencoded DataOne identifier
Returns:
--------
bool
Whether or not the dataset exists.
"""
identifier_esc = urllib.unquote(identifier).decode('utf8')
result = self.find(s='d1dataset:'+identifier_esc, limit=1)
if result is None or len(result) <= 0:
return False
else:
return True
def addDataset(self, identifier, doc=None):
"""Adds a dataset to the graph.
Parameters:
-----------
identifier : str
Non-urlencoded DataOne identifier
doc : XML Element
An XML element containing a result from the Solr index which
contains a number of fields relating to a dataset.
Returns: None
"""
if self.model is not None:
raise Exception("Model existed when addDataset was called. This means the last Model wasn't cleaned up after finishing.")
self.createModel()
# Get Solr fields if they weren't passed in
if doc is None:
doc = dataone.getSolrIndexFields(identifier)
identifier = dataone.extractDocumentIdentifier(doc)
identifier_esc = urllib.unquote(identifier).decode('utf8')
dataset_node = RDF.Uri(self.graph.ns['d1dataset'] + identifier_esc)
self.add(dataset_node, 'rdf:type', 'geolink:Dataset')
# Delete if dataset is already in graph
if self.datasetExists(identifier):
logging.info("Dataset with identifier %s already exists. Deleting then re-adding.", identifier)
self.deleteDataset(identifier)
scimeta = dataone.getScientificMetadata(identifier)
records = processing.extractCreators(identifier, scimeta)
vld = validator.Validator()
# Add Dataset triples first, we'll use them when we add people
# to match to existing people by the current dataset's 'obsoletes' field
self.addDatasetTriples(dataset_node, doc)
# Add people and organizations
people = [p for p in records if 'type' in p and p['type'] == 'person']
organizations = [o for o in records if 'type' in o and o['type'] == 'organization']
# Always do organizations first, so peoples' organization URIs exist
for organization in organizations:
organization = vld.validate(organization)
self.addOrganization(organization)
for person in people:
person = vld.validate(person)
self.addPerson(person)
# Commit or reject the model here
if self.model is None:
raise Exception("Model was None. It should have been an RDF.Model.")
self.insertModel()
self.model = None # Remove the model since we're done
return
def addDatasetTriples(self, dataset_node, doc):
"""Adds a dataset triples to the RDF model
Parameters:
-----------
dataset_node : str
The corresponding dataset node to be used to associate the properties in the model
doc : XML Element
An XML element containing a result from the Solr index which
contains a number of fields relating to a dataset.
Returns: None
"""
if self.model is None:
raise Exception("Model not found.")
identifier = dataone.extractDocumentIdentifier(doc)
identifier_esc = urllib.unquote(identifier).decode('utf8')
# type Dataset
self.add(dataset_node, 'rdf:type', 'geolink:Dataset')
# Title
title_element = doc.find("./str[@name='title']")
if title_element is not None:
self.add(dataset_node, 'rdfs:label', RDF.Node(title_element.text))
# Add geolink:Identifier
self.addIdentifierTriples(dataset_node, identifier)
# Abstract
abstract_element = doc.find("./str[@name='abstract']")
if abstract_element is not None:
self.add(dataset_node, 'geolink:description', RDF.Node(abstract_element.text))
# Spatial Coverage
bound_north = doc.find("./float[@name='northBoundCoord']")
bound_east = doc.find("./float[@name='eastBoundCoord']")
bound_south = doc.find("./float[@name='southBoundCoord']")
bound_west = doc.find("./float[@name='westBoundCoord']")
if all(ele is not None for ele in [bound_north, bound_east, bound_south, bound_west]):
if bound_north.text == bound_south.text and bound_west.text == bound_east.text:
wktliteral = "POINT (%s %s)" % (bound_north.text, bound_east.text)
else:
wktliteral = "POLYGON ((%s %s, %s %s, %s %s, %s, %s))" % (bound_west.text, bound_north.text, bound_east.text, bound_north.text, bound_east.text, bound_south.text, bound_west.text, bound_south.text)
self.add(dataset_node, 'geolink:hasGeometryAsWktLiteral', RDF.Node(wktliteral))
# Temporal Coverage
begin_date = doc.find("./date[@name='beginDate']")
end_date = doc.find("./date[@name='endDate']")
if begin_date is not None:
self.add(dataset_node, 'geolink:hasStartDate', RDF.Node(begin_date.text))
if end_date is not None:
self.add(dataset_node, 'geolink:hasEndDate', RDF.Node(end_date.text))
# Obsoletes as PROV#wasRevisionOf
obsoletes_node = doc.find("./str[@name='obsoletes']")
if obsoletes_node is not None:
other_document_esc = urllib.unquote(obsoletes_node.text).decode('utf8')
self.add(dataset_node, 'prov:wasRevisionOf', RDF.Uri(self.graph.ns['d1dataset'] + other_document_esc))
# Landing page
self.add(dataset_node, 'geolink:hasLandingPage', RDF.Uri("https://search.dataone.org/#view/" + identifier_esc))
# Digital Objects
# If this document has a resource map, get digital objects from there
# Otherwise, use the cito:documents field in Solr
resource_map_identifiers = doc.findall("./arr[@name='resourceMap']/str")
if len(resource_map_identifiers) > 0:
for resource_map_node in resource_map_identifiers:
resource_map_identifier = resource_map_node.text
digital_objects = dataone.getAggregatedIdentifiers(resource_map_identifier)
for digital_object in digital_objects:
digital_object_identifier = urllib.unquote(digital_object).decode('utf8')
self.addDigitalObject(identifier, digital_object_identifier)
else:
# If no resourceMap or documents field, at least add the metadata
# file as a digital object
# dataUrl e.g. https://cn.dataone.org/cn/v1/resolve/doi%3A10.6073%2FAA%2Fknb-lter-cdr.70061.123
data_url_node = doc.find("./str[@name='dataUrl']")
if data_url_node is not None:
data_url = data_url_node.text
digital_object = dataone.extractIdentifierFromFullURL(data_url)
digital_object = urllib.unquote(digital_object).decode('utf8')
self.addDigitalObject(identifier, digital_object)
return
def deleteDataset(self, identifier):
"""
Deletes the dataset from the graph
:param identifier: The identifier of the dataset to be deleted.
:return:
None
"""
self.deleteDatasetTriples(identifier)
return
def deleteDatasetTriples(self, identifier):
"""Delete all triples about this dataset. This includes:
- The dataset triples themselves (title, start date, etc)
- The dataset's digital objects
- The identifiers for the dataset and digital object(s)
- The isCreatorOf statement for people and organizations
This is a bit of extra work because identifiers and digital objects
are blank nodes and querying those takes some multi-statement SPARQL
queries.
"""
# Prepare some SPARQL query terms
identifier_esc = urllib.unquote(identifier).decode('utf8')
dataset = RDF.Uri(self.graph.ns['d1dataset']+identifier_esc)
has_identifier = RDF.Uri(self.graph.ns['geolink']+'hasIdentifier')
is_part_of = RDF.Uri(self.graph.ns['geolink']+'isPartOf')
has_part = RDF.Uri(self.graph.ns['geolink']+'hasPart')
"""Delete Dataset identifier
Find the blank node for the identifier of this dataset and delete
all statements about it.
"""
query = u"""DELETE WHERE
{
GRAPH <%s>
{
<%s> <%s> ?identifier .
?identifier ?s ?p
}
}
""" % (self.graph.name, dataset, has_identifier)
self.graph.update(query)
"""Delete Digital Object identifiers
Find all Digital Object (through Digital Object isPartOf) identifier
blank nodes and delete all statements about those blank nodes.
"""
query = u"""DELETE WHERE
{
GRAPH <%s>
{
?digital_object <%s> <%s> .
?digital_object <%s> ?identifier .
?identifier ?p ?o
}
}
""" % (self.graph.name, is_part_of, dataset, has_identifier)
self.graph.update(query)
"""Delete Digital Objects
Find all Digital Object blank nodes (through Dataset hasPart) and
delete statements about blank nodes.
"""
query = u"""DELETE WHERE
{
GRAPH <%s>
{
<%s> <%s> ?digital_object.
?digital_object ?p ?o
}
}
""" % (self.graph.name, dataset, has_part)
self.graph.update(query)
"""Delete statements about the dataset itself"""
self.delete('d1dataset:'+identifier_esc, '?p', '?o')
"""Delete respective isCreatorOf statements"""
self.delete('?s', 'geolink:isCreatorOf', '?o')
return
def addDigitalObject(self, dataset_identifier, digital_object_identifier):
"""
Generates and adds Dataset Object within the Virtuoso database
:param dataset_identifier: String
Dataset PID
:param digital_object_identifier: String
Corresponding digital identifier for the PID
:return:
None
"""
try:
self.addDigitalObjectTriples(dataset_identifier, digital_object_identifier)
except Exception as e:
logging.error(e)
return
def addDigitalObjectTriples(self, dataset_identifier, digital_object_identifier):
"""
Generates a new node for the dataset and adds metadata triples associated to the digital Object.
:param dataset_identifier: String
Dataset PID
:param digital_object_identifier: String
Corresponding digital identifier for the PID
:return:
None
"""
if self.model is None:
raise Exception("Model not found.")
dataset_identifier_esc = urllib.unquote(dataset_identifier).decode('utf8')
do_node = RDF.Node(blank=str(uuid.uuid4()))
# Get data object meta
data_meta = dataone.getSystemMetadata(digital_object_identifier)
if data_meta is None:
raise Exception("System metadata for data object %s was not found. Continuing to next data object." % digital_object_identifier)
self.add(do_node, 'rdf:type', 'geolink:DigitalObject')
self.add(do_node, 'geolink:isPartOf', 'd1dataset:'+dataset_identifier_esc)
self.add('d1dataset:'+dataset_identifier_esc, 'geolink:hasPart', do_node)
self.addIdentifierTriples(do_node, digital_object_identifier)
# Checksum and checksum algorithm
checksum_node = data_meta.find(".//checksum")
if checksum_node is not None:
self.add(do_node, 'geolink:hasChecksum', RDF.Node(checksum_node.text))
self.add(do_node, 'geolink:hasChecksumAlgorithm', RDF.Node(checksum_node.get("algorithm")))
else:
raise Exception('Sysmeta XML for PID %s had no checksum element' % digital_object_identifier)
# Size
size_node = data_meta.find("./size")
if size_node is not None:
self.add(do_node, 'geolink:hasByteLength', RDF.Node(size_node.text))
else:
raise Exception('Sysmeta XML for PID %s had no size element' % digital_object_identifier)
# Format
format_id_node = data_meta.find("./formatId")
if format_id_node is not None:
if format_id_node.text in self.formats:
self.add(do_node, 'geolink:hasFormat', RDF.Uri(self.formats[format_id_node.text]['uri']))
else:
raise Exception('Format %s not found in list of known formats.' % format_id_node.text)
else:
raise Exception('Sysmeta XML for PID %s had no formatId element' % digital_object_identifier)
# Date uploaded
date_uploaded_node = data_meta.find("./dateUploaded")
if date_uploaded_node is not None:
self.add(do_node, 'geolink:dateUploaded', RDF.Node(date_uploaded_node.text))
else:
raise Exception('Sysmeta XML for PID %s had no dataUploaded element' % digital_object_identifier)
# Authoritative MN
authoritative_mn = data_meta.find("./authoritativeMemberNode")
if authoritative_mn is not None:
self.add(do_node, 'geolink:hasAuthoritativeDigitalGraph', 'd1node:' + authoritative_mn.text)
else:
raise Exception('Sysmeta XML for PID %s had no authoritativeMemberNode element' % digital_object_identifier)
# Replica MN's
replica_mns = data_meta.findall("./replica")
if replica_mns is None:
raise Exception('Sysmeta XML for PID %s had no replica element' % digital_object_identifier)
for replica_mn in replica_mns:
replica_node = replica_mn.find("./replicaMemberNode")
if replica_node is not None:
self.add(do_node, 'geolink:hasReplicaDigitalGraph', 'd1node:' + replica_node.text)
else:
raise Exception('Sysmeta XML for PID %s had no replicaMemberNode element' % digital_object_identifier)
# Origin MN
origin_mn = data_meta.find("./originMemberNode")
if origin_mn is not None:
self.add(do_node, 'geolink:hasOriginDigitalgraph', 'd1node:' + origin_mn.text)
else:
raise Exception('Sysmeta XML for PID %s had no originMemberNode element' % digital_object_identifier)
# Obsoletes (mapped as PROV#wasRevisionOf)
obsoletes_node = data_meta.find("./obsoletes")
if obsoletes_node is not None:
other_document = urllib.unquote(obsoletes_node.text).decode('utf8')
self.add(do_node, 'prov:wasRevisionOf', 'd1dataset:'+other_document)
# Submitter and rights holders
# submitter_node = data_meta.find("./submitter")
#
# if submitter_node is not None:
# submitter_node_text = " ".join(re.findall(r"o=(\w+)", submitter_node.text, re.IGNORECASE))
#
# if len(submitter_node_text) > 0:
# self.insert('d1dataset:'+data_id, 'geolink:hasCreator', ])
# rights_holder_node = data_meta.find("./rightsHolder")
#
# if rights_holder_node is not None:
# rights_holder_node_text = " ".join(re.findall(r"o=(\w+)", rights_holder_node.text, re.IGNORECASE))
#
# if len(rights_holder_node_text) > 0:
# addStatement(model, d1dataset+data_id, self.graph.ns["geolink"]+"hasRightsHolder", RDF.Uri("urn:node:" + rights_holder_node_text.upper()))
return
def addPerson(self, record):
"""
Tries to find the person uri in the database. If found, it adds triples to the corresponding
person. Otherwise, it mints a new URI for the person identity and adds the information to the Dataset model.
:param record: Dict
Details related to the person
:return:
None
"""
if record is None:
return
logging.info("Calling findPersonURI on %s.", record)
person_uri = self.findPersonURI(record)
if person_uri is None:
person_uri = self.mintPersonPrefixedURIString()
logging.info("Person was not found. Minted URI of %s", person_uri)
self.addPersonTriples(person_uri, record)
def addPersonTriples(self, uri, record):
"""
Given an person URI, it adds the corresponding information as triples and adds the triples
to the models to be inserted into the Virtuoso database.
:param uri: String
The person URI
:param record: Dict
Dictionary object of information related to the organization
:return:
None
"""
if self.model is None:
raise Exception("Model not found.")
logging.info("Adding person triples for person with uri '%s' and record '%s'", uri, record)
self.add(uri, 'rdf:type', 'geolink:Person')
if 'salutation' in record:
self.add(uri, 'geolink:namePrefix', record['salutation'])
if 'full_name' in record:
self.add(uri, 'geolink:nameFull', record['full_name'])
if 'first_name' in record:
self.add(uri, 'geolink:nameGiven', record['first_name'])
if 'last_name' in record:
self.add(uri, 'geolink:nameFamily', record['last_name'])
if 'organization' in record:
organization_name = record['organization']
logging.info("Looking up organization with name '%s'", organization_name)
if self.organizationExists(organization_name):
logging.info("Organization with name '%s' exists.", organization_name)
organization_uri = self.findOrganizationURI({'name' : organization_name})
else:
organization_uri = self.mintOrganizationPrefixedURIString()
logging.info("Minted new organization URI of '%s' and adding triples.", organization_uri)
self.add(organization_uri, 'rdfs:label', organization_name)
self.add(uri, 'geolink:hasAffiliation', RDF.Uri(organization_uri))
if 'email' in record:
self.add(uri, 'foaf:mbox', RDF.Uri('mailto:' + record['email'].lower()))
if 'address' in record:
self.add(uri, 'geolink:address', record['address'])
if 'role' in record and 'document' in record:
if record['role'] == 'creator':
self.add(uri, 'geolink:isCreatorOf', 'd1dataset:' + urllib.unquote(record['document']).decode('utf8'))
elif record['role'] == 'contact':
self.add(uri, 'geolink:isContactOf', 'd1dataset:' + urllib.unquote(record['document']).decode('utf8'))
def addOrganization(self, record):
"""
Tries to find the organization uri in the database. If found, it adds triples to the corresponding
organization. Otherwise, it mints a new URI for the organization and adds the information related to the
organization
:param record: Dict
Details related to the organization
:return:
None
"""
if record is None:
return
logging.info("Calling findOrganizationURI on %s.", record)
organization_uri = self.findOrganizationURI(record)
if organization_uri is None:
organization_uri = self.mintOrganizationPrefixedURIString()
logging.info("Organization was not found. Minted URI of %s", organization_uri)
self.addOrganizationTriples(organization_uri, record)
def addOrganizationTriples(self, uri, record):
"""
Given an organization URI, it adds the corresponding information as triples and adds the triples
to the models to be inserted into the Virtuoso database.
:param uri: String
The organization URI
:param record: Dict
Dictionary object of information related to the organization
:return:
None
"""
if self.model is None:
raise Exception("Model not found.")
logging.info("Adding organization triples for organization with uri '%s' and record '%s'", uri, record)
self.add(uri, 'rdf:type', 'geolink:Organization')
if 'name' in record:
self.add(uri, 'rdfs:label', record['name'])
if 'email' in record:
self.add(uri, 'foaf:mbox', RDF.Uri('mailto:' + record['email'].lower()))
if 'address' in record:
self.add(uri, 'geolink:address', record['address'])
if 'role' in record and 'document' in record:
if record['role'] == 'creator':
self.add(uri, 'geolink:isCreatorOf', 'd1dataset:' + urllib.unquote(record['document']).decode('utf8'))
elif record['role'] == 'contact':
self.add(uri, 'geolink:isContactOf', 'd1dataset:' + urllib.unquote(record['document']).decode('utf8'))
def findPersonURI(self, record):
"""Find a person record in the graph according to a set of rules
for matching records.
A record is said to already exist in the graph if exactly one
person exists in graph with the same non-zero-length last name and
email. This is the only rule used right now.
Arguments:
----------
record : Dict
A Dictionary of keys for the record ('last_name, 'email', etc.)
"""
if record is None:
return None
logging.info("Looking up person URI for %s", record)
# Match via last name and email
if 'last_name' in record and 'email' in record:
logging.info("Attempting to match %s via last name and email.", record)
last_name = record['last_name']
email = record['email']
if len(last_name) < 1 or len(email) < 1:
return None
query_string = u"""
SELECT ?s
WHERE {
?s rdf:type geolink:Person .
?s geolink:nameFamily '''%s''' .
?s foaf:mbox <mailto:%s>
}
""" % (last_name,
email.lower())
find_result = self.graph.query(query_string)
if find_result is None or len(find_result) != 1:
logging.info("No match found.")
return None
# Remove < and > around string
person_uri_string = find_result[0]['s']
person_uri_string = person_uri_string.replace('<', '')
person_uri_string = person_uri_string.replace('>', '')