Java Code Examples for org.eclipse.rdf4j.query.parser.QueryParserUtil#parseTupleQuery()
The following examples show how to use
org.eclipse.rdf4j.query.parser.QueryParserUtil#parseTupleQuery() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void testValueRoundTrip(Resource subj, IRI pred, Value obj) throws Exception { con.begin(); con.addStatement(subj, pred, obj); con.commit(); CloseableIteration<? extends Statement, SailException> stIter = con.getStatements(null, null, null, false); try { Assert.assertTrue(stIter.hasNext()); Statement st = stIter.next(); Assert.assertEquals(subj, st.getSubject()); Assert.assertEquals(pred, st.getPredicate()); Assert.assertEquals(obj, st.getObject()); Assert.assertTrue(!stIter.hasNext()); } finally { stIter.close(); } ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT S, P, O FROM {S} P {O} WHERE P = <" + pred.stringValue() + ">", null); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false); try { Assert.assertTrue(iter.hasNext()); BindingSet bindings = iter.next(); Assert.assertEquals(subj, bindings.getValue("S")); Assert.assertEquals(pred, bindings.getValue("P")); Assert.assertEquals(obj, bindings.getValue("O")); Assert.assertTrue(!iter.hasNext()); } finally { iter.close(); } }
Example 2
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testDualConnections() throws Exception { SailConnection con2 = sail.getConnection(); try { Assert.assertEquals(0, countAllElements()); con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter, context1); con.addStatement(guernica, RDF.TYPE, painting, context1); con.commit(); Assert.assertEquals(4, countAllElements()); con2.begin(); con2.addStatement(RDF.NIL, RDF.TYPE, RDF.LIST); String query = "SELECT S, P, O FROM {S} P {O}"; ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, query, null); Assert.assertEquals(5, countElements( con2.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false))); Runnable clearer = () -> { try { con.begin(); con.clear(); con.commit(); } catch (SailException e) { throw new RuntimeException(e); } }; Thread thread = new Thread(clearer); thread.start(); Thread.yield(); Thread.yield(); con2.commit(); thread.join(); } finally { con2.close(); } }
Example 3
Source File: BulkedExternalInnerJoin.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public BulkedExternalInnerJoin(PlanNode leftNode, SailConnection connection, String query, boolean skipBasedOnPreviousConnection, SailConnection previousStateConnection, String... variables) { this.leftNode = leftNode; String completeQuery = "select * where { VALUES (?a) {}" + query + "} order by ?a"; parsedQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, completeQuery, null); this.connection = connection; this.skipBasedOnPreviousConnection = skipBasedOnPreviousConnection; this.variables = variables; this.previousStateConnection = previousStateConnection; }
Example 4
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testQueryBindings() throws Exception { // Add some data to the repository con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter, context1); con.addStatement(guernica, RDF.TYPE, painting, context1); con.addStatement(picasso, paints, guernica, context1); con.commit(); // Query 1 ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "select X from {X} rdf:type {Y} rdf:type {rdfs:Class}", null); TupleExpr tupleExpr = tupleQuery.getTupleExpr(); MapBindingSet bindings = new MapBindingSet(2); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; iter = con.evaluate(tupleExpr, null, bindings, false); int resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 2, resultCount); bindings.addBinding("Y", painter); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 1, resultCount); bindings.addBinding("Z", painting); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 1, resultCount); bindings.removeBinding("Y"); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 2, resultCount); // Query 2 tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "select X from {X} rdf:type {Y} rdf:type {rdfs:Class} where Y = Z", null); tupleExpr = tupleQuery.getTupleExpr(); bindings.clear(); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 0, resultCount); bindings.addBinding("Z", painter); iter = con.evaluate(tupleExpr, null, bindings, false); resultCount = verifyQueryResult(iter, 1); Assert.assertEquals("Wrong number of query results", 1, resultCount); }
Example 5
Source File: RDFStoreTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
protected int countQueryResults(String query) throws Exception { ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, query + " using namespace ex = <" + EXAMPLE_NS + ">", null); return countElements(con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false)); }
Example 6
Source File: StoreSerializationTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void testSerialization() throws Exception { MemoryStore store = new MemoryStore(dataDir); store.initialize(); ValueFactory factory = store.getValueFactory(); IRI foo = factory.createIRI("http://www.foo.example/foo"); IRI bar = factory.createIRI("http://www.foo.example/bar"); SailConnection con = store.getConnection(); con.begin(); con.addStatement(foo, RDF.TYPE, bar); con.commit(); ParsedTupleQuery query = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT X, P, Y FROM {X} P {Y}", null); TupleExpr tupleExpr = query.getTupleExpr(); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter = con.evaluate(tupleExpr, null, EmptyBindingSet.getInstance(), false); BindingSet bindingSet = iter.next(); assertEquals(bindingSet.getValue("X"), foo); assertEquals(bindingSet.getValue("P"), RDF.TYPE); assertEquals(bindingSet.getValue("Y"), bar); iter.close(); con.close(); store.shutDown(); store = new MemoryStore(dataDir); store.initialize(); factory = store.getValueFactory(); foo = factory.createIRI("http://www.foo.example/foo"); bar = factory.createIRI("http://www.foo.example/bar"); con = store.getConnection(); iter = con.evaluate(tupleExpr, null, EmptyBindingSet.getInstance(), false); bindingSet = iter.next(); assertEquals(bindingSet.getValue("X"), foo); assertEquals(bindingSet.getValue("P"), RDF.TYPE); assertEquals(bindingSet.getValue("Y"), bar); iter.close(); con.begin(); con.addStatement(bar, RDF.TYPE, foo); con.commit(); con.close(); store.shutDown(); }