/** * Read a SPARQL query from a file. * * @param url * URL (file: or http: or anything a FileManager can handle) * @return A new query object */ static public Query read(String url) { return read(url, null, null, null) ; }
/** Read a SPARQL query from a file. * * @param url URL (file: or http: or anything a FileManager can handle) * @param baseURI BaseURI for the query * @return A new query object */ static public Query read(String url, String baseURI) { return read(url, null, baseURI, null) ; }
/** Read a query from a file. * * @param url URL (file: or http: or anything a FileManager can handle) * @param baseURI BaseURI for the query * @param langURI Query syntax * @return A new query object */ static public Query read(String url, String baseURI, Syntax langURI) { return read(url, null, baseURI, langURI) ; }
/** Read a query from a file. * * @param url URL (file: or http: or anything a FileManager can handle) * @param langURI Query syntax * @return A new query object */ static public Query read(String url, Syntax langURI) { return read(url, null, null, langURI) ; }
@Override public void setQueryURI(final String queryURI) { if (_queryURI.equals(queryURI)) return; _queryURI = queryURI; _query = QueryFactory.read(queryURI); }
/** * {@inheritDoc} */ @Override public boolean isParsable() { try { _query = QueryFactory.read(_queryURI); return true; } catch (final Exception e) { _logger.log(Level.INFO, e.getMessage(), e); return false; } }
public static void main(String[] args) { FileManager.get().addLocatorClassLoader(ExampleARQ_01.class.getClassLoader()); Model model = FileManager.get().loadModel("data/data.ttl"); System.out.println("Input data:"); model.write(System.out, "TURTLE"); File path = new File("src/main/resources/data/queries"); File[] files = path.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith("construct-") && name.endsWith(".sparql"); } }); Arrays.sort(files); for (File file : files) { System.out.println("Executing " + file.getName() + " ..."); Query query = QueryFactory.read(file.getAbsolutePath()); QueryExecution qexec = QueryExecutionFactory.create(query, model); try { Model result = qexec.execConstruct(); model.add(result); } finally { qexec.close(); } } System.out.println("Output data:"); model.write(System.out, "TURTLE"); }
public void run() { // register the Terp _parser ARQTerpParser.registerFactory(); for (final String query : queries) { // First create a Jena ontology model backed by the Pellet reasoner // (note, the Pellet reasoner is required) final OntModel m = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); // Then read the _data from the file into the ontology model m.read(ontology); // Now read the query file into a query object // Important: specifying that the query is in Terp syntax final Query q = QueryFactory.read(query, TerpSyntax.getInstance()); // Create a SPARQL-DL query execution for the given query and // ontology model final QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // We want to execute a SELECT query, do it, and return the result set final ResultSet rs = qe.execSelect(); // There are different things we can do with the result set, for // instance iterate over it and process the query solutions or, what we // do here, just print out the results ResultSetFormatter.out(rs); // And an empty line to make it pretty System.out.println(); } }
public void run() { // register the Terp _parser ARQTerpParser.registerFactory(); for (final String query : queries) { // First create a Jena ontology model backed by the Pellet reasoner // (note, the Pellet reasoner is required) final OntModel m = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); // Then read the _data from the file into the ontology model m.read(ontology); // Now read the query file into a query object // Important: specifying that the query is in Terp syntax final Query q = QueryFactory.read(query, TerpSyntax.getInstance()); // Create a SPARQL-DL query execution for the given query and // ontology model final QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // We want to execute a SELECT query, do it, and return the result set final ResultSet rs = qe.execSelect(); // There are different things we can do with the result set, for // instance iterate over it and process the query solutions or, what we // do here, just print out the results ResultSetFormatter.out(rs); // And an empty line to make it pretty System.out.println(); } }
public void run() throws Exception { final FileServer fs = new FileServer(); fs.start(); for (final String query : queries) { // First create a Jena ontology model backed by the Pellet reasoner // (note, the Pellet reasoner is required) final OntModel m = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); // Then read the _data from the file into the ontology model m.read(ontology); // Now read the query file into a query object final Query q = QueryFactory.read(query); // Create a SPARQL-DL query execution for the given query and // ontology model final QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // We want to execute a SELECT query, do it, and return the result set final ResultSet rs = qe.execSelect(); // Print the query for better understanding System.out.println(q.toString()); // There are different things we can do with the result set, for // instance iterate over it and process the query solutions or, what we // do here, just print out the results ResultSetFormatter.out(rs); // And an empty line to make it pretty System.out.println(); } fs.stop(); }
public void run() throws Exception { final FileServer fs = new FileServer(); fs.start(); for (final String query : queries) { // First create a Jena ontology model backed by the Pellet reasoner // (note, the Pellet reasoner is required) final OntModel m = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); // Then read the _data from the file into the ontology model m.read(ontology); // Now read the query file into a query object final Query q = QueryFactory.read(query); // Create a SPARQL-DL query execution for the given query and // ontology model final QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // We want to execute a SELECT query, do it, and return the result set final ResultSet rs = qe.execSelect(); // Print the query for better understanding System.out.println(q.toString()); // There are different things we can do with the result set, for // instance iterate over it and process the query solutions or, what we // do here, just print out the results ResultSetFormatter.out(rs); // And an empty line to make it pretty System.out.println(); } fs.stop(); }
/** * {@inheritDoc} */ @Override public void setQueryURI(final String queryURI) { if (_queryURI.equals(queryURI)) return; _queryURI = queryURI; final org.apache.jena.query.Query query = QueryFactory.read(queryURI); _query = QueryEngineBuilder.getParser().parse(query.toString(Syntax.syntaxSPARQL), ((PelletInfGraph) _model.getGraph()).getKB()); }
protected Query queryFromTestItem(TestItem testItem) { if ( testItem.getQueryFile() == null ) { fail("Query test file is null") ; return null ; } Query query = QueryFactory.read(testItem.getQueryFile(), null, testItem.getFileSyntax()) ; return query ; }
@Test public void test() { final Query query = QueryFactory.read(_queryURL); final Dataset dataset = DatasetFactory.create(_model); final QueryExecution qe = SparqlDLExecutionFactory.create(query, dataset, null, _queryEngineType); assertEquals("Failed query engine: " + _queryEngineType + " query: " + _queryURL, _expectedResult, qe.execAsk()); } }
@Test public void testDAWG2() { // Query PREFIX : <http://example/> SELECT * { ?s ?p ?o } // The same as testDAWG3 but here we handle the variable SPO pattern // with the SPARQL-DL engine, i.e. we fall back to ARQ as the SPARQL-DL // engine cannot handle it. The result set size is 20 because the // PelletInfGraph contains all inferred triples. final OntModel model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); model.read("file:test/data/sparql-dawg-tests/data-r2/graph/data-g1.ttl", "N3"); final Query query = QueryFactory.read("file:test/data/sparql-dawg-tests/data-r2/graph/graph-01.rq"); final QueryExecution qe = SparqlDLExecutionFactory.create(query, model); final ResultSet rs = qe.execSelect(); ResultSetFormatter.consume(rs); assertEquals(model.listStatements().toList().size(), rs.getRowNumber()); }
@Test public void testDAWG4() { final Model model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); model.read("file:test/data/sparql-dawg-tests/data-r2/optional-filter/data-1.ttl", "N3"); final Query query = QueryFactory.read("file:test/data/sparql-dawg-tests/data-r2/optional-filter/expr-5.rq"); final QueryExecution qe = SparqlDLExecutionFactory.create(query, model); final ResultSet rs = qe.execSelect(); ResultSetFormatter.consume(rs); assertEquals(3, rs.getRowNumber()); }
@Test public void testDAWG1() { final OntModel model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); model.read("file:test/data/sparql-dawg-tests/data-r2/basic/data-1.ttl", "N3"); final Query query = QueryFactory.read("file:test/data/sparql-dawg-tests/data-r2/basic/base-prefix-1.rq"); final QueryExecution qe = SparqlDLExecutionFactory.create(query, model); final ResultSet rs = qe.execSelect(); //ResultSetFormatter.out( rs ); ResultSetFormatter.consume(rs); // Type, sameAs, one entry for each property assertion assertEquals(1 + 1 + 2, rs.getRowNumber()); }
@Test public void testDAWG3() { // Query PREFIX : <http://example/> SELECT * { ?s ?p ?o } // The same as testDAWG2 but this time we don't handle the variable SPO // pattern, i.e. we handle the pattern as a property value final OntModel model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); model.read("file:test/data/sparql-dawg-tests/data-r2/graph/data-g1.ttl", "N3"); final Query query = QueryFactory.read("file:test/data/sparql-dawg-tests/data-r2/graph/graph-01.rq"); final QueryExecution qe = SparqlDLExecutionFactory.create(query, DatasetFactory.create(model), null, QueryEngineType.MIXED, false); final ResultSet rs = qe.execSelect(); ResultSetFormatter.consume(rs); assertEquals(2, rs.getRowNumber()); }
Query query = QueryFactory.read(queryFile) ; Dataset ds = DatasetFactory.create(defaultGraphURIs, namedGraphURIs) ;
@Test public void testCombinedQueryEngine() { // This tests annotations using the SPARQL-DL combined query engine final OntModel model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); model.read(DATA1_RDF); final Query query = QueryFactory.read(QUERY1_RQ); try (final QueryExecution qe = SparqlDLExecutionFactory.create(query, model)) { assertTrue("qe must not be null", qe != null); final ResultSet rs = qe.execSelect(); while (rs.hasNext()) { final QuerySolution qs = rs.nextSolution(); final Resource s = qs.getResource("s"); final Literal o = qs.getLiteral("o"); assertEquals("http://example.org#i", s.getURI()); assertEquals("o2", o.getLexicalForm()); } } }