| 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 "CReasonerEvaluationOntologyExtractionLoader.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Control { |
| 27 |
|
| 28 |
namespace Loader { |
| 29 |
|
| 30 |
|
| 31 |
CReasonerEvaluationOntologyExtractionLoader::CReasonerEvaluationOntologyExtractionLoader() { |
| 32 |
loaderConfig = nullptr; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
CReasonerEvaluationOntologyExtractionLoader::~CReasonerEvaluationOntologyExtractionLoader() { |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
CLoader *CReasonerEvaluationOntologyExtractionLoader::init(CLoaderFactory *loaderFactory, CConfiguration *config) { |
| 42 |
loaderConfig = config; |
| 43 |
mInputDir = CConfigDataReader::readConfigString(config,"Konclude.Test.Generator.InputDirectory"); |
| 44 |
if (!mInputDir.endsWith("/")) { |
| 45 |
mInputDir += QString("/"); |
| 46 |
} |
| 47 |
mOutputDir = CConfigDataReader::readConfigString(config,"Konclude.Test.Generator.OutputDirectory"); |
| 48 |
if (!mOutputDir.endsWith("/")) { |
| 49 |
mOutputDir += QString("/"); |
| 50 |
} |
| 51 |
|
| 52 |
mOntologyStatisticsDir = CConfigDataReader::readConfigString(config,"Konclude.Test.Generator.OntologyStatisticsDirectory"); |
| 53 |
if (!mOntologyStatisticsDir.endsWith("/")) { |
| 54 |
mOntologyStatisticsDir += QString("/"); |
| 55 |
} |
| 56 |
|
| 57 |
mFiltering = new CReasonerEvaluationFiltering(mOntologyStatisticsDir); |
| 58 |
|
| 59 |
mFilterFile = CConfigDataReader::readConfigString(config,"Konclude.Test.Generator.FilterFile"); |
| 60 |
loadFilterConditions(mFilterFile); |
| 61 |
|
| 62 |
QDir newOntoFileDir("."); |
| 63 |
newOntoFileDir.mkpath(mOutputDir); |
| 64 |
|
| 65 |
mExtractedOntStatOutputFileString = mOutputDir+"ExtractedOntologies.csv"; |
| 66 |
|
| 67 |
mCollectExtractedOntologyStatistics = true; |
| 68 |
mCopyExtractedOntologyToOutput = true; |
| 69 |
|
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
bool CReasonerEvaluationOntologyExtractionLoader::loadFilterConditions(const QString& fileNameString) { |
| 75 |
return mFiltering->loadFilterFromFile(fileNameString); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
bool CReasonerEvaluationOntologyExtractionLoader::satisfiesExtractionCondition(const QString& testFileNameString, const QString& testFilePathString, const QString& subDirPathString, const QStringList& statisticList) { |
| 80 |
return mFiltering->testSatisfiesFilter(testFileNameString,testFilePathString,subDirPathString,statisticList); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
CLoader* CReasonerEvaluationOntologyExtractionLoader::addExtractedOntologyStatistics(const QString& testFileNameString, const QString& testFilePathString, const QString& subDirPathString, const QStringList& statisticList) { |
| 85 |
QHash<QString,QString>* ontStatHash = new QHash<QString,QString>(); |
| 86 |
ontStatHash->insert("Path",subDirPathString); |
| 87 |
ontStatHash->insert("Ontology",testFileNameString); |
| 88 |
foreach (const QString& statisticString, statisticList) { |
| 89 |
QStringList statDataList(statisticString.split("\t")); |
| 90 |
if (statDataList.count() == 2) { |
| 91 |
QString statNameString = statDataList.takeFirst().replace(":","").trimmed(); |
| 92 |
QString statValueString = statDataList.takeFirst().trimmed(); |
| 93 |
ontStatHash->insert(statNameString,statValueString); |
| 94 |
if (!mExtractedStatsSet.contains(statNameString)) { |
| 95 |
mExtractedStatsSet.insert(statNameString); |
| 96 |
mExtractedStatsList.append(statNameString); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
mExtractedOntStatHashList.append(ontStatHash); |
| 101 |
return this; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
CLoader* CReasonerEvaluationOntologyExtractionLoader::saveExtractedOntologyStatistics(const QString& fileNameString) { |
| 106 |
mExtractedStatsList.prepend("Ontology"); |
| 107 |
mExtractedStatsList.prepend("Path"); |
| 108 |
|
| 109 |
QString outFileString = fileNameString; |
| 110 |
QFile outFile(outFileString); |
| 111 |
if (outFile.open(QIODevice::WriteOnly)) { |
| 112 |
|
| 113 |
QString titleString; |
| 114 |
for (QList<QString>::const_iterator itVal = mExtractedStatsList.constBegin(), itValEnd = mExtractedStatsList.constEnd(); itVal != itValEnd; ++itVal) { |
| 115 |
QString valString(*itVal); |
| 116 |
if (!titleString.isEmpty()) { |
| 117 |
titleString.append("\t"); |
| 118 |
} |
| 119 |
titleString += valString; |
| 120 |
} |
| 121 |
outFile.write(QString("%1\r\n").arg(titleString).toLocal8Bit()); |
| 122 |
|
| 123 |
for (QList< QHash<QString,QString>* >::const_iterator it = mExtractedOntStatHashList.constBegin(), itEnd = mExtractedOntStatHashList.constEnd(); it != itEnd; ++it) { |
| 124 |
QHash<QString,QString>* ontStatHash(*it); |
| 125 |
|
| 126 |
QString dataString; |
| 127 |
for (QList<QString>::const_iterator itVal = mExtractedStatsList.constBegin(), itValEnd = mExtractedStatsList.constEnd(); itVal != itValEnd; ++itVal) { |
| 128 |
QString valString(*itVal); |
| 129 |
QString valDataString(ontStatHash->value(valString)); |
| 130 |
if (!dataString.isEmpty()) { |
| 131 |
dataString.append("\t"); |
| 132 |
} |
| 133 |
dataString += valDataString; |
| 134 |
} |
| 135 |
outFile.write(QString("%1\r\n").arg(dataString).toLocal8Bit()); |
| 136 |
} |
| 137 |
|
| 138 |
outFile.close(); |
| 139 |
} |
| 140 |
return this; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
CLoader* CReasonerEvaluationOntologyExtractionLoader::copyExtractedOntology(const QString& testFileNameString, const QString& testFilePathString, const QString& subDirPathString) { |
| 145 |
QString ontoFileString = testFilePathString+testFileNameString; |
| 146 |
QFile ontoFile(ontoFileString); |
| 147 |
if (ontoFile.open(QIODevice::ReadOnly)) { |
| 148 |
QDir newOntoFileDir(mOutputDir); |
| 149 |
newOntoFileDir.mkpath(subDirPathString); |
| 150 |
QString newOntoFileString = mOutputDir+subDirPathString+testFileNameString; |
| 151 |
QFile newOntoFile(newOntoFileString); |
| 152 |
if (newOntoFile.open(QIODevice::WriteOnly)) { |
| 153 |
while (!ontoFile.atEnd()) { |
| 154 |
QByteArray ontoData(ontoFile.read(1000000)); |
| 155 |
newOntoFile.write(ontoData); |
| 156 |
} |
| 157 |
newOntoFile.close(); |
| 158 |
ontoFile.close(); |
| 159 |
} |
| 160 |
} |
| 161 |
return this; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
CLoader* CReasonerEvaluationOntologyExtractionLoader::extractOntologiesForTestcase(const QString& testFileNameString, const QString& testFilePathString, const QString& subDirPathString) { |
| 168 |
QStringList statisticList; |
| 169 |
QString ontoStatFileString = mOntologyStatisticsDir+subDirPathString+testFileNameString+".txt"; |
| 170 |
QFile ontoStatFile(ontoStatFileString); |
| 171 |
if (ontoStatFile.open(QIODevice::ReadOnly)) { |
| 172 |
while (!ontoStatFile.atEnd()) { |
| 173 |
QByteArray statDataLine(ontoStatFile.readLine()); |
| 174 |
statisticList.append(statDataLine.trimmed()); |
| 175 |
} |
| 176 |
ontoStatFile.close(); |
| 177 |
|
| 178 |
if (!statisticList.isEmpty()) { |
| 179 |
if (satisfiesExtractionCondition(testFileNameString,testFilePathString,subDirPathString,statisticList)) { |
| 180 |
// extract ontology |
| 181 |
if (mCollectExtractedOntologyStatistics) { |
| 182 |
addExtractedOntologyStatistics(testFileNameString,testFilePathString,subDirPathString,statisticList); |
| 183 |
} |
| 184 |
if (mCopyExtractedOntologyToOutput) { |
| 185 |
copyExtractedOntology(testFileNameString,testFilePathString,subDirPathString); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
return this; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
CLoader* CReasonerEvaluationOntologyExtractionLoader::extractOntologiesForDirectory(const QString& dirString, const QString& subDirPathString) { |
| 197 |
QDir inputDir(dirString); |
| 198 |
foreach (const QString& subDirString, inputDir.entryList(QDir::Dirs)) { |
| 199 |
if (subDirString != ".." && subDirString != ".") { |
| 200 |
extractOntologiesForDirectory(dirString+subDirString+"/",subDirPathString+subDirString+"/"); |
| 201 |
} |
| 202 |
} |
| 203 |
foreach (const QString& fileString, inputDir.entryList(QDir::Files)) { |
| 204 |
|
| 205 |
extractOntologiesForTestcase(fileString,dirString,subDirPathString); |
| 206 |
|
| 207 |
} |
| 208 |
LOG(INFO,"::Konclude::Generator::TestCaseGenerator",logTr("Extracted ontologies in %1.").arg(dirString),this); |
| 209 |
return this; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
CLoader *CReasonerEvaluationOntologyExtractionLoader::load() { |
| 214 |
extractOntologiesForDirectory(mInputDir,""); |
| 215 |
saveExtractedOntologyStatistics(mExtractedOntStatOutputFileString); |
| 216 |
return this; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
CLoader *CReasonerEvaluationOntologyExtractionLoader::exit() { |
| 223 |
return this; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
}; // end namespace Loader |
| 229 |
|
| 230 |
}; // end namespace Control |
| 231 |
|
| 232 |
}; // end namespace Konclude |