|
| 1 | +package be.uclouvain.service; |
| 2 | + |
| 3 | +import org.apache.commons.cli.*; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.util.Scanner; |
| 7 | + |
| 8 | +import org.apache.jena.ontology.OntModel; |
| 9 | +import org.apache.jena.query.Query; |
| 10 | +import org.apache.jena.query.QueryExecution; |
| 11 | +import org.apache.jena.query.QueryExecutionFactory; |
| 12 | +import org.apache.jena.query.QueryFactory; |
| 13 | +import org.apache.jena.query.ResultSet; |
| 14 | +import org.apache.jena.query.ResultSetFormatter; |
| 15 | +import org.apache.jena.rdf.model.ModelFactory; |
| 16 | + |
| 17 | +public class Querier { |
| 18 | + |
| 19 | + public static String loadQueryFromFile(String queryPath) { |
| 20 | + //Load the query |
| 21 | + StringBuilder queryString = new StringBuilder(); |
| 22 | + try { |
| 23 | + File myfile = new File(queryPath); |
| 24 | + Scanner myReader = new Scanner(myfile); |
| 25 | + while (myReader.hasNextLine()) { |
| 26 | + String data = myReader.nextLine(); |
| 27 | + queryString.append(data).append("\n"); |
| 28 | + } |
| 29 | + myReader.close(); |
| 30 | + } catch (FileNotFoundException e) { |
| 31 | + System.err.println("Error reading query file: " + e.getMessage()); |
| 32 | + System.exit(1); |
| 33 | + } |
| 34 | + return queryString.toString(); |
| 35 | + } |
| 36 | + |
| 37 | + public static void runFromString(String ontologyPath, String queryString) { |
| 38 | + //Load the ontology |
| 39 | + OntModel model = ModelFactory.createOntologyModel(); |
| 40 | + model.read(ontologyPath); |
| 41 | + |
| 42 | + //Query |
| 43 | + Query query = QueryFactory.create(queryString); |
| 44 | + QueryExecution qexec = QueryExecutionFactory.create(query, model); |
| 45 | + try { |
| 46 | + ResultSet results = qexec.execSelect(); |
| 47 | + ResultSetFormatter.out(System.out, results); |
| 48 | + } finally { |
| 49 | + qexec.close(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static void runFromFile(String ontologyPath, String queryPath) { |
| 54 | + //Query |
| 55 | + String queryString = loadQueryFromFile(queryPath); |
| 56 | + runFromString(ontologyPath, queryString); |
| 57 | + } |
| 58 | + |
| 59 | + public static void printHelp(Options options) { |
| 60 | + HelpFormatter formatter = new HelpFormatter(); |
| 61 | + formatter.printHelp("Querier <ontology path> <query path>", options); |
| 62 | + } |
| 63 | + |
| 64 | + //Do a SPARQL query on the ontology |
| 65 | + public static void main(String[] args) { |
| 66 | + |
| 67 | + Options options = new Options(); |
| 68 | + options.addOption("h", "help", false, "print this message"); |
| 69 | + options.addOption("q", "query", true, "Use a string as query instead of a file"); |
| 70 | + |
| 71 | + CommandLineParser parser = new DefaultParser(); |
| 72 | + try { |
| 73 | + CommandLine cmd = parser.parse(options, args); |
| 74 | + |
| 75 | + if (cmd.hasOption("h")) { |
| 76 | + printHelp(options); |
| 77 | + } else if (cmd.hasOption("q")){ |
| 78 | + String ontologyPath = cmd.getArgs()[0]; |
| 79 | + String queryString = cmd.getOptionValue("q"); |
| 80 | + runFromString(ontologyPath, queryString); |
| 81 | + } else { |
| 82 | + // Handle the positional argument |
| 83 | + String[] remainingArgs = cmd.getArgs(); |
| 84 | + if (remainingArgs.length != 2) { |
| 85 | + System.out.println("Invalid number of arguments"); |
| 86 | + printHelp(options); |
| 87 | + System.exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + String ontologyPath = remainingArgs[0]; |
| 91 | + String queryPath = remainingArgs[1]; |
| 92 | + |
| 93 | + runFromFile(ontologyPath, queryPath); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + } catch (ParseException e) { |
| 98 | + System.err.println("Parsing failed. Reason: " + e.getMessage()); |
| 99 | + printHelp(options); |
| 100 | + System.exit(1); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + } |
| 105 | +} |
0 commit comments