Skip to content

Commit e3fab26

Browse files
committed
Update version of owlapi and fixed issues introduced in previous pr
1 parent 2ace56a commit e3fab26

5 files changed

Lines changed: 95 additions & 81 deletions

File tree

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>es.oeg</groupId>
1212
<artifactId>fair_ontologies</artifactId>
13-
<version>0.3.1</version>
13+
<version>0.3.2</version>
1414
<packaging>jar</packaging>
1515
<name>fair_ontologies</name>
1616
<description>FOOPS!: An Ontology Pitfall scanner for the FAIR principles</description>
@@ -79,10 +79,15 @@
7979
</dependency>
8080

8181
<!-- OWL API-->
82-
<dependency>
82+
<!--<dependency>
8383
<groupId>net.sourceforge.owlapi</groupId>
8484
<artifactId>owlapi-distribution</artifactId>
8585
<version>5.1.14</version>
86+
</dependency>-->
87+
<dependency>
88+
<groupId>net.sourceforge.owlapi</groupId>
89+
<artifactId>owlapi-apibinding</artifactId>
90+
<version>5.5.0</version>
8691
</dependency>
8792

8893
</dependencies>

src/main/java/entities/Ontology.java

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
import fair.Constants;
2222
import fair.Utils;
2323
import org.jsoup.nodes.Document;
24+
import org.semanticweb.owlapi.apibinding.OWLManager;
25+
import org.semanticweb.owlapi.io.StreamDocumentSource;
2426
import org.semanticweb.owlapi.model.*;
2527
import org.semanticweb.owlapi.search.EntitySearcher;
2628
import org.slf4j.Logger;
2729
import org.slf4j.LoggerFactory;
2830
import server.FileTooLargeException;
2931

