| 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 "CTaxonomy.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Taxonomy { |
| 29 |
|
| 30 |
|
| 31 |
CTaxonomy::CTaxonomy(CHierarchyNode *topHierarchyNode, CHierarchyNode *bottomHierarchyNode) { |
| 32 |
topNode = topHierarchyNode; |
| 33 |
bottomNode = bottomHierarchyNode; |
| 34 |
topNode->makeChild(bottomNode); |
| 35 |
nodeContainer.append(topNode); |
| 36 |
nodeContainer.append(bottomNode); |
| 37 |
topIsUnsatisfiable = false; |
| 38 |
foreach (CConcept *con, *topNode->getEquivalentConceptList()) { |
| 39 |
conNodeHash.insert(con,topNode); |
| 40 |
} |
| 41 |
foreach (CConcept *con, *bottomNode->getEquivalentConceptList()) { |
| 42 |
conNodeHash.insert(con,bottomNode); |
| 43 |
} |
| 44 |
taxonomyComplete = false; |
| 45 |
} |
| 46 |
|
| 47 |
CTaxonomy::CTaxonomy(CConcept *topConcept, CConcept *bottomConcept) { |
| 48 |
topNode = createHierarchyNode(topConcept); |
| 49 |
bottomNode = createHierarchyNode(bottomConcept); |
| 50 |
topNode->makeChild(bottomNode); |
| 51 |
nodeContainer.append(topNode); |
| 52 |
nodeContainer.append(bottomNode); |
| 53 |
topIsUnsatisfiable = false; |
| 54 |
foreach (CConcept *con, *topNode->getEquivalentConceptList()) { |
| 55 |
conNodeHash.insert(con,topNode); |
| 56 |
} |
| 57 |
foreach (CConcept *con, *bottomNode->getEquivalentConceptList()) { |
| 58 |
conNodeHash.insert(con,bottomNode); |
| 59 |
} |
| 60 |
taxonomyComplete = false; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
CTaxonomy::~CTaxonomy() { |
| 66 |
qDeleteAll(nodeContainer); |
| 67 |
} |
| 68 |
|
| 69 |
bool CTaxonomy::isSubsumption(CConcept *subsumerConcept, CConcept *subsumedConcept) { |
| 70 |
if (topIsUnsatisfiable) { |
| 71 |
return true; |
| 72 |
} |
| 73 |
bool subsumed = false; |
| 74 |
CHierarchyNode *subsumerNode = conNodeHash.value(subsumerConcept); |
| 75 |
if (subsumerNode) { |
| 76 |
CHierarchyNode *subsumedNode = conNodeHash.value(subsumedConcept); |
| 77 |
if (subsumedNode) { |
| 78 |
return subsumerNode == subsumedNode || subsumerNode->hasPredecessorNode(subsumedNode); |
| 79 |
} |
| 80 |
} |
| 81 |
return subsumed; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
bool CTaxonomy::isSatisfiable(CConcept *concept) { |
| 87 |
if (topIsUnsatisfiable) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
CHierarchyNode *node = conNodeHash.value(concept); |
| 91 |
if (node) { |
| 92 |
if (node == topNode) { |
| 93 |
return topNode->hasIncompatiblePredecessorNode(bottomNode); |
| 94 |
} |
| 95 |
return node != bottomNode; |
| 96 |
} |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
bool CTaxonomy::isNotSatisfiable(CConcept *concept) { |
| 101 |
if (topIsUnsatisfiable) { |
| 102 |
return true; |
| 103 |
} |
| 104 |
CHierarchyNode *node = conNodeHash.value(concept); |
| 105 |
if (node) { |
| 106 |
return node == bottomNode; |
| 107 |
} |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
bool CTaxonomy::isTopHierarchyNode(CConcept *concept) { |
| 112 |
if (topIsUnsatisfiable) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
CHierarchyNode *node = conNodeHash.value(concept); |
| 116 |
if (node) { |
| 117 |
return node == topNode; |
| 118 |
} |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
bool CTaxonomy::isBottomHierarchyNode(CConcept *concept) { |
| 123 |
if (topIsUnsatisfiable) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
CHierarchyNode *node = conNodeHash.value(concept); |
| 127 |
if (node) { |
| 128 |
return node == bottomNode; |
| 129 |
} |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
bool CTaxonomy::isEquivalence(CConcept *concept1, CConcept *concept2) { |
| 134 |
if (topIsUnsatisfiable) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
CHierarchyNode *node1 = conNodeHash.value(concept1); |
| 138 |
CHierarchyNode *node2 = conNodeHash.value(concept2); |
| 139 |
if (node1 && node2) { |
| 140 |
return node1 == node2; |
| 141 |
} else { |
| 142 |
return false; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
bool CTaxonomy::isDisjoint(CConcept *disjointConcept1, CConcept *disjointConcept2) { |
| 148 |
if (topIsUnsatisfiable) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
CHierarchyNode *node1 = conNodeHash.value(disjointConcept1); |
| 152 |
CHierarchyNode *node2 = conNodeHash.value(disjointConcept2); |
| 153 |
if (node1 && node2) { |
| 154 |
if (node1 == node2) { |
| 155 |
return false; |
| 156 |
} else { |
| 157 |
return node1->hasDisjointNode(node2); |
| 158 |
} |
| 159 |
} else { |
| 160 |
return false; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
bool CTaxonomy::isNotDisjoint(CConcept *disjointConcept1, CConcept *disjointConcept2) { |
| 165 |
if (topIsUnsatisfiable) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
CHierarchyNode *node1 = conNodeHash.value(disjointConcept1); |
| 169 |
CHierarchyNode *node2 = conNodeHash.value(disjointConcept2); |
| 170 |
if (node1 && node2) { |
| 171 |
if (node1 == node2) { |
| 172 |
return true; |
| 173 |
} else if (node1->hasPredecessorNode(node2)) { |
| 174 |
return true; |
| 175 |
} else if (node2->hasPredecessorNode(node1)) { |
| 176 |
return true; |
| 177 |
} |
| 178 |
} |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
CHierarchyNode *CTaxonomy::getTopHierarchyNode() { |
| 184 |
return topNode; |
| 185 |
} |
| 186 |
|
| 187 |
CHierarchyNode *CTaxonomy::getBottomHierarchyNode() { |
| 188 |
return bottomNode; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
CHierarchyNode *CTaxonomy::createHierarchyNode(CConcept *concept) { |
| 194 |
return new CHierarchyNode(concept); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
CHierarchyNode *CTaxonomy::getHierarchyNode(CConcept *concept, bool create) { |
| 199 |
CHierarchyNode *node = 0; |
| 200 |
if (concept) { |
| 201 |
node = conNodeHash.value(concept,0); |
| 202 |
if (topIsUnsatisfiable) { |
| 203 |
if (node != bottomNode && (node || create)) { |
| 204 |
conNodeHash.insert(concept,bottomNode); |
| 205 |
bottomNode->addEquivalentConcept(concept); |
| 206 |
} |
| 207 |
} |
| 208 |
if (!node && create) { |
| 209 |
node = createHierarchyNode(concept); |
| 210 |
conNodeHash.insert(concept,node); |
| 211 |
nodeContainer.append(node); |
| 212 |
} |
| 213 |
} |
| 214 |
return node; |
| 215 |
} |
| 216 |
|
| 217 |
bool CTaxonomy::hasHierarchyNode(CConcept *concept) { |
| 218 |
return conNodeHash.contains(concept); |
| 219 |
} |
| 220 |
|
| 221 |
bool CTaxonomy::isNotSubsumption(CConcept *notSubsumerConcept, CConcept *notSubsumedConcept) { |
| 222 |
if (topIsUnsatisfiable) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
bool notSubsumed = false; |
| 226 |
CHierarchyNode *notSubsumerNode = conNodeHash.value(notSubsumerConcept); |
| 227 |
if (notSubsumerNode) { |
| 228 |
CHierarchyNode *notSubsumedNode = conNodeHash.value(notSubsumedConcept); |
| 229 |
if (notSubsumedNode) { |
| 230 |
return notSubsumerNode != notSubsumedNode && notSubsumerNode->hasIncompatiblePredecessorNode(notSubsumedNode); |
| 231 |
} |
| 232 |
} |
| 233 |
return notSubsumed; |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
QString CTaxonomy::getConceptHierarchyString() { |
| 238 |
QString string = topNode->getHierarchyConceptsNodeString(0); |
| 239 |
return string; |
| 240 |
} |
| 241 |
|
| 242 |
bool CTaxonomy::isComplete() { |
| 243 |
return taxonomyComplete; |
| 244 |
} |
| 245 |
|
| 246 |
CTaxonomy *CTaxonomy::setTaxonomyComplete(bool completed) { |
| 247 |
taxonomyComplete = completed; |
| 248 |
return this;; |
| 249 |
} |
| 250 |
|
| 251 |
bool CTaxonomy::isActiveNode(CHierarchyNode *node) { |
| 252 |
return node->isActive(); |
| 253 |
} |
| 254 |
|
| 255 |
CTaxonomy *CTaxonomy::setActiveNode(CHierarchyNode *node, bool nodeActive) { |
| 256 |
node->setActive(nodeActive); |
| 257 |
if (nodeActive) { |
| 258 |
activeNodeSet.insert(node); |
| 259 |
} else { |
| 260 |
activeNodeSet.remove(node); |
| 261 |
} |
| 262 |
return this; |
| 263 |
} |
| 264 |
|
| 265 |
CTaxonomy *CTaxonomy::setInactiveNode(CHierarchyNode *node, bool nodeInactive) { |
| 266 |
node->setInactive(nodeInactive); |
| 267 |
if (!nodeInactive) { |
| 268 |
activeNodeSet.insert(node); |
| 269 |
} else { |
| 270 |
activeNodeSet.remove(node); |
| 271 |
} |
| 272 |
return this; |
| 273 |
} |
| 274 |
|
| 275 |
CHierarchyNode *CTaxonomy::getActiveNode(CHierarchyNode *node) { |
| 276 |
if (node->isActive()) { |
| 277 |
return node; |
| 278 |
} else { |
| 279 |
CConcept *con = node->getOneEquivalentConcept(); |
| 280 |
return getHierarchyNode(con,false); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
CTaxonomy *CTaxonomy::updateNodeEquivalences(CHierarchyNode *node) { |
| 285 |
foreach (CConcept *con, *node->getEquivalentConceptList()) { |
| 286 |
conNodeHash.insert(con,node); |
| 287 |
} |
| 288 |
return this; |
| 289 |
} |
| 290 |
|
| 291 |
QHash<CConcept*,CHierarchyNode*>* CTaxonomy::getConceptHierarchyNodeHash() { |
| 292 |
return &conNodeHash; |
| 293 |
} |
| 294 |
|
| 295 |
}; // end namespace Taxonomy |
| 296 |
|
| 297 |
}; // end namespace Reasoner |
| 298 |
|
| 299 |
}; // end namespace Konclude |