| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CReasonerEvaluationEvaluatorSPARQL.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
|
| 28 |
namespace Evaluation { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
CReasonerEvaluationEvaluatorSPARQL::CReasonerEvaluationEvaluatorSPARQL() { |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
CReasonerEvaluationEvaluatorSPARQL::~CReasonerEvaluationEvaluatorSPARQL() { |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
bool CReasonerEvaluationEvaluatorSPARQL::evaluateResults(CReasonerEvaluationRequestResult* requestResult, CReasonerEvaluationTerminationResult* terminationResult, const QString& inputFileString, const QString& responseFileString, CConfiguration* config) { |
| 41 |
try { |
| 42 |
evaluateResults(requestResult, terminationResult, inputFileString, responseFileString, config, true, true); |
| 43 |
} catch (...) { |
| 44 |
LOG(WARN, "::Konclude::Test::ReasonerEvaluationEvaluatorSPARQL", logTr("Writing evaluation result to '%1' failed, trying to write it without reasoner outputs.").arg(responseFileString), this); |
| 45 |
try { |
| 46 |
evaluateResults(requestResult, terminationResult, inputFileString, responseFileString, config, true, false); |
| 47 |
} catch (...) { |
| 48 |
LOG(WARN, "::Konclude::Test::ReasonerEvaluationEvaluatorSPARQL", logTr("Writing evaluation result to '%1' failed, trying to write it without full responses.").arg(responseFileString), this); |
| 49 |
try { |
| 50 |
evaluateResults(requestResult, terminationResult, inputFileString, responseFileString, config, false, true); |
| 51 |
} catch (...) { |
| 52 |
LOG(WARN, "::Konclude::Test::ReasonerEvaluationEvaluatorSPARQL", logTr("Writing evaluation result to '%1' failed, trying to write it without full responses and without reasoner outputs.").arg(responseFileString), this); |
| 53 |
try { |
| 54 |
evaluateResults(requestResult, terminationResult, inputFileString, responseFileString, config, false, false); |
| 55 |
} catch (...) { |
| 56 |
LOG(ERROR, "::Konclude::Test::ReasonerEvaluationEvaluatorSPARQL", logTr("Writing evaluation result to '%1' failed.").arg(responseFileString), this); |
| 57 |
return false; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
bool CReasonerEvaluationEvaluatorSPARQL::evaluateResults(CReasonerEvaluationRequestResult* requestResult, CReasonerEvaluationTerminationResult* terminationResult, const QString& inputFileString, const QString& responseFileString, CConfiguration* config, bool includeResponses, bool includeOutputs) { |
| 66 |
|
| 67 |
QList<CReasonerEvaluationRequestResponse*>* responseList = requestResult->getResponseList(); |
| 68 |
QDomDocument document = createResponseDocument(); |
| 69 |
QDomElement responseNode = document.documentElement(); |
| 70 |
responseNode.setAttribute("response-time",requestResult->getResponseTime()); |
| 71 |
responseNode.setAttribute("test",inputFileString); |
| 72 |
if (!requestResult->getResponsedInTime()) { |
| 73 |
cint64 timeoutCount = 0; |
| 74 |
for (CReasonerEvaluationRequestResponse* response : *responseList) { |
| 75 |
if (response->isTimedOut()) { |
| 76 |
timeoutCount++; |
| 77 |
} |
| 78 |
} |
| 79 |
responseNode.setAttribute("timeout", 1); |
| 80 |
if (timeoutCount > 1) { |
| 81 |
responseNode.setAttribute("timeout-restart-count", timeoutCount); |
| 82 |
} |
| 83 |
} else { |
| 84 |
responseNode.setAttribute("timeout",0); |
| 85 |
} |
| 86 |
|
| 87 |
cint64 maxMemoryUsage = terminationResult->getMaxMemoryUsage(); |
| 88 |
if (maxMemoryUsage > 0) { |
| 89 |
responseNode.setAttribute("maxMemoryUsage", maxMemoryUsage); |
| 90 |
} |
| 91 |
|
| 92 |
QDateTime dateTime = QDateTime::currentDateTime(); |
| 93 |
responseNode.appendChild(document.createComment(QString("Executed date: %1").arg(dateTime.toString()))); |
| 94 |
|
| 95 |
if (terminationResult->hasProcessError()) { |
| 96 |
responseNode.appendChild(document.createComment(QString("Reasoner error: %1").arg(terminationResult->getErrorText()))); |
| 97 |
} |
| 98 |
|
| 99 |
QString output = terminationResult->getStandardOutputText().replace("--", "##"); |
| 100 |
QString errorOutput = terminationResult->getErrorOutputText().replace("--", "##"); |
| 101 |
if (includeOutputs) { |
| 102 |
if (!output.isEmpty()) { |
| 103 |
responseNode.appendChild(document.createComment(QString("Reasoner output:\r\n%1").arg(output))); |
| 104 |
} |
| 105 |
if (!errorOutput.isEmpty()) { |
| 106 |
responseNode.appendChild(document.createComment(QString("Reasoner error output:\r\n%1").arg(errorOutput))); |
| 107 |
} |
| 108 |
} else { |
| 109 |
responseNode.appendChild(document.createComment(QString("Reasoner (error) output with length (%1) %2 omitted due to size limitations").arg(output.length()).arg(errorOutput.length()))); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
foreach (CReasonerEvaluationRequestResponse* response, *responseList) { |
| 114 |
CReasonerEvaluationRequestResponseSPARQL* sparqlResponse = (CReasonerEvaluationRequestResponseSPARQL*)response; |
| 115 |
if (response->isTimedOut()) { |
| 116 |
QDomElement errorElm = document.createElement("Error"); |
| 117 |
errorElm.setAttribute("timeout",1); |
| 118 |
cint64 responseTime = response->getResponseTime(); |
| 119 |
errorElm.setAttribute("response-time",responseTime); |
| 120 |
errorElm.setAttribute("error","timeout"); |
| 121 |
errorElm.setAttribute("request-command", sparqlResponse->getOperationName()); |
| 122 |
responseNode.appendChild(errorElm); |
| 123 |
} else if (response->hasResponseError()) { |
| 124 |
QDomElement errorElm = document.createElement("Error"); |
| 125 |
cint64 responseTime = response->getResponseTime(); |
| 126 |
errorElm.setAttribute("response-time",responseTime); |
| 127 |
if (!response->getResponseErrorString().isEmpty()) { |
| 128 |
errorElm.setAttribute("error", response->getResponseErrorString()); |
| 129 |
} else { |
| 130 |
errorElm.setAttribute("error", "unknown error"); |
| 131 |
} |
| 132 |
errorElm.setAttribute("request-command", sparqlResponse->getOperationName()); |
| 133 |
responseNode.appendChild(errorElm); |
| 134 |
} else if (response->hasEvaluationError()) { |
| 135 |
QDomElement errorElm = document.createElement("EvaluationError"); |
| 136 |
cint64 responseTime = response->getResponseTime(); |
| 137 |
errorElm.setAttribute("response-time", responseTime); |
| 138 |
QString evalErrorString = response->getEvaluationErrorString(); |
| 139 |
if (!evalErrorString.isEmpty()) { |
| 140 |
errorElm.setAttribute("evaluation-error", QString("Could not process response due to '%1'").arg(response->getEvaluationErrorString())); |
| 141 |
} else { |
| 142 |
errorElm.setAttribute("evaluation-error", "Could not process response"); |
| 143 |
} |
| 144 |
errorElm.setAttribute("request-command", sparqlResponse->getOperationName()); |
| 145 |
responseNode.appendChild(errorElm); |
| 146 |
} else { |
| 147 |
QList<QDomElement> domElms = sparqlResponse->getResponseNodes(); |
| 148 |
if (includeResponses || domElms.isEmpty()) { |
| 149 |
if (domElms.isEmpty()) { |
| 150 |
domElms.append(document.createElement("OK")); |
| 151 |
} |
| 152 |
bool first = true; |
| 153 |
for (const QDomElement& domElm : domElms) { |
| 154 |
cint64 responseTime = response->getResponseTime(); |
| 155 |
QDomNode importedNode = document.importNode(domElm, true); |
| 156 |
QDomElement impDomElm = importedNode.toElement(); |
| 157 |
if (first) { |
| 158 |
first = false; |
| 159 |
impDomElm.setAttribute("response-time", responseTime); |
| 160 |
impDomElm.setAttribute("request-command", sparqlResponse->getOperationName()); |
| 161 |
} |
| 162 |
responseNode.appendChild(impDomElm); |
| 163 |
} |
| 164 |
} else { |
| 165 |
QDomElement errorElm = document.createElement("OK"); |
| 166 |
errorElm.setAttribute("evaluation-error", "Response omitted due to size limitations"); |
| 167 |
cint64 responseTime = response->getResponseTime(); |
| 168 |
errorElm.setAttribute("response-time", responseTime); |
| 169 |
errorElm.setAttribute("request-command", sparqlResponse->getOperationName()); |
| 170 |
responseNode.appendChild(errorElm); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
QFile file(responseFileString); |
| 176 |
if (file.open(QIODevice::WriteOnly)) { |
| 177 |
QTextStream fileTextStream(&file); |
| 178 |
document.save(fileTextStream, 1); |
| 179 |
file.close(); |
| 180 |
} |
| 181 |
|
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
QDomDocument CReasonerEvaluationEvaluatorSPARQL::createResponseDocument() { |
| 186 |
QDomDocument document; |
| 187 |
|
| 188 |
QString sparqlNS = "http://www.w3.org/2005/sparql-results#"; |
| 189 |
QString owlNS = "http://www.w3.org/2002/07/owl#"; |
| 190 |
|
| 191 |
QDomProcessingInstruction procInstHeader = document.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); |
| 192 |
document.appendChild(procInstHeader); |
| 193 |
QDomElement rootNode; |
| 194 |
rootNode = document.createElementNS(sparqlNS, "sparql"); |
| 195 |
rootNode.setAttribute("xml:base", sparqlNS); |
| 196 |
rootNode.setAttribute("xmlns:owl", owlNS); |
| 197 |
rootNode.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema#"); |
| 198 |
document.appendChild(rootNode); |
| 199 |
|
| 200 |
return document; |
| 201 |
} |
| 202 |
|
| 203 |
}; // end namespace Evaluation |
| 204 |
|
| 205 |
}; // end namespace Test |
| 206 |
|
| 207 |
}; // end namespace Konclude |