Skip to content

Commit

Permalink
28 suppressing named individuals (#37)
Browse files Browse the repository at this point in the history
* Add documentation update and upgrade the project version

Co-authored-by: Rafał Trójczak <[email protected]>
  • Loading branch information
trojczak and Rafał Trójczak authored Nov 14, 2022
1 parent a4869c0 commit 8450ce7
Show file tree
Hide file tree
Showing 17 changed files with 1,619 additions and 1,290 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ java -jar rdf-toolkit.jar --source C:/source_ontologies/example.rdf --target C:/
turtle (Turtle)
-sip,--short-iri-priority <arg> set what takes priority when shortening IRIs: prefix
[default], base-iri
-sni,--suppress-named-individuals suppresses all instances of owl:NamedIndividual
-t,--target <arg> target (output) RDF file
-tc,--trailing-comment <arg> sets the text of the trailing comment in the ontology. Can
be repeated for a multi-line comment
Expand Down
19 changes: 14 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

<groupId>org.edmcouncil</groupId>
<artifactId>rdf-toolkit</artifactId>
<version>1.13.0</version>
<version>1.14.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<!-- Versions -->
<logback.version>1.4.4</logback.version>
<junit.jupiter.version>5.9.0</junit.jupiter.version>
</properties>

<repositories>
Expand Down Expand Up @@ -82,19 +86,25 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -252,7 +262,6 @@
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
4 changes: 2 additions & 2 deletions src/main/java/org/edmcouncil/rdf_toolkit/RdfFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

package org.edmcouncil.rdf_toolkit;

import java.io.PrintWriter;
import java.io.StringWriter;
import org.edmcouncil.rdf_toolkit.runner.RdfToolkitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
* RDF formatter that formats in a consistent order, friendly for version control systems.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Enterprise Data Management Council
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.edmcouncil.rdf_toolkit.runner;

import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.HELP;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.VERSION;
import static org.edmcouncil.rdf_toolkit.runner.constant.RunningMode.EXIT;
import static org.edmcouncil.rdf_toolkit.runner.constant.RunningMode.PRINT_AND_EXIT;
import static org.edmcouncil.rdf_toolkit.runner.constant.RunningMode.PRINT_USAGE_AND_EXIT;
import static org.edmcouncil.rdf_toolkit.runner.constant.RunningMode.RUN_ON_DIRECTORY;
import static org.edmcouncil.rdf_toolkit.runner.constant.RunningMode.RUN_ON_FILE;

import com.jcabi.manifests.Manifests;
import java.io.FileNotFoundException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.sail.memory.model.MemValueFactory;
import org.edmcouncil.rdf_toolkit.RdfFormatter;
import org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption;
import org.edmcouncil.rdf_toolkit.runner.exception.RdfToolkitOptionHandlingException;

public class CommandLineArgumentsHandler {

private final Options options;
private final ValueFactory valueFactory;

public CommandLineArgumentsHandler() {
this.options = CommandLineOption.prepareOptions();
this.valueFactory = new MemValueFactory();
}

public RdfToolkitOptions handleArguments(String[] args)
throws RdfToolkitOptionHandlingException, FileNotFoundException, ParseException {
var rdfToolkitOptions = new RdfToolkitOptions(args);

// Parse the command line options.
CommandLineParser parser = new DefaultParser();
CommandLine line = parser.parse(options, args);
rdfToolkitOptions.setCommandLine(line);

var optionHandler = new OptionHandler(rdfToolkitOptions);

// Print out version, if requested.
if (line.hasOption(VERSION.getShortOpt())) {
rdfToolkitOptions.setOutput(getVersion());
rdfToolkitOptions.setRunningMode(PRINT_AND_EXIT);
return rdfToolkitOptions;
}

// Print out help, if requested.
if (line.hasOption(HELP.getShortOpt())) {
usage(options);
rdfToolkitOptions.setRunningMode(EXIT);
return rdfToolkitOptions;
}

optionHandler.handleRunningOnDirectory(line, rdfToolkitOptions);
if (rdfToolkitOptions.getRunningMode() == PRINT_USAGE_AND_EXIT) {
usage(options);
return rdfToolkitOptions;
}
if (rdfToolkitOptions.getRunningMode() == RUN_ON_DIRECTORY) {
return rdfToolkitOptions;
}

var sourceFile = optionHandler.handleSourceFile();
optionHandler.handleTargetFile();
optionHandler.handleBaseIri(valueFactory);
optionHandler.handleIriReplacementOptions();
optionHandler.handleUseDtdSubset();
optionHandler.handleInlineBlankNodes();
optionHandler.handleInferBaseIri();
optionHandler.handleLeadingComments();
optionHandler.handleTrailingComments();
optionHandler.handleStringDataTyping();
optionHandler.handleOverrideStringLanguage();
optionHandler.handleIndent();
optionHandler.handleSourceFormat(sourceFile);
optionHandler.handleTargetFormat();
optionHandler.handleShortUriPref();
optionHandler.handleLineEnd();
optionHandler.handleOmitXmlnsNamespace();
optionHandler.handleSuppressNamedIndividuals();

rdfToolkitOptions.setRunningMode(RUN_ON_FILE);

return rdfToolkitOptions;
}

private void usage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.setWidth(100);
formatter.printHelp(getVersion(), options);
}

private String getVersion() {
String implementationTitle = Manifests.read("Implementation-Title");
String implementationVersion = Manifests.read("Implementation-Version");
return String.format(
"%s (%s version %s)",
RdfFormatter.class.getSimpleName(),
implementationTitle,
implementationVersion);
}
}
89 changes: 47 additions & 42 deletions src/main/java/org/edmcouncil/rdf_toolkit/runner/OptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,32 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.edmcouncil.rdf_toolkit.runner;

