Added a new SPARQL query and using a external SPARQL Endpoint

This commit is contained in:
Marcel 2017-11-21 18:36:14 +01:00
parent 27ca4898e7
commit 08a8c05b2d
1 changed files with 45 additions and 14 deletions

View File

@ -9,7 +9,6 @@ import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution; import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory; import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory; import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.ModelFactory;
@ -17,6 +16,7 @@ import hsh.ins_jena.model.Generator;
public class App { public class App {
public static String OUTPUT_PATH = "./output"; public static String OUTPUT_PATH = "./output";
public static String SPARQL_ENDPOINT = "http://localhost:3030/foobar/query";
public static void main(String[] args) { public static void main(String[] args) {
generateFiles(OUTPUT_PATH); generateFiles(OUTPUT_PATH);
@ -36,32 +36,63 @@ public class App {
e.printStackTrace(); e.printStackTrace();
} }
System.err.println("Loaded model:");
System.out.println(model); System.out.println(model);
Query query = QueryFactory.create("" + "PREFIX : <" + Generator.OWN_URI + ">" + "" + "SELECT ?console\n" Query queryReleaseYear = QueryFactory.create(
"PREFIX : <" + Generator.OWN_URI + ">"
+ "SELECT ?console\n"
+ "WHERE {\n" + " ?console :madeBy :Nintendo .\n" + " ?console :releaseYear ?releaseYear . \n" + "WHERE {\n" + " ?console :madeBy :Nintendo .\n" + " ?console :releaseYear ?releaseYear . \n"
+ " FILTER(?releaseYear > 2015)\n" + "}"); + " FILTER(?releaseYear > 2015)\n" + "}");
Query queryCEOForConsole = QueryFactory.create(
"PREFIX : <" + Generator.OWN_URI + ">" +
"PREFIX rdf: <" + Generator.RDF_URI + ">" +
"PREFIX rdfs: <" + Generator.RDFS_URI + ">" +
"PREFIX foaf: <" + Generator.FOAF_URI + ">" +
"CONSTRUCT {\n" +
" ?ceo :isRelatedToConsole ?console .\n" +
"}\n" +
"WHERE {\n" +
" ?console rdf:type :GameConsole .\n" +
" ?console :madeBy ?org .\n" +
" ?org :ceo ?ceo .\n" +
" ?ceo rdf:type foaf:Person .\n" +
"}\n" +
"");
// Query query = QueryFactory // Query query = QueryFactory
// .create("PREFIX : <" + Generator.OWN_URI + ">" + " " + " SELECT ?s ?p ?p WHERE {" + "?s ?p ?o .}"); // .create("PREFIX : <" + Generator.OWN_URI + ">" + " " + " SELECT ?s ?p ?p WHERE {" + "?s ?p ?o .}");
QueryExecution queryExec = QueryExecutionFactory.create(query, model); QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryReleaseYear, model);
QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,queryReleaseYear);
ResultSet rs = queryExec.execSelect(); QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, model);
QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,queryCEOForConsole);
if (rs.hasNext()) { System.err.println("Doing local release date query");
rs.forEachRemaining(qs -> { printQueryResult(queryExecLocalReleaseDate);
System.out.println(qs); System.err.println("Doing remote release date query");
}); printQueryResult(queryExecRemoteReleaseDate);
System.err.println("Doing local CEO query");
printQueryConstructs(queryExecLocalCEOtoConsole);
System.err.println("Doing remote CEO query");
printQueryConstructs(queryExecRemoteCEOtoConsole);
} }
private static void printQueryResult(QueryExecution queryExec) {
queryExec.execSelect().forEachRemaining(qs -> System.out.println(qs));
}
private static void printQueryConstructs(QueryExecution queryExec) {
queryExec.execConstruct().listStatements().toList().stream().forEach(s -> System.out.println(s));
} }
private static void generateFiles(String outputhPath) { private static void generateFiles(String outputhPath) {
new File(OUTPUT_PATH).mkdirs(); new File(OUTPUT_PATH).mkdirs();
System.out.print("Generating ... "); System.err.println("Generating model and saving it...");
Generator.createTBoxAndABox(OUTPUT_PATH); Generator.createTBoxAndABox(OUTPUT_PATH);
System.out.println("done."); System.out.println("done.");
} }
} }