| 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 "COWLFileOWLlinkIncrementalAssertionRequestGenerator.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
COWLFileOWLlinkIncrementalAssertionRequestGenerator::COWLFileOWLlinkIncrementalAssertionRequestGenerator(CConfiguration *config) |
| 31 |
: CLogIdentifier("::Konclude::Test::OWLFileOWLlinkIncrementalAssertionRequestGenerator",this),mConfig(config) { |
| 32 |
|
| 33 |
|
| 34 |
mReasonerTestKBName = "http://www.konclude.com/evaluation-testsuite/test-ontology"; |
| 35 |
|
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
COWLFileOWLlinkIncrementalAssertionRequestGenerator::~COWLFileOWLlinkIncrementalAssertionRequestGenerator() { |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
QDomDocument COWLFileOWLlinkIncrementalAssertionRequestGenerator::createRequestDocument() { |
| 46 |
QDomDocument document; |
| 47 |
|
| 48 |
QString owllinkNS = "http://www.owllink.org/owllink#"; |
| 49 |
QString owlNS = "http://www.w3.org/2002/07/owl#"; |
| 50 |
|
| 51 |
QDomElement rootNode; |
| 52 |
|
| 53 |
rootNode = document.createElementNS(owllinkNS,"RequestMessage"); |
| 54 |
//rootNode.setAttributeNS("xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd"); |
| 55 |
rootNode.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance","xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd"); |
| 56 |
rootNode.setAttribute("xmlns:owl",owlNS); |
| 57 |
document.appendChild(rootNode); |
| 58 |
|
| 59 |
return document; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
QList<QDomElement> COWLFileOWLlinkIncrementalAssertionRequestGenerator::loadOntologyAssertions(const QString& requestFileName) { |
| 64 |
QList<QDomElement> assertionList; |
| 65 |
QFile ontologyFile(requestFileName); |
| 66 |
QDomDocument ontologyDocument; |
| 67 |
if (ontologyDocument.setContent(&ontologyFile,true)) { |
| 68 |
QDomElement rootElement = ontologyDocument.documentElement(); |
| 69 |
QDomElement nextElement = rootElement.firstChildElement(); |
| 70 |
while (!nextElement.isNull()) { |
| 71 |
QString tagName = nextElement.tagName(); |
| 72 |
bool isAssertionNode = false; |
| 73 |
if (tagName == "ClassAssertion" || tagName == "ObjectPropertyAssertion" || tagName == "SameIndividual" || tagName == "DifferentIndividuals") { |
| 74 |
isAssertionNode = true; |
| 75 |
} |
| 76 |
else if (tagName == "DataPropertyAssertion" || tagName == "NegativeDataPropertyAssertion" || tagName == "NegativeObjectPropertyAssertion") { |
| 77 |
isAssertionNode = true; |
| 78 |
} |
| 79 |
if (isAssertionNode) { |
| 80 |
assertionList.append(nextElement); |
| 81 |
} |
| 82 |
nextElement = nextElement.nextSiblingElement(); |
| 83 |
} |
| 84 |
} |
| 85 |
return assertionList; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
bool COWLFileOWLlinkIncrementalAssertionRequestGenerator::generateOWLlinkIncrementalAssertionRequests(const QString& requestFileName, const QString& responseFileName, double removeAssertionPercentage, double addAssertionPercentage, cint64 testCount) { |
| 94 |
bool generatedTestfiles = false; |
| 95 |
QList<QDomElement> assertionList = loadOntologyAssertions(requestFileName); |
| 96 |
cint64 assertionCount = assertionList.count(); |
| 97 |
|
| 98 |
|
| 99 |
cint64 removeAssertionCount = assertionCount*removeAssertionPercentage; |
| 100 |
cint64 addAssertionCount = assertionCount*addAssertionPercentage; |
| 101 |
|
| 102 |
addAssertionCount = qMax((cint64)1,addAssertionCount); |
| 103 |
removeAssertionCount = qMax((cint64)1,removeAssertionCount); |
| 104 |
|
| 105 |
if (assertionList.count() > removeAssertionCount + addAssertionCount) { |
| 106 |
|
| 107 |
cint64 slashIndex = qMax(requestFileName.lastIndexOf("/"),requestFileName.lastIndexOf("\\")); |
| 108 |
QString fileNameString = requestFileName; |
| 109 |
if (slashIndex >= 0) { |
| 110 |
fileNameString = requestFileName.mid(slashIndex+1); |
| 111 |
} |
| 112 |
uint randInitNumber = 0; |
| 113 |
for (QString::const_iterator it = requestFileName.constBegin(), itEnd = requestFileName.constEnd(); it != itEnd; ++it) { |
| 114 |
QChar nameChar(*it); |
| 115 |
randInitNumber += nameChar.unicode(); |
| 116 |
} |
| 117 |
qsrand(randInitNumber); |
| 118 |
|
| 119 |
for (cint64 testNr = 0; testNr < testCount; ++testNr) { |
| 120 |
|
| 121 |
cint64 removalNrHashCode = 0; |
| 122 |
cint64 additionNrHashCode = 0; |
| 123 |
|
| 124 |
QList<QDomElement> removalAssertionList; |
| 125 |
QList<QDomElement> additionAssertionList; |
| 126 |
QSet<cint64> assertionNrSet; |
| 127 |
for (cint64 remNr = 0; remNr < removeAssertionCount; ++remNr) { |
| 128 |
cint64 nextAssertionNr = (double)qrand() / (double)RAND_MAX * (double)(assertionCount-1); |
| 129 |
removalNrHashCode += nextAssertionNr; |
| 130 |
if (!assertionNrSet.contains(nextAssertionNr)) { |
| 131 |
assertionNrSet.insert(nextAssertionNr); |
| 132 |
QDomElement nextAssertionNode = assertionList.at(nextAssertionNr); |
| 133 |
removalAssertionList.append(nextAssertionNode); |
| 134 |
} |
| 135 |
} |
| 136 |
for (cint64 addNr = 0; addNr < addAssertionCount; ++addNr) { |
| 137 |
cint64 nextAssertionNr = (double)qrand() / (double)RAND_MAX * (double)(assertionCount-1); |
| 138 |
additionNrHashCode += nextAssertionNr; |
| 139 |
if (!assertionNrSet.contains(nextAssertionNr)) { |
| 140 |
assertionNrSet.insert(nextAssertionNr); |
| 141 |
QDomElement nextAssertionNode = assertionList.at(nextAssertionNr); |
| 142 |
additionAssertionList.append(nextAssertionNode); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
QString addRemTestFileAppendix = QString("%1~#%2-%3~#%4").arg(addAssertionCount).arg(additionNrHashCode).arg(removeAssertionCount).arg(removalNrHashCode); |
| 147 |
QString testResponseFileName = QString(responseFileName).arg(addRemTestFileAppendix); |
| 148 |
|
| 149 |
generatedTestfiles |= generateOWLlinkIncrementalAssertionConsistencyRequest(requestFileName,testResponseFileName,removalAssertionList,additionAssertionList); |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
return generatedTestfiles; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
bool COWLFileOWLlinkIncrementalAssertionRequestGenerator::generateOWLlinkIncrementalAssertionRequests(const QString& requestFileName, const QString& responseFileName, cint64 removeAssertionCount, cint64 addAssertionCount, cint64 testCount) { |
| 160 |
bool generatedTestfiles = false; |
| 161 |
QList<QDomElement> assertionList = loadOntologyAssertions(requestFileName); |
| 162 |
cint64 assertionCount = assertionList.count(); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
if (assertionList.count() > removeAssertionCount + addAssertionCount) { |
| 167 |
|
| 168 |
for (cint64 testNr = 0; testNr < testCount; ++testNr) { |
| 169 |
|
| 170 |
cint64 removalNrHashCode = 0; |
| 171 |
cint64 additionNrHashCode = 0; |
| 172 |
|
| 173 |
QList<QDomElement> removalAssertionList; |
| 174 |
QList<QDomElement> additionAssertionList; |
| 175 |
QSet<cint64> assertionNrSet; |
| 176 |
for (cint64 remNr = 0; remNr < removeAssertionCount; ++remNr) { |
| 177 |
cint64 nextAssertionNr = (double)qrand() / (double)RAND_MAX * (double)(assertionCount-1); |
| 178 |
removalNrHashCode += nextAssertionNr; |
| 179 |
if (!assertionNrSet.contains(nextAssertionNr)) { |
| 180 |
assertionNrSet.insert(nextAssertionNr); |
| 181 |
QDomElement nextAssertionNode = assertionList.at(nextAssertionNr); |
| 182 |
removalAssertionList.append(nextAssertionNode); |
| 183 |
} |
| 184 |
} |
| 185 |
for (cint64 addNr = 0; addNr < addAssertionCount; ++addNr) { |
| 186 |
cint64 nextAssertionNr = (double)qrand() / (double)RAND_MAX * (double)(assertionCount-1); |
| 187 |
additionNrHashCode += nextAssertionNr; |
| 188 |
if (!assertionNrSet.contains(nextAssertionNr)) { |
| 189 |
assertionNrSet.insert(nextAssertionNr); |
| 190 |
QDomElement nextAssertionNode = assertionList.at(nextAssertionNr); |
| 191 |
additionAssertionList.append(nextAssertionNode); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
QString addRemTestFileAppendix = QString("%1~#%2-%3~#%4").arg(addAssertionCount).arg(additionNrHashCode).arg(removeAssertionCount).arg(removalNrHashCode); |
| 196 |
QString testResponseFileName = QString(responseFileName).arg(addRemTestFileAppendix); |
| 197 |
|
| 198 |
generatedTestfiles |= generateOWLlinkIncrementalAssertionConsistencyRequest(requestFileName,testResponseFileName,removalAssertionList,additionAssertionList); |
| 199 |
} |
| 200 |
|
| 201 |
} |
| 202 |
return generatedTestfiles; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
bool COWLFileOWLlinkIncrementalAssertionRequestGenerator::generateOWLlinkIncrementalAssertionConsistencyRequest(const QString& requestFileName, const QString& responseFileName, QList<QDomElement>& removalAssertionList, QList<QDomElement>& additionAssertionList) { |
| 207 |
|
| 208 |
QDomDocument requestDocument = createRequestDocument(); |
| 209 |
QDomElement rootElement = requestDocument.documentElement(); |
| 210 |
|
| 211 |
QDomElement cKBElement = requestDocument.createElement("CreateKB"); |
| 212 |
cKBElement.setAttribute("kb",mReasonerTestKBName); |
| 213 |
rootElement.appendChild(cKBElement); |
| 214 |
|
| 215 |
QDomElement loadElement = requestDocument.createElement("LoadOntologies"); |
| 216 |
loadElement.setAttribute("kb",mReasonerTestKBName); |
| 217 |
QDomElement ontoIRIElement = requestDocument.createElement("OntologyIRI"); |
| 218 |
ontoIRIElement.setAttribute("IRI",QString("file:%1").arg(requestFileName)); |
| 219 |
loadElement.appendChild(ontoIRIElement); |
| 220 |
rootElement.appendChild(loadElement); |
| 221 |
|
| 222 |
|
| 223 |
QDomElement retractAdditionsElement = requestDocument.createElement("Retract"); |
| 224 |
retractAdditionsElement.setAttribute("kb",mReasonerTestKBName); |
| 225 |
for (QList<QDomElement>::const_iterator addIt = additionAssertionList.constBegin(), addItEnd = additionAssertionList.constEnd(); addIt != addItEnd; ++addIt) { |
| 226 |
QDomElement assertionElement(*addIt); |
| 227 |
QDomNode importedAssertionElement = requestDocument.importNode(assertionElement,true); |
| 228 |
retractAdditionsElement.appendChild(importedAssertionElement); |
| 229 |
} |
| 230 |
rootElement.appendChild(retractAdditionsElement); |
| 231 |
|
| 232 |
QDomElement reasonElement = getFirstReasoningRequestNode(requestDocument); |
| 233 |
rootElement.appendChild(reasonElement); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
QDomElement tellAdditionsElement = requestDocument.createElement("Tell"); |
| 239 |
tellAdditionsElement.setAttribute("kb",mReasonerTestKBName); |
| 240 |
for (QList<QDomElement>::const_iterator addIt = additionAssertionList.constBegin(), addItEnd = additionAssertionList.constEnd(); addIt != addItEnd; ++addIt) { |
| 241 |
QDomElement assertionElement(*addIt); |
| 242 |
QDomNode importedAssertionElement = requestDocument.importNode(assertionElement,true); |
| 243 |
tellAdditionsElement.appendChild(importedAssertionElement); |
| 244 |
} |
| 245 |
rootElement.appendChild(tellAdditionsElement); |
| 246 |
|
| 247 |
QDomElement retractRemovalElement = requestDocument.createElement("Retract"); |
| 248 |
retractRemovalElement.setAttribute("kb",mReasonerTestKBName); |
| 249 |
for (QList<QDomElement>::const_iterator remIt = removalAssertionList.constBegin(), remItEnd = removalAssertionList.constEnd(); remIt != remItEnd; ++remIt) { |
| 250 |
QDomElement assertionElement(*remIt); |
| 251 |
QDomNode importedAssertionElement = requestDocument.importNode(assertionElement,true); |
| 252 |
retractRemovalElement.appendChild(importedAssertionElement); |
| 253 |
} |
| 254 |
rootElement.appendChild(retractRemovalElement); |
| 255 |
|
| 256 |
|
| 257 |
QDomElement incReasonElement = getSecondReasoningRequestNode(requestDocument); |
| 258 |
rootElement.appendChild(incReasonElement); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
QDomElement releaseKBElement = requestDocument.createElement("ReleaseKB"); |
| 263 |
releaseKBElement.setAttribute("kb",mReasonerTestKBName); |
| 264 |
rootElement.appendChild(releaseKBElement); |
| 265 |
|
| 266 |
QFile outFile(responseFileName); |
| 267 |
if (outFile.open(QIODevice::WriteOnly)) { |
| 268 |
outFile.write(requestDocument.toByteArray()); |
| 269 |
outFile.close(); |
| 270 |
} |
| 271 |
|
| 272 |
LOG(INFO,getLogDomain(),logTr("Generated OWLlink incremental assertion %3 request '%1' for ontology '%2'.").arg(responseFileName).arg(requestFileName).arg(mRequestTypeName),this); |
| 273 |
|
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
}; // end namespace Test |
| 279 |
|
| 280 |
}; // end namespace Konclude |