org.openrdf.query.parser.ParsedTupleQuery Java Examples

The following examples show how to use org.openrdf.query.parser.ParsedTupleQuery. 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 database with GNU General Public License v2.0 5 votes vote down vote up
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 #2
Source File: QueryRewriter.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new query based on a tuple expression and original query. The
 * new query will have the same type null
 * ({@link org.openrdf.query.TupleQuery},
 * {@link org.openrdf.query.GraphQuery} or
 * {@link org.openrdf.query.BooleanQuery}) as the given original query.
 *
 * @param orig the original query
 * @param expr the expression used for the new query
 * @return new query based on expression
 */
protected Query getExprQuery(ParsedQuery orig, TupleExpr expr) {
	if (orig instanceof ParsedTupleQuery) {
		return new SailTupleExprQuery(
				new ParsedTupleQuery(expr), conn);
	} else if (orig instanceof ParsedGraphQuery) {
		return new SailGraphExprQuery(
				new ParsedGraphQuery(expr), conn);
	} else {
		return new SailBooleanExprQuery(
				new ParsedBooleanQuery(expr), conn);
	}
}
 
Example #3
Source File: SPARQLRewriter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public String rewrite(String selectQuery) throws Exception {
	SPARQLParserFactory fac = new SPARQLParserFactory();
	QueryParser parser = fac.getParser();
	ParsedTupleQuery query = (ParsedTupleQuery) parser.parseQuery(selectQuery, null);

	resolutionEngine.unfold(query);

	SPARQLQueryRenderer renderer = new SPARQLQueryRenderer();
	String renderedQuery = renderer.render(query);

	return renderedQuery;
}
 
Example #4
Source File: SparqlQuery.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public boolean isTupleQuery() {
	return query instanceof ParsedTupleQuery;
}
 
Example #5
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
protected void testValueRoundTrip(Resource subj, URI 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 {
		assertTrue(stIter.hasNext());

		Statement st = stIter.next();
		assertEquals(subj, st.getSubject());
		assertEquals(pred, st.getPredicate());
		assertEquals(obj, st.getObject());
		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 {
		assertTrue(iter.hasNext());

		BindingSet bindings = iter.next();
		assertEquals(subj, bindings.getValue("S"));
		assertEquals(pred, bindings.getValue("P"));
		assertEquals(obj, bindings.getValue("O"));
		assertTrue(!iter.hasNext());
	}
	finally {
		iter.close();
	}
}
 
Example #6
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@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);
	assertEquals("Wrong number of query results", 2, resultCount);

	bindings.addBinding("Y", painter);
	iter = con.evaluate(tupleExpr, null, bindings, false);
	resultCount = verifyQueryResult(iter, 1);
	assertEquals("Wrong number of query results", 1, resultCount);

	bindings.addBinding("Z", painting);
	iter = con.evaluate(tupleExpr, null, bindings, false);
	resultCount = verifyQueryResult(iter, 1);
	assertEquals("Wrong number of query results", 1, resultCount);

	bindings.removeBinding("Y");
	iter = con.evaluate(tupleExpr, null, bindings, false);
	resultCount = verifyQueryResult(iter, 1);
	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);
	assertEquals("Wrong number of query results", 0, resultCount);

	bindings.addBinding("Z", painter);
	iter = con.evaluate(tupleExpr, null, bindings, false);
	resultCount = verifyQueryResult(iter, 1);
	assertEquals("Wrong number of query results", 1, resultCount);
}
 
Example #7
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testDualConnections()
	throws Exception
{
	SailConnection con2 = sail.getConnection();
	try {
		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();
		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);
		assertEquals(5, countElements(con2.evaluate(tupleQuery.getTupleExpr(), null,
				EmptyBindingSet.getInstance(), false)));
		Runnable clearer = new Runnable() {

			public void run() {
				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 #8
Source File: TestSingleTailRule.java    From database with GNU General Public License v2.0 4 votes vote down vote up
private void testValueRoundTrip(final SailConnection con, 
            final Resource subj, final URI pred, final Value obj)
        throws Exception
    {
        con.addStatement(subj, pred, obj);
        con.commit();
    
        CloseableIteration<? extends Statement, SailException> stIter = 
            con.getStatements(null, null, null, false);
    
        try {
            assertTrue(stIter.hasNext());
    
            Statement st = stIter.next();
            assertEquals(subj, st.getSubject());
            assertEquals(pred, st.getPredicate());
            assertEquals(obj, st.getObject());
            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);
        ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL,
                "SELECT ?S ?P ?O " +
                "WHERE { " +
                "  ?S ?P ?O . " +
                "  FILTER( ?P = <" + pred.stringValue() + "> ) " +
                "}", null);
    
        CloseableIteration<? extends BindingSet, QueryEvaluationException> iter;
        iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false);
    
        try {
            assertTrue(iter.hasNext());
    
            BindingSet bindings = iter.next();
            assertEquals(subj, bindings.getValue("S"));
            assertEquals(pred, bindings.getValue("P"));
            assertEquals(obj, bindings.getValue("O"));
            assertTrue(!iter.hasNext());
        }
        finally {
            iter.close();
        }
    }
 
Example #9
Source File: SailTupleExprQuery.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
public SailTupleExprQuery(ParsedTupleQuery tupleQuery, SailRepositoryConnection sailConnection) {
	super(tupleQuery, sailConnection);
}
 
Example #10
Source File: ResolutionEngine.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/***
 * Takes a SELECT query and 
 * @param query
 */
public void unfold(ParsedTupleQuery query) {
	
	ResolutionVisitor visitor = new ResolutionVisitor(rules);
	TupleExpr body = query.getTupleExpr();
	
	
	
	VarNameCollector collector = new VarNameCollector();
	body.visit(collector);
	Set<String> bindingNames = collector.getVarNames();
	
	visitor.setBindingNames(bindingNames);
	
	body.visit(visitor);
	while (visitor.producedChange) {
		visitor.producedChange = false;
		body.visit(visitor);
	}
	
}