-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
cli/src/main/java/de/atextor/owlcli/OWLCLIInferCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
infer/src/main/java/de/atextor/owlcli/infer/Configuration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
infer/src/test/java/de/atextor/owlcli/infer/InferrerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters