Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fix for the context download issue #107

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-jsonld</artifactId>
<version>2.3.2</version>
<version>3.7.7</version>
<scope>runtime</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.eclipse.rdf4j/rdf4j-rio-api -->
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-api</artifactId>
<version>2.3.2</version>
<version>3.7.7</version>
</dependency>

<!-- REASONERS -->
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/phyloref/jphyloref/helpers/JSONLDHelper.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
package org.phyloref.jphyloref.helpers;

import com.github.jsonldjava.core.DocumentLoader;
import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.core.RemoteDocument;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.helpers.JSONLDSettings;
import org.semanticweb.owlapi.formats.RDFJsonLDDocumentFormat;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration;
import org.semanticweb.owlapi.rio.RioOWLRDFConsumerAdapter;
import org.semanticweb.owlapi.util.AnonymousNodeChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* JSONLDHelper provides methods to help read and process JSON-LD files.
*
* @author Gaurav Vaidya
*/
public class JSONLDHelper {
private static final Logger logger = LoggerFactory.getLogger(JSONLDHelper.class);

/**
* Create an RDFParser for JSON-LD files. When the parser's <code>parse()</code> method is called,
* its contents will be added to the OWLOntology passed to this method.
Expand Down Expand Up @@ -60,6 +73,59 @@ public boolean isAnonymousNode(IRI iri) {
RDFParser parser = Rio.createParser(RDFFormat.JSONLD);
parser.setRDFHandler(rdfHandler);

// We get some indistinct errors if any of the context URLs in the JSON-LD file are
// incorrect or inaccessible. However, we can set up our own document loader so we
// can detect when this occurs.)
parser.set(
JSONLDSettings.DOCUMENT_LOADER,
new DocumentLoader() {
{
// Load built-in context files as injected documents.
try {
this.addInjectedDoc(
"https://www.phyloref.org/phyx.js/context/v1.0.0/phyx.json",
loadResourceFileAsString("context/phyx-context-v1.0.0.json"));
} catch (IOException e) {
logger.error("Could not load Phyx context v1.0.0: " + e);
}

try {
this.addInjectedDoc(
"https://www.phyloref.org/phyx.js/context/v1.1.0/phyx.json",
loadResourceFileAsString("context/phyx-context-v1.1.0.json"));
} catch (IOException e) {
logger.error("Could not load Phyx context v1.1.0: " + e);
}
}

/**
* Load a text Resource as a String. Based on the code at
* https://stackoverflow.com/a/46613809/27310
*
* @return A String representation of the Resource specified.
*/
private String loadResourceFileAsString(String fileName) throws IOException {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try (InputStream is = classLoader.getResourceAsStream(fileName)) {
if (is == null) throw new IOException("Could not find resource: " + fileName);
try (InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr)) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
}

@Override
public RemoteDocument loadDocument(String url) throws JsonLdError {
try {
return super.loadDocument(url);
} catch (Exception err) {
logger.error("Error occurred while loading document " + url + ": " + err);
throw new JsonLdError(JsonLdError.Error.LOADING_REMOTE_CONTEXT_FAILED, url, err);
}
}
});

return parser;
}
}
143 changes: 143 additions & 0 deletions src/main/resources/context/phyx-context-v1.0.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"@context": {
"xsd": "http://www.w3.org/2001/XMLSchema#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"obo": "http://purl.obolibrary.org/obo/",
"dwc": "http://rs.tdwg.org/dwc/terms/",
"dct": "http://purl.org/dc/terms/",
"tc": "http://rs.tdwg.org/ontology/voc/TaxonConcept#",
"tn": "http://rs.tdwg.org/ontology/voc/TaxonName#",
"testcase": "http://vocab.phyloref.org/phyloref/testcase.owl#",
"pso": "http://purl.org/spar/pso/",
"tvc": "http://www.essepuntato.it/2012/04/tvc/",
"timeinterval": "http://www.ontologydesignpatterns.org/cp/owl/timeinterval.owl#",
"phyloref": "http://ontology.phyloref.org/phyloref.owl#",
"opentree": "http://purl.org/opentree/nexson#",
"subClasses": {
"@reverse": "rdfs:subClassOf"
},
"label": {
"@id": "rdfs:label",
"@type": "xsd:string"
},
"labels": {
"@id": "rdfs:label",
"@type": "xsd:string",
"@container": "@set"
},
"namePublishedIn": "dwc:namePublishedIn",
"definitionSource": "obo:IAO_0000119",
"definition": {
"@id": "obo:IAO_0000115",
"@type": "xsd:string"
},
"hasName": {
"@id": "tc:hasName",
"@type": "tn:TaxonName"
},
"nameComplete": {
"@id": "tn:nameComplete",
"@type": "xsd:string"
},
"nomenclaturalCode": {
"@id": "tn:nomenclaturalCode",
"@type": "@id"
},
"scientificName": {
"@id": "dwc:scientificName",
"@type": "xsd:string"
},
"genusPart": {
"@id": "tn:genusPart",
"@type": "xsd:string"
},
"specificEpithet": {
"@id": "tn:specificEpithet",
"@type": "xsd:string"
},
"basisOfRecord": "dwc:basisOfRecord",
"occurrenceID": "dwc:occurrenceID",
"catalogNumber": "dwc:catalogNumber",
"collectionCode": "dwc:collectionCode",
"institutionCode": "dwc:institutionCode",
"subClassOf": {
"@id": "rdfs:subClassOf",
"@type": "@id"
},
"parent": {
"@id": "obo:CDAO_0000179",
"@type": "@id"
},
"children": {
"@id": "obo:CDAO_0000149",
"@type": "@id"
},
"siblings": {
"@id": "phyloref:has_Sibling",
"@type": "@id"
},
"owl:imports": {
"@id": "owl:imports",
"@type": "@id"
},
"equivalentClass": "owl:equivalentClass",
"intersectionOf": {
"@id": "owl:intersectionOf",
"@container": "@list"
},
"onProperty": {
"@id": "owl:onProperty",
"@type": "@id"
},
"someValuesFrom": {
"@id": "owl:someValuesFrom",
"@container": "@set"
},
"unionOf": {
"@id": "owl:unionOf",
"@container": "@list"
},
"hasValue": {
"@id": "owl:hasValue",
"@type": "xsd:string"
},
"nodes": {
"@reverse": "obo:CDAO_0000200",
"@container": "@set"
},
"hasRootNode": {
"@id": "obo:CDAO_0000148",
"@type": "@id"
},
"newick": {
"@id": "phyloref:newick_expression",
"@type": "xsd:string"
},
"curator": {
"@id": "opentree:curatorName",
"@type": "xsd:string"
},
"scientificNameAuthorship": {
"@id": "dwc:scientificNameAuthorship",
"@type": "xsd:string"
},
"nameAccordingTo": "tc:accordingTo",
"phylogenies": {
"@reverse": "rdfs:isDefinedBy",
"@container": "@set"
},
"phylorefs": {
"@reverse": "rdfs:isDefinedBy",
"@container": "@set"
},
"title": "dct:title",
"source": "dct:source",
"bibliographicCitation": "dct:bibliographicCitation",
"doi": "http://prismstandard.org/namespaces/basic/2.0/doi",
"representsTaxonomicUnits": "obo:CDAO_0000187",
"apomorphy": "phyloref:apomorphy"
}
}
Loading
Loading