Search in sources :

Example 6 with MalformedQueryException

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);
    }
}
Also used : OpenRDFQueryHandler(query.OpenRDFQueryHandler) QueryHandler(query.QueryHandler) ArrayList(java.util.ArrayList) ASTQueryContainer(org.openrdf.query.parser.sparql.ast.ASTQueryContainer) TokenMgrError(org.openrdf.query.parser.sparql.ast.TokenMgrError) RenderVisitor(openrdffork.RenderVisitor) StandardizingSPARQLParser(openrdffork.StandardizingSPARQLParser) MalformedQueryException(org.openrdf.query.MalformedQueryException) ParseException(org.openrdf.query.parser.sparql.ast.ParseException) VisitorException(org.openrdf.query.parser.sparql.ast.VisitorException)

Example 7 with MalformedQueryException

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);
    }
}
Also used : NonSimplePropertyPathVisitor(query.statistics.NonSimplePropertyPathVisitor) StandardizingSPARQLParser(openrdffork.StandardizingSPARQLParser) MalformedQueryException(org.openrdf.query.MalformedQueryException)

Example 8 with MalformedQueryException

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);
}
Also used : BindingSet(org.openrdf.query.BindingSet) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) HashMap(java.util.HashMap) MalformedQueryException(org.openrdf.query.MalformedQueryException) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) TupleQueryResult(org.openrdf.query.TupleQueryResult) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with MalformedQueryException

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();
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) MalformedQueryException(org.openrdf.query.MalformedQueryException) UnsupportedQueryTypeException(org.apache.stanbol.entityhub.servicesapi.query.UnsupportedQueryTypeException) TupleQuery(org.openrdf.query.TupleQuery)

Example 10 with MalformedQueryException

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);
    }
}
Also used : ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) SailQuery(org.openrdf.repository.sail.SailQuery) Query(org.openrdf.query.Query) TupleQuery(org.openrdf.query.TupleQuery) BooleanQuery(org.openrdf.query.BooleanQuery) UnsupportedSPARQLStatementException(com.thoughtworks.studios.shine.semweb.UnsupportedSPARQLStatementException) MalformedQueryException(org.openrdf.query.MalformedQueryException) MalformedSPARQLException(com.thoughtworks.studios.shine.semweb.MalformedSPARQLException) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) RepositoryException(org.openrdf.repository.RepositoryException) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) MoreThanOneResultFoundException(com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException) MalformedSPARQLException(com.thoughtworks.studios.shine.semweb.MalformedSPARQLException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) OpenRDFException(org.openrdf.OpenRDFException) NoSuchElementException(java.util.NoSuchElementException) MalformedQueryException(org.openrdf.query.MalformedQueryException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) UnsupportedSPARQLStatementException(com.thoughtworks.studios.shine.semweb.UnsupportedSPARQLStatementException)

Aggregations

MalformedQueryException (org.openrdf.query.MalformedQueryException)16 IOException (java.io.IOException)4 StandardizingSPARQLParser (openrdffork.StandardizingSPARQLParser)4 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)4 RepositoryException (org.openrdf.repository.RepositoryException)4 HashMap (java.util.HashMap)3 TupleQueryResult (org.openrdf.query.TupleQueryResult)3 Bigdata2ASTSPARQLParser (com.bigdata.rdf.sail.sparql.Bigdata2ASTSPARQLParser)2 ASTContainer (com.bigdata.rdf.sparql.ast.ASTContainer)2 RenderVisitor (openrdffork.RenderVisitor)2 TupleQuery (org.openrdf.query.TupleQuery)2 UpdateExecutionException (org.openrdf.query.UpdateExecutionException)2 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)2 ParsedQuery (org.openrdf.query.parser.ParsedQuery)2 BigdataSail (com.bigdata.rdf.sail.BigdataSail)1 BigdataSailRepository (com.bigdata.rdf.sail.BigdataSailRepository)1 BigdataSailRepositoryConnection (com.bigdata.rdf.sail.BigdataSailRepositoryConnection)1 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)1 MalformedSPARQLException (com.thoughtworks.studios.shine.semweb.MalformedSPARQLException)1 MoreThanOneResultFoundException (com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException)1