From 08a8c05b2d330b6d786ebb7fc288d0c068a1abce Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 21 Nov 2017 18:36:14 +0100 Subject: [PATCH] Added a new SPARQL query and using a external SPARQL Endpoint --- src/main/java/hsh/ins_jena/App.java | 59 ++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/java/hsh/ins_jena/App.java b/src/main/java/hsh/ins_jena/App.java index 0c545ad..d95db9d 100644 --- a/src/main/java/hsh/ins_jena/App.java +++ b/src/main/java/hsh/ins_jena/App.java @@ -9,7 +9,6 @@ import org.apache.jena.query.Query; import org.apache.jena.query.QueryExecution; import org.apache.jena.query.QueryExecutionFactory; 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.ModelFactory; @@ -17,6 +16,7 @@ import hsh.ins_jena.model.Generator; public class App { public static String OUTPUT_PATH = "./output"; + public static String SPARQL_ENDPOINT = "http://localhost:3030/foobar/query"; public static void main(String[] args) { generateFiles(OUTPUT_PATH); @@ -36,32 +36,63 @@ public class App { e.printStackTrace(); } + System.err.println("Loaded 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" + " 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 // .create("PREFIX : <" + Generator.OWN_URI + ">" + " " + " SELECT ?s ?p ?p WHERE {" + "?s ?p ?o .}"); - QueryExecution queryExec = QueryExecutionFactory.create(query, model); - - ResultSet rs = queryExec.execSelect(); - - if (rs.hasNext()) { - rs.forEachRemaining(qs -> { - System.out.println(qs); - }); - } - + QueryExecution queryExecLocalReleaseDate = QueryExecutionFactory.create(queryReleaseYear, model); + QueryExecution queryExecRemoteReleaseDate = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,queryReleaseYear); + + QueryExecution queryExecLocalCEOtoConsole = QueryExecutionFactory.create(queryCEOForConsole, model); + QueryExecution queryExecRemoteCEOtoConsole = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT,queryCEOForConsole); + + System.err.println("Doing local release date query"); + printQueryResult(queryExecLocalReleaseDate); + 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) { new File(OUTPUT_PATH).mkdirs(); - System.out.print("Generating ... "); + System.err.println("Generating model and saving it..."); Generator.createTBoxAndABox(OUTPUT_PATH); System.out.println("done."); - } }