| 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 "COWLFileOWLlinkTypesRequestGenerator.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
COWLFileOWLlinkTypesRequestGenerator::COWLFileOWLlinkTypesRequestGenerator(CConfiguration *config) |
| 31 |
: CLogIdentifier("::Konclude::Test::OWLFileOWLlinkIndividualifyRequestGenerator",this),mConfig(config) { |
| 32 |
|
| 33 |
|
| 34 |
mReasonerTestKBName = "http://www.konclude.com/evaluation-testsuite/test-ontology"; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
COWLFileOWLlinkTypesRequestGenerator::~COWLFileOWLlinkTypesRequestGenerator() { |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
QDomDocument COWLFileOWLlinkTypesRequestGenerator::createRequestDocument() { |
| 44 |
QDomDocument document; |
| 45 |
|
| 46 |
QString owllinkNS = "http://www.owllink.org/owllink#"; |
| 47 |
QString owlNS = "http://www.w3.org/2002/07/owl#"; |
| 48 |
|
| 49 |
QDomElement rootNode; |
| 50 |
|
| 51 |
rootNode = document.createElementNS(owllinkNS,"RequestMessage"); |
| 52 |
rootNode.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance","xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd"); |
| 53 |
rootNode.setAttribute("xmlns:owl",owlNS); |
| 54 |
document.appendChild(rootNode); |
| 55 |
|
| 56 |
return document; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
QSet<QString> COWLFileOWLlinkTypesRequestGenerator::loadIndividualEntities(const QString& requestFileName) { |
| 61 |
|
| 62 |
CConcreteOntologyEntityCollectorBuilder entityGenBuilder; |
| 63 |
COWL2QtXMLOntologyStreamParser* owl2Parser = new COWL2QtXMLOntologyStreamParser(&entityGenBuilder); |
| 64 |
owl2Parser->parseOntology(requestFileName); |
| 65 |
delete owl2Parser; |
| 66 |
|
| 67 |
QSet<QString> individualNameSet = *entityGenBuilder.getIndividualNameSet(); |
| 68 |
return individualNameSet; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
bool COWLFileOWLlinkTypesRequestGenerator::generateOWLlinkTypesRequests(const QString& requestFileName, const QString& responseFileName, cint64 testCount) { |
| 74 |
bool generatedTestfiles = false; |
| 75 |
QSet<QString> individualNameTestingSet; |
| 76 |
QSet<QString> individualNameSet = loadIndividualEntities(requestFileName); |
| 77 |
cint64 maxTestIndividualCount = qMin(testCount,(cint64)individualNameSet.count()); |
| 78 |
|
| 79 |
cint64 slashIndex = qMax(requestFileName.lastIndexOf("/"),requestFileName.lastIndexOf("\\")); |
| 80 |
QString fileNameString = requestFileName; |
| 81 |
if (slashIndex >= 0) { |
| 82 |
fileNameString = requestFileName.mid(slashIndex+1); |
| 83 |
} |
| 84 |
uint randInitNumber = 0; |
| 85 |
for (QString::const_iterator it = requestFileName.constBegin(), itEnd = requestFileName.constEnd(); it != itEnd; ++it) { |
| 86 |
QChar nameChar(*it); |
| 87 |
randInitNumber += nameChar.unicode(); |
| 88 |
} |
| 89 |
qsrand(randInitNumber); |
| 90 |
|
| 91 |
for (cint64 testNr = 0; testNr < maxTestIndividualCount; ++testNr) { |
| 92 |
cint64 nextTextIndividualNr = qrand() % individualNameSet.count(); |
| 93 |
QSet<QString>::iterator individualNameIt = individualNameSet.begin(); |
| 94 |
for (cint64 i = 0; i < nextTextIndividualNr; ++i) { |
| 95 |
++individualNameIt; |
| 96 |
} |
| 97 |
QString nextTestIndividualName = *individualNameIt; |
| 98 |
cint64 nextIndividualSwitchCount = individualNameSet.count(); |
| 99 |
while (individualNameTestingSet.contains(nextTestIndividualName) && nextIndividualSwitchCount > 0) { |
| 100 |
++individualNameIt; |
| 101 |
++nextTextIndividualNr; |
| 102 |
if (individualNameIt == individualNameSet.end()) { |
| 103 |
individualNameIt == individualNameSet.begin(); |
| 104 |
nextTextIndividualNr = 0; |
| 105 |
} |
| 106 |
nextTestIndividualName = *individualNameIt; |
| 107 |
--nextIndividualSwitchCount; |
| 108 |
} |
| 109 |
if (individualNameTestingSet.contains(nextTestIndividualName)) { |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
QString individualNameTestFileAppendix = getIndividualNameRequestFileAppendix(nextTestIndividualName,nextTextIndividualNr); |
| 114 |
QString testResponseFileName = QString(responseFileName).arg(individualNameTestFileAppendix); |
| 115 |
|
| 116 |
generatedTestfiles |= generateOWLlinkTypesRequest(requestFileName,testResponseFileName,nextTestIndividualName); |
| 117 |
} |
| 118 |
return generatedTestfiles; |
| 119 |
} |
| 120 |
|
| 121 |
QString COWLFileOWLlinkTypesRequestGenerator::getIndividualNameRequestFileAppendix(const QString& individualName, cint64 testNumber) { |
| 122 |
QString individualNameFileAppendixString = individualName; |
| 123 |
cint64 lastSlash = individualNameFileAppendixString.lastIndexOf("/"); |
| 124 |
if (lastSlash != -1) { |
| 125 |
individualNameFileAppendixString = individualNameFileAppendixString.mid(lastSlash+1); |
| 126 |
} |
| 127 |
cint64 lastGatt = individualNameFileAppendixString.lastIndexOf("#"); |
| 128 |
if (lastGatt != -1) { |
| 129 |
individualNameFileAppendixString = individualNameFileAppendixString.mid(lastGatt+1); |
| 130 |
} |
| 131 |
QString nextIndividualNameFileAppendixString; |
| 132 |
cint64 maxIndividualChar = 15; |
| 133 |
for (QString::const_iterator charIt = individualNameFileAppendixString.constBegin(), charItEnd = individualNameFileAppendixString.constEnd(); charIt != charItEnd; ++charIt) { |
| 134 |
QChar character(*charIt); |
| 135 |
if (character.isLetterOrNumber()) { |
| 136 |
char asciiCharacter = character.toLatin1(); |
| 137 |
if (asciiCharacter != 0 && maxIndividualChar > 0) { |
| 138 |
--maxIndividualChar; |
| 139 |
nextIndividualNameFileAppendixString += asciiCharacter; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
return QString("%1~%2").arg(testNumber).arg(nextIndividualNameFileAppendixString); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
bool COWLFileOWLlinkTypesRequestGenerator::generateOWLlinkTypesRequest(const QString& requestFileName, const QString& responseFileName, const QString& individualName) { |
| 148 |
|
| 149 |
QDomDocument requestDocument = createRequestDocument(); |
| 150 |
QDomElement rootElement = requestDocument.documentElement(); |
| 151 |
|
| 152 |
QDomElement cKBElement = requestDocument.createElement("CreateKB"); |
| 153 |
cKBElement.setAttribute("kb",mReasonerTestKBName); |
| 154 |
rootElement.appendChild(cKBElement); |
| 155 |
|
| 156 |
QDomElement loadElement = requestDocument.createElement("LoadOntologies"); |
| 157 |
loadElement.setAttribute("kb",mReasonerTestKBName); |
| 158 |
QDomElement ontoIRIElement = requestDocument.createElement("OntologyIRI"); |
| 159 |
ontoIRIElement.setAttribute("IRI",QString("file:%1").arg(requestFileName)); |
| 160 |
loadElement.appendChild(ontoIRIElement); |
| 161 |
rootElement.appendChild(loadElement); |
| 162 |
|
| 163 |
QDomElement realizeKBElement = requestDocument.createElement("Realize"); |
| 164 |
realizeKBElement.setAttribute("kb",mReasonerTestKBName); |
| 165 |
rootElement.appendChild(realizeKBElement); |
| 166 |
|
| 167 |
QDomElement typesIndividualElement = requestDocument.createElement("GetFlattenedTypes"); |
| 168 |
typesIndividualElement.setAttribute("kb",mReasonerTestKBName); |
| 169 |
QDomElement individualElement = requestDocument.createElement("owl:NamedIndividual"); |
| 170 |
individualElement.setAttribute("IRI",individualName); |
| 171 |
typesIndividualElement.appendChild(individualElement); |
| 172 |
rootElement.appendChild(typesIndividualElement); |
| 173 |
|
| 174 |
QDomElement releaseKBElement = requestDocument.createElement("ReleaseKB"); |
| 175 |
releaseKBElement.setAttribute("kb",mReasonerTestKBName); |
| 176 |
rootElement.appendChild(releaseKBElement); |
| 177 |
|
| 178 |
QFile outFile(responseFileName); |
| 179 |
if (outFile.open(QIODevice::WriteOnly)) { |
| 180 |
outFile.write(requestDocument.toByteArray()); |
| 181 |
outFile.close(); |
| 182 |
} |
| 183 |
|
| 184 |
LOG(INFO,getLogDomain(),logTr("Generated OWLlink types request '%1' for ontology '%2'.").arg(responseFileName).arg(requestFileName),this); |
| 185 |
|
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
}; // end namespace Test |
| 191 |
|
| 192 |
}; // end namespace Konclude |