Debian Codegraph @

Source/Control/Loader/CCommandLineLoader.cpp

raw
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 "CCommandLineLoader.h"
22
23
24 namespace Konclude {
25
26 namespace Control {
27
28 namespace Loader {
29
30
31 CCommandLineLoader::CCommandLineLoader() : CThread("CommandLineLoader") {
32 }
33
34
35 QStringList CCommandLineLoader::getEncodedArguments(int argc, char *argv[], bool skipFirst, bool encodeSpaces) {
36 QString argString;
37 int startArg = 0;
38 if (skipFirst) {
39 startArg = 1;
40 }
41 for (int i = startArg; i < argc; ++i) {
42 if (!argString.isEmpty()) {
43 argString += QString(" ");
44 }
45 QString translatedArgString = QString::fromUtf8(argv[i]);
46 //QString translatedArgString = QString::fromLocal8Bit(argv[i]).trimmed();
47 if (translatedArgString.contains(" ") && encodeSpaces) {
48 argString += QString("\"%1\"").arg(translatedArgString);
49 } else {
50 argString += QString("%1").arg(translatedArgString);
51 }
52 }
53 QString encodedString;
54 bool encodeModus = false;
55 for (QString::const_iterator it = argString.constBegin(), itEnd = argString.constEnd(); it != itEnd; ++it) {
56 QChar character(*it);
57 if (character == QChar('"')) {
58 encodeModus = !encodeModus;
59 } else if (character == QChar(' ') && encodeModus) {
60 encodedString += QString("?_");
61 //} else if (character == QChar('=') && encodeModus) {
62 // encodedString += QString("?~");
63 } else if (character == QChar('?')) {
64 encodedString += QString("??");
65 } else {
66 encodedString += character;
67 }
68 }
69 while (encodedString.contains(" ")) {
70 encodedString.replace(" "," ");
71 }
72 QStringList encodedList;
73 if (!encodedString.isEmpty()) {
74 encodedList = encodedString.split(" ");
75 }
76 return encodedList;
77 }
78
79 QString CCommandLineLoader::getDecodedArgument(const QString& string) {
80 QString decodedString;
81 for (QString::const_iterator it = string.constBegin(), itEnd = string.constEnd(); it != itEnd; ++it) {
82 QChar character(*it);
83 if (character == QChar('?')) {
84 ++it;
85 QChar nextCharacter(*it);
86 if (nextCharacter == QChar('?')) {
87 decodedString += QString("?");
88 } else if (nextCharacter == QChar('_')) {
89 decodedString += QString(" ");
90 //} else if (nextCharacter == QChar('~')) {
91 // decodedString += QString("=");
92 }
93 } else {
94 decodedString += character;
95 }
96 }
97 return decodedString;
98 }
99
100 CCommandLineLoader::CCommandLineLoader(int argc, char *argv[], bool qtGUIApplication) : CThread("CommandLineLoader") {
101 for (int i = 0; i < argc; ++i) {
102 commandLineParameters.append(argv[i]);
103 }
104 qtGuiUsable = qtGUIApplication;
105 }
106
107
108 CCommandLineLoader::CCommandLineLoader(const QStringList &argList, bool qtGUIApplication) : CThread("CommandLineLoader") {
109 foreach (QString arg, argList) {
110 commandLineParameters.append(arg);
111 }
112 qtGuiUsable = qtGUIApplication;
113 }
114
115
116 CCommandLineLoader::~CCommandLineLoader() {
117 qDeleteAll(loaderContainer);
118 qDeleteAll(configContainer);
119 }
120
121
122 CLoader *CCommandLineLoader::init(CLoaderFactory *loaderFactory, CConfiguration *config) {
123 configuration = config;
124 lFactory = loaderFactory;
125
126 qint64 paramCount = commandLineParameters.count();
127
128 CConfigurationGroup *reasonerConfigGroup = 0;
129 if (configuration) {
130 reasonerConfigGroup = configuration->getConfigurationGroup();
131 } else {
132 reasonerConfigGroup = CSingletonProvider<CReasonerConfigurationGroup>::getInstance()->getReferencedConfigurationGroup();
133 configuration = new CConfiguration(reasonerConfigGroup);
134 }
135
136 reasonerConfigGroup->addConfigProperty(new CConfigDescription("Konclude.Execution.CommandLine.ParameterCount","",new CIntegerConfigType(paramCount)));
137
138 QString text;
139 QStringList::const_iterator it = commandLineParameters.constBegin();
140 for (qint64 i = 0; i < paramCount && it != commandLineParameters.constEnd(); ++i,++it) {
141 text += (*it) + " ";
142 reasonerConfigGroup->addConfigProperty(new CConfigDescription(QString("Konclude.Execution.CommandLine.Parameter%1").arg(i),"",new CStringConfigType(getDecodedArgument(*it))));
143 }
144 reasonerConfigGroup->addConfigProperty(new CConfigDescription("Konclude.Execution.CommandLine.Text","",new CStringConfigType(getDecodedArgument(text))));
145
146 configuration->extendConfigurationGroup(reasonerConfigGroup);
147
148
149
150 qint64 nextPos = getFirstTextOccur(text,"-");
151 QStringList matchesList(getMatchesFromPattern("(\\+\\=\\s*(\\w+\\.?)+\\s*\\=\\s*\\S+)",text,nextPos));
152 foreach (QString configString, matchesList) {
153 setConfiguration(configString,configuration,true);
154 }
155
156 loadingText = text.mid(nextPos);
157
158
159 QString nextLoadingText = loadingText;
160
161 bool oneLoadingSuccess = false;
162
163 while (!nextLoadingText.isEmpty()) {
164
165 qint64 nextLoadingPos = getFirstTextOccur(nextLoadingText," -",1);
166 qint64 nextConfigPos = qMin(getFirstTextOccur(nextLoadingText,"+",1),nextLoadingPos-1);
167
168 QStringList loadStringList = getMatchesFromPattern("(-\\s*\\w+\\s+)",nextLoadingText,nextConfigPos);
169 QString loadString;
170 if (loadStringList.count() > 0) {
171 loadString = loadStringList.first();
172 }
173
174 QStringList matchesList(getMatchesFromPattern("(\\+\\=\\s*(\\w+\\.?)+\\s*\\=\\s*\\S+)",nextLoadingText,nextLoadingPos));
175 foreach (QString configString, matchesList) {
176 setConfiguration(configString,configuration,true);
177 }
178
179 if (!loadString.isEmpty()) {
180
181 loadString = loadString.remove("-");
182 loadString = loadString.trimmed();
183
184 CLoader *loader = lFactory->createLoaderFromName(loadString);
185
186 if (loader) {
187 oneLoadingSuccess = true;
188 CConfiguration *loadConfiguration = new CConfiguration(configuration);
189
190 configContainer.append(loadConfiguration);
191 loaderContainer.append(loader);
192
193 QStringList matchesList(getMatchesFromPattern("(\\+\\s*(\\w+\\.?)+\\s*\\=\\s*\\S+)",nextLoadingText,nextLoadingPos));
194 foreach (QString configString, matchesList) {
195 setConfiguration(configString,loadConfiguration);
196 }
197
198 loader->init(lFactory,loadConfiguration);
199 } else {
200 LOG(ERROR,"::Konclude::Loader::CommandLineLoader",logTr("No Loader for '%1' available.").arg(loadString),0);
201 }
202 }
203
204 nextLoadingText = nextLoadingText.mid(nextLoadingPos);
205 }
206
207 if (!oneLoadingSuccess) {
208 LOG(ERROR,"::Konclude::Loader::CommandLineLoader",logTr("No loader specified for '%1'.").arg(commandLineParameters.join(", ")),0);
209 CLoader *loader1 = lFactory->createLoaderFromName("ConfigurableCoutLogObserverLoader");
210 CLoader *loader2 = lFactory->createLoaderFromName("CommandLineTranlatorHelpLoader");
211 loaderContainer.append(loader1);
212 loaderContainer.append(loader2);
213 }
214
215 return this;
216 }
217
218
219 QStringList CCommandLineLoader::getMatchesFromPattern(const QString &pattern, const QString &text, qint64 maxPosition) {
220 QStringList list;
221 QRegExp configExp(pattern);
222 int pos = 0;
223 while ((pos = configExp.indexIn(text,pos)) != -1 && pos <= maxPosition) {
224 list << configExp.cap(1);
225 pos += configExp.matchedLength();
226 }
227 return list;
228 }
229
230
231 qint64 CCommandLineLoader::getFirstTextOccur(const QString &text, const QString &textOccur, qint64 beginSearchPos) {
232 qint64 pos = text.indexOf(textOccur,beginSearchPos);
233 if (pos == -1) {
234 pos = text.length();
235 }
236 return pos;
237 }
238
239 QStringList CCommandLineLoader::getMatchesFromPattern(const QString &pattern, const QString &text) {
240 return getMatchesFromPattern(pattern,text,text.length());
241 }
242
243
244 CCommandLineLoader *CCommandLineLoader::setConfiguration(const QString &configString, CConfiguration *configuration, bool equalsConfSet) {
245 QRegExp probNameRegExp;
246 if (equalsConfSet) {
247 probNameRegExp = QRegExp("(\\+\\=\\s*(\\w+\\.?)+\\s*\\=\\s*)");
248 } else {
249 probNameRegExp = QRegExp("(\\+\\s*(\\w+\\.?)+\\s*\\=\\s*)");
250 }
251 probNameRegExp.indexIn(configString);
252 QString name = probNameRegExp.cap(1);
253 name = name.remove("+");
254 name = name.remove("=");
255 name = name.trimmed();
256 qint64 lastEqIdx = configString.lastIndexOf("=");
257 QString dat = configString.mid(lastEqIdx);
258 dat = dat.remove("=");
259 dat = dat.trimmed();
260
261 CConfigurationGroup *configGroup = configuration->getConfigurationGroup();
262
263 QString decodedConfigName = getDecodedArgument(name);
264 QString decodedConfigData = getDecodedArgument(dat);
265 if (!decodedConfigName.isEmpty()) {
266
267 if (!configGroup->getConfigDescription(decodedConfigName) && decodedConfigName.startsWith("Load.")) {
268
269 configGroup = configGroup->getReferencedConfigurationGroup();
270
271 CConfigType *configType = new CStringConfigType();
272
273 CConfigDescription *configDes = new CConfigDescription(decodedConfigName,"",configType);
274 CConfigData *confData = new CConfigData(configDes,configType->getTypeCopy());
275 confData->readFromString(decodedConfigData);
276 configGroup->addConfigProperty(configDes,confData);
277 configuration->extendConfigurationGroup(configGroup);
278 } else {
279 CConfigDescription *conDes = configGroup->getConfigDescription(decodedConfigName);
280 if (conDes) {
281 if (conDes->supportsStringConvertion()) {
282 CConfigData *confData = configuration->createAndSetConfig(decodedConfigName);
283 if (confData) {
284 confData->readFromString(decodedConfigData);
285 }
286 } else {
287 LOG(ERROR,"::Konclude::Loader::CommandLineLoader",logTr("Configuration '%1' couldn't parsed from string.").arg(decodedConfigName),0);
288 }
289 } else {
290 LOG(ERROR,"::Konclude::Loader::CommandLineLoader",logTr("Configuration '%1' not supported.").arg(decodedConfigName),0);
291 }
292 }
293 }
294 return this;
295 }
296
297
298 CLoader *CCommandLineLoader::load() {
299 startThread();
300 return this;
301 }
302
303 CLoader *CCommandLineLoader::exit() {
304 stopThread();
305 return this;
306 }
307
308 void CCommandLineLoader::threadStarted() {
309 containerSync.lock();
310 foreach (CLoader *loader, loaderContainer) {
311 loader->load();
312 }
313 containerSync.unlock();
314 }
315
316
317
318 void CCommandLineLoader::threadStopped() {
319 containerSync.lock();
320 foreach (CLoader *loader, loaderContainer) {
321 loader->exit();
322 }
323 containerSync.unlock();
324 }
325
326
327 }; // end namespace Loader
328
329 }; // end namespace Control
330
331 }; // end namespace Konclude