| 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 "CCommandLinePreparationTranslator.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Control { |
| 27 |
|
| 28 |
namespace Interface { |
| 29 |
|
| 30 |
namespace CommandLine { |
| 31 |
|
| 32 |
CCommandLinePreparationTranslator::CCommandLinePreparationTranslator(CLoaderFactory *loaderFactory) : CLogIdentifier("::Konclude::Loader::CommandLineTranslator",this) { |
| 33 |
mSkipTranslation = false; |
| 34 |
mGenerateAutoResponseFile = false; |
| 35 |
mLoaderFac = loaderFactory; |
| 36 |
mHelpRequested = false; |
| 37 |
mExtendendLogging = false; |
| 38 |
mInterfaceTypeString = QString("CLI"); |
| 39 |
mRequestFileTranslated = false; |
| 40 |
mResponseFileTranslated = false; |
| 41 |
mConfigFileTranslated = false; |
| 42 |
mIRITranslated = false; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
CCommandLinePreparationTranslator::~CCommandLinePreparationTranslator() { |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
QStringList CCommandLinePreparationTranslator::translateArguments(int argc, char *argv[]) { |
| 51 |
QStringList argList; |
| 52 |
for (int i = 0; i < argc; ++i) { |
| 53 |
QString argumentText = QString(argv[i]); |
| 54 |
argList.append(argumentText); |
| 55 |
} |
| 56 |
return translateArguments(argList); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
QStringList CCommandLinePreparationTranslator::combineTranslatedArguments() { |
| 62 |
QStringList translatedArgList; |
| 63 |
if (!mHelpRequested) { |
| 64 |
if (mLogConfList.isEmpty()) { |
| 65 |
mLogConfList.append(QString("-ConfigurableCoutLogObserverLoader -LoggerConfigurationLoader")); |
| 66 |
} |
| 67 |
translatedArgList += mLogConfList; |
| 68 |
translatedArgList += mGeneralConfList; |
| 69 |
|
| 70 |
translatedArgList += QString("-DefaultReasonerLoader "); |
| 71 |
translatedArgList += mReasonerConfList; |
| 72 |
translatedArgList += mGeneralConfList; |
| 73 |
|
| 74 |
if (!mConfigConfList.isEmpty()) { |
| 75 |
translatedArgList += QString("-%1BatchFileLoader +=Konclude.%1.CloseAfterProcessedRequest=false +=Konclude.%1.BlockUntilProcessedRequest=true").arg(mInterfaceTypeString); |
| 76 |
translatedArgList += mConfigConfList; |
| 77 |
} |
| 78 |
|
| 79 |
translatedArgList += QString("-%1 +=Konclude.%2.CloseAfterProcessedRequest=true").arg(mLoaderTypeString).arg(mInterfaceTypeString); |
| 80 |
if (mExtendendLogging) { |
| 81 |
translatedArgList += QString("+=Konclude.OWLlink.LoggingExtendedProcessingTimes=true"); |
| 82 |
} |
| 83 |
translatedArgList += mRequestConfList; |
| 84 |
|
| 85 |
translatedArgList += mMiscellaneousList; |
| 86 |
} else { |
| 87 |
translatedArgList += QString("-ConfigurableCoutLogObserverLoader -CommandLineTranlatorHelpLoader"); |
| 88 |
} |
| 89 |
return translatedArgList; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
QStringList CCommandLinePreparationTranslator::translateArguments(const QStringList& argList) { |
| 95 |
QStringList processList(argList); |
| 96 |
if (processList.count() <= 0) { |
| 97 |
mHelpRequested = true; |
| 98 |
} |
| 99 |
while (processList.count() > 0) { |
| 100 |
QString arg = processList.takeFirst(); |
| 101 |
if (!translate(arg,processList)) { |
| 102 |
mRemainingLoadList.append(arg); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
if (mGenerateAutoResponseFile) { |
| 108 |
QString responseFile = mRequestFileName; |
| 109 |
if (responseFile.indexOf("request.xml") >= 0) { |
| 110 |
responseFile.replace("request.xml","response.xml"); |
| 111 |
} else if (responseFile.indexOf(".xml") >= 0) { |
| 112 |
responseFile.replace(".xml","-response.xml"); |
| 113 |
} else { |
| 114 |
responseFile.append("-response.xml"); |
| 115 |
} |
| 116 |
mRequestConfList.append(QString("+Load.%1.ResponseFile=%2").arg(mInterfaceTypeString).arg(responseFile)); |
| 117 |
} |
| 118 |
|
| 119 |
QStringList translatedArgList; |
| 120 |
if (!mSkipTranslation) { |
| 121 |
translatedArgList = combineTranslatedArguments(); |
| 122 |
} else { |
| 123 |
translatedArgList = argList; |
| 124 |
} |
| 125 |
translatedArgList.append(mRemainingLoadList); |
| 126 |
return translatedArgList; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
bool CCommandLinePreparationTranslator::translate(const QString& argumentText, QStringList& remainingArgList) { |
| 131 |
|
| 132 |
bool argumentTranslated = false; |
| 133 |
|
| 134 |
|
| 135 |
QString modArgText = argumentText.trimmed(); |
| 136 |
if (!mSkipTranslation) { |
| 137 |
|
| 138 |
if (modArgText.indexOf("-") == 0) { |
| 139 |
|
| 140 |
while (modArgText.startsWith("-")) { |
| 141 |
modArgText.remove(0,1); |
| 142 |
modArgText = modArgText.trimmed(); |
| 143 |
} |
| 144 |
|
| 145 |
if (mLoaderFac->canCreateLoaderFromName(modArgText)) { |
| 146 |
argumentTranslated = true; |
| 147 |
mRemainingLoadList += argumentText; |
| 148 |
|
| 149 |
while (!remainingArgList.isEmpty() && remainingArgList.first().startsWith("+")) { |
| 150 |
mRemainingLoadList.append(remainingArgList.takeFirst()); |
| 151 |
} |
| 152 |
|
| 153 |
} else if (modArgText.indexOf("t") == 0 || modArgText.indexOf("T") == 0) { |
| 154 |
mSkipTranslation = true; |
| 155 |
argumentTranslated = true; |
| 156 |
|
| 157 |
} else if (modArgText.indexOf("v") == 0 || modArgText.indexOf("V") == 0) { |
| 158 |
mExtendendLogging = true; |
| 159 |
argumentTranslated = true; |
| 160 |
|
| 161 |
} else if (modArgText.indexOf("u") == 0 || modArgText.indexOf("U") == 0) { |
| 162 |
mExtendendLogging = true; |
| 163 |
argumentTranslated = true; |
| 164 |
mLogConfList += QString("-CoutLogObserverLoader -LoggerConfigurationLoader"); |
| 165 |
|
| 166 |
} else if (modArgText.indexOf("h") == 0 || modArgText.indexOf("H") == 0 || modArgText.indexOf("?") == 0) { |
| 167 |
mHelpRequested = true; |
| 168 |
argumentTranslated = true; |
| 169 |
|
| 170 |
|
| 171 |
} else if (modArgText.indexOf("e") == 0 || modArgText.indexOf("E") == 0) { |
| 172 |
mReasonerConfList.append(QString("+Konclude.Execution.DefaultReasonerManager=ASExperimentalReasonerManager")); |
| 173 |
argumentTranslated = true; |
| 174 |
} else if (modArgText.indexOf("a") == 0 || modArgText.indexOf("A") == 0) { |
| 175 |
mReasonerConfList.append(QString("+Konclude.Execution.DefaultReasonerManager=ASAnalyseReasonerManager")); |
| 176 |
argumentTranslated = true; |
| 177 |
|
| 178 |
|
| 179 |
} else if (modArgText.indexOf("w") == 0 || modArgText.indexOf("W") == 0) { |
| 180 |
// worker count |
| 181 |
QString configVal = "AUTO"; |
| 182 |
if (remainingArgList.count() > 0) { |
| 183 |
configVal = remainingArgList.first(); |
| 184 |
if (configVal.trimmed().indexOf("-") == 0 || configVal.trimmed().indexOf("+") == 0) { |
| 185 |
configVal = "AUTO"; |
| 186 |
} else { |
| 187 |
remainingArgList.removeFirst(); |
| 188 |
} |
| 189 |
} |
| 190 |
mReasonerConfList.append(QString("+Konclude.Calculation.ProcessorCount=%1").arg(configVal)); |
| 191 |
argumentTranslated = true; |
| 192 |
|
| 193 |
} else if (modArgText.indexOf("q") == 0 || modArgText.indexOf("Q") == 0 || modArgText.indexOf("i") == 0 || modArgText.indexOf("I") == 0) { |
| 194 |
// query/input file |
| 195 |
QString configVal = "request.xml"; |
| 196 |
if (remainingArgList.count() > 0) { |
| 197 |
configVal = remainingArgList.takeFirst(); |
| 198 |
} |
| 199 |
mRequestFileName = configVal.trimmed(); |
| 200 |
mRequestConfList.append(QString("+Konclude.%1.RequestFile=%2").arg(mInterfaceTypeString).arg(configVal.trimmed())); |
| 201 |
argumentTranslated = true; |
| 202 |
mRequestFileTranslated = true; |
| 203 |
|
| 204 |
} else if (modArgText.indexOf("c") == 0 || modArgText.indexOf("C") == 0) { |
| 205 |
// owllink query file |
| 206 |
QString configVal = "config.xml"; |
| 207 |
if (remainingArgList.count() > 0) { |
| 208 |
configVal = remainingArgList.takeFirst(); |
| 209 |
} |
| 210 |
mConfigConfList.append(QString("+Konclude.OWLlink.RequestFile=%1").arg(configVal.trimmed())); |
| 211 |
argumentTranslated = true; |
| 212 |
mConfigFileTranslated = true; |
| 213 |
|
| 214 |
} else if (modArgText.indexOf("r") == 0 || modArgText.indexOf("R") == 0 || modArgText.indexOf("o") == 0 || modArgText.indexOf("O") == 0) { |
| 215 |
// response/output file |
| 216 |
argumentTranslated = true; |
| 217 |
QString configVal = "response.xml"; |
| 218 |
if (remainingArgList.count() > 0) { |
| 219 |
configVal = remainingArgList.first(); |
| 220 |
if (configVal.trimmed().indexOf("-") == 0 || configVal.trimmed().indexOf("+") == 0) { |
| 221 |
mGenerateAutoResponseFile = true; |
| 222 |
} else { |
| 223 |
mGenerateAutoResponseFile = false; |
| 224 |
} |
| 225 |
} else { |
| 226 |
mGenerateAutoResponseFile = true; |
| 227 |
} |
| 228 |
if (!mGenerateAutoResponseFile) { |
| 229 |
mRequestConfList.append(QString("+Konclude.%1.ResponseFile=%2").arg(mInterfaceTypeString).arg(configVal.trimmed())); |
| 230 |
} |
| 231 |
mResponseFileTranslated = true; |
| 232 |
|
| 233 |
} else if (modArgText.indexOf("x") == 0 || modArgText.indexOf("X") == 0) { |
| 234 |
// port |
| 235 |
QString iriVal = ""; |
| 236 |
if (remainingArgList.count() > 0) { |
| 237 |
iriVal = remainingArgList.takeFirst(); |
| 238 |
} |
| 239 |
mRequestConfList.append(QString("+Konclude.%1.IRIName=%2").arg(mInterfaceTypeString).arg(iriVal.trimmed())); |
| 240 |
argumentTranslated = true; |
| 241 |
mIRITranslated = true; |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
} else if (modArgText.indexOf("+") == 0) { |
| 246 |
|
| 247 |
mGeneralConfList.append(modArgText); |
| 248 |
while (!remainingArgList.isEmpty() && !remainingArgList.first().startsWith("+") && !remainingArgList.first().startsWith("-")) { |
| 249 |
mGeneralConfList.append(remainingArgList.takeFirst()); |
| 250 |
} |
| 251 |
argumentTranslated = true; |
| 252 |
|
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return argumentTranslated; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
}; // end namespace CommandLine |
| 262 |
|
| 263 |
}; // end namespace Interface |
| 264 |
|
| 265 |
}; // end namespace Control |
| 266 |
|
| 267 |
}; // end namespace Konclude |