| 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 "CReasonerOWLlinkEvaluationProvider.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
|
| 28 |
namespace Evaluation { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
CReasonerOWLlinkEvaluationProvider::CReasonerOWLlinkEvaluationProvider() { |
| 33 |
mAdapterProcess = nullptr; |
| 34 |
mSupportProcess = nullptr; |
| 35 |
mKillTimeout = 2000; |
| 36 |
mReasonerPort = 8080; |
| 37 |
mMemoryLimit = (cint64)1024*1024*1024*10; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
CReasonerOWLlinkEvaluationProvider::~CReasonerOWLlinkEvaluationProvider() { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
bool CReasonerOWLlinkEvaluationProvider::createReasoner(CConfiguration *config) { |
| 46 |
|
| 47 |
mReasonerName = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Name"); |
| 48 |
|
| 49 |
mReasonerLibDirectory = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Execution.OWLlinkOWLAPIServerAdapter.ClassPathLibraries"); |
| 50 |
mReasonerLibSeparator = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Execution.OWLlinkOWLAPIServerAdapter.ClassPathLibrariesSeparator"); |
| 51 |
mReasonerClassCallName = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Execution.OWLlinkOWLAPIServerAdapter.MainClass"); |
| 52 |
mReasonerPort = CConfigDataReader::readConfigInteger(config,"Konclude.Evaluation.Reasoner.Address.Port"); |
| 53 |
mMemoryLimit = CConfigDataReader::readConfigInteger(config,"Konclude.Evaluation.MemoryLimit"); |
| 54 |
mReasonerAdditionalParameters = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Execution.OWLlinkOWLAPIServerAdapter.AdditionalParameters"); |
| 55 |
|
| 56 |
mSupportProcessString = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Execution.OWLlinkOWLAPIServerAdapter.AdditionalProcess"); |
| 57 |
if (!mSupportProcessString.isEmpty()) { |
| 58 |
mSupportProcess = new QProcess(); |
| 59 |
mSupportProcess->start(mSupportProcessString); |
| 60 |
} |
| 61 |
|
| 62 |
QStringList jarFilesStringList; |
| 63 |
addLibrariesRecursive(mReasonerLibDirectory,jarFilesStringList); |
| 64 |
QString argString = QString("-Xmx%1 -Djava.library.path=%2 -cp .%3\"%4\" %5 -port %6 %7").arg(mMemoryLimit).arg(mReasonerLibDirectory).arg(mReasonerLibSeparator).arg(jarFilesStringList.join(mReasonerLibSeparator)).arg(mReasonerClassCallName).arg(mReasonerPort).arg(mReasonerAdditionalParameters); |
| 65 |
QString appString = QString("java %1").arg(argString); |
| 66 |
|
| 67 |
|
| 68 |
mAdapterProcess = new QProcess(); |
| 69 |
|
| 70 |
connect(mAdapterProcess,SIGNAL(error(QProcess::ProcessError)),this,SLOT(processError(QProcess::ProcessError)),Qt::DirectConnection); |
| 71 |
connect(mAdapterProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus)),Qt::DirectConnection); |
| 72 |
|
| 73 |
mProcessFinished = false; |
| 74 |
mProcessError = false; |
| 75 |
|
| 76 |
mAdapterProcess->start(appString); |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
QString CReasonerOWLlinkEvaluationProvider::getReasonerAddressString() { |
| 83 |
return QString("127.0.0.1:%1").arg(mReasonerPort); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
CReasonerEvaluationTerminationResult* CReasonerOWLlinkEvaluationProvider::destroyReasoner() { |
| 88 |
CReasonerEvaluationTerminationResult* result = new CReasonerEvaluationTerminationResult(); |
| 89 |
|
| 90 |
QString stdOutText; |
| 91 |
QString stdErrText; |
| 92 |
QString errorString; |
| 93 |
try { |
| 94 |
QByteArray standOutput = mAdapterProcess->readAllStandardOutput(); |
| 95 |
QByteArray standError = mAdapterProcess->readAllStandardError(); |
| 96 |
stdOutText = QString(standOutput); |
| 97 |
stdErrText = QString(standError); |
| 98 |
errorString = mAdapterProcess->errorString(); |
| 99 |
} catch (std::bad_alloc& ba) { |
| 100 |
stdErrText = "Reasoners outputs exceed size limitations\r\n"; |
| 101 |
LOG(INFO, "::Konclude::Test::ReasonerEvaluationProvider", logTr("Failed to read reasoner output due to size limitations."), this); |
| 102 |
} |
| 103 |
|
| 104 |
bool forcedTermination = false; |
| 105 |
bool finishedInTime = mProcessFinished; |
| 106 |
|
| 107 |
disconnect(mAdapterProcess,SIGNAL(error(QProcess::ProcessError)),this,SLOT(processError(QProcess::ProcessError))); |
| 108 |
disconnect(mAdapterProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus))); |
| 109 |
disconnect(mAdapterProcess); |
| 110 |
disconnect(this); |
| 111 |
if (!mProcessFinished) { |
| 112 |
mProcessFinished = true; |
| 113 |
forcedTermination = true; |
| 114 |
mAdapterProcess->terminate(); |
| 115 |
mAdapterProcess->waitForFinished(mKillTimeout); |
| 116 |
mAdapterProcess->kill(); |
| 117 |
mAdapterProcess->waitForFinished(mKillTimeout); |
| 118 |
mAdapterProcess->kill(); |
| 119 |
mAdapterProcess->waitForFinished(mKillTimeout); |
| 120 |
} |
| 121 |
if (mSupportProcess) { |
| 122 |
mSupportProcess->terminate(); |
| 123 |
mSupportProcess->waitForFinished(mKillTimeout); |
| 124 |
mSupportProcess->kill(); |
| 125 |
mSupportProcess->waitForFinished(mKillTimeout); |
| 126 |
mSupportProcess->kill(); |
| 127 |
mSupportProcess->waitForFinished(mKillTimeout); |
| 128 |
} |
| 129 |
mAdapterProcess->close(); |
| 130 |
delete mAdapterProcess; |
| 131 |
mAdapterProcess = nullptr; |
| 132 |
result->initResult(mProcessError,finishedInTime,forcedTermination,errorString,stdOutText,stdErrText); |
| 133 |
return result; |
| 134 |
} |
| 135 |
|
| 136 |
void CReasonerOWLlinkEvaluationProvider::processError(QProcess::ProcessError error) { |
| 137 |
if (!mProcessFinished) { |
| 138 |
mProcessError = true; |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
void CReasonerOWLlinkEvaluationProvider::processFinished(int exitCode, QProcess::ExitStatus exitStatus) { |
| 144 |
if (!mProcessFinished) { |
| 145 |
mProcessFinished = true; |
| 146 |
if (exitStatus != QProcess::NormalExit) { |
| 147 |
mProcessError = true; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
void CReasonerOWLlinkEvaluationProvider::addLibrariesRecursive(const QString& dirString, QStringList& jarStringList) { |
| 154 |
QDir dir(dirString); |
| 155 |
QStringList filterList; |
| 156 |
filterList<<QString("*.jar"); |
| 157 |
QStringList entrylist(dir.entryList(filterList,QDir::Files)); |
| 158 |
foreach (QString fileString, entrylist) { |
| 159 |
jarStringList.append(dirString+fileString); |
| 160 |
} |
| 161 |
foreach (QString subDir, dir.entryList(QDir::Dirs)) { |
| 162 |
if (!subDir.endsWith(".") && !subDir.endsWith("..")) { |
| 163 |
addLibrariesRecursive(dirString+subDir+"/",jarStringList); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
}; // end namespace Evaluation |
| 169 |
|
| 170 |
}; // end namespace Test |
| 171 |
|
| 172 |
}; // end namespace Konclude |