1
1
package org .obolibrary .robot ;
2
2
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 ;
7
3
import com .hp .hpl .jena .sparql .core .DatasetGraph ;
8
4
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 ;
15
5
import org .apache .commons .cli .CommandLine ;
16
6
import org .apache .commons .cli .Option ;
17
7
import org .apache .commons .cli .Options ;
8
+ import org .apache .commons .io .FileUtils ;
18
9
import org .apache .commons .io .FilenameUtils ;
19
- import org .obolibrary .robot .exceptions .CannotReadQuery ;
20
10
import org .slf4j .Logger ;
21
11
import org .slf4j .LoggerFactory ;
22
12
@@ -36,6 +26,10 @@ public class VerifyCommand implements Command {
36
26
private static final String missingQueryError =
37
27
NS + "MISSING QUERY ERROR at least one query is required" ;
38
28
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
+
39
33
/** Store the command-line options for the command. */
40
34
private Options options ;
41
35
@@ -122,44 +116,24 @@ public CommandState execute(CommandState state, String[] args) throws Exception
122
116
123
117
File outputDir = new File (CommandLineHelper .getDefaultValue (line , "output-dir" , "." ));
124
118
125
- Map <File , Tuple <ResultSetRewindable , OutputStream >> resultMap = new HashMap <>();
126
119
String [] queryFilePaths = line .getOptionValues ("queries" );
127
- if (queryFilePaths .length == 0 ) {
128
- throw new IllegalArgumentException (missingQueryError );
129
- }
120
+ boolean passing = true ;
130
121
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 );
134
124
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 ;
140
130
}
141
131
}
142
132
143
- boolean violationsExist = QueryOperation .execVerify (resultMap );
144
- if (violationsExist ) {
145
- System .exit (1 );
133
+ if (!passing ) {
134
+ throw new Exception (verificationFailed );
146
135
}
147
136
148
137
return state ;
149
138
}
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
- }
165
139
}
0 commit comments