use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.
the class OutputHandlerAnonymizer method writeLine.
@Override
public void writeLine(String queryToAnalyze, Validity validityStatus, String userAgent, String timeStamp, long currentLine, int currentDay, String currentFile) throws IOException {
List<Object> line = new ArrayList<>();
QueryHandler queryHandler = queryHandlerFactory.getQueryHandler(validityStatus, currentLine, currentDay, queryToAnalyze, userAgent, currentFile, threadNumber);
if (queryHandler.getValidityStatus().equals(QueryHandler.Validity.VALID)) {
ASTQueryContainer qc;
try {
qc = SyntaxTreeBuilder.parseQuery(queryToAnalyze);
} catch (TokenMgrError | ParseException e) {
logger.error("Failed to parse the query although it was found valid - this is a serious bug.", e);
return;
}
try {
StandardizingSPARQLParser.debug(qc);
StringEscapesProcessor.process(qc);
BaseDeclProcessor.process(qc, OpenRDFQueryHandler.BASE_URI);
StandardizingPrefixDeclProcessor.process(qc);
StandardizingSPARQLParser.anonymize(qc);
} catch (MalformedQueryException e) {
logger.error("Failed to debug or anonymize query. " + queryToAnalyze);
}
String renderedQueryString;
try {
renderedQueryString = qc.jjtAccept(new RenderVisitor(), "").toString();
} catch (VisitorException e) {
logger.error("Failed to render the query.", e);
return;
}
try {
new StandardizingSPARQLParser().parseQuery(renderedQueryString, OpenRDFQueryHandler.BASE_URI);
} catch (MalformedQueryException e) {
String queryName = this.threadNumber + "_" + this.failedQueriesNumber + ".query";
logger.error("Anonymized query was not valid anymore. " + queryName, e);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(this.outputFile.substring(0, this.outputFile.lastIndexOf("/")) + "failedQueriesFolder/" + queryName))) {
bw.write(queryToAnalyze);
this.failedQueriesNumber++;
} catch (IOException i) {
logger.error("Could not write the failed query to failed queries folder.", i);
}
return;
} catch (ClassCastException e) {
logger.error("Unexpected class cast exception after anonymization.", e);
}
String encodedRenderedQueryString;
try {
encodedRenderedQueryString = URLEncoder.encode(renderedQueryString, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("Apparently this system does not support UTF-8. Please fix this before running the program again.");
return;
}
line.add(encodedRenderedQueryString);
line.add(timeStamp);
if (queryHandler.getSourceCategory().equals(QueryHandler.SourceCategory.USER)) {
line.add("organic");
} else {
line.add("robotic");
}
if (QueryHandler.isOrganicUserAgent(queryHandler.getUserAgent())) {
line.add("browser");
} else if (Anonymizer.allowedToolNames.contains(queryHandler.getToolName())) {
line.add(queryHandler.getToolName());
} else if (Anonymizer.allowedUserAgents.containsKey(queryHandler.getUserAgent())) {
line.add(Anonymizer.allowedUserAgents.get(queryHandler.getUserAgent()));
} else {
line.add("other");
}
csvPrinter.printRecord(line);
}
}
use of org.openrdf.query.MalformedQueryException in project QueryAnalysis by Wikidata.
the class OpenRDFQueryHandler method computeNonSimplePropertyPaths.
@Override
protected final void computeNonSimplePropertyPaths() {
if (getValidityStatus() != QueryHandler.Validity.VALID) {
this.nonSimplePropertyPaths = getValidityStatus().toString();
return;
}
try {
ASTQueryContainer qc = new StandardizingSPARQLParser().getASTQueryContainerPrefixesProcessed(getQueryString(), BASE_URI);
Set<String> nonSimplePropertyPaths = new NonSimplePropertyPathVisitor().getNonSimplePropertyPaths(qc);
this.nonSimplePropertyPaths = this.computeAnyIDString(nonSimplePropertyPaths);
if (this.nonSimplePropertyPaths.equals("")) {
this.nonSimplePropertyPaths = "NONE";
}
} catch (VisitorException | MalformedQueryException e) {
this.nonSimplePropertyPaths = "INTERNAL_ERROR";
logger.error("Unexpected error while calculating non-simple property paths.", e);
}
}
use of org.openrdf.query.MalformedQueryException in project minerva by geneontology.
the class TaxonHandler method get.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Taxa get() {
Map<String, String> id_label = new HashMap<String, String>();
String sparql = "select distinct ?taxon where { ?model <" + BlazegraphOntologyManager.in_taxon_uri + "> ?taxon }";
TupleQueryResult result;
try {
result = (TupleQueryResult) m3.executeSPARQLQuery(sparql, 1000);
while (result.hasNext()) {
BindingSet bs = result.next();
String taxon = bs.getBinding("taxon").getValue().stringValue();
String label = m3.getGolego_repo().getLabel(taxon);
String tcurie = taxon.replace("http://purl.obolibrary.org/obo/NCBITaxon_", "NCBITaxon:");
id_label.put(tcurie, label);
}
} catch (MalformedQueryException | QueryEvaluationException | RepositoryException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new Taxa(id_label);
}
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();
}
use of org.openrdf.query.MalformedQueryException in project gocd by gocd.
the class SesameGraph method validate.
public void validate(String arq) {
try {
Query query = conn.prepareQuery(QueryLanguage.SPARQL, arq);
contextualize(query);
} catch (UnsupportedSPARQLStatementException e) {
throw e;
} catch (MalformedQueryException e) {
throw new MalformedSPARQLException(e);
} catch (Exception e) {
throw new ShineRuntimeException(e);
}
}
Aggregations