32+
import java.io.File;
33+
import java.io.FileInputStream;
3034
import java.nio.file.Path;
3135
import java.util.ArrayList;
3236
import java.util.List;
@@ -65,28 +69,29 @@ public class Ontology {
6569
private final ArrayList<String> supportedMetadata;
6670
private final ArrayList<String> reusedMetadataVocabularies;
6771
private final ArrayList<String> reusedVocabularies;
68-
List<OWLImportsDeclaration> importedVocabularies;
72+
List<IRI> importedVocabularies;
6973
private final ArrayList<String> termsWithLabel;
7074
private final ArrayList<String> terms; // all terms
7175
private final ArrayList<String> termsWithDescription;
7276
private boolean isSKOS = false;
7377

7478
/**
75-
*
79+
* Ontology class constructor
7680
* @param o ontology path or URI
7781
* @param isFromFile flag to indicate whether the ontology should be loaded from file or URL
7882
* @param tmpFolder temporary folder where to store the downloaded ontology
7983
*/
8084
public Ontology(String o, boolean isFromFile, Path tmpFolder) throws FileTooLargeException {
8185
supportedMetadata = new ArrayList<>();
86+
importedVocabularies = new ArrayList<>();
8287
reusedMetadataVocabularies = new ArrayList<>();
8388
reusedVocabularies = new ArrayList<>();
8489
termsWithLabel = new ArrayList<>();
8590
termsWithDescription = new ArrayList<>();
8691
terms = new ArrayList<>();
8792
//Download ontology (any serialization)
8893
try {
89-
this.ontologyModel = Utils.loadModelToDocument(o, isFromFile, tmpFolder.toString());
94+
this.loadModelToDocument(o, isFromFile, tmpFolder.toString());
9095
if (this.ontologyModel != null && this.ontologyModel.getOntologyID().getOntologyIRI().isPresent()) {
9196
this.ontologyURI = this.ontologyModel.getOntologyID().getOntologyIRI().get().toString();
9297
}
@@ -456,8 +461,10 @@ private void completeMetadata(OWLAnnotation a) {
456461
private void getOntologyCoverage(){
457462
//metadata vocabularies
458463
this.ontologyModel.annotations().forEach(this::checkNamespaces);
459-
//imports
460-
this.importedVocabularies = this.ontologyModel.importsDeclarations().collect(Collectors.toList());
464+
// imports -> they are loaded when the ontology is loaded.
465+
// due to slow imports, we have disabled loading of imports
466+
//this.importedVocabularies = this.ontologyModel.importsDeclarations().collect(Collectors.toList());
467+
461468
//vocabulary reuse
462469
logger.info("Extracting namespaces, labels, descriptions");
463470
if(isSKOS) {
@@ -643,7 +650,7 @@ public ArrayList<String> getReusedMetadataVocabularies() {
643650
return reusedMetadataVocabularies;
644651
}
645652

646-
public List<OWLImportsDeclaration> getImportedVocabularies() {
653+
public List<IRI> getImportedVocabularies() {
647654
return importedVocabularies;
648655
}
649656

@@ -727,4 +734,71 @@ public String getNamespaceUri(){
727734
if (namespaceUri.isEmpty()) return "unknown";
728735
else return namespaceUri;
729736
}
737+
738+
/**
739+
* Method that will download the ontology to process with FOOPS!
740+
* Note: lazy loading of imports is implemented (making them fail).
741+
*
742+
* @param isFromFile boolean to indicate whether the ontology is from file or from URI.
743+
*/
744+
private void loadModelToDocument(String pathOrURI,boolean isFromFile, String downloadFolder) throws Exception {
745+
String ontologyPath = pathOrURI;
746+
if (!isFromFile) {
747+
ontologyPath = downloadFolder + File.separator + "ontology";
748+
Utils.downloadOntology(pathOrURI, ontologyPath);
749+
}
750+
logger.info("Loading ontology ");
751+
File ontologyFile = new File(ontologyPath);
752+
// if ontology is bigger than 50 MB, we do not process it
753+
if (ontologyFile.length() > Constants.MAX_ONTOLOGY_SIZE) {
754+
throw new FileTooLargeException("File is larger than maximum allowed (50 MB): " );
755+
}
756+
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
757+
758+
// --- BLOCK TO PREVENT IMPORTS IN OWLAPI 5.5.0 ---
759+
manager.getIRIMappers().add(iri -> {
760+
if (!iri.equals(IRI.create(pathOrURI))) {
761+
logger.warn("Blocking import: " + iri);
762+
//record the imported vocabulary for later purposes
763+
this.importedVocabularies.add(iri);
764+
return IRI.create("file:///dev/null");
765+
// we make the import fail along with MissingImportHandlingStrategy.SILENT
766+
// and follow redirects set to false
767+
}
768+
////return IRI.create("urn:dummy:" + iri);
769+
return null;
770+
});
771+
772+
// -----------------------------------------------------
773+
774+
//this is for debugging purposes, to check that the imports are being blocked.
775+
// but add helped information about the loading process
776+
manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
777+
@Override
778+
public void startedLoadingOntology(OWLOntologyLoaderListener.LoadingStartedEvent event) {
779+
if (!event.getDocumentIRI().equals(IRI.create(pathOrURI))) {
780+
logger.debug("Skipping import: " + event.getDocumentIRI());
781+
} else {
782+
logger.debug("Loading main ontology: " + event.getDocumentIRI());
783+
}
784+
}
785+
786+
@Override
787+
public void finishedLoadingOntology(OWLOntologyLoaderListener.LoadingFinishedEvent event) {
788+
logger.info("Finished loading: " + event.getDocumentIRI());
789+
}
790+
});
791+
792+
793+
OWLOntologyLoaderConfiguration loadingConfig = new OWLOntologyLoaderConfiguration();
794+
loadingConfig = loadingConfig.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
795+
loadingConfig = loadingConfig.setFollowRedirects(false);
796+
797+
logger.info("Parsing type: "+loadingConfig.isStrict());
798+
799+
this.ontologyModel = manager.loadOntologyFromOntologyDocument(
800+
new StreamDocumentSource(new FileInputStream(ontologyFile), IRI.create(pathOrURI)),
801+
loadingConfig
802+
);
803+
}
730804
}

src/main/java/entities/checks/Check_VOC1_VocabReuseMetadata.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void check(){
4444
super.check();
4545
this.reference_resources = new ArrayList<>();
4646
reference_resources = ontology.getReusedMetadataVocabularies();
47-
if(reference_resources.size()>0){
47+
if(!reference_resources.isEmpty()){
4848
this.total_passed_tests++;
4949
status = Constants.OK;
5050
explanation = Constants.VOC1_EXPLANATION_OK;
@@ -53,7 +53,7 @@ public void check(){
5353
explanation = Constants.VOC1_EXPLANATION_ERROR;
5454
}
5555
//to avoid returning empty lists
56-
if(reference_resources.size() == 0){
56+
if(reference_resources.isEmpty()){
5757
reference_resources = null;
5858
}
5959
}

src/main/java/entities/checks/Check_VOC2_VocabReuse.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import entities.Ontology;
2222
import fair.Constants;
2323
import org.apache.tomcat.util.bcel.Const;
24+
import org.semanticweb.owlapi.model.IRI;
2425
import org.semanticweb.owlapi.model.OWLImportsDeclaration;
2526

2627
import java.util.ArrayList;
@@ -46,11 +47,11 @@ public void check(){
4647
super.check();
4748
try{
4849
//first, if there are imports, we are done.
49-
List<OWLImportsDeclaration> imports = this.ontology.getImportedVocabularies();
50+
List<IRI> imports = this.ontology.getImportedVocabularies();
5051
this.reference_resources = new ArrayList<>();
5152
if (!imports.isEmpty()) {
52-
for (OWLImportsDeclaration imp:imports){
53-
reference_resources.add(imp.getIRI().getIRIString());
53+
for (IRI imp:imports){
54+
reference_resources.add(imp.getIRIString());
5455
}
5556
total_passed_tests ++;
5657
status = Constants.OK;

src/main/java/fair/Utils.java

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -34,79 +34,13 @@
3434
import java.nio.file.Files;
3535
import java.nio.file.Paths;
3636
import java.nio.file.StandardCopyOption;
37+
import java.util.ArrayList;
38+
import java.util.Optional;
3739

3840
public class Utils {
3941

4042
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
4143

42-
/**
43-
* Method that will download the ontology to document with Widoco.
44-
*
45-
* @param isFromFile boolean to indicate whether the ontology is from file or from URI.
46-
*/
47-
public static OWLOntology loadModelToDocument(String pathOrURI,boolean isFromFile, String downloadFolder) throws Exception {
48-
String ontologyPath = pathOrURI;
49-
if (!isFromFile) {
50-
ontologyPath = downloadFolder + File.separator + "ontology";
51-
downloadOntology(pathOrURI, ontologyPath);
52-
}
53-
logger.info("Loading ontology ");
54-
File ontologyFile = new File(ontologyPath);
55-
// if ontology is bigger than 50 MB, we do not process it
56-
if (ontologyFile.length() > Constants.MAX_ONTOLOGY_SIZE) {
57-
throw new FileTooLargeException("File is larger than maximum allowed (50 MB): " );
58-
}
59-
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
60-
61-
// --- BLOCK TO PREVENT IMPORTS IN OWLAPI 5.1.14 ---
62-
manager.getIRIMappers().add(iri -> {
63-
if (!iri.equals(IRI.create(pathOrURI))) {
64-
System.out.println("Blocking import: " + iri);
65-
return IRI.create("file:///dev/null");
66-
// we make the import fail along with MissingImportHandlingStrategy.SILENT
67-
// and follow redirects set to false
68-
}
69-
return null;
70-
});
71-
// -----------------------------------------------------
72-
73-
//this is for debugging purposes, to check that the imports are being blocked.
74-
// but add helped information about the loading process
75-
manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
76-
@Override
77-
public void startedLoadingOntology(OWLOntologyLoaderListener.LoadingStartedEvent event) {
78-
if (!event.getDocumentIRI().equals(IRI.create(pathOrURI))) {
79-
System.out.println("Skipping import: " + event.getDocumentIRI());
80-
} else {
81-
System.out.println("Loading main ontology: " + event.getDocumentIRI());
82-
}
83-
}
84-
85-
@Override
86-
public void finishedLoadingOntology(OWLOntologyLoaderListener.LoadingFinishedEvent event) {
87-
System.out.println("Finished loading: " + event.getDocumentIRI());
88-
}
89-
});
90-
91-
92-
OWLOntologyLoaderConfiguration loadingConfig = new OWLOntologyLoaderConfiguration();
93-
loadingConfig = loadingConfig.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
94-
loadingConfig = loadingConfig.setFollowRedirects(false);
95-
96-
// only works for owlpai 6.x, but not for 5.1.14, which is the one we are using in the tests
97-
// loadingConfig = loadingConfig.setLoadImports(false);
98-
// loadingConfig = loadingConfig.setProcessImports(false);
99-
100-
logger.info("Parsing type: "+loadingConfig.isStrict());
101-
102-
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(
103-
new StreamDocumentSource(new FileInputStream(ontologyFile), IRI.create(pathOrURI)),
104-
loadingConfig
105-
);
106-
107-
return ontology;
108-
}
109-
11044
public static void addAnnotationToOntology(OWLDataFactory df, OWLOntology o,IRI ontoURI, String propertyURI, String value){
11145
OWLAnnotationProperty predicate = df.getOWLAnnotationProperty(IRI.create(propertyURI));
11246
OWLAnnotation annotation = null;

0 commit comments

Comments
 (0)