Search in sources :

Example 1 with RenderVisitor

use of openrdffork.RenderVisitor 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) {
    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("?query=" + 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 {
            line.add(queryHandler.getUserAgent());
        }
        writer.writeRow(line);
    }
}
Also used : OpenRDFQueryHandler(query.OpenRDFQueryHandler) QueryHandler(query.QueryHandler) ArrayList(java.util.ArrayList) RenderVisitor(openrdffork.RenderVisitor) StandardizingSPARQLParser(openrdffork.StandardizingSPARQLParser) MalformedQueryException(org.openrdf.query.MalformedQueryException)

Example 2 with RenderVisitor

use of openrdffork.RenderVisitor in project QueryAnalysis by Wikidata.

the class Test method main.

public static void main(String[] args) {
    Main.loadStandardPrefixes();
    Anonymizer.loadWhitelistDatatypes();
    int worked = 0;
    int failed = 0;
    int failedToParse = 0;
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("/home/adrian/workspace/java/months/exampleQueries/"))) {
        for (Path filePath : directoryStream) {
            if (Files.isRegularFile(filePath)) {
                String queryString = new String(readAllBytes(filePath));
                try {
                    ParsedQuery parsedQuery = new StandardizingSPARQLParser().parseQuery(queryString, OpenRDFQueryHandler.BASE_URI);
                } catch (MalformedQueryException e) {
                    failedToParse++;
                    continue;
                }
                ASTQueryContainer qc;
                try {
                    qc = SyntaxTreeBuilder.parseQuery(queryString);
                } catch (TokenMgrError | ParseException e) {
                    // e.printStackTrace();
                    continue;
                }
                try {
                    StandardizingSPARQLParser.debug(qc);
                    StringEscapesProcessor.process(qc);
                    BaseDeclProcessor.process(qc, OpenRDFQueryHandler.BASE_URI);
                    StandardizingPrefixDeclProcessor.process(qc);
                    StandardizingSPARQLParser.anonymize(qc);
                } catch (MalformedQueryException e) {
                    System.out.println("Failed to debug or anonymize query. " + queryString);
                }
                String renderedQueryString;
                try {
                    renderedQueryString = qc.jjtAccept(new RenderVisitor(), "").toString();
                // System.out.println(renderedQueryString);
                } catch (VisitorException e) {
                    // e.printStackTrace();
                    continue;
                }
                try {
                    ParsedQuery parsedQuery = new StandardizingSPARQLParser().parseQuery(renderedQueryString, OpenRDFQueryHandler.BASE_URI);
                    worked++;
                } catch (MalformedQueryException | ClassCastException e) {
                    failed++;
                    System.out.println("-----------------------------------");
                    System.out.println(filePath);
                    System.out.println(queryString);
                    continue;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Worked: " + worked + " Failed: " + failed + " Failed to Parse: " + failedToParse);
}
Also used : Path(java.nio.file.Path) ParsedQuery(org.openrdf.query.parser.ParsedQuery) RenderVisitor(openrdffork.RenderVisitor) IOException(java.io.IOException) StandardizingSPARQLParser(openrdffork.StandardizingSPARQLParser) MalformedQueryException(org.openrdf.query.MalformedQueryException)

Aggregations

RenderVisitor (openrdffork.RenderVisitor)2 StandardizingSPARQLParser (openrdffork.StandardizingSPARQLParser)2 MalformedQueryException (org.openrdf.query.MalformedQueryException)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 ParsedQuery (org.openrdf.query.parser.ParsedQuery)1 OpenRDFQueryHandler (query.OpenRDFQueryHandler)1 QueryHandler (query.QueryHandler)1