org.openrdf.query.Query Java Examples

The following examples show how to use org.openrdf.query.Query. 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: QueryRewriter.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Rewrite a given query to add inference.
 *
 * @param ql the query language used for the query
 * @param query the query to rewrite
 * @param baseuri a base URI to use for the query
 * @return rewritten query that includes inference
 * @throws MalformedQueryException if the query is malformed
 * @throws RepositoryException if there was a problem while rewriting
 */
public Query rewrite(QueryLanguage ql, String query, String baseuri)
		throws MalformedQueryException, RepositoryException {
	// parse query using Sesame
	QueryParserFactory f = QueryParserRegistry.getInstance().get(ql);
	QueryParser parser = f.getParser();
	ParsedQuery parsed = parser.parseQuery(query, baseuri);
	// get SPARQL algebra expression from parsed query
	TupleExpr expr = parsed.getTupleExpr();
	// rewrite query using visitor pattern
	RuleTransformationVisitor visitor
			= new RuleTransformationVisitor(rules);
	expr.visit(visitor);
	// return new query based on rewritten algebra expression
	return getExprQuery(parsed, expr);
}
 
Example #2
Source File: QueryEvaluation.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<List<Object>> evaluate(String queries) throws Exception {
	List<List<Object>> report = new ArrayList<List<Object>>();
			
	List<String> qnames = Arrays.asList(queries.split(" "));
	for (String curQueryName : qnames)
	{
		List<Object> reportRow = new ArrayList<Object>();
		report.add(reportRow);
		String curQuery = qp.getQuery(curQueryName);
		reportRow.add(curQueryName);
		
		long startTime = System.currentTimeMillis();
		//ParsedOperation pO = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, curQuery, null);
		RepositoryConnection repCon = this.repository.getConnection();
		try {
			Query tempq = repCon.prepareQuery(QueryLanguage.SPARQL, curQuery);
			TupleQuery q = (TupleQuery)tempq;
			
			SyncTupleQueryResultHandler rhandler = new SyncTupleQueryResultHandler();
            q.evaluate(rhandler);
            		
		    long runTime = System.currentTimeMillis() - startTime;
		    reportRow.add((Long)rhandler.resultCount); reportRow.add((Long)runTime);

		    log.info(curQueryName + ": Query exection time (msec): "+ runTime + ", Total Number of Records: " + rhandler.resultCount);
		} catch (Exception e) {
			reportRow.add(null); reportRow.add(null);
		} finally {
			repCon.close();
        }
	}
	return report;
}
 
Example #3
Source File: SesameRepositoryImpl.java    From sparql-playground with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Query prepareQuery(String sparqlQuery) {
	try {
		return conn.prepareQuery(QueryLanguage.SPARQL, sparqlQuery);
	} catch (RepositoryException | MalformedQueryException e) {
		e.printStackTrace();
		throw new SparqlTutorialException(e);
	}
}
 
Example #4
Source File: BigdataSailRemoteRepositoryConnection.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Query prepareQuery(final QueryLanguage ql, final String query,
        final String baseURI) throws RepositoryException,
        MalformedQueryException {

    return prepareQuery(ql, query);

}
 
Example #5
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 #6
Source File: BlazeGraph.java    From tinkerpop3 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Utility function to set the Query timeout to the global
 * setting if it is configured.
 */
protected void setMaxQueryTime(final Query query) {
    if (maxQueryTime > 0) {
        query.setMaxQueryTime(maxQueryTime);
    }
}
 
Example #7
Source File: BigdataSailRemoteRepositoryConnection.java    From database with GNU General Public License v2.0 3 votes vote down vote up
@Override
   public Query prepareQuery(final QueryLanguage ql, final String query)
           throws RepositoryException, MalformedQueryException {

	throw new UnsupportedOperationException("please use the specific operation for your query type: prepare[Boolean/Tuple/Graph]Query");
	
}
 
Example #8
Source File: QueryRewriter.java    From neo4j-sparql-extension with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Rewrite a given query to add inference.
 *
 * @param ql the query language used for the query
 * @param query the query to rewrite
 * @return rewritten query that includes inference
 * @throws MalformedQueryException if the query is malformed
 * @throws RepositoryException if there was a problem while rewriting
 */
public Query rewrite(QueryLanguage ql, String query)
		throws MalformedQueryException, RepositoryException {
	return rewrite(ql, query, null);
}
 
Example #9
Source File: SesameRepository.java    From sparql-playground with GNU General Public License v2.0 votes vote down vote up
Query prepareQuery(String sparqlQuery);