use of org.eclipse.rdf4j.query.parser.ParsedDescribeQuery in project rdf4j by eclipse.
the class SPARQLParser method parseQuery.
@Override
public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
try {
ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
StringEscapesProcessor.process(qc);
BaseDeclProcessor.process(qc, baseURI);
Map<String, String> prefixes = PrefixDeclProcessor.process(qc);
WildcardProjectionProcessor.process(qc);
BlankNodeVarProcessor.process(qc);
if (qc.containsQuery()) {
// handle query operation
TupleExpr tupleExpr = buildQueryModel(qc);
ParsedQuery query;
ASTQuery queryNode = qc.getQuery();
if (queryNode instanceof ASTSelectQuery) {
query = new ParsedTupleQuery(queryStr, tupleExpr);
} else if (queryNode instanceof ASTConstructQuery) {
query = new ParsedGraphQuery(queryStr, tupleExpr, prefixes);
} else if (queryNode instanceof ASTAskQuery) {
query = new ParsedBooleanQuery(queryStr, tupleExpr);
} else if (queryNode instanceof ASTDescribeQuery) {
query = new ParsedDescribeQuery(queryStr, tupleExpr, prefixes);
} else {
throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
}
// Handle dataset declaration
Dataset dataset = DatasetDeclProcessor.process(qc);
if (dataset != null) {
query.setDataset(dataset);
}
return query;
} else {
throw new IncompatibleOperationException("supplied string is not a query operation");
}
} catch (ParseException e) {
throw new MalformedQueryException(e.getMessage(), e);
} catch (TokenMgrError e) {
throw new MalformedQueryException(e.getMessage(), e);
}
}
use of org.eclipse.rdf4j.query.parser.ParsedDescribeQuery in project rdf4j by eclipse.
the class QueryBuilderFactory method describe.
/**
* Create a QueryBuilder for creating a describe query
*
* @param theVars
* the variables to be described
* @param theValues
* the specific bound URI values to be described
* @return a describe query builder
*/
public static QueryBuilder<ParsedGraphQuery> describe(String[] theVars, Resource... theValues) {
QueryBuilder<ParsedGraphQuery> aBuilder = new AbstractQueryBuilder<ParsedGraphQuery>(new ParsedDescribeQuery());
aBuilder.reduced();
aBuilder.addProjectionVar("descr_subj", "descr_pred", "descr_obj");
GroupBuilder<?, ?> aGroup = aBuilder.group();
if (theVars != null) {
for (String aVar : theVars) {
Var aVarObj = new Var(aVar);
aVarObj.setAnonymous(true);
aGroup.filter().or(new SameTerm(aVarObj, new Var("descr_subj")), new SameTerm(aVarObj, new Var("descr_obj")));
}
}
if (theValues != null) {
for (Resource aVar : theValues) {
Var aSubjVar = new Var("descr_subj");
aSubjVar.setAnonymous(true);
Var aObjVar = new Var("descr_obj");
aObjVar.setAnonymous(true);
aGroup.filter().or(new SameTerm(new ValueConstant(aVar), aSubjVar), new SameTerm(new ValueConstant(aVar), aObjVar));
}
}
aGroup.atom("descr_subj", "descr_pred", "descr_obj");
return aBuilder;
}
Aggregations