use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.
the class OpenRDFQueryHandler method computeSparqlStatistics.
@Override
protected void computeSparqlStatistics() {
if (getValidityStatus() != QueryHandler.Validity.VALID) {
this.sparqlStatistics = new HashMap<>();
return;
}
try {
ASTQueryContainer queryContainer = new StandardizingSPARQLParser().getDebuggedASTQueryContainer(getQueryString(), BASE_URI);
QueryContainerSparqlStatisticsCollector queryContainerSparqlStatisticsCollector = new QueryContainerSparqlStatisticsCollector();
queryContainer.jjtAccept(queryContainerSparqlStatisticsCollector, null);
this.sparqlStatistics = queryContainerSparqlStatisticsCollector.getStatistics();
TupleExprSparqlStatisticsCollector tupleExprSparqlStatisticsCollector = new TupleExprSparqlStatisticsCollector();
this.query.getTupleExpr().visitChildren(tupleExprSparqlStatisticsCollector);
this.query.getTupleExpr().visit(tupleExprSparqlStatisticsCollector);
this.sparqlStatistics.putAll(tupleExprSparqlStatisticsCollector.getStatistics());
this.primaryLanguage = tupleExprSparqlStatisticsCollector.getPrimaryLanguage();
} catch (TokenMgrError | MalformedQueryException e) {
logger.error("Failed to parse the query although it was found valid - this is a serious bug.", e);
} catch (VisitorException e) {
logger.error("Failed to calculate the SPARQL Keyword Statistics. Error occured while visiting the query.", e);
} catch (Exception e) {
logger.error("An unknown error occured while computing the sparql statistics: ", e);
}
}
use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.
the class StandardizingPrefixDeclProcessor method process.
/**
* Processes prefix declarations in queries. This method collects all
* prefixes that are declared in the supplied query, verifies that prefixes
* are not redefined and replaces any {@link ASTQName} nodes in the query
* with equivalent {@link ASTIRI} nodes.
*
* @param qc The query that needs to be processed.
* @return A map containing the prefixes that are declared in the query (key)
* and the namespace they map to (value).
* @throws MalformedQueryException If the query contains redefined prefixes or qnames that use
* undefined prefixes.
*/
public static Map<String, String> process(ASTOperationContainer qc) throws MalformedQueryException {
List<ASTPrefixDecl> prefixDeclList = qc.getPrefixDeclList();
// Build a prefix --> IRI map
Map<String, String> prefixMap = new LinkedHashMap<String, String>();
for (ASTPrefixDecl prefixDecl : prefixDeclList) {
String prefix = prefixDecl.getPrefix();
String iri = prefixDecl.getIRI().getValue();
if (prefixMap.containsKey(prefix)) {
throw new MalformedQueryException("Multiple prefix declarations for prefix '" + prefix + "'");
}
prefixMap.put(prefix, iri);
}
// insert some default prefixes (if not explicitly defined in the query)
for (Map.Entry<String, String> entry : Main.prefixes.entrySet()) {
insertDefaultPrefix(prefixMap, entry.getKey(), entry.getValue());
}
ASTUnparsedQuadDataBlock dataBlock = null;
if (qc.getOperation() instanceof ASTInsertData) {
ASTInsertData insertData = (ASTInsertData) qc.getOperation();
dataBlock = insertData.jjtGetChild(ASTUnparsedQuadDataBlock.class);
} else if (qc.getOperation() instanceof ASTDeleteData) {
ASTDeleteData deleteData = (ASTDeleteData) qc.getOperation();
dataBlock = deleteData.jjtGetChild(ASTUnparsedQuadDataBlock.class);
}
if (dataBlock != null) {
String prefixes = createPrefixesInSPARQLFormat(prefixMap);
dataBlock.setDataBlock(prefixes + dataBlock.getDataBlock());
} else {
QNameProcessor visitor = new QNameProcessor(prefixMap);
try {
qc.jjtAccept(visitor, null);
} catch (VisitorException e) {
throw new MalformedQueryException(e);
}
}
return prefixMap;
}
use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.
the class StandardizingSPARQLParser method getDebuggedASTQueryContainer.
/**
* @param queryString The query to parsed.
* @param baseURI The base URI to resolve any possible relative URIs against.
* @return The ASTQueryContainer representing this query after debugging.
* @throws MalformedQueryException If the query was in any way malformed.
*/
public final ASTQueryContainer getDebuggedASTQueryContainer(String queryString, String baseURI) throws MalformedQueryException {
try {
ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryString);
debug(qc);
return qc;
} catch (TokenMgrError | ParseException e) {
throw new MalformedQueryException(e.getMessage(), e);
}
}
use of org.openrdf.query.MalformedQueryException in project stanbol by apache.
the class SesameYard method executeSparqlFieldQuery.
/**
* Returns the SPARQL result set for a given {@link SparqlFieldQuery} that
* was executed on this yard
* @param con the repository connection to use
* @param fieldQuery the SparqlFieldQuery instance
* @param limit the maximum number of results
* @return the results of the SPARQL query in the {@link #contexts} of the
* Sesame Repository
* @throws RepositoryException on any error while using the parsed connection
* @throws QueryEvaluationException on any error while executing the query
* @throws YardException if the SPARQL query created for the parsed FieldQuery
* was illegal formatted or if the {@link #repository} does not support
* SPARQL.
*/
private TupleQueryResult executeSparqlFieldQuery(RepositoryConnection con, final SparqlFieldQuery fieldQuery, int limit, boolean select) throws RepositoryException, YardException, QueryEvaluationException {
log.debug("> execute FieldQuery: {}", fieldQuery);
String sparqlQueryString = SparqlQueryUtils.createSparqlSelectQuery(fieldQuery, select, limit, SparqlEndpointTypeEnum.Sesame);
log.debug(" - SPARQL Query: {}", sparqlQueryString);
TupleQuery sparqlOuery;
try {
sparqlOuery = con.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQueryString);
} catch (MalformedQueryException e) {
log.error("Unable to pparse SPARQL Query generated for a FieldQuery");
log.error("FieldQuery: {}", fieldQuery);
log.error("SPARQL Query: {}", sparqlQueryString);
log.error("Exception ", e);
throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
} catch (UnsupportedQueryTypeException e) {
String message = "The Sesame Repository '" + repository + "'(class: " + repository.getClass().getName() + ") does not support SPARQL!";
log.error(message, e);
throw new YardException(message, e);
}
if (dataset != null) {
// respect the configured contexts
sparqlOuery.setDataset(dataset);
}
return sparqlOuery.evaluate();
}
Aggregations