Skip to content

Commit

Permalink
Make anonNodeIdGenerator and NumberPattern configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed May 17, 2021
1 parent b4a8dca commit de4f4ea
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions cli/src/main/java/de/atextor/owlcli/OWLCLIWriteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import de.atextor.owlcli.write.RdfWriter;
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.apache.jena.sys.JenaSystem;
Expand All @@ -35,6 +36,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@CommandLine.Command( name = "write",
Expand All @@ -46,15 +48,15 @@
"https://atextor.de/owl-cli/main/" + OWLCLIConfig.VERSION + "/usage.html#write-command"
)
public class OWLCLIWriteCommand extends AbstractCommand implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger( OWLCLIWriteCommand.class );

private static final Configuration config = RdfWriter.DEFAULT_CONFIGURATION;

static {
JenaSystem.setSubsystemRegistry( new StaticJenaSubsystemRegistry() );
JenaSystem.init();
}

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

private static final Configuration config = RdfWriter.DEFAULT_CONFIGURATION;

@CommandLine.Mixin
LoggingMixin loggingMixin;

Expand Down Expand Up @@ -153,6 +155,15 @@ public class OWLCLIWriteCommand extends AbstractCommand implements Runnable {
description = "Sort order for predicates (Default: ${DEFAULT-VALUE})" )
private List<Property> predicateOrder = FormattingStyle.DEFAULT.predicateOrder;

@CommandLine.Option( names = { "--objectOrder" },
description = "Sort order for objects (Default: ${DEFAULT-VALUE})" )
private List<RDFNode> objectOrder = FormattingStyle.DEFAULT.objectOrder;

@CommandLine.Option( names = { "--anonymousNodeIdPattern" },
description = "Name pattern for blank node IDs (Default: ${DEFAULT-VALUE})" )
private String anonymousNodeIdPattern =
FormattingStyle.DEFAULT.anonymousNodeIdGenerator.apply( ResourceFactory.createResource(), 0 );

@CommandLine.Parameters( paramLabel = "INPUT", description = "File name, URL, or - for stdin", arity = "1",
index = "0" )
private String input;
Expand Down Expand Up @@ -185,6 +196,8 @@ public void run() {
.prefixOrder( prefixOrder )
.subjectOrder( subjectOrder )
.predicateOrder( predicateOrder )
.objectOrder( objectOrder )
.anonymousNodeIdGenerator( buildAnonymousNodeIdGenerator( anonymousNodeIdPattern ) )
.build();

final Configuration.ConfigurationBuilder configurationBuilder = Configuration.builder()
Expand Down Expand Up @@ -220,6 +233,11 @@ public void registerTypeConverters( final CommandLine commandLine ) {
commandLine.registerConverter( NumberFormat.class, new NumberFormatConverter() );
commandLine.registerConverter( Property.class, new PropertyConverter() );
commandLine.registerConverter( Resource.class, new ResourceConverter() );
commandLine.registerConverter( RDFNode.class, new RDFNodeConverter() );
}

private BiFunction<Resource, Integer, String> buildAnonymousNodeIdGenerator( final String pattern ) {
return ( resource, integer ) -> pattern.replace( "0", "" + integer );
}

private static class NumberFormatConverter implements CommandLine.ITypeConverter<NumberFormat> {
Expand Down Expand Up @@ -267,4 +285,13 @@ public Resource convert( final String value ) throws Exception {
return ResourceFactory.createResource( propertyUri );
}
}

private class RDFNodeConverter extends AbstractResourceConverter implements CommandLine.ITypeConverter<RDFNode> {
@Override
public RDFNode convert( final String value ) throws Exception {
final String propertyUri = buildResourceUri( value );
return ResourceFactory.createResource( propertyUri );
}
}

}

0 comments on commit de4f4ea

Please sign in to comment.