| 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 "CCommand.h" |
| 22 |
|
| 23 |
|
| 24 |
#include "Records/CClosureProcessCommandRecord.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
namespace Konclude { |
| 29 |
|
| 30 |
namespace Control { |
| 31 |
|
| 32 |
namespace Command { |
| 33 |
|
| 34 |
using namespace Records; |
| 35 |
|
| 36 |
CCommand::CCommand(CCommand *parentSuperCommand) { |
| 37 |
subCommandLinker = 0; |
| 38 |
superCommand = parentSuperCommand; |
| 39 |
|
| 40 |
processed = false; |
| 41 |
processing = false; |
| 42 |
|
| 43 |
recordData = 0; |
| 44 |
recorder = 0; |
| 45 |
closured = false; |
| 46 |
errorLevel = 0; |
| 47 |
|
| 48 |
mUnclosedCommandCount = 1; |
| 49 |
|
| 50 |
if (parentSuperCommand) { |
| 51 |
parentSuperCommand->makeToSubCommand(this); |
| 52 |
} |
| 53 |
|
| 54 |
reportErrorFromSubCommands = true; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
CCommand::~CCommand() { |
| 59 |
deleteDependencies(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
bool CCommand::hasReportErrorFromSubCommands() { |
| 64 |
return reportErrorFromSubCommands; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
CCommand *CCommand::setReportErrorFromSubCommands(bool reportSubCommandErrors) { |
| 69 |
reportErrorFromSubCommands = reportSubCommandErrors; |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
double CCommand::getMaxErrorLevel() { |
| 75 |
return errorLevel; |
| 76 |
} |
| 77 |
|
| 78 |
CCommand *CCommand::reportError(double errorLevelReport) { |
| 79 |
if (errorLevelReport > errorLevel) { |
| 80 |
errorSyncMutex.lock(); |
| 81 |
errorLevel = qMax(errorLevel,errorLevelReport); |
| 82 |
errorSyncMutex.unlock(); |
| 83 |
if (superCommand && superCommand->hasReportErrorFromSubCommands()) { |
| 84 |
superCommand->reportError(errorLevelReport); |
| 85 |
} |
| 86 |
} |
| 87 |
return this; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
CLinker<CCommand *> *CCommand::getSubCommandLinker() { |
| 92 |
return subCommandLinker; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
CCommand *CCommand::addSubCommand(CCommand *subCommand) { |
| 97 |
subCommandLinker = new CLinker<CCommand *>(subCommand,subCommandLinker); |
| 98 |
return this; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
CCommand *CCommand::makeToSubCommand(CCommand *subCommand) { |
| 103 |
++mUnclosedCommandCount; |
| 104 |
addSubCommand(subCommand); |
| 105 |
subCommand->setSuperCommand(this); |
| 106 |
subCommand->setRecorder(recorder); |
| 107 |
return this; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
CCommand *CCommand::getSuperCommand() { |
| 112 |
return superCommand; |
| 113 |
} |
| 114 |
|
| 115 |
CCommand *CCommand::setSuperCommand(CCommand *parentSuperCommand) { |
| 116 |
superCommand = parentSuperCommand; |
| 117 |
return this; |
| 118 |
} |
| 119 |
|
| 120 |
CCommand *CCommand::setProcessed(bool processedCommand) { |
| 121 |
if (processed) { |
| 122 |
closureSyncMutex.lock(); |
| 123 |
mUnclosedCommandCount++; |
| 124 |
processed = processedCommand; |
| 125 |
closureSyncMutex.unlock(); |
| 126 |
} else { |
| 127 |
processed = processedCommand; |
| 128 |
reduceUnclosedCheckCallback(); |
| 129 |
} |
| 130 |
return this; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
bool CCommand::areAllSubCommandsProcessed() { |
| 135 |
bool allSubCommPro = true; |
| 136 |
CLinker<CCommand *> *subCommIt = subCommandLinker; |
| 137 |
while (subCommIt) { |
| 138 |
CCommand *subCommand = subCommIt->getData(); |
| 139 |
if (!subCommand->isProcessed()) { |
| 140 |
allSubCommPro = false; |
| 141 |
break; |
| 142 |
} |
| 143 |
subCommIt = (CLinker<CCommand *> *)subCommIt->getNext(); |
| 144 |
} |
| 145 |
return allSubCommPro; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
bool CCommand::isProcessed() { |
| 150 |
return mUnclosedCommandCount <= 0; |
| 151 |
} |
| 152 |
|
| 153 |
bool CCommand::isThisCommandProcessed() { |
| 154 |
return processed; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
CCommand *CCommand::getNextProcessableSubCommand() { |
| 159 |
CLinker<CCommand *> *subCommandIt = subCommandLinker; |
| 160 |
while (subCommandIt) { |
| 161 |
CCommand *subCommand = subCommandIt->getData(); |
| 162 |
if (subCommand && subCommand->isProcessable() && !subCommand->isProcessing()) { |
| 163 |
return subCommand; |
| 164 |
} |
| 165 |
} |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
QString CCommand::getBriefCommandDescription() { |
| 171 |
return QString("Undefined Command"); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
CCommand *CCommand::setProcessing(bool processingCommand) { |
| 177 |
processing = processingCommand; |
| 178 |
return this; |
| 179 |
} |
| 180 |
|
| 181 |
bool CCommand::isProcessing() { |
| 182 |
return processing; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
CCommandRecordData *CCommand::getRecordData() { |
| 188 |
return recordData; |
| 189 |
} |
| 190 |
|
| 191 |
CCommand *CCommand::setRecordData(CCommandRecordData *takeRecordData) { |
| 192 |
if (recordData) { |
| 193 |
delete recordData; |
| 194 |
} |
| 195 |
recordData = takeRecordData; |
| 196 |
return this; |
| 197 |
} |
| 198 |
|
| 199 |
CCommand *CCommand::overwriteRecordData(CCommandRecordData *takeRecordData) { |
| 200 |
recordData = takeRecordData; |
| 201 |
return this; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
CCommand *CCommand::appendRecordData(CCommandRecordData *takeRecordData) { |
| 206 |
takeRecordData->setNextRecordData(recordData); |
| 207 |
recordData = takeRecordData; |
| 208 |
return this; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
CCommandRecorder *CCommand::getRecorder() { |
| 214 |
return recorder; |
| 215 |
} |
| 216 |
|
| 217 |
CCommand *CCommand::setRecorder(CCommandRecorder *useCommandRecorder) { |
| 218 |
recorder = useCommandRecorder; |
| 219 |
return this; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
CCommand *CCommand::deleteDependencies() { |
| 224 |
deleteSubCommands(); |
| 225 |
deleteRecordData(); |
| 226 |
return this; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
CCommand *CCommand::deleteSubCommands() { |
| 231 |
CLinker<CCommand *> *subCommandIt = subCommandLinker; |
| 232 |
while (subCommandIt) { |
| 233 |
CCommand *subCommand = subCommandIt->getData(); |
| 234 |
subCommand->deleteDependencies(); |
| 235 |
delete subCommand; |
| 236 |
CLinker<CCommand *> *tmpSubCommIt = subCommandIt; |
| 237 |
subCommandIt = (CLinker<CCommand *> *)subCommandIt->getNext(); |
| 238 |
delete tmpSubCommIt; |
| 239 |
} |
| 240 |
return this; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
CCommand *CCommand::deleteRecordData() { |
| 245 |
CCommandRecordData *recordIt = recordData; |
| 246 |
while (recordIt) { |
| 247 |
CCommandRecordData *tmpRecordData = recordIt; |
| 248 |
recordIt = recordIt->getNextCommandRecordData(); |
| 249 |
delete tmpRecordData; |
| 250 |
} |
| 251 |
return this; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
CCommand *CCommand::addProcessedCallback(CCallbackData *callback) { |
| 256 |
if (isProcessed()) { |
| 257 |
callback->doCallback(); |
| 258 |
} else { |
| 259 |
processedCallbackExecuter.addCallbackData(callback); |
| 260 |
} |
| 261 |
return this; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
CCommand* CCommand::closedSubCommand() { |
| 266 |
reduceUnclosedCheckCallback(); |
| 267 |
return this; |
| 268 |
} |
| 269 |
|
| 270 |
CCommand *CCommand::reduceUnclosedCheckCallback() { |
| 271 |
closureSyncMutex.lock(); |
| 272 |
mUnclosedCommandCount--; |
| 273 |
bool closeCommand = false; |
| 274 |
if (mUnclosedCommandCount == 0) { |
| 275 |
closeCommand = true; |
| 276 |
} |
| 277 |
closureSyncMutex.unlock(); |
| 278 |
if (!closured && closeCommand) { |
| 279 |
CClosureProcessCommandRecord::makeRecord(recorder,"::Konclude::Command::Command",this); |
| 280 |
closured = true; |
| 281 |
CCommand* tmpSuperCommand = superCommand; |
| 282 |
processedCallbackExecuter.doCallback(); |
| 283 |
if (tmpSuperCommand) { |
| 284 |
tmpSuperCommand->closedSubCommand(); |
| 285 |
} |
| 286 |
} |
| 287 |
return this; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
}; // end namespace Command |
| 292 |
|
| 293 |
}; // end namespace Control |
| 294 |
|
| 295 |
}; // end namespace Konclude |