org.openrdf.query.parser.ParsedQuery Java Examples

The following examples show how to use org.openrdf.query.parser.ParsedQuery. 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: BGPGroupGenerator.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
/**
 * Generate BGP groups from a SPARQL query
 * @param parsedQuery TupleExpr of the SPARQL query
 * @return DNFGrps Map of DNF groups
 * @throws MalformedQueryException 
 */
public static HashMap<Integer, List<StatementPattern>>  generateBgpGroups(String strQuery) throws MalformedQueryException
{
	HashMap<Integer, List<StatementPattern>> bgpGrps = new HashMap<Integer, List<StatementPattern>>();
	int grpNo = 0;
	SPARQLParser parser = new SPARQLParser();
	ParsedQuery parsedQuery = parser.parseQuery(strQuery, null);
	TupleExpr query = parsedQuery.getTupleExpr();
	// collect all basic graph patterns

	for (TupleExpr bgp : BasicGraphPatternExtractor.process(query)) {
		//System.out.println(bgp);
		List<StatementPattern> patterns = StatementPatternCollector.process(bgp);	
		bgpGrps.put(grpNo, patterns );
		grpNo++;
	}

	return bgpGrps;
}
 
Example #2
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 #3
Source File: SPARQLRewriter.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private List<Rule> parse(String rulesString) throws Exception {
	String[] rules = rulesString.split("###");
	List<Rule> result = new LinkedList<Rule>();
	for (String rule : rules) {

		SPARQLParserFactory fac = new SPARQLParserFactory();
		QueryParser parser = fac.getParser();
		ParsedQuery query = parser.parseQuery(rule, null);

		Rule r = new Rule(query);
		result.add(r);


	}
	return result;
}
 
Example #4
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 #5
Source File: Rule.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 
 */
public Rule(ParsedQuery constructQuery) {

	/* Getting elements of the construct part, they are always implemented 
	*  as a pair of projection and extension before the actual algebra of the query
	   e.g.,
	   Projection
	      ProjectionElemList
	         ProjectionElem "x" AS "subject"
	         ProjectionElem "_const-f5e5585a-uri" AS "predicate"
	         ProjectionElem "_const-a31c101d-uri" AS "object"
	      Extension
	         ExtensionElem (_const-a31c101d-uri)
	            ValueConstant (value=http://example.org/Company)
	         ExtensionElem (_const-f5e5585a-uri)
	            ValueConstant (value=http://www.w3.org/1999/02/22-rdf-syntax-ns#type)
	         Join...
	 */

	TupleExpr expr = constructQuery.getTupleExpr();
	Projection projection = (Projection)((Reduced) expr).getArg();
	Extension extension = (Extension) projection.getArg();
	
	antecedent = extension.getArg();

	extractConsequent(expr, projection, extension);
}