import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.BASE_IRI;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.INDENT;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.INFER_BASE_IRI;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.INLINE_BLANK_NODES;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.LEADING_COMMENT;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.LINE_END;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.OMIT_XMLNS_NAMESPACE;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.OVERRIDE_STRING_LANGUAGE;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.SHORT_IRI_PRIORITY;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.SOURCE;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.SOURCE_DIRECTORY;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.SOURCE_DIRECTORY_PATTERN;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.SOURCE_FORMAT;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.STRING_DATA_TYPING;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.TARGET;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.TARGET_DIRECTORY;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.TARGET_DIRECTORY_PATTERN;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.TARGET_FORMAT;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.TRAILING_COMMENT;
import static org.edmcouncil.rdf_toolkit.runner.CommandLineOption.USE_DTD_SUBSET;
import static org.edmcouncil.rdf_toolkit.runner.RunningMode.PRINT_USAGE_AND_EXIT;
import static org.edmcouncil.rdf_toolkit.runner.RunningMode.RUN_ON_DIRECTORY;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.BASE_IRI;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.INDENT;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.INFER_BASE_IRI;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.INLINE_BLANK_NODES;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.LEADING_COMMENT;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.LINE_END;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.OMIT_XMLNS_NAMESPACE;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.OVERRIDE_STRING_LANGUAGE;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.SHORT_IRI_PRIORITY;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.SOURCE;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.SOURCE_DIRECTORY;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.SOURCE_DIRECTORY_PATTERN;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.SOURCE_FORMAT;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.STRING_DATA_TYPING;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.SUPPRESS_NAMED_INDIVIDUALS;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.TARGET;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.TARGET_DIRECTORY;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.TARGET_DIRECTORY_PATTERN;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.TARGET_FORMAT;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.TRAILING_COMMENT;
import static org.edmcouncil.rdf_toolkit.runner.constant.CommandLineOption.USE_DTD_SUBSET;
import static org.edmcouncil.rdf_toolkit.runner.constant.RunningMode.PRINT_USAGE_AND_EXIT;
import static org.edmcouncil.rdf_toolkit.runner.constant.RunningMode.RUN_ON_DIRECTORY;
import org.apache.commons.cli.CommandLine;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.ValueFactory;
Expand All @@ -60,9 +62,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class OptionHandler {

Expand Down Expand Up @@ -159,29 +159,29 @@ public File handleSourceFile() throws RdfToolkitOptionHandlingException, FileNot

public void handleRunningOnDirectory(CommandLine commandLine, RdfToolkitOptions rdfToolkitOptions) {
// Check if the command-line options suggest that a directory of files is to be formatted
if (commandLine.hasOption(SOURCE_DIRECTORY.getShortOpt()) ||
commandLine.hasOption(SOURCE_DIRECTORY_PATTERN.getShortOpt()) ||
commandLine.hasOption(TARGET_DIRECTORY.getShortOpt()) ||
commandLine.hasOption(TARGET_DIRECTORY_PATTERN.getShortOpt())) {
if (commandLine.hasOption(SOURCE_DIRECTORY.getShortOpt())
|| commandLine.hasOption(SOURCE_DIRECTORY_PATTERN.getShortOpt())
|| commandLine.hasOption(TARGET_DIRECTORY.getShortOpt())
|| commandLine.hasOption(TARGET_DIRECTORY_PATTERN.getShortOpt())) {
// Assume user wants to process a directory of files.
if (!commandLine.hasOption(SOURCE_DIRECTORY.getShortOpt()) ||
!commandLine.hasOption(SOURCE_DIRECTORY_PATTERN.getShortOpt()) ||
!commandLine.hasOption(TARGET_DIRECTORY.getShortOpt()) ||
!commandLine.hasOption(TARGET_DIRECTORY_PATTERN.getShortOpt())) {
LOGGER.error("Directory processing options must all be used together: -sd (--source-directory), " +
"-sdp (--source-directory-pattern), -td (--target-directory), -tdp (--target-directory-pattern)");
if (!commandLine.hasOption(SOURCE_DIRECTORY.getShortOpt())
|| !commandLine.hasOption(SOURCE_DIRECTORY_PATTERN.getShortOpt())
|| !commandLine.hasOption(TARGET_DIRECTORY.getShortOpt())
|| !commandLine.hasOption(TARGET_DIRECTORY_PATTERN.getShortOpt())) {
LOGGER.error("Directory processing options must all be used together: -sd (--source-directory), "
+ "-sdp (--source-directory-pattern), -td (--target-directory), -tdp (--target-directory-pattern)");
rdfToolkitOptions.setRunningMode(PRINT_USAGE_AND_EXIT);
}
if (commandLine.hasOption(SOURCE.getShortOpt()) ||
commandLine.hasOption(TARGET.getShortOpt())) {
LOGGER.error("Source (-s or --source) and target (-t or --target) options cannot be used together with " +
"directory processing options.");
if (commandLine.hasOption(SOURCE.getShortOpt())
|| commandLine.hasOption(TARGET.getShortOpt())) {
LOGGER.error("Source (-s or --source) and target (-t or --target) options cannot be used together with "
+ "directory processing options.");
rdfToolkitOptions.setRunningMode(PRINT_USAGE_AND_EXIT);
}
if (!commandLine.hasOption(SOURCE_FORMAT.getShortOpt()) ||
!commandLine.hasOption(TARGET_FORMAT.getShortOpt())) {
LOGGER.error("Source format (-sfmt or --source-format) and target format (-tfmt or --target-format) options " +
"must be provided when using directory processing options.");
if (!commandLine.hasOption(SOURCE_FORMAT.getShortOpt())
|| !commandLine.hasOption(TARGET_FORMAT.getShortOpt())) {
LOGGER.error("Source format (-sfmt or --source-format) and target format (-tfmt or --target-format) options "
+ "must be provided when using directory processing options.");
rdfToolkitOptions.setRunningMode(PRINT_USAGE_AND_EXIT);
}

Expand Down Expand Up @@ -386,4 +386,9 @@ public void handleOmitXmlnsNamespace() {
boolean omitXmlnsNamespace = commandLine.hasOption(OMIT_XMLNS_NAMESPACE.getShortOpt());
rdfToolkitOptions.setOmitXmlnsNamespace(omitXmlnsNamespace);
}

public void handleSuppressNamedIndividuals() {
boolean suppressNamedIndividuals = commandLine.hasOption(SUPPRESS_NAMED_INDIVIDUALS.getShortOpt());
rdfToolkitOptions.setSuppressNamedIndividuals(suppressNamedIndividuals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.edmcouncil.rdf_toolkit.util.Constants.OVERRIDE_STRING_LANGUAGE;
import static org.edmcouncil.rdf_toolkit.util.Constants.SHORT_URI_PREF;
import static org.edmcouncil.rdf_toolkit.util.Constants.STRING_DATA_TYPE_OPTION;
import static org.edmcouncil.rdf_toolkit.util.Constants.SUPPRESS_NAMED_INDIVIDUALS;
import static org.edmcouncil.rdf_toolkit.util.Constants.TRAILING_COMMENTS;
import static org.edmcouncil.rdf_toolkit.util.Constants.USE_DTD_SUBSET;

Expand All @@ -44,6 +45,7 @@
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.edmcouncil.rdf_toolkit.io.format.TargetFormats;
import org.edmcouncil.rdf_toolkit.runner.constant.RunningMode;
import org.edmcouncil.rdf_toolkit.util.ShortIriPreferences;
import org.edmcouncil.rdf_toolkit.util.StringDataTypeOptions;

Expand Down Expand Up @@ -74,6 +76,7 @@ public class RdfToolkitOptions {
private String overrideStringLanguage;
private String lineEnd;
private boolean omitXmlnsNamespace;
private boolean suppressNamedIndividuals;

public RdfToolkitOptions(String[] args) {
this.args = args;
Expand Down Expand Up @@ -104,6 +107,7 @@ public Map<String, Object> getOptions() {
options.put(OVERRIDE_STRING_LANGUAGE, getOverrideStringLanguage());
options.put(LINE_END, getLineEnd());
options.put(OMIT_XMLNS_NAMESPACE, getOmitXmlnsNamespace());
options.put(SUPPRESS_NAMED_INDIVIDUALS, getSuppressNamedIndividuals());
return options;
}

Expand Down Expand Up @@ -290,4 +294,12 @@ public boolean getOmitXmlnsNamespace() {
public void setOmitXmlnsNamespace(boolean omitXmlnsNamespace) {
this.omitXmlnsNamespace = omitXmlnsNamespace;
}

public boolean getSuppressNamedIndividuals() {
return suppressNamedIndividuals;
}

public void setSuppressNamedIndividuals(boolean suppressNamedIndividuals) {
this.suppressNamedIndividuals = suppressNamedIndividuals;
}
}
Loading

0 comments on commit 8450ce7

Please sign in to comment.