| 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 "CBranchStatisticsExtenderPreProcess.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Preprocess { |
| 29 |
|
| 30 |
|
| 31 |
CBranchStatisticsExtenderPreProcess::CBranchStatisticsExtenderPreProcess() { |
| 32 |
mOnto = nullptr; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
CBranchStatisticsExtenderPreProcess::~CBranchStatisticsExtenderPreProcess() { |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
CConcreteOntology *CBranchStatisticsExtenderPreProcess::preprocess(CConcreteOntology *ontology, CPreProcessContext* context) { |
| 41 |
|
| 42 |
mOnto = nullptr; |
| 43 |
mLastConceptId = 0; |
| 44 |
|
| 45 |
bool skipForELOntologies = CConfigDataReader::readConfigBoolean(context->getConfiguration(),"Konclude.Calculation.Preprocessing.BranchingStatisticsExtender.SkipForELFragment",true); |
| 46 |
bool nonELConstructsUsed = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getBuildConstructFlags()->isNonELConstructUsed(); |
| 47 |
|
| 48 |
if (nonELConstructsUsed || !skipForELOntologies) { |
| 49 |
|
| 50 |
mTBox = ontology->getDataBoxes()->getTBox(); |
| 51 |
|
| 52 |
mConceptVec = mTBox->getConceptVector(); |
| 53 |
|
| 54 |
mMemMan = ontology->getDataBoxes()->getBoxContext()->getMemoryAllocationManager(); |
| 55 |
mOnto = ontology; |
| 56 |
|
| 57 |
mExtendedDisjunctionsBranchingStatistics = 0; |
| 58 |
mExtendedDisjunctsBranchingStatistics = 0; |
| 59 |
|
| 60 |
extendDisjunctions(); |
| 61 |
|
| 62 |
|
| 63 |
LOG(INFO,"::Konclude::Reasoner::Preprocess::BranchStatisticsExtender",logTr("Extended %1 potential disjunctions with branching statistics for %2 disjuncts.").arg(mExtendedDisjunctionsBranchingStatistics).arg(mExtendedDisjunctsBranchingStatistics),this); |
| 64 |
} |
| 65 |
return ontology; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
CConcreteOntology* CBranchStatisticsExtenderPreProcess::continuePreprocessing() { |
| 70 |
if (mOnto) { |
| 71 |
extendDisjunctions(); |
| 72 |
} |
| 73 |
return mOnto; |
| 74 |
} |
| 75 |
|
| 76 |
CBranchStatisticsExtenderPreProcess* CBranchStatisticsExtenderPreProcess::extendDisjunctions() { |
| 77 |
cint64 conCount = mTBox->getConceptCount(); |
| 78 |
for (cint64 conIdx = mLastConceptId; conIdx < conCount; ++conIdx) { |
| 79 |
CConcept* concept = mConceptVec->getLocalData(conIdx); |
| 80 |
if (concept) { |
| 81 |
cint64 conOpCode = concept->getOperatorCode(); |
| 82 |
cint64 opCount = concept->getOperandCount(); |
| 83 |
if (opCount > 1) { |
| 84 |
bool extendBranchStats = false; |
| 85 |
bool disjunctionNegation = false; |
| 86 |
if (conOpCode == CCAND || conOpCode == CCEQ) { |
| 87 |
extendBranchStats = true; |
| 88 |
disjunctionNegation = true; |
| 89 |
} else if (conOpCode == CCOR) { |
| 90 |
extendBranchStats = true; |
| 91 |
} |
| 92 |
if (extendBranchStats) { |
| 93 |
++mExtendedDisjunctionsBranchingStatistics; |
| 94 |
extendDisjunction(concept, disjunctionNegation); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
mLastConceptId = conCount; |
| 100 |
return this; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
CBranchStatisticsExtenderPreProcess* CBranchStatisticsExtenderPreProcess::extendDisjunction(CConcept* concept, bool disjunctionNegation) { |
| 105 |
CConceptProcessData* conData = (CConceptProcessData*)concept->getConceptData(); |
| 106 |
if (conData) { |
| 107 |
if (!conData->getBranchingStatistics()) { |
| 108 |
CDisjunctionBranchingStatistics* disjunctionBranchingStats = CObjectAllocator<CDisjunctionBranchingStatistics>::allocateAndConstruct(mMemMan); |
| 109 |
disjunctionBranchingStats->initDisjunctionBranchingStatistics(); |
| 110 |
CDisjunctBranchingStatistics* firstDisjunctBranchStats = nullptr; |
| 111 |
CDisjunctBranchingStatistics* lastDisjunctBranchStats = nullptr; |
| 112 |
for (CSortedNegLinker<CConcept*>* opConLinkerIt = concept->getOperandList(); opConLinkerIt; opConLinkerIt = opConLinkerIt->getNext()) { |
| 113 |
CConcept* opConcept = opConLinkerIt->getData(); |
| 114 |
CDisjunctBranchingStatistics* disjunctBranchingStats = CObjectAllocator<CDisjunctBranchingStatistics>::allocateAndConstruct(mMemMan); |
| 115 |
disjunctBranchingStats->initDisjunctBranchingStatistics(opConLinkerIt); |
| 116 |
if (!firstDisjunctBranchStats) { |
| 117 |
firstDisjunctBranchStats = disjunctBranchingStats; |
| 118 |
} |
| 119 |
if (lastDisjunctBranchStats) { |
| 120 |
lastDisjunctBranchStats->setNext(disjunctBranchingStats); |
| 121 |
} |
| 122 |
lastDisjunctBranchStats = disjunctBranchingStats; |
| 123 |
} |
| 124 |
disjunctionBranchingStats->setDisjunctBranchingStatistics(firstDisjunctBranchStats); |
| 125 |
conData->setBranchingStatistics(disjunctionBranchingStats); |
| 126 |
} |
| 127 |
} |
| 128 |
return this; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
}; // end namespace Preprocess |
| 133 |
|
| 134 |
}; // end namespace Reasoner |
| 135 |
|
| 136 |
}; // end namespace Konclude |