Skip to content

Commit

Permalink
ont-converter: ontapi-1.4.0-snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
sszuev committed Sep 12, 2018
1 parent 4551d2d commit 53fc7b6
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 71 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Ontology Converter v1.0

## A simple command-line utility to convert any rdf graph to OWL2-DL ontology.
This is a kind of [ONT-API](https://github.com/avicomp/ont-api) extension, without a claim. Can work both with single ontology file source and with directory containing dependent or independent sources.
The utility automatically transforms the source RDF Graph to the OWL2 DL syntax according to the inner rules and command-line options. For example, if there is no `owl:Ontology` section inside rdf-graph, which is required by OWL, an anonymous ontology header will be generated.
## A simple command-line utility to convert any RDF graph to OWL2-DL ontology.
This is a kind of [ONT-API](https://github.com/avicomp/ont-api) extension with intentionally straightforward realisation.
Can work both with single ontology file source and with directory containing dependent or independent sources.
The utility automatically transforms the source RDF Graph to the OWL2 DL syntax according to the internal rules and command-line options.
For example, if there is no `owl:Ontology` section inside rdf-graph, which is required by OWL, an anonymous ontology header will be generated.
This is a tool, not a library, and, therefore, it is available only in the form of code and prebuilt jar (see [/releases](https://github.com/sszuev/ont-converter/releases)).

### Usage: `java -jar ont-converter.jar [-f] [-h] -i <path> [-if <format>] -o <path> -of <format> [-p <0|1|2>] [-r] [-s] [-v] [-w]`

Expand Down Expand Up @@ -114,5 +117,8 @@ where
* java1.8
* maven to build (`mvn package`)

### Issues:
In case of any issue (for example the program hangs), please report it into the [/ont-converter/issues](https://github.com/sszuev/ont-converter/issues) page, but only if you really sure that the problem is in the program, not in the underlying API. Otherwise please refer to the [/ont-api/issues](https://github.com/avicomp/ont-api/issues) page.

### Dependencies:
[ONT-API](https://github.com/avicomp/ont-api) (__version 1.1.0-SNAPSHOT__)
[ONT-API](https://github.com/avicomp/ont-api) (__version 1.4.0-SNAPSHOT__)
21 changes: 15 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
<version.ontapi>1.4.0-SNAPSHOT</version.ontapi>
<version.jena>3.8.0</version.jena>
<version.owlapi>5.1.7</version.owlapi>
<version.slf4j>1.7.25</version.slf4j>
</properties>

<repositories>
Expand All @@ -32,27 +36,27 @@
<dependency>
<groupId>ru.avicomp</groupId>
<artifactId>ontapi</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>${version.ontapi}</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-csv</artifactId>
<version>3.6.0</version>
<version>${version.jena}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-parsers</artifactId>
<version>5.1.4</version>
<version>${version.owlapi}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-rio</artifactId>
<version>5.1.4</version>
<version>${version.owlapi}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-oboformat</artifactId>
<version>5.1.4</version>
<version>${version.owlapi}</version>
</dependency>
<dependency>
<groupId>org.topbraid</groupId>
Expand All @@ -69,10 +73,15 @@
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${version.slf4j}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
<version>${version.slf4j}</version>
</dependency>
</dependencies>

Expand Down
31 changes: 20 additions & 11 deletions src/main/java/com/github/sszuev/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import ru.avicomp.ontapi.OntFormat;
import ru.avicomp.ontapi.jena.impl.configuration.Configurable;
import ru.avicomp.ontapi.jena.impl.configuration.OntModelConfig;
import ru.avicomp.ontapi.jena.impl.configuration.OntPersonality;
import ru.avicomp.ontapi.jena.impl.conf.OntModelConfig;
import ru.avicomp.ontapi.jena.impl.conf.OntPersonality;

import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -32,12 +31,20 @@ public class Args {
private static final int READ_WRITE_COL_LENGTH = 13;
private final Path input, output;
private final OntFormat outFormat, inFormat;
private final Configurable.Mode personality;
private final OntModelConfig.StdMode personality;
private final boolean spin, force, refine, verbose, webAccess;
private boolean outDir, inDir;

private Args(Path input, Path output, OntFormat outFormat, OntFormat inFormat, Configurable.Mode personality,
boolean spin, boolean force, boolean clear, boolean verbose, boolean webAccess) {
private Args(Path input,
Path output,
OntFormat outFormat,
OntFormat inFormat,
OntModelConfig.StdMode personality,
boolean spin,
boolean force,
boolean clear,
boolean verbose,
boolean webAccess) {
this.input = input;
this.output = output;
this.outFormat = outFormat;
Expand Down Expand Up @@ -67,7 +74,7 @@ public static Args parse(String... args) throws IOException, IllegalArgumentExce
// parse
OntFormat outputFormat = parseOutputFormat(cmd);
OntFormat inputFormat = parseInputFormat(cmd);
Configurable.Mode mode = parsePersonalities(cmd);
OntModelConfig.StdMode mode = parsePersonalities(cmd);

Path in = Paths.get(cmd.getOptionValue(Opts.INPUT.longName)).toRealPath();
Path out = Paths.get(cmd.getOptionValue(Opts.OUTPUT.longName));
Expand Down Expand Up @@ -111,13 +118,14 @@ private static OntFormat parseOutputFormat(CommandLine cmd) {
return res;
}

private static Configurable.Mode parsePersonalities(CommandLine cmd) {
private static OntModelConfig.StdMode parsePersonalities(CommandLine cmd) {
if (!cmd.hasOption(Opts.PUNNING.longName)) {
return Configurable.Mode.LAX;
return OntModelConfig.StdMode.LAX;
}
String val = cmd.getOptionValue(Opts.PUNNING.longName);
OntModelConfig.StdMode[] values = OntModelConfig.StdMode.values();
try {
return Configurable.Mode.values()[Configurable.Mode.values().length - Integer.parseInt(val)];
return values[values.length - Integer.parseInt(val)];
} catch (NumberFormatException | IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Wrong --" + Opts.PUNNING.longName + ":" + val, e);
}
Expand All @@ -129,7 +137,8 @@ private static String help(Options opts, boolean usage) {
sb.append("A simple command-line utility to convert any rdf graph to OWL2-DL ontology.").append("\n");
}
StringWriter sw = new StringWriter();
new HelpFormatter().printHelp(new PrintWriter(sw), 74, "java -jar " + JAR_NAME, "options:", opts, 1, 3, null, true);
new HelpFormatter().printHelp(new PrintWriter(sw), 74, "java -jar " + JAR_NAME,
"options:", opts, 1, 3, null, true);
sb.append(sw);
if (usage) {
sb.append("Full list of supported formats:").append("\n");
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
Expand Up @@ -103,7 +103,9 @@ private static void processDir(Args args) throws IOException, OntApiException {
}
}

private static OWLOntologyID load(OntologyManager manager, OWLOntologyDocumentSource source, boolean ignoreErrors) throws OntApiException {
private static OWLOntologyID load(OntologyManager manager,
OWLOntologyDocumentSource source,
boolean ignoreErrors) throws OntApiException {
try {
OWLOntology o = Managers.loadOntology(manager, source);
return o.getOntologyID();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/sszuev/spin/SpinTransform.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void perform() {
.map(Statement::getObject)
.filter(RDFNode::isURIResource)
.map(RDFNode::asResource)
.findFirst().orElseThrow(OntJenaException.supplier("No type for " + literal));
.findFirst().orElseThrow(() -> new OntJenaException("No type for " + literal));
Set<Statement> remove = Models.getAssociatedStatements(query);
remove.stream()
.filter(s -> !(RDF.type.equals(s.getPredicate()) && type.equals(s.getObject())))
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/github/sszuev/utils/Formats.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.github.sszuev.utils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Utils to work with {@link OntFormat format}s.
*
* <p>
* Created by @szuev on 11.01.2018.
*/
@SuppressWarnings("WeakerAccess")
Expand All @@ -40,6 +39,7 @@ public static OntFormat find(String key) {

/**
* Returns format aliases
*
* @param f {@link OntFormat}, not null
* @return List of stings corresponding specified format.
*/
Expand All @@ -52,6 +52,7 @@ 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() {
Expand All @@ -61,6 +62,7 @@ public static void registerJenaCSV() {

/**
* unregisters csv format
*
* @see #registerJenaCSV()
*/
public static void unregisterJenaCSV() {
Expand All @@ -69,6 +71,7 @@ public static void unregisterJenaCSV() {

/**
* Determines is the specified resource can be treated as csv-file.
*
* @param iri {@link IRI}
* @return true if the resource has extension '.csv'
*/
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/com/github/sszuev/utils/IRIs.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.github.sszuev.utils;

import org.apache.commons.io.FilenameUtils;
import org.semanticweb.owlapi.io.FileDocumentSource;
import org.semanticweb.owlapi.io.IRIDocumentSource;
import org.semanticweb.owlapi.io.OWLOntologyDocumentSource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyID;
import ru.avicomp.ontapi.OntFormat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -11,19 +20,9 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FilenameUtils;
import org.semanticweb.owlapi.io.FileDocumentSource;
import org.semanticweb.owlapi.io.IRIDocumentSource;
import org.semanticweb.owlapi.io.OWLOntologyDocumentSource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyID;

import ru.avicomp.ontapi.OntFormat;

/**
* The helper to work with {@link IRI}s, usually related to local files.
*
* <p>
* Created by @szuev on 15.01.2018.
*/
public class IRIs {
Expand All @@ -41,6 +40,7 @@ public static boolean hasExtension(String extension, IRI iri) {

/**
* Returns all files from a directory, most deep go first, empty files are excluded
*
* @param dir {@link Path}, the directory (but can be file also)
* @return List of {@link IRI}s
* @throws IOException if any i/o error occurs.
Expand All @@ -52,7 +52,8 @@ public static List<IRI> getFiles(Path dir) throws IOException {

/**
* Walks through directory, the output stream are sorted in descending order of file depth
* @param dir {@link Path}
*
* @param dir {@link Path}
* @param holder {@link IOException} to collect any internal (file-specified) exceptions
* @return Stream of {@link IRI}s
* @throws IOException if i/o error occurs when read directory
Expand Down Expand Up @@ -85,8 +86,9 @@ public static Stream<IRI> walk(Path dir, IOException holder) throws IOException

/**
* Creates a document-source from iri and format
*
* @param document {@link IRI}, not null
* @param format {@link OntFormat}, nullable
* @param format {@link OntFormat}, nullable
* @return {@link OWLOntologyDocumentSource}
*/
public static OWLOntologyDocumentSource toSource(IRI document, OntFormat format) {
Expand Down
Loading

0 comments on commit 53fc7b6

Please sign in to comment.