Skip to content

Commit

Permalink
Add infer command
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed Dec 10, 2021
1 parent 2c79e8c commit cdd2b01
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
"${projectDir}/cli/build/classes/java/main",
"${projectDir}/diagram/build/classes/java/main",
"${projectDir}/write/build/classes/java/main",
"${projectDir}/infer/build/classes/java/main",
]
sourceDirectories.from = [
"${projectDir}/cli/src/main/java",
"${projectDir}/diagram/src/main/java",
"${projectDir}/write/src/main/java",
"${projectDir}/infer/src/main/java",
]

executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
Expand All @@ -79,6 +81,7 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
dependsOn ':cli:test'
dependsOn ':diagram:test'
dependsOn ':write:test'
dependsOn ':infer:test'
}

defaultTasks 'test', 'shadowJar'
Expand Down
1 change: 1 addition & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ apply from: file("${rootDir}/dependencies.gradle")
dependencies {
implementation project(':diagram')
implementation project(':write')
implementation project(':infer')
implementation(deps.owlapi) {
exclude group: 'org.slf4j', module: 'slf4j-simple'
}
Expand Down
6 changes: 5 additions & 1 deletion cli/src/main/java/de/atextor/owlcli/OWLCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ private static void printError( final CommandLine commandLine, final Exception e

public static void main( final String[] args ) {
LogManager.getLogManager().reset();
final List<AbstractCommand> commands = List.of( new OWLCLIDiagramCommand(), new OWLCLIWriteCommand() );
final List<AbstractCommand> commands = List.of(
new OWLCLIDiagramCommand(),
new OWLCLIWriteCommand(),
new OWLCLIInferCommand()
);
final CommandLine cmd = commands.foldLeft( new OWLCLI().commandLine, CommandLine::addSubcommand )
.setParameterExceptionHandler( PARAMETER_EXCEPTION_HANDLER )
.setExecutionExceptionHandler( EXECUTION_EXCEPTION_HANDLER )
Expand Down
91 changes: 91 additions & 0 deletions cli/src/main/java/de/atextor/owlcli/OWLCLIInferCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2021 Andreas Textor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.atextor.owlcli;

import de.atextor.owlcli.infer.Configuration;
import de.atextor.owlcli.infer.Inferrer;
import de.atextor.turtle.formatter.FormattingStyle;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@CommandLine.Command( name = "infer",
description = "Runs an OWL reasoner on an ontology",
descriptionHeading = "%n@|bold Description|@:%n%n",
parameterListHeading = "%n@|bold Parameters|@:%n",
optionListHeading = "%n@|bold Options|@:%n",
footer = "%nSee the online documentation for details:%n" +
"https://atextor.de/owl-cli/main/" + OWLCLIConfig.VERSION + "/usage.html#infer-command"
)
public class OWLCLIInferCommand extends AbstractCommand implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger( OWLCLIInferCommand.class );

private static final Configuration config = Inferrer.DEFAULT_CONFIGURATION;

@CommandLine.Mixin
LoggingMixin loggingMixin;

@CommandLine.Parameters( paramLabel = "INPUT", description = "File name, URL, or - for stdin", arity = "1",
index = "0" )
private String input;

@CommandLine.Parameters( paramLabel = "OUTPUT",
description = "File name or - for stdout. If left out, output is written to stdout.",
arity = "0..1", index = "1" )
private String output;

@Override
public void run() {
final Configuration.ConfigurationBuilder configurationBuilder = Configuration.builder();

final Inferrer inferrer = new Inferrer();

if ( input.toLowerCase().startsWith( "http:" ) || input.toLowerCase().startsWith( "https:" ) ) {
final Configuration configuration = configurationBuilder.build();
try {
final URL inputUrl = new URL( input );
openOutput( input, output != null ? output : "-", "ttl" )
.map( outputStream -> inferrer.infer( inputUrl, outputStream, configuration ) )
.onFailure( throwable -> exitWithErrorMessage( LOG, loggingMixin, throwable ) );
return;
} catch ( final MalformedURLException exception ) {
exitWithErrorMessage( LOG, loggingMixin, exception );
}
}

openInput( input ).flatMap( inputStream -> {
final Configuration configuration = configurationBuilder.build();
return openOutput( input, output != null ? output : "-", "ttl" )
.flatMap( outputStream -> inferrer.infer( inputStream, outputStream, configuration ) );
}
).onFailure( throwable -> exitWithErrorMessage( LOG, loggingMixin, throwable ) );
}
}
63 changes: 63 additions & 0 deletions infer/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 Andreas Textor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id 'java'
id 'jacoco'
id 'io.freefair.lombok'
id 'com.adarshr.test-logger'
}

repositories {
maven { url 'https://jitpack.io' }
}

apply from: file("${rootDir}/dependencies.gradle")
dependencies {
implementation(deps.jena)
implementation(deps.jena_core)
implementation(deps.slf4j_api)
implementation(deps.turtle_formatter)
implementation(deps.vavr)

// Test
testImplementation(deps.junit_jupiter_api)
testImplementation(deps.assertj)
testImplementation(deps.jqwik)
testRuntimeOnly(deps.junit_jupiter_engine)
}

compileJava {
sourceCompatibility = 17
targetCompatibility = 17
}

compileTestJava {
sourceCompatibility = 17
targetCompatibility = 17
}

test {
useJUnitPlatform()
maxHeapSize = '1G'
ignoreFailures = false
failFast = true

filter {
includeTestsMatching "*Test"
}
}

25 changes: 25 additions & 0 deletions infer/src/main/java/de/atextor/owlcli/infer/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 Andreas Textor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.atextor.owlcli.infer;

import lombok.Builder;

@Builder
public class Configuration {
@Builder.Default
public boolean test = true;
}
61 changes: 61 additions & 0 deletions infer/src/main/java/de/atextor/owlcli/infer/Inferrer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2021 Andreas Textor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.atextor.owlcli.infer;

import io.vavr.control.Try;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Inferrer {
public static final Configuration DEFAULT_CONFIGURATION = Configuration.builder().build();

private static final Logger LOG = LoggerFactory.getLogger( Inferrer.class );

public Try<Void> infer( final URL inputUrl, final OutputStream output, final Configuration configuration ) {
try {
final HttpClient client = HttpClient.newBuilder()
.followRedirects( HttpClient.Redirect.ALWAYS )
.build();
final HttpRequest request = HttpRequest.newBuilder()
.uri( inputUrl.toURI() )
.build();
final HttpResponse<String> response = client.send( request, HttpResponse.BodyHandlers.ofString() );
if ( response.statusCode() == HttpURLConnection.HTTP_OK ) {
final ByteArrayInputStream inputStream = new ByteArrayInputStream( response.body().getBytes() );
return infer( inputStream, output, configuration );
}
return Try.failure( new RuntimeException( "Got unexpected HTTP response: " + response.statusCode() ) );
} catch ( final Exception exception ) {
LOG.debug( "Failure during reading from URL: {}", inputUrl );
return Try.failure( exception );
}
}

public Try<Void> infer( final InputStream input, final OutputStream output, final Configuration configuration ) {
return Try.success( null );
}
}
28 changes: 28 additions & 0 deletions infer/src/test/java/de/atextor/owlcli/infer/InferrerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 Andreas Textor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.atextor.owlcli.infer;

import org.junit.jupiter.api.Test;

public class InferrerTest {
private final Inferrer writer = new Inferrer();

@Test
public void testFoo() {

}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
rootProject.name = 'owl-cli'
include ':diagram'
include ':write'
include ':infer'
include ':cli'
include ':docs'

0 comments on commit cdd2b01

Please sign in to comment.