Skip to content

Commit

Permalink
Implement #342
Browse files Browse the repository at this point in the history
  • Loading branch information
enridaga committed Feb 6, 2023
1 parent d59771a commit a083c63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class CLI {
public static final String OUTPUT = "o";
public static final String OUTPUT_LONG = "output";

public static final String OUTPUT_APPEND = "a";
public static final String OUTPUT_APPEND_LONG = "append";

public static final String FORMAT = "f";
public static final String FORMAT_LONG = "format";

Expand Down Expand Up @@ -118,6 +121,9 @@ void init(){
options.addOption(Option.builder(OUTPUT).argName("file").hasArg()
.desc("OPTIONAL - The path to the output file. [Default: STDOUT]").longOpt(OUTPUT_LONG).build());

options.addOption(Option.builder(OUTPUT_APPEND).hasArg(false)
.desc("OPTIONAL - Should output to file be appended? WARNING: this option does not ensure that the whole file is valid -- that is up to the user to set up the conditions (such as using NQ serialization and not using bnodes)").longOpt(OUTPUT_APPEND_LONG).build());

options.addOption(Option.builder(EXPLAIN).argName("explain").hasArg(false)
.desc("OPTIONAL - Explain query execution").longOpt(EXPLAIN_LONG).build());

Expand Down Expand Up @@ -172,6 +178,9 @@ public String getOutputFile() {
return commandLine.getOptionValue(CLI.OUTPUT);
}

public boolean getOutputAppend() {
return commandLine.hasOption(CLI.OUTPUT_APPEND);
}
public String getOutputPattern() {
return commandLine.getOptionValue(CLI.OUTPUTPATTERN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -166,10 +167,10 @@ private static void executeQuery(String outputFormat, Dataset kb, Query query, P
}
}

private static PrintStream getPrintWriter(String fileName) throws FileNotFoundException {
private static PrintStream getPrintWriter(String fileName, boolean append) throws FileNotFoundException {

if (fileName != null) {
return new PrintStream(new File(fileName));
return new PrintStream(new FileOutputStream(fileName, append));
}

return System.out;
Expand Down Expand Up @@ -531,7 +532,7 @@ public static void main(String[] args) throws Exception {
if (inputFile == null && values == null) {
logger.debug("No input file");
Query q = QueryFactory.create(query);
executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFileName));
executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFileName, cli.getOutputAppend()));
} else {

if (inputFile != null && values != null) {
Expand Down Expand Up @@ -576,7 +577,7 @@ public static void main(String[] args) throws Exception {
}
try {
logger.trace("Executing Query: {}", q);
executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFile));
executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFile, cli.getOutputAppend()));
} catch (Exception e1) {
logger.error(
"Iteration " + parameters.getRowNumber() + " failed with error: " + e1.getMessage());
Expand Down

0 comments on commit a083c63

Please sign in to comment.