| 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 "CSPARQLResultsComparisonLoader.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Control { |
| 27 |
|
| 28 |
namespace Loader { |
| 29 |
|
| 30 |
|
| 31 |
CSPARQLResultsComparisonLoader::CSPARQLResultsComparisonLoader() : mLogIdentifier("::Konclude::Loader::SPARQLResultComparer", this) { |
| 32 |
loaderConfig = 0; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
CSPARQLResultsComparisonLoader::~CSPARQLResultsComparisonLoader() { |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
CLoader *CSPARQLResultsComparisonLoader::init(CLoaderFactory *loaderFactory, CConfiguration *config) { |
| 42 |
loaderConfig = config; |
| 43 |
|
| 44 |
mReqFileString = CConfigDataReader::readConfigString(config, "Konclude.SPARQL.RequestFile"); |
| 45 |
mCompFileString = CConfigDataReader::readConfigString(config, "Konclude.SPARQL.ComparisonFile"); |
| 46 |
mResFileString = CConfigDataReader::readConfigString(config, "Konclude.SPARQL.ResponseFile"); |
| 47 |
|
| 48 |
mDifferencesLoggingLimit = CConfigDataReader::readConfigInteger(config, "Konclude.SPARQL.Comparison.DifferencesLoggingLimit", 10); |
| 49 |
|
| 50 |
return this; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
CLoader *CSPARQLResultsComparisonLoader::load() { |
| 56 |
if (!mReqFileString.isEmpty() && !mCompFileString.isEmpty()) { |
| 57 |
QString reportingFileLogString; |
| 58 |
if (!mResFileString.isEmpty()) { |
| 59 |
reportingFileLogString = QString(", reporting differences to '%1'").arg(mResFileString); |
| 60 |
} |
| 61 |
LOG(INFO, mLogIdentifier.getLogDomain(), logTr("Comparing request file '%1' with comparison file '%2'%3.").arg(mReqFileString).arg(mCompFileString).arg(reportingFileLogString), this); |
| 62 |
isVariableBindingResultSimilarTo(mReqFileString, mCompFileString, mResFileString); |
| 63 |
} else { |
| 64 |
LOG(ERROR, mLogIdentifier.getLogDomain(), logTr("Request file or comparison file not sprecified."), this); |
| 65 |
} |
| 66 |
return this; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
bool CSPARQLResultsComparisonLoader::isVariableBindingResultSimilarTo(const QString& responseFileString, const QString& otherResponseFileString, const QString& reportingFileName) { |
| 71 |
bool resultSimilar = true; |
| 72 |
QFile responseFile(responseFileString); |
| 73 |
if (responseFile.open(QIODevice::ReadOnly)) { |
| 74 |
QDomDocument document; |
| 75 |
document.setContent(&responseFile, false); |
| 76 |
QDomElement rootEl = document.documentElement(); |
| 77 |
QDomElement resultElement = rootEl.firstChildElement(); |
| 78 |
cint64 currNodeNumber = 0; |
| 79 |
cint64 comparedResultCount = 0; |
| 80 |
cint64 differentResultCount = 0; |
| 81 |
QHash<QString, cint64>* variableNameIndexHash = nullptr; |
| 82 |
if (!resultElement.isNull()) { |
| 83 |
while (!resultElement.isNull()) { |
| 84 |
if (resultElement.nodeName() == "Error") { |
| 85 |
break; |
| 86 |
|
| 87 |
} else if (resultElement.nodeName() == "results") { |
| 88 |
CVariableBindingsAnswersSetResult* varBindingResult = mSPARQLResultParser.parseVariableBindingsSet(&resultElement, variableNameIndexHash, true); |
| 89 |
if (varBindingResult) { |
| 90 |
++comparedResultCount; |
| 91 |
if (!isVariableBindingResultSimilarTo(varBindingResult, currNodeNumber, otherResponseFileString, variableNameIndexHash, reportingFileName)) { |
| 92 |
differentResultCount++; |
| 93 |
resultSimilar = false; |
| 94 |
} |
| 95 |
} else { |
| 96 |
LOG(WARN, mLogIdentifier.getLogDomain(), logTr("Result %1 not parsable in request file.").arg(currNodeNumber + 1), this); |
| 97 |
} |
| 98 |
} |
| 99 |
if (resultElement.nodeName() != "answering-statistics") { |
| 100 |
++currNodeNumber; |
| 101 |
} |
| 102 |
resultElement = resultElement.nextSiblingElement(); |
| 103 |
} |
| 104 |
} |
| 105 |
LOG(INFO, mLogIdentifier.getLogDomain(), logTr("Compared %1 results, found %2 differences.").arg(comparedResultCount).arg(differentResultCount), this); |
| 106 |
|
| 107 |
return resultSimilar; |
| 108 |
} else { |
| 109 |
LOG(ERROR, mLogIdentifier.getLogDomain(), logTr("Request file '%1' cannot be opened.").arg(responseFileString), this); |
| 110 |
} |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
bool CSPARQLResultsComparisonLoader::isVariableBindingResultSimilarTo(CVariableBindingsAnswersSetResult* varBindingResult, cint64 nodeNumber, const QString& otherResponseFileString, QHash<QString, cint64>* variableNameIndexHash, const QString& reportingFileName) { |
| 117 |
bool resultSimilar = true; |
| 118 |
bool resultFound = false; |
| 119 |
QFile responseFile(otherResponseFileString); |
| 120 |
if (responseFile.open(QIODevice::ReadOnly)) { |
| 121 |
QDomDocument document; |
| 122 |
document.setContent(&responseFile, false); |
| 123 |
QDomElement rootEl = document.documentElement(); |
| 124 |
QDomElement resultElement = rootEl.firstChildElement(); |
| 125 |
cint64 currNodeNumber = 0; |
| 126 |
QFile reportingFile(reportingFileName); |
| 127 |
cint64 additionalAnswerCount = 0; |
| 128 |
cint64 missingAnswerCount = 0; |
| 129 |
if (!resultElement.isNull()) { |
| 130 |
while (!resultElement.isNull()) { |
| 131 |
if (resultElement.nodeName() == "Error") { |
| 132 |
break; |
| 133 |
|
| 134 |
} else if (currNodeNumber == nodeNumber && resultElement.nodeName() == "results") { |
| 135 |
CVariableBindingsAnswersSetResult* otherVarBindingResult = mSPARQLResultParser.parseVariableBindingsSet(&resultElement, variableNameIndexHash, true); |
| 136 |
if (otherVarBindingResult) { |
| 137 |
|
| 138 |
if (!varBindingResult || !varBindingResult->isResultEquivalentTo(otherVarBindingResult)) { |
| 139 |
resultSimilar = false; |
| 140 |
|
| 141 |
if (varBindingResult && otherVarBindingResult) { |
| 142 |
CVariableBindingsAnswersResultIterator* it = varBindingResult->getVariableBindingsAnswersIterator(); |
| 143 |
while (it->hasNext()) { |
| 144 |
CVariableBindingsAnswerResult* answerResult = it->getNext(); |
| 145 |
if (!otherVarBindingResult->hasResultVariableBindings(answerResult)) { |
| 146 |
QString differenceMessage = QString("Additional answer for result %1 in request file with bindings: %2.").arg(currNodeNumber + 1).arg(answerResult->getQueryResultString()); |
| 147 |
if (mDifferencesLoggingLimit-- > 0) { |
| 148 |
LOG(WARN, mLogIdentifier.getLogDomain(), differenceMessage, this); |
| 149 |
} |
| 150 |
++additionalAnswerCount; |
| 151 |
if (reportingFile.open(QFile::Append)) { |
| 152 |
reportingFile.write(differenceMessage.toLocal8Bit()); |
| 153 |
reportingFile.close(); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
CVariableBindingsAnswersResultIterator* otherIt = otherVarBindingResult->getVariableBindingsAnswersIterator(); |
| 158 |
while (otherIt->hasNext()) { |
| 159 |
CVariableBindingsAnswerResult* answerResult = otherIt->getNext(); |
| 160 |
if (!varBindingResult->hasResultVariableBindings(answerResult)) { |
| 161 |
QString differenceMessage = QString("Missing answer for result %1 in request file with bindings: %2").arg(currNodeNumber + 1).arg(answerResult->getQueryResultString()); |
| 162 |
if (mDifferencesLoggingLimit-- > 0) { |
| 163 |
LOG(WARN, mLogIdentifier.getLogDomain(), differenceMessage, this); |
| 164 |
} |
| 165 |
missingAnswerCount++; |
| 166 |
if (reportingFile.open(QFile::Append)) { |
| 167 |
reportingFile.write(differenceMessage.toLocal8Bit()); |
| 168 |
reportingFile.close(); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (additionalAnswerCount > 0 || missingAnswerCount > 0) { |
| 176 |
LOG(WARN, mLogIdentifier.getLogDomain(), logTr("Result %1 has %2 additional and %3 missing answers.").arg(nodeNumber + 1).arg(additionalAnswerCount).arg(missingAnswerCount), this); |
| 177 |
} else { |
| 178 |
LOG(INFO, mLogIdentifier.getLogDomain(), logTr("Result %1 seems identical.").arg(nodeNumber + 1), this); |
| 179 |
} |
| 180 |
|
| 181 |
resultFound = true; |
| 182 |
delete otherVarBindingResult; |
| 183 |
|
| 184 |
} else if (varBindingResult) { |
| 185 |
LOG(WARN, mLogIdentifier.getLogDomain(), logTr("Result %1 not found or not parsable in comparison file.").arg(nodeNumber + 1), this); |
| 186 |
resultSimilar = false; |
| 187 |
} |
| 188 |
break; |
| 189 |
|
| 190 |
} else { |
| 191 |
if (resultElement.nodeName() != "answering-statistics") { |
| 192 |
++currNodeNumber; |
| 193 |
} |
| 194 |
if (currNodeNumber > nodeNumber) { |
| 195 |
break; |
| 196 |
} |
| 197 |
resultElement = resultElement.nextSiblingElement(); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (!resultSimilar) { |
| 203 |
LOG(WARN, mLogIdentifier.getLogDomain(), logTr("Result %1 is not identical.").arg(nodeNumber + 1), this); |
| 204 |
} |
| 205 |
|
| 206 |
} else { |
| 207 |
LOG(ERROR, mLogIdentifier.getLogDomain(), logTr("Comparison file '%1' cannot be opened.").arg(otherResponseFileString), this); |
| 208 |
} |
| 209 |
|
| 210 |
if (!resultFound) { |
| 211 |
LOG(WARN, mLogIdentifier.getLogDomain(), logTr("Result %1 not found in comparison file.").arg(nodeNumber + 1), this); |
| 212 |
resultSimilar = false; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
return resultSimilar; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
CLoader *CSPARQLResultsComparisonLoader::exit() { |
| 223 |
return this; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
}; // end namespace Loader |
| 229 |
|
| 230 |
}; // end namespace Control |
| 231 |
|
| 232 |
}; // end namespace Konclude |