Skip to content

Commit

Permalink
[#5] Refactoring - generalizing for the whole ontology header
Browse files Browse the repository at this point in the history
  • Loading branch information
psiotwo committed Mar 14, 2023
1 parent da4d518 commit f5521b6
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 79 deletions.
20 changes: 10 additions & 10 deletions src/main/java/cz/sio2/obo/OBOFoundryVersionExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
@Slf4j
public class OBOFoundryVersionExtractor {

private static void writeRDF(final String file, final Map<String, Version> map) throws IOException {
private static void writeRDF(final String file, final Map<String, OntologyHeader> map) throws IOException {
try (final OutputStream os = new FileOutputStream(file)) {
new TurtleReport().write(map, os);
}
}

private static void writeCSV(final String file, final Map<String, Version> map) throws IOException {
private static void writeCSV(final String file, final Map<String, OntologyHeader> map) throws IOException {
try (final OutputStream os = new FileOutputStream(file)) {
new CSVReport().write(map, os);
}
}

private static void writeHTML(final String file, final Map<String, Version> map) throws IOException {
private static void writeHTML(final String file, final Map<String, OntologyHeader> map) throws IOException {
try (final OutputStream os = new FileOutputStream(file)) {
new HTMLReport().writeHTML(map, os);
}
Expand All @@ -58,29 +58,29 @@ private static List<String> getOntologyUrls(final String registry) {
return list;
}

private static Map<String, Version> fetchVersions(final List<String> ontologyUrls, final int headerLength) throws MalformedURLException {
private static Map<String, OntologyHeader> fetchVersions(final List<String> ontologyUrls, final int headerLength) throws MalformedURLException {
final VersionFetcher f = new VersionFetcher();
final Map<String, Version> map = new HashMap<>();
final Map<String, OntologyHeader> map = new HashMap<>();
for (final String url : ontologyUrls) {
log.info(url);
final Version v = f.fetch(new URL(url), headerLength);
final OntologyHeader v = f.fetch(new URL(url), headerLength);
map.put(url, v);
}
return map;
}

public void extract(final String registryUrl, final String outputFile, final int headerLength) throws IOException {
final List<String> ontologyUrls = getOntologyUrls(registryUrl);
final Map<String, Version> ontologyVersions = fetchVersions(ontologyUrls, headerLength);
final Map<String, OntologyHeader> ontologyVersions = fetchVersions(ontologyUrls, headerLength);
writeRDF(outputFile, ontologyVersions);
}

private Map<String, Version> loadVersions(final String inputFile) {
final Map<String, Version> map = new HashMap<>();
private Map<String, OntologyHeader> loadVersions(final String inputFile) {
final Map<String, OntologyHeader> map = new HashMap<>();
final Model model = ModelFactory.createDefaultModel();
model.read(inputFile, Lang.TURTLE.toString());
model.listSubjectsWithProperty(RDF.type, OWL.Ontology).forEach(ontology -> {
final Version version = new Version();
final OntologyHeader version = new OntologyHeader();
version.setOwlOntologyIri(ontology.getURI());
final Statement versionIri = ontology.getProperty(OWL2.versionIRI);
if ( versionIri != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import static cz.sio2.obo.Constants.*;

/**
* Object that represents the version extracted from the ontology.
* Object that represents the ontology header extracted from the ontology.
*/
@Getter
@Setter
@Slf4j
public class Version {
public class OntologyHeader {

/**
* owl:Ontology IRI.
Expand Down Expand Up @@ -109,7 +109,7 @@ String extractVersionFromVersionIri() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Version version = (Version) o;
OntologyHeader version = (OntologyHeader) o;
return Objects.equals(owlOntologyIri, version.owlOntologyIri) && Objects.equals(owlVersionIri, version.owlVersionIri) && Objects.equals(owlVersionInfo, version.owlVersionInfo);
}

Expand Down
24 changes: 12 additions & 12 deletions src/main/java/cz/sio2/obo/VersionFetcher.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cz.sio2.obo;

import cz.sio2.obo.versionextractor.FSVersionExtractor;
import cz.sio2.obo.versionextractor.RDFXMLVersionExtractor;
import cz.sio2.obo.versionextractor.VersionExtractor;
import cz.sio2.obo.versionextractor.XMLVersionExtractor;
import cz.sio2.obo.extractor.FSExtractor;
import cz.sio2.obo.extractor.RDFXMLExtractor;
import cz.sio2.obo.extractor.Extractor;
import cz.sio2.obo.extractor.XMLExtractor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
Expand All @@ -28,12 +28,12 @@
@Slf4j
public class VersionFetcher {

final static List<VersionExtractor> extractors = new ArrayList<>();
final static List<Extractor> extractors = new ArrayList<>();

static {
extractors.add(new RDFXMLVersionExtractor());
extractors.add(new FSVersionExtractor());
extractors.add(new XMLVersionExtractor());
extractors.add(new RDFXMLExtractor());
extractors.add(new FSExtractor());
extractors.add(new XMLExtractor());
}

/**
Expand All @@ -45,7 +45,7 @@ public class VersionFetcher {
* @param maxBytes maximal number of bytes to fetch from each side of the document
* @return Version information from the ontology
*/
public Version fetch(final URL url, final int maxBytes) {
public OntologyHeader fetch(final URL url, final int maxBytes) {
final RequestConfig cfg = RequestConfig.custom().setConnectTimeout(Timeout.ofMinutes(1)).build();
final HttpClientBuilder httpClientBuilder = createBuilder().setDefaultRequestConfig(cfg);
try (final CloseableHttpClient httpClient = httpClientBuilder.build()) {
Expand Down Expand Up @@ -90,9 +90,9 @@ private String extractContentFromResponse(final CloseableHttpResponse response,
}
}

private Version extractVersion(final String content) {
final Version version = new Version();
for (final VersionExtractor e : extractors) {
private OntologyHeader extractVersion(final String content) {
final OntologyHeader version = new OntologyHeader();
for (final Extractor e : extractors) {
if (e.extract(content, version)) {
return version;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cz.sio2.obo.versionextractor;
package cz.sio2.obo.extractor;

import cz.sio2.obo.Version;
import cz.sio2.obo.OntologyHeader;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cz.sio2.obo.Constants.NS_OBO_HTTP;

public abstract class VersionExtractor {
public abstract class Extractor {

protected abstract Pattern getFormatMatcher();

Expand All @@ -33,7 +33,7 @@ private String sanitize(final String input) {
return input.replace("&obo;", NS_OBO_HTTP);
}

public boolean extract(final String s, final Version version) {
public boolean extract(final String s, final OntologyHeader version) {
final String singleLine = s.replace('\n', ' ');
if (!getFormatMatcher().matcher(singleLine).matches()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cz.sio2.obo.versionextractor;
package cz.sio2.obo.extractor;

import java.util.regex.Pattern;

public class FSVersionExtractor extends VersionExtractor {
public class FSExtractor extends Extractor {

@Override
protected Pattern getFormatMatcher() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cz.sio2.obo.versionextractor;
package cz.sio2.obo.extractor;

import java.util.regex.Pattern;

public class RDFXMLVersionExtractor extends VersionExtractor {
public class RDFXMLExtractor extends Extractor {

@Override
protected Pattern getFormatMatcher() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cz.sio2.obo.versionextractor;
package cz.sio2.obo.extractor;

import java.util.regex.Pattern;

public class XMLVersionExtractor extends VersionExtractor {
public class XMLExtractor extends Extractor {

@Override
protected Pattern getFormatMatcher() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/cz/sio2/obo/report/CSVReport.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cz.sio2.obo.report;

import cz.sio2.obo.Version;
import cz.sio2.obo.OntologyHeader;
import cz.sio2.obo.VersionType;

import java.io.IOException;
Expand All @@ -11,10 +11,10 @@

public class CSVReport {

public void write(Map<String, Version> map, final OutputStream os) throws IOException {
public void write(Map<String, OntologyHeader> map, final OutputStream os) throws IOException {
try (final Writer writer = new OutputStreamWriter(os)) {
for (final Map.Entry<String, Version> entry : map.entrySet()) {
final Version v = entry.getValue();
for (final Map.Entry<String, OntologyHeader> entry : map.entrySet()) {
final OntologyHeader v = entry.getValue();
writer.append(v != null ? VersionType.get(v.getOwlOntologyIri()
, v.getOwlVersionIri(), v.getOwlVersionInfo()).name() : VersionType.UNKNOWN.name())
.append(',')
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/cz/sio2/obo/report/HTMLReport.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cz.sio2.obo.report;

import cz.sio2.obo.OntologyRecord;
import cz.sio2.obo.Version;
import cz.sio2.obo.OntologyHeader;
import cz.sio2.obo.VersionType;
import freemarker.template.Configuration;
import freemarker.template.Template;
Expand Down Expand Up @@ -31,7 +31,7 @@ public class HTMLReport {
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
}

public void writeHTML(Map<String, Version> map, final OutputStream os) throws IOException {
public void writeHTML(Map<String, OntologyHeader> map, final OutputStream os) throws IOException {
try {
final Map<String, Object> dataModel = new HashMap<>();
final Set<OntologyRecord> records = new HashSet<>();
Expand All @@ -41,9 +41,9 @@ public void writeHTML(Map<String, Version> map, final OutputStream os) throws IO
final String date = simpleDateFormat.format(new Date());
dataModel.put("date", date);

final List<Map.Entry<String, Version>> list = new ArrayList<>(map.entrySet());
final List<Map.Entry<String, OntologyHeader>> list = new ArrayList<>(map.entrySet());

for (Map.Entry<String, Version> entry : list) {
for (Map.Entry<String, OntologyHeader> entry : list) {
boolean failed = entry.getValue() == null;
final OntologyRecord record = new OntologyRecord();
record
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/cz/sio2/obo/report/TurtleReport.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cz.sio2.obo.report;

import cz.sio2.obo.Version;
import cz.sio2.obo.OntologyHeader;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
Expand All @@ -16,18 +16,18 @@

public class TurtleReport {

public void write(Map<String, Version> map, final OutputStream os) {
public void write(Map<String, OntologyHeader> map, final OutputStream os) {
final Model model = createModel(map);
RDFDataMgr.write(os, model, Lang.TURTLE);
}

private Model createModel(Map<String, Version> map) {
private Model createModel(Map<String, OntologyHeader> map) {
final Model model = ModelFactory.createDefaultModel();
for (final Map.Entry<String, Version> entry : map.entrySet()) {
for (final Map.Entry<String, OntologyHeader> entry : map.entrySet()) {
final Resource ontology = ResourceFactory.createResource(entry.getKey());
model.add(ontology, RDF.type, OWL.Ontology);

final Version version = entry.getValue();
final OntologyHeader version = entry.getValue();
if ( version != null ) {
final String versionIriString = version.getOwlVersionIri();
if (versionIriString != null && !versionIriString.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

public class TestVersion {
public class TestOntologyHeader {

@ParameterizedTest
@CsvFileSource(resources = "/version-testcases.csv", numLinesToSkip = 1, delimiter = ',')
public void testGetOboVersionReturnsVersionIriIfPresent(String ontologyIri, String versionIri, String versionInfo, String computedVersionIri, String computedVersion) {
final Version version = new Version();
version.setOwlOntologyIri(ontologyIri);
version.setOwlVersionIri(versionIri);
version.setOwlVersionInfo(versionInfo);
Assertions.assertEquals(computedVersion, version.getVersion());
Assertions.assertEquals(computedVersionIri, version.getOboVersionIri());
final OntologyHeader header = new OntologyHeader();
header.setOwlOntologyIri(ontologyIri);
header.setOwlVersionIri(versionIri);
header.setOwlVersionInfo(versionInfo);
Assertions.assertEquals(computedVersion, header.getVersion());
Assertions.assertEquals(computedVersionIri, header.getOboVersionIri());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cz.sio2.obo.versionextractor;
package cz.sio2.obo.extractor;

import cz.sio2.obo.Version;
import cz.sio2.obo.versionextractor.FSVersionExtractor;
import cz.sio2.obo.OntologyHeader;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
Expand All @@ -12,7 +11,7 @@
import java.nio.file.Paths;
import java.util.Objects;

public class TestOWLVersionExtractor {
public class TestOWLExtractor {

@ParameterizedTest
@CsvFileSource(resources = "/owl-testcases.csv", numLinesToSkip = 1, delimiter = ',')
Expand All @@ -22,8 +21,8 @@ public void testExtractVersionIriFromXMLExtractsVersionInfoCorrectlyIfPresent(
String versionIri,
String versionInfo) throws URISyntaxException, IOException {
final String s = Files.readString(Paths.get(Objects.requireNonNull(getClass().getResource("/owl-testcases/" + file)).toURI()));
final Version version = new Version();
new FSVersionExtractor().extract(s, version);
final OntologyHeader version = new OntologyHeader();
new FSExtractor().extract(s, version);
Assertions.assertEquals(ontologyIri, version.getOwlOntologyIri());
Assertions.assertEquals(versionIri, version.getOwlVersionIri());
Assertions.assertEquals(versionInfo, version.getOwlVersionInfo());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cz.sio2.obo.versionextractor;
package cz.sio2.obo.extractor;

import cz.sio2.obo.Version;
import cz.sio2.obo.versionextractor.RDFXMLVersionExtractor;
import cz.sio2.obo.OntologyHeader;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
Expand All @@ -12,7 +11,7 @@
import java.nio.file.Paths;
import java.util.Objects;

public class TestRDFXMLVersionExtractor {
public class TestRDFXMLExtractor {

@ParameterizedTest
@CsvFileSource(resources = "/rdf-testcases.csv", numLinesToSkip = 1, delimiter = ',')
Expand All @@ -21,10 +20,10 @@ public void testExtractVersionFromVersionIriFromRDFXMLWorksCorrectly(String file
String versionIri,
String versionInfo) throws URISyntaxException, IOException {
final String s = Files.readString(Paths.get(Objects.requireNonNull(getClass().getResource("/rdf-testcases/" + file)).toURI()));
final Version version = new Version();
new RDFXMLVersionExtractor().extract(s, version);
Assertions.assertEquals(ontologyIri, version.getOwlOntologyIri());
Assertions.assertEquals(versionIri, version.getOwlVersionIri());
Assertions.assertEquals(versionInfo, version.getOwlVersionInfo());
final OntologyHeader header = new OntologyHeader();
new RDFXMLExtractor().extract(s, header);
Assertions.assertEquals(ontologyIri, header.getOwlOntologyIri());
Assertions.assertEquals(versionIri, header.getOwlVersionIri());
Assertions.assertEquals(versionInfo, header.getOwlVersionInfo());
}
}
Loading

0 comments on commit f5521b6

Please sign in to comment.