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