Skip to content

Commit e69ef9d

Browse files
Merge pull request #315 from ontodev/more-consistent-query
More consistent query
2 parents aaf531f + 0c987ff commit e69ef9d

File tree

3 files changed

+80
-42
lines changed

3 files changed

+80
-42
lines changed

docs/verify.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ Should output as a response:
2222
And the CSV file `results/equivalent.csv` should have:
2323

2424
first,second,firstLabel,secondLabel
25-
http://purl.obolibrary.org/obo/TEST_A,http://purl.obolibrary.org/obo/TEST_B,,
25+
http://purl.obolibrary.org/obo/TEST_A,http://purl.obolibrary.org/obo/TEST_B,,
26+
27+
---
28+
29+
## Error Messages
30+
31+
### Verification Failed
32+
33+
At least one of the query you specifies returned results. The number of failures for each rule will be printed. A CSV file will be generated with the results that matched the rule.
34+
35+
### Missing Query Error
36+
37+
You must specify a query to execute with `--query` or `--queries`.

robot-command/src/main/java/org/obolibrary/robot/VerifyCommand.java

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
package org.obolibrary.robot;
22

3-
import com.google.common.io.Files;
4-
import com.hp.hpl.jena.query.ResultSet;
5-
import com.hp.hpl.jena.query.ResultSetFactory;
6-
import com.hp.hpl.jena.query.ResultSetRewindable;
73
import com.hp.hpl.jena.sparql.core.DatasetGraph;
84
import java.io.File;
9-
import java.io.FileOutputStream;
10-
import java.io.IOException;
11-
import java.io.OutputStream;
12-
import java.nio.charset.Charset;
13-
import java.util.HashMap;
14-
import java.util.Map;
155
import org.apache.commons.cli.CommandLine;
166
import org.apache.commons.cli.Option;
177
import org.apache.commons.cli.Options;
8+
import org.apache.commons.io.FileUtils;
189
import org.apache.commons.io.FilenameUtils;
19-
import org.obolibrary.robot.exceptions.CannotReadQuery;
2010
import org.slf4j.Logger;
2111
import org.slf4j.LoggerFactory;
2212

