| 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 "CDisjunctSortingPreProcess.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Preprocess { |
| 29 |
|
| 30 |
|
| 31 |
CDisjunctSortingPreProcess::CDisjunctSortingPreProcess() { |
| 32 |
mOnto = nullptr; |
| 33 |
mLastConceptId = 0; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
CDisjunctSortingPreProcess::~CDisjunctSortingPreProcess() { |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
CConcreteOntology *CDisjunctSortingPreProcess::preprocess(CConcreteOntology *ontology, CPreProcessContext* context) { |
| 42 |
|
| 43 |
mOnto = nullptr; |
| 44 |
mLastConceptId = 0; |
| 45 |
|
| 46 |
bool skipForELOntologies = CConfigDataReader::readConfigBoolean(context->getConfiguration(),"Konclude.Calculation.Preprocessing.DisjunctSorting.SkipForELFragment",true); |
| 47 |
bool nonELConstructsUsed = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getBuildConstructFlags()->isNonELConstructUsed(); |
| 48 |
|
| 49 |
if (nonELConstructsUsed || !skipForELOntologies) { |
| 50 |
|
| 51 |
mTBox = ontology->getDataBoxes()->getTBox(); |
| 52 |
|
| 53 |
mConceptVec = mTBox->getConceptVector(); |
| 54 |
|
| 55 |
mMemMan = ontology->getDataBoxes()->getBoxContext()->getMemoryAllocationManager(); |
| 56 |
mOnto = ontology; |
| 57 |
|
| 58 |
mSortedDisjunctCount = 0; |
| 59 |
mPotentialDisjunctionCount = 0; |
| 60 |
|
| 61 |
sortDisjuncts(); |
| 62 |
|
| 63 |
LOG(INFO,"::Konclude::Reasoner::Preprocess::DisjunctSorting",logTr("Sorted %1 disjuncts for %2 possible disjunctions.").arg(mSortedDisjunctCount).arg(mPotentialDisjunctionCount),this); |
| 64 |
} |
| 65 |
return ontology; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
CConcreteOntology* CDisjunctSortingPreProcess::continuePreprocessing() { |
| 70 |
if (mOnto) { |
| 71 |
sortDisjuncts(); |
| 72 |
} |
| 73 |
return mOnto; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
CDisjunctSortingPreProcess* CDisjunctSortingPreProcess::sortDisjuncts() { |
| 79 |
|
| 80 |
cint64 conCount = mTBox->getConceptCount(); |
| 81 |
for (cint64 conIdx = mLastConceptId; conIdx < conCount; ++conIdx) { |
| 82 |
CConcept* concept = mConceptVec->getLocalData(conIdx); |
| 83 |
if (concept) { |
| 84 |
cint64 conOpCode = concept->getOperatorCode(); |
| 85 |
cint64 opCount = concept->getOperandCount(); |
| 86 |
if (opCount > 1) { |
| 87 |
bool sortOperands = false; |
| 88 |
bool disjunctionNegation = false; |
| 89 |
if (conOpCode == CCAND || conOpCode == CCEQ) { |
| 90 |
sortOperands = true; |
| 91 |
disjunctionNegation = true; |
| 92 |
} |
| 93 |
else if (conOpCode == CCOR) { |
| 94 |
sortOperands = true; |
| 95 |
} |
| 96 |
if (sortOperands) { |
| 97 |
++mPotentialDisjunctionCount; |
| 98 |
sortDisjuncts(concept, disjunctionNegation); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
mLastConceptId = conCount; |
| 104 |
return this; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
void CDisjunctSortingPreProcess::addDisjunctToSort(CSortedNegLinker<CConcept*>* conLinker, bool negated) { |
| 110 |
CConcept* concept = conLinker->getData(); |
| 111 |
bool conNegation = conLinker->isNegated() ^ negated; |
| 112 |
cint64 conCode = concept->getOperatorCode(); |
| 113 |
CConceptOperator* conceptOperator = concept->getConceptOperator(); |
| 114 |
if (conCode == CCATOM) { |
| 115 |
mAtomicDisjList.append(conLinker); |
| 116 |
} else if (conNegation && conCode == CCSUB) { |
| 117 |
mPrimNegDisjList.append(conLinker); |
| 118 |
//} else if (!conNegation && conCode == CCSUB) { |
| 119 |
// mPrimDisjList.append(conLinker); |
| 120 |
} else if (!conNegation && (conCode == CCSELF || conCode == CCVALUE)) { |
| 121 |
mSimpleExistDisjList.append(conLinker); |
| 122 |
} else if (!conNegation && (conCode == CCSOME || conCode == CCAQSOME || conCode == CCAQCHOOCE || conCode == CCATLEAST) || conNegation && (conCode == CCALL || conCode == CCATMOST)) { |
| 123 |
mExistDisjList.append(conLinker); |
| 124 |
} else if (!conNegation && conCode == CCNOMINAL) { |
| 125 |
mPosNominalDisjList.append(conLinker); |
| 126 |
} else if (conNegation && conCode == CCNOMINAL) { |
| 127 |
mNegNominalDisjList.append(conLinker); |
| 128 |
} else { |
| 129 |
if (!conNegation && conceptOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_ALL_AQALL_TYPE) || conNegation && conceptOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_SOME_TYPE)) { |
| 130 |
mPropDisjList.append(conLinker); |
| 131 |
} else if (!conNegation && conceptOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_TRIG_AND_AQAND_TYPE)) { |
| 132 |
mTriggerDisjList.append(conLinker); |
| 133 |
} else if (!conNegation && conceptOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_IMPL_TYPE)) { |
| 134 |
mImplDisjList.append(conLinker); |
| 135 |
} else { |
| 136 |
mOtherDisjList.append(conLinker); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
void CDisjunctSortingPreProcess::sortDisjuncts(CConcept* concept, bool negated) { |
| 143 |
CSortedNegLinker<CConcept*>* opConcepts = concept->getOperandList(); |
| 144 |
mAtomicDisjList.clear(); |
| 145 |
mExistDisjList.clear(); |
| 146 |
mNegNominalDisjList.clear(); |
| 147 |
mPosNominalDisjList.clear(); |
| 148 |
mPrimDisjList.clear(); |
| 149 |
mPrimNegDisjList.clear(); |
| 150 |
mSimpleExistDisjList.clear(); |
| 151 |
mPropDisjList.clear(); |
| 152 |
mTriggerDisjList.clear(); |
| 153 |
mImplDisjList.clear(); |
| 154 |
mOtherDisjList.clear(); |
| 155 |
for (CSortedNegLinker<CConcept*>* opConceptIt = concept->getOperandList(); opConceptIt; opConceptIt = opConceptIt->getNext()) { |
| 156 |
CSortedNegLinker<CConcept*>* opConcept = opConceptIt; |
| 157 |
addDisjunctToSort(opConcept,negated); |
| 158 |
++mSortedDisjunctCount; |
| 159 |
} |
| 160 |
|
| 161 |
CSortedNegLinker<CConcept*>* newOperandLinker = nullptr; |
| 162 |
CSortedNegLinker<CConcept*>* lastNewOperandLinker = nullptr; |
| 163 |
addDisjunctFromList(mAtomicDisjList,newOperandLinker,lastNewOperandLinker); |
| 164 |
addDisjunctFromList(mPrimNegDisjList,newOperandLinker,lastNewOperandLinker); |
| 165 |
addDisjunctFromList(mSimpleExistDisjList,newOperandLinker,lastNewOperandLinker); |
| 166 |
addDisjunctFromList(mNegNominalDisjList,newOperandLinker,lastNewOperandLinker); |
| 167 |
addDisjunctFromList(mImplDisjList,newOperandLinker,lastNewOperandLinker); |
| 168 |
addDisjunctFromList(mTriggerDisjList,newOperandLinker,lastNewOperandLinker); |
| 169 |
addDisjunctFromList(mPropDisjList,newOperandLinker,lastNewOperandLinker); |
| 170 |
addDisjunctFromList(mOtherDisjList,newOperandLinker,lastNewOperandLinker); |
| 171 |
addDisjunctFromList(mExistDisjList,newOperandLinker,lastNewOperandLinker); |
| 172 |
addDisjunctFromList(mPosNominalDisjList,newOperandLinker,lastNewOperandLinker); |
| 173 |
addDisjunctFromList(mPrimDisjList, newOperandLinker, lastNewOperandLinker); |
| 174 |
concept->setOperandList(newOperandLinker); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
void CDisjunctSortingPreProcess::addDisjunctFromList(QList< CSortedNegLinker<CConcept*>* >& disjunctList, CSortedNegLinker<CConcept*>*& newOperandLinker, CSortedNegLinker<CConcept*>*& lastNewOperandLinker) { |
| 179 |
for (QList< CSortedNegLinker<CConcept*>* >::const_iterator it = disjunctList.constBegin(), itEnd = disjunctList.constEnd(); it != itEnd; ++it) { |
| 180 |
CSortedNegLinker<CConcept*>* opConLinker(*it); |
| 181 |
opConLinker->setNext(nullptr); |
| 182 |
if (!newOperandLinker) { |
| 183 |
newOperandLinker = opConLinker; |
| 184 |
} else { |
| 185 |
lastNewOperandLinker->setNext(opConLinker); |
| 186 |
} |
| 187 |
lastNewOperandLinker = opConLinker; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
}; // end namespace Preprocess |
| 194 |
|
| 195 |
}; // end namespace Reasoner |
| 196 |
|
| 197 |
}; // end namespace Konclude |