-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOntology.java
More file actions
805 lines (737 loc) · 34.3 KB
/
Copy pathOntology.java
File metadata and controls
805 lines (737 loc) · 34.3 KB
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
/*
* Copyright 2021-22 Ontology Engineering Group, Universidad Politecnica de Madrid, Spain
*
* 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.
*
* Author: Daniel Garijo and Maria Poveda
*/
package entities;
import fair.Constants;
import fair.Utils;
import org.jsoup.nodes.Document;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StreamDocumentSource;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.search.EntitySearcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.FileTooLargeException;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Optional;
public class Ontology {
private OWLOntology ontologyModel;
private Document htmlDocumentation;
private static final Logger logger = LoggerFactory.getLogger(Ontology.class);
//metadata attributes
private String ontologyURI;
private String namespacePrefix;
private String namespaceUri;
private String title;
private String name;
private String description;
private String versionIRI;
private String versionInfo;
private ArrayList<String> authors;
private ArrayList<String> contributors;
private String license;
private String rights;
private String status;
private String previousVersion;
private String creationDate;
private String issuedDate;
private String modifiedDate;
private String source;
private String backwardCompatibility;
private String publisher;
private String citation;
private String doi;
private String logo;
private final ArrayList<String> supportedMetadata;
private final ArrayList<String> reusedMetadataVocabularies;
private final ArrayList<String> reusedVocabularies;
List<IRI> importedVocabularies;
private final ArrayList<String> termsWithLabel;
private final ArrayList<String> terms; // all terms
private final ArrayList<String> termsWithDescription;
private boolean isSKOS = false;
/**
* Ontology class constructor
* @param o ontology path or URI
* @param isFromFile flag to indicate whether the ontology should be loaded from file or URL
* @param tmpFolder temporary folder where to store the downloaded ontology
*/
public Ontology(String o, boolean isFromFile, Path tmpFolder) throws FileTooLargeException {
supportedMetadata = new ArrayList<>();
importedVocabularies = new ArrayList<>();
reusedMetadataVocabularies = new ArrayList<>();
reusedVocabularies = new ArrayList<>();
termsWithLabel = new ArrayList<>();
termsWithDescription = new ArrayList<>();
terms = new ArrayList<>();
//Download ontology (any serialization)
try {
this.loadModelToDocument(o, isFromFile, tmpFolder.toString());
if (this.ontologyModel != null && this.ontologyModel.getOntologyID().getOntologyIRI().isPresent()) {
this.ontologyURI = this.ontologyModel.getOntologyID().getOntologyIRI().get().toString();
}
logger.info("Ontology URI to assess: "+ontologyURI);
}catch(FileTooLargeException e) {
throw e;
}
catch(Exception e){
logger.error("Could not load the ontology: " + e.getMessage());
}
if (this.ontologyURI == null && this.ontologyModel != null) {
List<OWLIndividual> cs = EntitySearcher.getInstances(
ontologyModel.getOWLOntologyManager().getOWLDataFactory().getOWLClass("http://www.w3.org/2004/02/skos/core#ConceptScheme"),
ontologyModel).collect(Collectors.toList());
if (!cs.isEmpty()) {
if (cs.size() > 1) {
logger.warn("More than one concept scheme detected!");
}
this.ontologyURI = cs.get(0).toStringID();
this.isSKOS = true;
logger.info("SKOS ConceptScheme detected: " + ontologyURI);
}
}
if (this.ontologyURI == null || this.ontologyURI.isEmpty()) {
if (!isFromFile) {
this.ontologyURI = o.strip();
} else {
this.ontologyURI = o.strip();
}
}
if (this.ontologyURI == null || this.ontologyURI.isEmpty()) {
this.ontologyURI = o.strip();
}
if (this.namespaceUri == null || this.namespaceUri.isEmpty()) {
this.namespaceUri = this.ontologyURI;
}
//Download HTML of the onto (if available)
try{
htmlDocumentation = Utils.loadOntologyHTML(ontologyURI);
}
catch(Exception e){
logger.error("Could not load ontology in HTML");
htmlDocumentation = null;
}
try {
this.getOntologyMetadata();
this.getOntologyCoverage();
}catch(Exception e){
logger.error("Error when extracting metadata or annotations");
}
}
/**
* Method that loads all available ontology metadata, using a series of common vocabularies as reference
*/
private void getOntologyMetadata(){
logger.info("Retrieving ontology metadata");
if (this.ontologyModel == null) {
logger.error("No ontology loaded! Metadata cannot be extracted");
return;
}
this.authors = new ArrayList<>();
this.contributors = new ArrayList<>();
this.title = "Title unavailable";
if (isSKOS){
logger.info("Is a SKOS vocabulary, retrieving metadata from the concept scheme");
// get annotations on the onto URI.
OWLIndividual cs = EntitySearcher.getInstances(ontologyModel.getOWLOntologyManager().getOWLDataFactory().
getOWLClass(Constants.SKOS_CONCEPT_SCHEME), ontologyModel).collect(Collectors.toList()).get(0);
EntitySearcher.getAnnotations((OWLEntity) cs, this.getOntologyModel()).forEach(this::completeMetadata);
} else {
logger.info("NO SKOS vocabulary");
try {
if (this.ontologyModel.getOntologyID().getVersionIRI().isPresent()) {
this.versionIRI = this.ontologyModel.getOntologyID().getVersionIRI().get().toString();
this.supportedMetadata.add(Constants.FOOPS_VERSION_IRI);
}
} catch (Exception e) {
logger.info("No version IRI detected");
}
this.ontologyModel.annotations().forEach(this::completeMetadata);
// extension for those axioms that extend properties but are annotation properties.
// see https://github.com/dgarijo/Widoco/issues/530 for context
logger.info("Extracting axioms");
for (OWLAxiom axiom : ontologyModel.getAxioms()) {
String subject = "", predicate ="", object ="";
try {
if (axiom instanceof OWLDataPropertyAssertionAxiom) {
OWLDataPropertyAssertionAxiom dataPropertyAssertionAxiom = (OWLDataPropertyAssertionAxiom) axiom;
subject = dataPropertyAssertionAxiom.getSubject().toStringID();
predicate = dataPropertyAssertionAxiom.getProperty().asOWLDataProperty().toStringID();
object = dataPropertyAssertionAxiom.getObject().getLiteral();
} else if (axiom instanceof OWLObjectPropertyAssertionAxiom) {
OWLObjectPropertyAssertionAxiom objectPropertyAssertionAxiom = (OWLObjectPropertyAssertionAxiom) axiom;
subject = objectPropertyAssertionAxiom.getSubject().toStringID();
predicate = objectPropertyAssertionAxiom.getProperty().asOWLObjectProperty().toStringID();
object = objectPropertyAssertionAxiom.getObject().toStringID();
}
if (subject.equals(this.ontologyURI)){
OWLDataFactory dataFactory = this.ontologyModel.getOWLOntologyManager().getOWLDataFactory();
OWLAnnotationProperty pAux = dataFactory.getOWLAnnotationProperty(IRI.create(predicate));
OWLAnnotationValue oAux = dataFactory.getOWLLiteral(object);
completeMetadata(dataFactory.getOWLAnnotation(pAux, oAux));
}
} catch (Exception e) {
logger.warn("Failed to process axiom: " + axiom.toString());
continue;
}
}
}
}
private void completeMetadata(OWLAnnotation a) {
String propertyName = a.getProperty().getIRI().getIRIString();
String value = null;
switch (propertyName) {
case Constants.PROP_DC_TITLE:
case Constants.PROP_DCTERMS_TITLE:
case Constants.PROP_SCHEMA_NAME:
case Constants.PROP_SCHEMA_NAME_HTTP:
try {
// valueLanguage = a.getValue().asLiteral().get().getLang();
this.title = a.getValue().asLiteral().get().getLiteral();
this.supportedMetadata.add(Constants.FOOPS_TITLE);
} catch (Exception e) {
logger.error("Error while getting ontology title. No literal provided");
}
break;
case Constants.PROP_DCTERMS_ABSTRACT:
case Constants.PROP_DC_ABSTRACT:
case Constants.PROP_DCTERMS_DESCRIPTION:
case Constants.PROP_DC_DESCRIPTION:
case Constants.PROP_SCHEMA_DESCRIPTION:
case Constants.PROP_SCHEMA_DESCRIPTION_HTTP:
case Constants.PROP_RDFS_COMMENT:
case Constants.PROP_DOAP_DESCRIPTION:
case Constants.PROP_DOAP_SHORT_DESC:
case Constants.PROP_SKOS_NOTE:
try {
this.description = a.getValue().asLiteral().get().getLiteral();
this.supportedMetadata.add(Constants.FOOPS_DESCRIPTION);
} catch (Exception e) {
logger.error("Error while getting ontology abstract. No literal provided");
}
break;
case Constants.PROP_DCTERMS_REPLACES:
case Constants.PROP_DC_REPLACES:
case Constants.PROP_PROV_WAS_REVISION_OF:
case Constants.PROP_OWL_PRIOR_VERSION:
case Constants.PROP_PAV_PREVIOUS_VERSION:
this.previousVersion = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_PREVIOUS_VERSION);
break;
case Constants.PROP_OWL_VERSION_INFO:
case Constants.PROP_SCHEMA_SCHEMA_VERSION:
case Constants.PROP_SCHEMA_SCHEMA_VERSION_HTTP:
try {
this.versionInfo = a.getValue().asLiteral().get().getLiteral();
this.supportedMetadata.add(Constants.FOOPS_VERSION_INFO);
} catch (Exception e) {
logger.error("Error while getting ontology abstract. No literal provided");
}
break;
case Constants.PROP_VANN_PREFIX:
this.namespacePrefix = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_NS_PREFIX);
break;
case Constants.PROP_VOID_URI_SPACE:
case Constants.PROP_VANN_URI:
value = Utils.getValueAsLiteralOrURI(a.getValue());
this.namespaceUri = value;
this.supportedMetadata.add(Constants.FOOPS_NS_URI);
if (!this.ontologyURI.equals(value)) {
logger.warn("Ontology NS URI declared and annotated is not the same!");
}
break;
case Constants.PROP_DCTERMS_LICENSE:
case Constants.PROP_SCHEMA_LICENSE:
case Constants.PROP_SCHEMA_LICENSE_HTTP:
case Constants.PROP_DOAP_LICENSE:
case Constants.PROP_CC_LICENSE:
//add rights.
try {
this.license = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_LICENSE);
} catch (Exception e) {
logger.error("Could not retrieve license. Please avoid using blank nodes...");
}
break;
case Constants.PROP_DC_RIGHTS:
case Constants.PROP_DCTERMS_RIGHTS:
case Constants.PROP_DCTERMS_ACCESS_RIGHTS:
try {
this.rights = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_RIGHTS);
} catch (Exception e) {
logger.error("Could not retrieve rights. Please avoid using blank nodes...");
}
break;
case Constants.PROP_DC_CONTRIBUTOR:
case Constants.PROP_DCTERMS_CONTRIBUTOR:
case Constants.PROP_SCHEMA_CONTRIBUTOR:
case Constants.PROP_SCHEMA_CONTRIBUTOR_HTTP:
case Constants.PROP_PAV_CONTRIBUTED_BY:
case Constants.PROP_DC_CREATOR:
case Constants.PROP_DCTERMS_CREATOR:
case Constants.PROP_SCHEMA_CREATOR:
case Constants.PROP_SCHEMA_CREATOR_HTTP:
case Constants.PROP_PAV_CREATED_BY:
case Constants.PROP_PAV_AUTHORED_BY:
case Constants.PROP_FOAF_MAKER:
case Constants.PROP_PROV_ATTRIBUTED_TO:
case Constants.PROP_DC_PUBLISHER:
case Constants.PROP_DCTERMS_PUBLISHER:
case Constants.PROP_SCHEMA_PUBLISHER:
case Constants.PROP_SCHEMA_PUBLISHER_HTTP:
case Constants.PROP_DOAP_DOCUMENTER:
case Constants.PROP_DOAP_MAINTAINER:
case Constants.PROP_DOAP_DEVELOPER:
case Constants.PROP_DOAP_HELPER:
case Constants.PROP_DOAP_TRANSLATOR:
try {
value = Utils.getValueAsLiteralOrURI(a.getValue());
switch (propertyName) {
case Constants.PROP_DC_CONTRIBUTOR:
case Constants.PROP_DCTERMS_CONTRIBUTOR:
case Constants.PROP_SCHEMA_CONTRIBUTOR:
case Constants.PROP_SCHEMA_CONTRIBUTOR_HTTP:
case Constants.PROP_DOAP_DOCUMENTER:
case Constants.PROP_DOAP_MAINTAINER:
case Constants.PROP_DOAP_HELPER:
case Constants.PROP_DOAP_TRANSLATOR:
case Constants.PROP_PAV_CONTRIBUTED_BY:
this.contributors.add(value);
this.supportedMetadata.add(Constants.FOOPS_CONTRIBUTOR);
break;
case Constants.PROP_DC_CREATOR:
case Constants.PROP_DCTERMS_CREATOR:
case Constants.PROP_PAV_CREATED_BY:
case Constants.PROP_PAV_AUTHORED_BY:
case Constants.PROP_FOAF_MAKER:
case Constants.PROP_PROV_ATTRIBUTED_TO:
case Constants.PROP_SCHEMA_CREATOR:
case Constants.PROP_SCHEMA_CREATOR_HTTP:
case Constants.PROP_DOAP_DEVELOPER:
this.authors.add(value);
this.supportedMetadata.add(Constants.FOOPS_AUTHOR);
break;
default:
this.publisher = value;
this.supportedMetadata.add(Constants.FOOPS_PUBLISHER);
break;
}
} catch (Exception e) {
logger.error("Could not retrieve creator/contributor. Please avoid using blank nodes...");
}
break;
case Constants.PROP_DCTERMS_CREATED:
case Constants.PROP_SCHEMA_DATE_CREATED:
case Constants.PROP_SCHEMA_DATE_CREATED_HTTP:
case Constants.PROP_DOAP_CREATED:
case Constants.PROP_PROV_GENERATED_AT_TIME:
case Constants.PROP_PAV_CREATED_ON:
try {
this.creationDate = a.getValue().asLiteral().get().getLiteral();
this.supportedMetadata.add(Constants.FOOPS_CREATION_DATE);
} catch (Exception e) {
logger.error("Error while getting the date. No literal provided");
}
break;
case Constants.PROP_DCTERMS_MODIFIED:
case Constants.PROP_SCHEMA_DATE_MODIFIED:
case Constants.PROP_SCHEMA_DATE_MODIFIED_HTTP:
try {
this.modifiedDate = a.getValue().asLiteral().get().getLiteral();
this.supportedMetadata.add(Constants.FOOPS_MODIFIED);
} catch (Exception e) {
logger.error("Error while getting the date. No literal provided");
}
break;
case Constants.PROP_DCTERMS_BIBLIOGRAPHIC_CIT:
// case Constants.PROP_SCHEMA_CITATION: this refers to citing another work
// case Constants.PROP_SCHEMA_CITATION_HTTP:
this.citation = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_CITATION);
break;
case Constants.PROP_BIBO_DOI:
this.doi = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_DOI);
break;
case Constants.PROP_SCHEMA_IDENTIFIER:
case Constants.PROP_SCHEMA_IDENTIFIER_HTTP:
case Constants.PROP_DCTERMS_IDENTIFIER:
String identifier = Utils.getValueAsLiteralOrURI(a.getValue());
if (identifier.contains("doi.")){
this.supportedMetadata.add(Constants.FOOPS_DOI);
}
break;
case Constants.PROP_BIBO_STATUS:
case Constants.PROP_MOD_STATUS:
try {
if (a.getValue().isLiteral()) {
this.status = a.getValue().asLiteral().get().getLiteral();
}
if (a.getValue().isIRI()) {
this.status = a.getValue().asIRI().get().getIRIString();
}
this.supportedMetadata.add(Constants.FOOPS_STATUS);
} catch (Exception e) {
logger.error("Error while getting the status. No literal provided");
}
break;
case Constants.PROP_OWL_BACKWARDS_COMPATIBLE:
this.backwardCompatibility = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_B_COMPATIBILITY);
break;
case Constants.PROP_FOAF_LOGO:
case Constants.PROP_SCHEMA_LOGO:
case Constants.PROP_SCHEMA_LOGO_HTTP:
this.logo = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_LOGO);
break;
case Constants.PROP_DC_SOURCE:
case Constants.PROP_DCTERMS_SOURCE:
case Constants.PROP_PROV_HAD_ORIGINAL_SOURCE:
this.source = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_SOURCE);
break;
case Constants.PROP_DCTERMS_ISSUED:
case Constants.PROP_DCTERMS_SUBMITTED:
case Constants.PROP_SCHEMA_DATE_RELEASED:
case Constants.PROP_SCHEMA_DATE_RELEASED_HTTP:
case Constants.PROP_SCHEMA_DATE_PUBLISHED:
case Constants.PROP_SCHEMA_DATE_PUBLISHED_HTTP:
this.issuedDate = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_ISSUED);
break;
case Constants.PROP_SCHEMA_INCLUDED_IN_DATA_CATALOG_HTTP:
case Constants.PROP_SCHEMA_INCLUDED_IN_DATA_CATALOG:
this.supportedMetadata.add(Constants.FOOPS_INCLUDED_IN_DATA_CATALOG);
break;
case Constants.PROP_RDFS_LABEL:
case Constants.PROP_DOAP_NAME:
this.name = Utils.getValueAsLiteralOrURI(a.getValue());
this.supportedMetadata.add(Constants.FOOPS_NAME);
break;
}
}
/**
* Method to extract the coverage of the ontology:
* - Reused vocabularies
* - Imported vocabularies
* - Terms with no label
* - Terms with no description
* Since this iterates over all terms of the ontology, we do it once in this method
*/
private void getOntologyCoverage(){
//metadata vocabularies
this.ontologyModel.annotations().forEach(this::checkNamespaces);
// imports -> they are loaded when the ontology is loaded.
// due to slow imports, we have disabled loading of imports
//this.importedVocabularies = this.ontologyModel.importsDeclarations().collect(Collectors.toList());
//vocabulary reuse
logger.info("Extracting namespaces, labels, descriptions");
if(isSKOS) {
//extract only for skos:Concept
EntitySearcher.getInstances(ontologyModel.getOWLOntologyManager().getOWLDataFactory().getOWLClass(Constants.SKOS_CONCEPT), ontologyModel).forEach(a-> {
this.terms.add(a.toStringID());
OWLAnnotationProperty skosLabel = ontologyModel.getOWLOntologyManager().getOWLDataFactory().getOWLAnnotationProperty(Constants.PROP_SKOS_PREF_LABEL);
EntitySearcher.getAnnotations((OWLEntity) a, this.getOntologyModel(), skosLabel).forEach(ann -> {
OWLAnnotationValue val = ann.getValue();
if(val instanceof OWLLiteral) {
if(!termsWithLabel.contains(a.toStringID())) {
this.termsWithLabel.add(a.toStringID());
}
}
});
OWLAnnotationProperty skosDescription = ontologyModel.getOWLOntologyManager().getOWLDataFactory().getOWLAnnotationProperty(Constants.PROP_SKOS_PREF_DEFINITION);
EntitySearcher.getAnnotations((OWLEntity) a, this.getOntologyModel(), skosDescription).forEach(ann -> {
OWLAnnotationValue val = ann.getValue();
if(val instanceof OWLLiteral) {
if(!termsWithDescription.contains(a.toStringID())) {
this.termsWithDescription.add(a.toStringID());
}
}
});
});
}else{
try {
this.ontologyModel.classesInSignature().forEach(this::checkTermCoverage);
} catch (Exception e) {
logger.error("Error checking classes coverage");
}
try {
this.ontologyModel.objectPropertiesInSignature().forEach(this::checkTermCoverage);
} catch (Exception e) {
logger.error("Error checking object properties coverage");
}
try {
this.ontologyModel.dataPropertiesInSignature().forEach(this::checkTermCoverage);
} catch (Exception e) {
logger.error("Error checking data properties coverage");
}
}
}
/**
* This method checks if the metadata vocabularies used in the annotations of the ontologies match our white list
* @param a annotation to analyze
*/
private void checkNamespaces(OWLAnnotation a){
for (String vocab: Constants.VOCS_REUSE_METADATA){
if (a.getProperty().getIRI().getIRIString().contains(vocab)){
if(!reusedMetadataVocabularies.contains(vocab)) {
reusedMetadataVocabularies.add(vocab);
}
}
}
}
/**
* This method checks whether the ontology is reusing a vocabulary or not
* @param a named object to analyze.
*/
private void checkTermCoverage(OWLNamedObject a){
try {
String termNS = a.getIRI().getNamespace();
if(termNS.equals(Constants.NS_OWL)) return; //We ignore OWL reuse.
// if the URI is not the same as the main ontology provided, we count reuse.
// we verify as well if the preferred ns prefix is used
if(!termNS.toLowerCase().contains(this.ontologyURI.toLowerCase())
&& !termNS.toLowerCase().contains(this.namespaceUri)){
if (!this.reusedVocabularies.contains(termNS)) {
this.reusedVocabularies.add(termNS);
}
}else{
//get label/def coverage for the ontology URI considered
// for labels: rdfs:label, skos:prefLabel, obo:IAO_0000118
String termIRI = a.getIRI().getIRIString();
if(!this.terms.contains(termIRI)) { // to avoid duplicates
this.terms.add(termIRI);
}
OWLAnnotationProperty label = ontologyModel.getOWLOntologyManager().getOWLDataFactory().getRDFSLabel();
EntitySearcher.getAnnotations((OWLEntity) a, this.getOntologyModel(), label).forEach(ann -> {
OWLAnnotationValue val = ann.getValue();
if(val instanceof OWLLiteral) {
if(!termsWithLabel.contains(termIRI)) {
this.termsWithLabel.add(termIRI);
}
}
});
OWLAnnotationProperty skosLabel = ontologyModel.getOWLOntologyManager().getOWLDataFactory().getOWLAnnotationProperty(Constants.PROP_SKOS_PREF_LABEL);
EntitySearcher.getAnnotations((OWLEntity) a, this.getOntologyModel(), skosLabel).forEach(ann -> {
OWLAnnotationValue val = ann.getValue();
if(val instanceof OWLLiteral) {
if(!termsWithLabel.contains(termIRI)) {
this.termsWithLabel.add(termIRI);
}
}
});
OWLAnnotationProperty oboLabel = ontologyModel.getOWLOntologyManager().getOWLDataFactory().getOWLAnnotationProperty(Constants.PROP_OBO_ALT_LABEL);
EntitySearcher.getAnnotations((OWLEntity) a, this.getOntologyModel(), oboLabel).forEach(ann -> {
OWLAnnotationValue val = ann.getValue();
if(val instanceof OWLLiteral) {
if(!termsWithLabel.contains(termIRI)) {
this.termsWithLabel.add(termIRI);
}
}
});
// descriptions: rdfs:comment, skos:definition
OWLAnnotationProperty description = ontologyModel.getOWLOntologyManager().getOWLDataFactory().
getRDFSComment();
EntitySearcher.getAnnotations((OWLEntity) a, this.getOntologyModel(), description).forEach(ann -> {
OWLAnnotationValue val = ann.getValue();
if (val instanceof OWLLiteral) {
if(!termsWithDescription.contains(termIRI)) {
this.termsWithDescription.add(termIRI);
}
}
});
OWLAnnotationProperty skosDescription = ontologyModel.getOWLOntologyManager().getOWLDataFactory().getOWLAnnotationProperty(Constants.PROP_SKOS_PREF_DEFINITION);
EntitySearcher.getAnnotations((OWLEntity) a, this.getOntologyModel(), skosDescription).forEach(ann -> {
OWLAnnotationValue val = ann.getValue();
if(val instanceof OWLLiteral) {
if(!termsWithDescription.contains(termIRI)) {
this.termsWithDescription.add(termIRI);
}
}
});
}
} catch (Exception e) {
logger.debug("Skipping term coverage for: " + a.getIRI().getIRIString() + " due to error: " + e.getMessage());
}
}
public OWLOntology getOntologyModel() {
return ontologyModel;
}
public String getOntologyURI() {
return ontologyURI;
}
public Document getHtmlDocumentation() {
return htmlDocumentation;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getStatus() {
return status;
}
public String getLicense() {
return license;
}
public String getRights() {
return rights;
}
public String getNamespacePrefix() {
return namespacePrefix;
}
public String getVersionIRI() {
return versionIRI;
}
public ArrayList<String> getSupportedMetadata() {
return supportedMetadata;
}
public ArrayList<String> getReusedMetadataVocabularies() {
return reusedMetadataVocabularies;
}
public List<IRI> getImportedVocabularies() {
return importedVocabularies;
}
public ArrayList<String> getReusedVocabularies() {
return reusedVocabularies;
}
public ArrayList<String> getTermsWithLabel() {
return termsWithLabel;
}
public ArrayList<String> getTermsWithDescription() {
return termsWithDescription;
}
public ArrayList<String> getTerms() {
return terms;
}
public boolean isSKOS() {
return isSKOS;
}
public String getName() {
return name;
}
public String getVersionInfo() {
return versionInfo;
}
public String getPreviousVersion() {
return previousVersion;
}
public String getBackwardCompatibility() {
return backwardCompatibility;
}
public String getCitation() {
return citation;
}
public ArrayList<String> getAuthors() {
return authors;
}
public ArrayList<String> getContributors() {
return contributors;
}
public String getCreationDate() {
return creationDate;
}
public String getIssuedDate() {
return issuedDate;
}
public String getDoi() {
return doi;
}
public String getLogo() {
return logo;
}
public String getModifiedDate() {
return modifiedDate;
}
public String getPublisher() {
return publisher;
}
public String getSource() {
return source;
}
public String getNamespaceUri(){
if (namespaceUri.isEmpty()) return "unknown";
else return namespaceUri;
}
/**
* Method that will download the ontology to process with FOOPS!
* Note: lazy loading of imports is implemented (making them fail).
*
* @param isFromFile boolean to indicate whether the ontology is from file or from URI.
*/
private void loadModelToDocument(String pathOrURI,boolean isFromFile, String downloadFolder) throws Exception {
String ontologyPath = pathOrURI;
if (!isFromFile) {
ontologyPath = downloadFolder + File.separator + "ontology";
Utils.downloadOntology(pathOrURI, ontologyPath);
}
logger.info("Loading ontology ");
File ontologyFile = new File(ontologyPath);
// if ontology is bigger than 50 MB, we do not process it
if (ontologyFile.length() > Constants.MAX_ONTOLOGY_SIZE) {
throw new FileTooLargeException("File is larger than maximum allowed (50 MB): " );
}
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// --- BLOCK TO PREVENT IMPORTS IN OWLAPI 5.5.0 ---
manager.getIRIMappers().add(iri -> {
if (!iri.equals(IRI.create(pathOrURI))) {
logger.warn("Blocking import: " + iri);
//record the imported vocabulary for later purposes
this.importedVocabularies.add(iri);
return IRI.create("file:///dev/null");
// we make the import fail along with MissingImportHandlingStrategy.SILENT
// and follow redirects set to false
}
////return IRI.create("urn:dummy:" + iri);
return null;
});
// -----------------------------------------------------
//this is for debugging purposes, to check that the imports are being blocked.
// but add helped information about the loading process
manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
@Override
public void startedLoadingOntology(OWLOntologyLoaderListener.LoadingStartedEvent event) {
if (!event.getDocumentIRI().equals(IRI.create(pathOrURI))) {
logger.debug("Skipping import: " + event.getDocumentIRI());
} else {
logger.debug("Loading main ontology: " + event.getDocumentIRI());
}
}
@Override
public void finishedLoadingOntology(OWLOntologyLoaderListener.LoadingFinishedEvent event) {
logger.info("Finished loading: " + event.getDocumentIRI());
}
});
OWLOntologyLoaderConfiguration loadingConfig = new OWLOntologyLoaderConfiguration();
loadingConfig = loadingConfig.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
loadingConfig = loadingConfig.setFollowRedirects(false);
logger.info("Parsing type: "+loadingConfig.isStrict());
this.ontologyModel = manager.loadOntologyFromOntologyDocument(
new StreamDocumentSource(new FileInputStream(ontologyFile), IRI.create(pathOrURI)),
loadingConfig
);
}
}