public static GraphQuery getQuery(RepositoryConnection connection, GraphQueryModel graphQueryModel, boolean includeInferred) { try { QUERY_HOLDER.set(graphQueryModel); GraphQuery graphQuery = connection.prepareGraphQuery(DirectQuery.DIRECTQUERY, ""); graphQuery.setIncludeInferred(includeInferred); return graphQuery; } catch (StoreException e) { throw new RepositoryException(e); } catch (MalformedQueryException e) { throw new RepositoryException(e); } }
public static GraphQuery getQuery(RepositoryConnection connection, GraphQueryModel graphQueryModel, boolean includeInferred) { try { QUERY_HOLDER.set(graphQueryModel); GraphQuery graphQuery = connection.prepareGraphQuery(DirectQuery.DIRECTQUERY, ""); graphQuery.setIncludeInferred(includeInferred); return graphQuery; } catch (StoreException e) { throw new RepositoryException(e); } catch (MalformedQueryException e) { throw new RepositoryException(e); } }
public static GraphQuery getQuery(RepositoryConnection connection, ParsedGraphQuery graphQueryModel, boolean includeInferred) { try { QUERY_HOLDER.set(graphQueryModel); GraphQuery graphQuery = connection.prepareGraphQuery(DirectQuery.DIRECTQUERY, ""); graphQuery.setIncludeInferred(includeInferred); return graphQuery; } catch (MalformedQueryException e) { throw new RepositoryException(e); } catch (org.openrdf.repository.RepositoryException e) { throw new RepositoryException(e); } }
/** * Find edges based on a SPARQL construct query. The query MUST construct * edge statements: * <p> * construct { ?from ?edge ?to } where { ... } * * @see {@link BigdataGraphQuery} */ Iterable<Edge> getEdges(final String queryStr) throws Exception { final org.openrdf.query.GraphQuery query = cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); final GraphQueryResult stmts = query.evaluate(); /* * EdgeIterable will close the connection if necessary. */ return new EdgeIterable(stmts); }
/** * Find vertices based on a SPARQL construct query. If the subject parameter * is true, the vertices will be taken from the subject position of the * constructed statements, otherwise they will be taken from the object * position. * * @see {@link BigdataGraphQuery} */ Iterable<Vertex> getVertices(final String queryStr, final boolean subject) throws Exception { final org.openrdf.query.GraphQuery query = cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); final GraphQueryResult stmts = query.evaluate(); /* * VertexIterable will close the connection if necessary. */ return new VertexIterable(stmts, subject); }
public ClosableIterable<Statement> sparqlDescribe(String queryString) throws ModelRuntimeException { this.assertModel(); GraphQuery query; try { query = this.connection.prepareGraphQuery(QueryLanguage.SPARQL, queryString); GraphQueryResult graphQueryResult = query.evaluate(); return new StatementIterable(graphQueryResult, null); } catch (OpenRDFException e) { throw new ModelRuntimeException(e); } }
@Override public ClosableIterable<Statement> sparqlConstruct(String queryString) throws ModelRuntimeException { this.assertModel(); GraphQuery query; try { query = this.connection.prepareGraphQuery(QueryLanguage.SPARQL, queryString); GraphQueryResult graphQueryResult = query.evaluate(); return new StatementIterable(graphQueryResult, null); } catch(OpenRDFException e) { throw new ModelRuntimeException(e); } }
@Override public ClosableIterable<Statement> sparqlDescribe(String queryString) throws ModelRuntimeException { this.assertModel(); GraphQuery query; try { query = this.connection.prepareGraphQuery(QueryLanguage.SPARQL, queryString); GraphQueryResult graphQueryResult = query.evaluate(); return new StatementIterable(graphQueryResult, null); } catch(OpenRDFException e) { throw new ModelRuntimeException(e); } }
public ClosableIterable<Statement> sparqlConstruct(String queryString) throws ModelRuntimeException { this.assertModel(); GraphQuery query; try { query = this.connection.prepareGraphQuery(QueryLanguage.SPARQL, queryString); GraphQueryResult graphQueryResult = query.evaluate(); return new StatementIterable(graphQueryResult, null); } catch (OpenRDFException e) { throw new ModelRuntimeException(e); } }
@Override public ClosableIterable<Statement> queryConstruct(String queryString, String queryLanguage) throws QueryLanguageNotSupportedException, ModelRuntimeException { this.assertModel(); // resolve the query language String to a QueryLanguage QueryLanguage language = ConversionUtil.toOpenRDFQueryLanguage(queryLanguage); try { // determine query result GraphQuery query = this.connection.prepareGraphQuery(language, queryString); GraphQueryResult queryResult = query.evaluate(); // wrap it in a GraphIterable return new GraphIterable(queryResult, null); } catch(OpenRDFException e) { throw new ModelRuntimeException(e); } }
public ClosableIterable<Statement> queryConstruct(String queryString, String queryLanguage) throws QueryLanguageNotSupportedException, ModelRuntimeException { this.assertModel(); // resolve the query language String to a QueryLanguage QueryLanguage language = ConversionUtil .toOpenRDFQueryLanguage(queryLanguage); try { // determine query result GraphQuery query = this.connection.prepareGraphQuery(language, queryString); GraphQueryResult queryResult = query.evaluate(); // wrap it in a GraphIterable return new GraphIterable(queryResult, null); } catch (OpenRDFException e) { throw new ModelRuntimeException(e); } }
@Test public void testDescribeB() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:b"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI b = f.createURI("http://example.org/b"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Resource> subjects = result.filter(null, p, b).subjects(); assertNotNull(subjects); for (Value subject : subjects) { if (subject instanceof BNode) { assertTrue(result.contains(null, null, subject)); } } }
@Test public void testDescribeB() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:b"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); IRI b = f.createIRI("http://example.org/b"); IRI p = f.createIRI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Resource> subjects = result.filter(null, p, b).subjects(); assertNotNull(subjects); for (Value subject : subjects) { if (subject instanceof BNode) { assertTrue(result.contains(null, null, subject)); } } }
@Test public void testDescribeA() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:a"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI a = f.createURI("http://example.org/a"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource)object, null, null)); assertEquals(2, result.filter((Resource)object, null, null).size()); } } }
@Override public ClosableIterable<Statement> sparqlDescribe(String query) throws ModelRuntimeException { assertModel(); try { GraphQueryResult graphQueryResult = this.connection.prepareGraphQuery( QueryLanguage.SPARQL, query).evaluate(); return new GraphIterable(graphQueryResult, this); } catch(MalformedQueryException e) { throw new ModelRuntimeException(e); } catch(UnsupportedQueryLanguageException e) { throw new ModelRuntimeException(e); } catch(QueryEvaluationException e) { throw new ModelRuntimeException(e); } catch(RepositoryException e) { throw new ModelRuntimeException(e); } }
@Override public ClosableIterable<Statement> sparqlConstruct(String query) throws ModelRuntimeException { assertModel(); try { GraphQueryResult graphQueryResult = this.connection.prepareGraphQuery( QueryLanguage.SPARQL, query).evaluate(); return new GraphIterable(graphQueryResult, this); } catch(MalformedQueryException e) { throw new ModelRuntimeException(e); } catch(UnsupportedQueryLanguageException e) { throw new ModelRuntimeException(e); } catch(QueryEvaluationException e) { throw new ModelRuntimeException(e); } catch(RepositoryException e) { throw new ModelRuntimeException(e); } }
@Test public void testSES2104ConstructBGPSameURI() throws Exception { final String queryStr = "PREFIX : <urn:> CONSTRUCT {:x :p :x } WHERE {} "; conn.add(new StringReader("@prefix : <urn:> . :a :p :b . "), "", RDFFormat.TURTLE); final IRI x = conn.getValueFactory().createIRI("urn:x"); final IRI p = conn.getValueFactory().createIRI("urn:p"); GraphQuery query = conn.prepareGraphQuery(QueryLanguage.SPARQL, queryStr); Model result = QueryResults.asModel(query.evaluate()); assertNotNull(result); assertFalse(result.isEmpty()); assertTrue(result.contains(x, p, x)); }
@Test public void testDescribeAWhere() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ?x WHERE {?x rdfs:label \"a\". } "); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI a = f.createURI("http://example.org/a"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource)object, null, null)); assertEquals(2, result.filter((Resource)object, null, null).size()); } } }
@Test public void testDescribeA() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:a"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI a = f.createURI("http://example.org/a"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource)object, null, null)); assertEquals(2, result.filter((Resource)object, null, null).size()); } } }
public GraphQueryResult evaluate() throws QueryEvaluationException { try { RepositoryConnection con = getConnection(); QueryLanguage ql = getQueryLanguage(); String query = getQuery(); String baseURI = getBaseURI(); GraphQuery qry = con.prepareGraphQuery(ql, query, baseURI); qry.setDataset(getDataset()); qry.setIncludeInferred(getIncludeInferred()); BindingSet bindings = getBindings(); for (String name : bindings.getBindingNames()) { qry.setBinding(name, bindings.getValue(name)); } return qry.evaluate(); } catch (MalformedQueryException e) { throw new QueryEvaluationException(e); } catch (RepositoryException e) { throw new QueryEvaluationException(e); } }