| 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 "CCommanderManagerThread.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Control { |
| 27 |
|
| 28 |
namespace Command { |
| 29 |
|
| 30 |
|
| 31 |
CCommanderManagerThread::CCommanderManagerThread() : CThread("ReasonderCommander"), CLogIdentifier("::Konclude::Command::ReasonerCommanderThread",this) { |
| 32 |
configurationProvider = 0; |
| 33 |
reasonerManager = 0; |
| 34 |
kbCommandsDelegater = 0; |
| 35 |
classificationMan = 0; |
| 36 |
ontoRevMan = 0; |
| 37 |
startThread(); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
CCommanderManagerThread::~CCommanderManagerThread() { |
| 42 |
if (reasonerManager) { |
| 43 |
delete reasonerManager; |
| 44 |
} |
| 45 |
if (configurationProvider) { |
| 46 |
delete configurationProvider; |
| 47 |
} |
| 48 |
if (classificationMan) { |
| 49 |
delete classificationMan; |
| 50 |
} |
| 51 |
if (ontoRevMan) { |
| 52 |
delete ontoRevMan; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
CCommanderManagerThread *CCommanderManagerThread::realizeCommand(CCommand *command) { |
| 57 |
postEvent(new CRealizeCommandEvent(command)); |
| 58 |
return this; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
bool CCommanderManagerThread::processCustomsEvents(QEvent::Type type, CCustomEvent *event) { |
| 63 |
if (!CThread::processCustomsEvents(type,event)) { |
| 64 |
if (type == EVENTREALIZECOMMAND) { |
| 65 |
CRealizeCommandEvent *commandEvent = (CRealizeCommandEvent *)event; |
| 66 |
if (commandEvent) { |
| 67 |
CCommand *command = commandEvent->getCommand(); |
| 68 |
if (command) { |
| 69 |
if (command->getCommandTag() == INITIALIZECONFIGURATIONCOMMAND) { |
| 70 |
CInitializeConfigurationCommand *initializeConfigCommand = (CInitializeConfigurationCommand *)command; |
| 71 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 72 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 73 |
initializeConfiguration(initializeConfigCommand->getInitialConfiguration(),&commandRecordRouter); |
| 74 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 75 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 76 |
|
| 77 |
} else if (command->getCommandTag() == INITIALIZEREASONERCOMMAND) { |
| 78 |
CInitializeReasonerCommand *initializeReasonerCommand = (CInitializeReasonerCommand *)command; |
| 79 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 80 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 81 |
CCommanderInitializationFactory *initFact = initializeReasonerCommand->getInitializationFactory(); |
| 82 |
initializeCommander(initFact,&commandRecordRouter); |
| 83 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 84 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 85 |
|
| 86 |
} else if (command->getCommandTag() == GETKNOWLEGEBASECONFIGURATIONCOMMAND) { |
| 87 |
CGetKnowledgeBaseConfigCommand *getConfigCommand = (CGetKnowledgeBaseConfigCommand *)command; |
| 88 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 89 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 90 |
|
| 91 |
kbCommandsDelegater->delegateCommand(getConfigCommand); |
| 92 |
|
| 93 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 94 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 95 |
|
| 96 |
} else if (command->getCommandTag() == SETKNOWLEDGEBASECONFIGURATIONCOMMAND) { |
| 97 |
CSetKnowledgeBaseConfigCommand *setConfigCommand = (CSetKnowledgeBaseConfigCommand *)command; |
| 98 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 99 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 100 |
|
| 101 |
kbCommandsDelegater->delegateCommand(setConfigCommand); |
| 102 |
|
| 103 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 104 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 105 |
|
| 106 |
} else if (command->getCommandTag() == GETCONFIGURATIONCOMMAND) { |
| 107 |
CGetConfigCommand *getConfigCommand = (CGetConfigCommand *)command; |
| 108 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 109 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 110 |
|
| 111 |
CConfigurationBase *config = configurationProvider->getCurrentConfiguration(); |
| 112 |
CConfigData *data = config->getRelatedConfigChange(getConfigCommand->getConfigNameString(),true); |
| 113 |
if (!data) { |
| 114 |
CUnspecifiedMessageErrorRecord::makeRecord(QString("Configuration property '%1' not available.").arg(getConfigCommand->getConfigNameString()),&commandRecordRouter); |
| 115 |
} |
| 116 |
getConfigCommand->setConfigData(data); |
| 117 |
|
| 118 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 119 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 120 |
|
| 121 |
} else if (command->getCommandTag() == SETCONFIGURATIONCOMMAND) { |
| 122 |
CSetConfigCommand *setConfigCommand = (CSetConfigCommand *)command; |
| 123 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 124 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 125 |
|
| 126 |
CGlobalConfigurationBase *config = configurationProvider->createNextGlobalConfiguration(); |
| 127 |
if (!config->isConfigLevelLocked(setConfigCommand->getConfigNameString())) { |
| 128 |
CConfigData *configData = config->createAndSetConfig(setConfigCommand->getConfigNameString()); |
| 129 |
if (configData) { |
| 130 |
if (configData->supportsStringConvertion()) { |
| 131 |
if (configData->readFromString(setConfigCommand->getConfigValueString())) { |
| 132 |
CUnspecifiedMessageInformationRecord::makeRecord(QString("Setting configuration for '%1' to value '%2'.").arg(setConfigCommand->getConfigNameString()).arg(setConfigCommand->getConfigValueString()),&commandRecordRouter); |
| 133 |
configurationProvider->installGlobalConfiguration(config); |
| 134 |
} else { |
| 135 |
CUnspecifiedMessageErrorRecord::makeRecord(QString("Value for configuration property '%1' cannot be read from string '%2'.").arg(setConfigCommand->getConfigNameString()).arg(setConfigCommand->getConfigValueString()),&commandRecordRouter); |
| 136 |
} |
| 137 |
} else { |
| 138 |
CUnspecifiedMessageErrorRecord::makeRecord(QString("Value for configuration property '%1' cannot be from string '%2'.").arg(setConfigCommand->getConfigNameString()).arg(setConfigCommand->getConfigValueString()),&commandRecordRouter); |
| 139 |
} |
| 140 |
} else { |
| 141 |
CUnspecifiedMessageErrorRecord::makeRecord(QString("Configuration property '%1' not available.").arg(setConfigCommand->getConfigNameString()),&commandRecordRouter); |
| 142 |
} |
| 143 |
} else { |
| 144 |
CUnspecifiedMessageErrorRecord::makeRecord(QString("Configuration property '%1' is locked.").arg(setConfigCommand->getConfigNameString()),&commandRecordRouter); |
| 145 |
} |
| 146 |
|
| 147 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 148 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 149 |
|
| 150 |
} else if (command->getCommandTag() == GETDESCRIPTIONCOMMAND) { |
| 151 |
CGetDescriptionCommand *getDesCommand = (CGetDescriptionCommand *)command; |
| 152 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 153 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 154 |
getDesCommand->setConfiguration(configurationProvider->getCurrentConfiguration()); |
| 155 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 156 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 157 |
|
| 158 |
} else if (command->getCommandTag() == GETSETTINGSCOMMAND) { |
| 159 |
CGetSettingsCommand *getDesCommand = (CGetSettingsCommand *)command; |
| 160 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 161 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 162 |
getDesCommand->setConfiguration(configurationProvider->getCurrentConfiguration()); |
| 163 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 164 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 165 |
|
| 166 |
} else if (command->getCommandTag() == GETREASONERCOMMAND) { |
| 167 |
CGetReasonerCommand *getReasonerCommand = (CGetReasonerCommand *)command; |
| 168 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 169 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 170 |
getReasonerCommand->setReasonerManager(reasonerManager); |
| 171 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 172 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 173 |
|
| 174 |
} else if (command->getCommandTag() == GETCLASSIFICATIONMANAGERCOMMAND) { |
| 175 |
CGetClassificationManagerCommand *getClassificationManCommand = (CGetClassificationManagerCommand *)command; |
| 176 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 177 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 178 |
getClassificationManCommand->setClassificationManager(classificationMan); |
| 179 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 180 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 181 |
|
| 182 |
} else if (dynamic_cast<CKnowledgeBaseCommand *>(command)) { |
| 183 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 184 |
if (kbCommandsDelegater) { |
| 185 |
kbCommandsDelegater->delegateCommand(command); |
| 186 |
} else { |
| 187 |
CUnspecifiedMessageErrorRecord::makeRecord("Missing ontology manager.",&commandRecordRouter); |
| 188 |
} |
| 189 |
|
| 190 |
} else if (dynamic_cast<CCalculateQueryCommand *>(command)) { |
| 191 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 192 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 193 |
|
| 194 |
CCalculateQueryCommand *calcQueryCommand = (CCalculateQueryCommand *)command; |
| 195 |
CQuery *query = calcQueryCommand->getQuery(); |
| 196 |
if (reasonerManager) { |
| 197 |
if (query) { |
| 198 |
CUnspecifiedMessageInformationRecord::makeRecord("Sending query to reasoner manager.",&commandRecordRouter); |
| 199 |
|
| 200 |
CCommandCalculatedQueryCallbackEvent *calculatedQueryCE = new CCommandCalculatedQueryCallbackEvent(this,calcQueryCommand); |
| 201 |
reasonerManager->reasoningQuery(query,calculatedQueryCE); |
| 202 |
} else { |
| 203 |
CUnspecifiedMessageErrorRecord::makeRecord("Query cannot be be resolved.",&commandRecordRouter); |
| 204 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 205 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 206 |
} |
| 207 |
} else { |
| 208 |
CUnspecifiedMessageErrorRecord::makeRecord("Missing Reasoner Manager.",&commandRecordRouter); |
| 209 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 210 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 211 |
} |
| 212 |
|
| 213 |
} else if (dynamic_cast<CPrepareKnowledgeBaseCommand *>(command)) { |
| 214 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 215 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 216 |
|
| 217 |
CPrepareKnowledgeBaseCommand* prepareKBCommand = (CPrepareKnowledgeBaseCommand*)command; |
| 218 |
if (reasonerManager) { |
| 219 |
CUnspecifiedMessageInformationRecord::makeRecord("Preparing ontology.",&commandRecordRouter); |
| 220 |
|
| 221 |
CConcreteOntology* ontology = prepareKBCommand->getOntology(); |
| 222 |
if (ontology) { |
| 223 |
COntologyPreparedCallbackEvent *ontPrepQueryCE = new COntologyPreparedCallbackEvent(this,prepareKBCommand); |
| 224 |
reasonerManager->prepareOntology(ontology,*prepareKBCommand->getOntologyProcessingRequirementList(),ontPrepQueryCE); |
| 225 |
} else { |
| 226 |
CUnspecifiedMessageErrorRecord::makeRecord("Ontology not specified.",&commandRecordRouter); |
| 227 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 228 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 229 |
} |
| 230 |
} else { |
| 231 |
CUnspecifiedMessageErrorRecord::makeRecord("Missing reasoner manager.",&commandRecordRouter); |
| 232 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 233 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 234 |
} |
| 235 |
|
| 236 |
} else if (dynamic_cast<CSkipUnsupportedCommandCommand *>(command)) { |
| 237 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 238 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 239 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 240 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 241 |
|
| 242 |
} else { |
| 243 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 244 |
CStartProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 245 |
CUnspecifiedMessageErrorRecord::makeRecord("Unsupported Command.",&commandRecordRouter); |
| 246 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 247 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
} |
| 252 |
} |
| 253 |
return true; |
| 254 |
|
| 255 |
|
| 256 |
} else if (type == EVENTCOMMANDCALCULATEDQUERYCALLBACK) { |
| 257 |
CCommandCalculatedQueryCallbackEvent *commCalQueryCallbackEv = (CCommandCalculatedQueryCallbackEvent *)event; |
| 258 |
CCalculateQueryCommand *command = dynamic_cast<CCalculateQueryCommand *>(commCalQueryCallbackEv->getCommand()); |
| 259 |
if (command) { |
| 260 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 261 |
CUnspecifiedMessageInformationRecord::makeRecord("Query by reasoner manager calculated.",&commandRecordRouter); |
| 262 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 263 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 264 |
} |
| 265 |
return true; |
| 266 |
|
| 267 |
} else if (type == EVENTONTOLOGYPREPAREDCALLBACK) { |
| 268 |
COntologyPreparedCallbackEvent* ontPreparedCallbackEv = (COntologyPreparedCallbackEvent*)event; |
| 269 |
CPrepareKnowledgeBaseCommand* command = dynamic_cast<CPrepareKnowledgeBaseCommand*>(ontPreparedCallbackEv->getCommand()); |
| 270 |
if (command) { |
| 271 |
CCommandRecordRouter commandRecordRouter(command,this); |
| 272 |
CUnspecifiedMessageInformationRecord::makeRecord("Ontology for reasoner manager prepared.",&commandRecordRouter); |
| 273 |
CStopProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 274 |
CFinishProcessCommandRecord::makeRecord(&commandRecordRouter); |
| 275 |
} |
| 276 |
return true; |
| 277 |
|
| 278 |
} |
| 279 |
} else { |
| 280 |
return true; |
| 281 |
} |
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
void CCommanderManagerThread::initializeConfiguration(CConfiguration *initConfig, CCommandRecordRouter *commandRecordRouter) { |
| 288 |
if (configurationProvider) { |
| 289 |
CUnspecifiedMessageErrorRecord::makeRecord("Initial configuration already exists.",commandRecordRouter); |
| 290 |
} else { |
| 291 |
configurationProvider = new CReasonerCommanderGlobalConfiguration(initConfig); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
void CCommanderManagerThread::initializeCommander(CCommanderInitializationFactory *initializationFactory, CCommandRecordRouter *commandRecordRouter) { |
| 297 |
if (!configurationProvider) { |
| 298 |
CUnspecifiedMessageErrorRecord::makeRecord("Missing initial configuration.",commandRecordRouter); |
| 299 |
} else { |
| 300 |
CGlobalConfigurationBase *config = 0; |
| 301 |
config = configurationProvider->createNextGlobalConfiguration(); |
| 302 |
|
| 303 |
if (reasonerManager) { |
| 304 |
CUnspecifiedMessageErrorRecord::makeRecord("Reasoner manager already exists.",commandRecordRouter); |
| 305 |
} else { |
| 306 |
reasonerManager = initializationFactory->createReasonerManager(configurationProvider); |
| 307 |
((CReasonerManagerConfigType *)(config->createAndSetConfig("Konclude.Execution.ReasonerManager"))->getConfigType())->setReasonerManager(reasonerManager); |
| 308 |
} |
| 309 |
|
| 310 |
if (classificationMan) { |
| 311 |
CUnspecifiedMessageErrorRecord::makeRecord("Classification manager already exists.",commandRecordRouter); |
| 312 |
} else { |
| 313 |
classificationMan = initializationFactory->createClassificationManager(configurationProvider); |
| 314 |
((CClassificationManagerConfigType *)config->createAndSetConfig("Konclude.Execution.ClassificationManager")->getConfigType())->setClassificationManager(classificationMan); |
| 315 |
} |
| 316 |
|
| 317 |
if (ontoRevMan) { |
| 318 |
CUnspecifiedMessageErrorRecord::makeRecord("Ontology revision manager already exists.",commandRecordRouter); |
| 319 |
} else { |
| 320 |
ontoRevMan = initializationFactory->createOntologyRevisionManager(configurationProvider); |
| 321 |
((COntologyRevisionManagerConfigType *)config->createAndSetConfig("Konclude.Execution.OntologyRevisionManager")->getConfigType())->setOntologyRevisionManager(ontoRevMan); |
| 322 |
} |
| 323 |
|
| 324 |
configurationProvider->installGlobalConfiguration(config); |
| 325 |
|
| 326 |
initializationFactory->initializeReasonerManager(reasonerManager,configurationProvider); |
| 327 |
initializationFactory->initializeClassificationManager(classificationMan,configurationProvider); |
| 328 |
initializationFactory->initializeOntologyRevisionManager(ontoRevMan,configurationProvider); |
| 329 |
|
| 330 |
kbCommandsDelegater = dynamic_cast<CSPOntologyRevisionManager *>(ontoRevMan); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
}; // end namespace Command |
| 337 |
|
| 338 |
}; // end namespace Control |
| 339 |
|
| 340 |
}; // end namespace Konclude |