@@ -36,6 +26,10 @@ public class VerifyCommand implements Command {
3626
private static final String missingQueryError =
3727
NS + "MISSING QUERY ERROR at least one query is required";
3828

29+
/** Error message when no query is provided. */
30+
private static final String verificationFailed =
31+
NS + "VERIFICATION FAILED there were violations of at least one rule";
32+
3933
/** Store the command-line options for the command. */
4034
private Options options;
4135

@@ -122,44 +116,24 @@ public CommandState execute(CommandState state, String[] args) throws Exception
122116

123117
File outputDir = new File(CommandLineHelper.getDefaultValue(line, "output-dir", "."));
124118

125-
Map<File, Tuple<ResultSetRewindable, OutputStream>> resultMap = new HashMap<>();
126119
String[] queryFilePaths = line.getOptionValues("queries");
127-
if (queryFilePaths.length == 0) {
128-
throw new IllegalArgumentException(missingQueryError);
129-
}
120+
boolean passing = true;
130121
for (String filePath : queryFilePaths) {
131-
File query = new File(filePath);
132-
ResultSet results = QueryOperation.execQuery(graph, fileContents(query));
133-
ResultSetRewindable resultsCopy = ResultSetFactory.copyResults(results);
122+
File queryFile = new File(filePath);
123+
String queryString = FileUtils.readFileToString(queryFile);
134124
String csvPath = FilenameUtils.getBaseName(filePath).concat(".csv");
135-
File resultCsv = outputDir.toPath().resolve(csvPath).toFile();
136-
if (resultsCopy.size() > 0) {
137-
resultMap.put(query, new Tuple<>(resultsCopy, new FileOutputStream(resultCsv)));
138-
} else {
139-
System.out.println("Rule " + resultCsv.getCanonicalPath() + ": 0 violations");
125+
boolean result =
126+
QueryOperation.runVerify(
127+
graph, filePath, queryString, outputDir.toPath().resolve(csvPath), null);
128+
if (result) {
129+
passing = false;
140130
}
141131
}
142132

143-
boolean violationsExist = QueryOperation.execVerify(resultMap);
144-
if (violationsExist) {
145-
System.exit(1);
133+
if (!passing) {
134+
throw new Exception(verificationFailed);
146135
}
147136

148137
return state;
149138
}
150-
151-
/**
152-
* Utility function to get file contents.
153-
*
154-
* @param file the file to read
155-
*/
156-
private static String fileContents(File file) {
157-
try {
158-
return Files.toString(file, Charset.defaultCharset());
159-
} catch (IOException e) {
160-
String message = "Cannot read from " + file + ": " + e.getMessage();
161-
// TODO: Is this necessary?
162-
throw new CannotReadQuery(message, e);
163-
}
164-
}
165139
}

robot-core/src/main/java/org/obolibrary/robot/QueryOperation.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.hp.hpl.jena.sparql.core.DatasetGraph;
66
import com.hp.hpl.jena.sparql.core.DatasetGraphFactory;
77
import java.io.*;
8+
import java.nio.file.Path;
89
import java.util.Map;
910
import org.apache.jena.riot.Lang;
1011
import org.apache.jena.riot.RDFDataMgr;
@@ -427,6 +428,57 @@ public static boolean execVerify(
427428
return isViolation;
428429
}
429430

431+
/**
432+
* Execute a SPARQL query and return true if there are any results, false otherwise. Prints
433+
* violations to STDERR.
434+
*
435+
* @param dsg the graph to query over
436+
* @param query the SPARQL query string
437+
* @return true if the are results, false otherwise
438+
*/
439+
public static boolean execVerify(DatasetGraph dsg, String ruleName, String query) {
440+
ResultSetRewindable results = ResultSetFactory.copyResults(execQuery(dsg, query));
441+
System.out.println("Rule " + ruleName + ": " + results.size() + " violation(s)");
442+
if (results.size() == 0) {
443+
System.out.println("PASS Rule " + ruleName + ": 0 violation(s)");
444+
return false;
445+
} else {
446+
ResultSetMgr.write(System.err, results, Lang.CSV);
447+
System.out.println("FAIL Rule " + ruleName + ": " + results.size() + " violation(s)");
448+
return true;
449+
}
450+
}
451+
452+
/**
453+
* Run a SELECT query and write the result to a file. Prints violations to STDERR.
454+
*
455+
* @param dsg The graph to query over.
456+
* @param query The SPARQL query string.
457+
* @param outputPath The file path to write to, if there are results
458+
* @param outputFormat The file format.
459+
* @throws FileNotFoundException if output file is not found
460+
* @return true if the are results (so file is written), false otherwise
461+
*/
462+
public static boolean runVerify(
463+
DatasetGraph dsg, String ruleName, String query, Path outputPath, Lang outputFormat)
464+
throws FileNotFoundException {
465+
if (outputFormat == null) {
466+
outputFormat = Lang.CSV;
467+
}
468+
ResultSetRewindable results = ResultSetFactory.copyResults(execQuery(dsg, query));
469+
if (results.size() == 0) {
470+
System.out.println("PASS Rule " + ruleName + ": 0 violation(s)");
471+
return false;
472+
} else {
473+
System.out.println("FAIL Rule " + ruleName + ": " + results.size() + " violation(s)");
474+
ResultSetMgr.write(System.err, results, Lang.CSV);
475+
results.reset();
476+
FileOutputStream csvFile = new FileOutputStream(outputPath.toFile());
477+
writeResult(results, outputFormat, csvFile);
478+
return true;
479+
}
480+
}
481+
430482
/**
431483
* Count results.
432484
*

0 commit comments

Comments
 (0)