Skip to content

Commit 96c8552

Browse files
committed
added query tool in Querier class
1 parent 8e0a01f commit 96c8552

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

sparql_query_example.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
PREFIX cwaf: <http://visualdataweb.org/ontCWAF/>
2+
PREFIX dc: <http://purl.org/dc/elements/1.1/>
3+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
4+
PREFIX owl: <http://www.w3.org/2002/07/owl#>
5+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
6+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
7+
PREFIX vs: <http://www.w3.org/2003/06/sw-vocab-status/ns#>
8+
PREFIX wot: <http://xmlns.com/wot/0.1/>
9+
PREFIX xml: <http://www.w3.org/XML/1998/namespace>
10+
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
11+
SELECT ?s
12+
WHERE {
13+
?s rdf:type cwaf:Macro .
14+
}

0 commit comments

Comments
 (0)