Skip to content

Commit

Permalink
ont-converter: move initializing jena-csv to main, some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sszuev committed Mar 30, 2022
1 parent 96c8b9f commit d545b11
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 53 deletions.
12 changes: 4 additions & 8 deletions src/main/java/com/github/sszuev/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class Args {
private final OntFormat outFormat, inFormat;
private final OntModelConfig.StdMode personality;
private final boolean spin, force, refine, verbose, webAccess;
private boolean outDir, inDir;
private final boolean outDir;
private final boolean inDir;

private Args(Path input,
Path output,
Expand Down Expand Up @@ -85,7 +86,7 @@ public static Args parse(String... args) throws IOException, IllegalArgumentExce
out = out.getParent().toRealPath().resolve(out.getFileName());
}

if (Files.isDirectory(in) && Files.walk(in).filter(f -> Files.isRegularFile(f)).count() > 1) {
if (Files.isDirectory(in) && Files.walk(in).filter(Files::isRegularFile).count() > 1) {
// out should be directory
if (Files.exists(out)) {
if (!Files.isDirectory(out)) {
Expand Down Expand Up @@ -143,15 +144,10 @@ private static String help(Options opts, boolean usage) {
if (usage) {
sb.append("Full list of supported formats:").append("\n");
sb.append(" ").append(formatHeader()).append("\n");
try {
Formats.registerJenaCSV();
OntFormat.formats()
.filter(f -> f.isReadSupported() || f.isWriteSupported())
.map(Args::formatLine)
.forEach(x -> sb.append(" ").append(x).append("\n"));
} finally {
Formats.unregisterJenaCSV();
}
}
return sb.toString();
}
Expand All @@ -160,7 +156,7 @@ private static String formatLine(OntFormat f) {
return StringUtils.rightPad(f.name(), NAME_COL_LENGTH) +
StringUtils.rightPad(f.isJena() ? "Apache Jena" : "OWL-API", PROVIDER_COL_LENGTH) +
StringUtils.rightPad(f.isReadSupported() + "/" + f.isWriteSupported(), READ_WRITE_COL_LENGTH) +
Formats.aliases(f).stream().collect(Collectors.joining(", "));
String.join(", ", Formats.aliases(f));

}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/github/sszuev/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sszuev;

import com.github.sszuev.ontapi.IRIMap;
import com.github.sszuev.utils.Formats;
import com.github.sszuev.utils.IRIs;
import com.github.sszuev.utils.Managers;
import org.apache.log4j.Level;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class Main {

public static void main(String... inputs) throws Exception {
forceDisableExternalLogging();
Formats.registerJenaCSV();
Args args = null;
try {
args = Args.parse(inputs);
Expand Down Expand Up @@ -183,7 +185,7 @@ private static void forceDisableExternalLogging() {
java.util.logging.LogManager.getLogManager().reset();
try {
// java9:
Class clazz = Class.forName("jdk.internal.module.IllegalAccessLogger");
Class<?> clazz = Class.forName("jdk.internal.module.IllegalAccessLogger");
Field logger = clazz.getDeclaredField("logger");
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/com/github/sszuev/utils/Formats.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.apache.jena.lang.csv.ReaderRIOTFactoryCSV;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFParserRegistry;
import org.semanticweb.owlapi.io.OWLOntologyDocumentSource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDocumentFormat;
import org.semanticweb.owlapi.model.OWLOntology;
import ru.avicomp.ontapi.OntFormat;
Expand Down Expand Up @@ -52,33 +50,12 @@ public static List<String> aliases(OntFormat f) {
/**
* Registers {@link Lang#CSV} in jena system.
* This operation enables {@link OntFormat#CSV} for reading operations.
*
* @see #unregisterJenaCSV()
*/
public static void registerJenaCSV() {
RDFParserRegistry.removeRegistration(Lang.CSV);
RDFParserRegistry.registerLangTriples(Lang.CSV, new ReaderRIOTFactoryCSV());
}

/**
* unregisters csv format
*
* @see #registerJenaCSV()
*/
public static void unregisterJenaCSV() {
RDFParserRegistry.removeRegistration(Lang.CSV);
}

/**
* Determines is the specified resource can be treated as csv-file.
*
* @param iri {@link IRI}
* @return true if the resource has extension '.csv'
*/
public static boolean isCSV(IRI iri) {
return IRIs.hasExtension(OntFormat.CSV.getExt(), iri);
}

/**
* Retrieves format from ontology
*
Expand All @@ -90,8 +67,4 @@ public static Optional<OntFormat> format(OWLOntology o) {
if (f == null) return Optional.empty();
return Optional.ofNullable(OntFormat.get(f));
}

public static Optional<OntFormat> format(OWLOntologyDocumentSource source) {
return source.getFormat().map(OntFormat::get);
}
}
18 changes: 1 addition & 17 deletions src/main/java/com/github/sszuev/utils/Managers.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
public class Managers {
private static final Logger LOGGER = LoggerFactory.getLogger(Managers.class);

static {
// exclude csv by default:
Formats.unregisterJenaCSV();
}

/**
* Creates a new manager with default settings.
*
Expand Down Expand Up @@ -183,17 +178,7 @@ public static OntologyManager copyManager(OntologyManager from) {
*/
public static OntologyModel loadOntology(OntologyManager manager,
OWLOntologyDocumentSource source) throws OWLOntologyCreationException {
Optional<OntFormat> format = Formats.format(source);
boolean isCvs = (!format.isPresent() && Formats.isCSV(source.getDocumentIRI()))
|| format.filter(s -> Objects.equals(s, OntFormat.CSV)).isPresent();
try {
if (isCvs) {
Formats.registerJenaCSV();
}
return manager.loadOntologyFromOntologyDocument(source);
} finally {
Formats.unregisterJenaCSV();
}
return manager.loadOntologyFromOntologyDocument(source);
}

/**
Expand All @@ -210,7 +195,6 @@ public static List<IRIMap> createMappings(Path dir, OntFormat format) throws IOE
return loadDirectory(dir, format, Managers::createSoftManager, true);
}


/**
* Loads a directory to {@link IRIMap} object.
* Directory could content duplicated ontologies, in that case several mappings would be created
Expand Down

0 comments on commit d545b11

Please sign in to comment.