use of query.QueryHandler 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);
}
}
use of query.QueryHandler in project QueryAnalysis by Wikidata.
the class OutputHandlerTSV method writeLine.
/**
* Takes a query and the additional information from input and writes
* the available data to the active .tsv.
*
* @param queryToAnalyze The query that should be analyzed and written.
* @param validityStatus The validity status which was the result of the decoding process of the URI
* @param userAgent The user agent the query was being executed by.
* @param currentLine The line from which the data to be written originates.
* @param currentFile The file from which the data to be written originates.
*/
@Override
public final void writeLine(String queryToAnalyze, QueryHandler.Validity validityStatus, String userAgent, String timeStamp, long currentLine, int day, String currentFile) {
QueryHandler queryHandler = cache.getQueryHandler(validityStatus, queryToAnalyze, currentLine, day, userAgent, currentFile, threadNumber, queryHandlerFactory);
// the order in which fields are being written to this list is important - it needs to be the same as the one for the header above!
List<Object> line = new ArrayList<>();
line.add(queryHandler.getValidityStatus());
if (queryHandler.isFirst()) {
line.add("FIRST");
} else {
line.add("COPY");
}
line.add(queryHandler.getUniqeId());
line.add(queryHandler.getOriginalId());
line.add(queryHandler.getSourceCategory());
line.add(queryHandler.getToolName());
line.add(queryHandler.getToolVersion());
if (Main.withBots || queryHandler.getToolName().equals("UNKNOWN") || queryHandler.getToolName().equals("USER")) {
line.add(queryHandler.getExampleQueryStringMatch());
line.add(queryHandler.getExampleQueryTupleMatch());
line.add(queryHandler.getStringLength());
line.add(queryHandler.getQuerySize());
line.add(queryHandler.getVariableCountHead());
line.add(queryHandler.getVariableCountPattern());
line.add(queryHandler.getTripleCountWithService());
line.add(queryHandler.getTripleCountWithoutService());
line.add(queryHandler.getQueryType());
line.add(queryHandler.getSimpleOrComplex());
line.add(queryHandler.getNonSimplePropertyPathsString());
line.add(queryHandler.getqIDString());
line.add(queryHandler.getpIDString());
line.add(queryHandler.getCategoriesString());
line.add(queryHandler.getCoordinatesString());
// add all sparqlStatisticNodes
Map<String, Integer> sparqlStatistics = queryHandler.getSparqlStatistics();
String sparqlStatisticsLine = "";
for (Map.Entry<String, Integer> sparqlStatisticFeature : sparqlStatistics.entrySet()) {
if (sparqlStatisticFeature.getValue() != 0) {
sparqlStatisticsLine += sparqlStatisticFeature.getKey() + ", ";
}
}
line.add(sparqlStatisticsLine);
line.add(queryHandler.getPrimaryLanguage());
line.add(queryHandler.getServiceCallsString());
} else {
for (int i = 0; i < 13; i++) {
line.add(-1);
}
}
line.add(currentFile + "_" + currentLine);
writer.writeRow(line);
}
Aggregations