| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef MODULARITY_H |
| 21 |
#define MODULARITY_H |
| 22 |
|
| 23 |
#include <queue> |
| 24 |
|
| 25 |
|
| 26 |
#define RKG_USE_AD_IN_MODULE_EXTRACTION |
| 27 |
|
| 28 |
#include "tOntology.h" |
| 29 |
#include "SigIndex.h" |
| 30 |
#include "LocalityChecker.h" |
| 31 |
|
| 32 |
#include "ModuleType.h" |
| 33 |
|
| 34 |
#ifdef RKG_USE_AD_IN_MODULE_EXTRACTION |
| 35 |
# include "tOntologyAtom.h" |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
class TModularizer |
| 40 |
{ |
| 41 |
protected: |
| 42 |
|
| 43 |
typedef AxiomVec::const_iterator const_iterator; |
| 44 |
|
| 45 |
protected: |
| 46 |
|
| 47 |
TSignature sig; |
| 48 |
|
| 49 |
LocalityChecker* Checker; |
| 50 |
|
| 51 |
AxiomVec Module; |
| 52 |
|
| 53 |
SigIndex sigIndex; |
| 54 |
|
| 55 |
std::queue<const TNamedEntity*> WorkQueue; |
| 56 |
|
| 57 |
unsigned long long nChecks = 0; |
| 58 |
|
| 59 |
unsigned long long nNonLocal = 0; |
| 60 |
|
| 61 |
bool noAtomsProcessing = true; |
| 62 |
|
| 63 |
protected: |
| 64 |
|
| 65 |
void addAxiomSig ( const TSignature& axiomSig ) |
| 66 |
{ |
| 67 |
for ( const TNamedEntity* entity : axiomSig ) |
| 68 |
if ( !sig.contains(entity) ) |
| 69 |
{ |
| 70 |
WorkQueue.push(entity); |
| 71 |
sig.add(entity); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void addAxiomToModule ( TDLAxiom* axiom ) |
| 76 |
{ |
| 77 |
axiom->setInModule(true); |
| 78 |
Module.push_back(axiom); |
| 79 |
|
| 80 |
addAxiomSig(axiom->getSignature()); |
| 81 |
} |
| 82 |
|
| 83 |
bool isNonLocal ( const TDLAxiom* ax ) |
| 84 |
{ |
| 85 |
++nChecks; |
| 86 |
if ( Checker->local(ax) ) |
| 87 |
return false; |
| 88 |
++nNonLocal; |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
void addNonLocal ( TDLAxiom* ax, bool noCheck ) |
| 94 |
{ |
| 95 |
if ( unlikely(noCheck) || unlikely(isNonLocal(ax)) ) |
| 96 |
{ |
| 97 |
addAxiomToModule(ax); |
| 98 |
|
| 99 |
# ifdef RKG_USE_AD_IN_MODULE_EXTRACTION |
| 100 |
if ( noAtomsProcessing && ax->getAtom() != nullptr ) |
| 101 |
{ |
| 102 |
noAtomsProcessing = false; |
| 103 |
addNonLocal ( ax->getAtom()->getModule(), true ); |
| 104 |
noAtomsProcessing = true; |
| 105 |
} |
| 106 |
# endif |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void addNonLocal ( const AxiomVec& AxSet, bool noCheck ) |
| 111 |
{ |
| 112 |
for ( TDLAxiom* axiom : AxSet ) |
| 113 |
if ( !axiom->isInModule() && axiom->isInSS() ) |
| 114 |
addNonLocal ( axiom, noCheck ); |
| 115 |
} |
| 116 |
|
| 117 |
void extractModuleQueue ( void ) |
| 118 |
{ |
| 119 |
|
| 120 |
for ( const TNamedEntity* entity : sig ) |
| 121 |
WorkQueue.push(entity); |
| 122 |
|
| 123 |
addNonLocal ( sigIndex.getNonLocal(sig.topCLocal()), true ); |
| 124 |
|
| 125 |
while ( !WorkQueue.empty() ) |
| 126 |
{ |
| 127 |
const TNamedEntity* entity = WorkQueue.front(); |
| 128 |
WorkQueue.pop(); |
| 129 |
|
| 130 |
addNonLocal ( sigIndex.getAxioms(entity), false ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void extractModule ( const_iterator begin, const_iterator end ) |
| 135 |
{ |
| 136 |
size_t size = (size_t)(end-begin); |
| 137 |
Module.clear(); |
| 138 |
Module.reserve(size); |
| 139 |
|
| 140 |
const_iterator p; |
| 141 |
for ( p = begin; p != end; ++p ) |
| 142 |
(*p)->setInModule(false); |
| 143 |
for ( p = begin; p != end; ++p ) |
| 144 |
if ( (*p)->isUsed() ) |
| 145 |
(*p)->setInSS(true); |
| 146 |
extractModuleQueue(); |
| 147 |
for ( p = begin; p != end; ++p ) |
| 148 |
(*p)->setInSS(false); |
| 149 |
} |
| 150 |
|
| 151 |
public: |
| 152 |
|
| 153 |
explicit TModularizer ( ModuleMethod moduleMethod ) |
| 154 |
: Checker(createLocalityChecker(moduleMethod,&sig)) |
| 155 |
, sigIndex(Checker) |
| 156 |
{} |
| 157 |
|
| 158 |
~TModularizer() { delete Checker; } |
| 159 |
|
| 160 |
|
| 161 |
void preprocessOntology ( const AxiomVec& vec ) |
| 162 |
{ |
| 163 |
Checker->preprocessOntology(vec); |
| 164 |
sigIndex.clear(); |
| 165 |
sigIndex.preprocessOntology(vec); |
| 166 |
nChecks += 2*vec.size(); |
| 167 |
} |
| 168 |
|
| 169 |
void extract ( const_iterator begin, const_iterator end, const TSignature& signature, ModuleType type ) |
| 170 |
{ |
| 171 |
bool topLocality = (type == M_TOP); |
| 172 |
|
| 173 |
sig = signature; |
| 174 |
sig.setLocality(topLocality); |
| 175 |
extractModule ( begin, end ); |
| 176 |
|
| 177 |
if ( type != M_STAR ) |
| 178 |
return; |
| 179 |
|
| 180 |
|
| 181 |
size_t size; |
| 182 |
AxiomVec oldModule; |
| 183 |
do |
| 184 |
{ |
| 185 |
size = Module.size(); |
| 186 |
oldModule.swap(Module); |
| 187 |
topLocality = !topLocality; |
| 188 |
|
| 189 |
sig = signature; |
| 190 |
sig.setLocality(topLocality); |
| 191 |
extractModule ( oldModule.begin(), oldModule.end() ); |
| 192 |
} while ( size != Module.size() ); |
| 193 |
} |
| 194 |
|
| 195 |
void extract ( const AxiomVec& Vec, const TSignature& signature, ModuleType type ) |
| 196 |
{ extract ( Vec.begin(), Vec.end(), signature, type ); } |
| 197 |
|
| 198 |
void extract ( const TOntology& O, const TSignature& signature, ModuleType type ) |
| 199 |
{ extract ( O.getAxioms(), signature, type ); } |
| 200 |
|
| 201 |
bool isTautology ( TDLAxiom* ax, ModuleType type ) |
| 202 |
{ |
| 203 |
bool topLocality = (type == M_TOP); |
| 204 |
sig = ax->getSignature(); |
| 205 |
sig.setLocality(topLocality); |
| 206 |
|
| 207 |
bool toReturn = Checker->local(ax); |
| 208 |
if ( likely ( type != M_STAR || !toReturn ) ) |
| 209 |
return toReturn; |
| 210 |
|
| 211 |
sig.setLocality(!topLocality); |
| 212 |
return Checker->local(ax); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
SigIndex* getSigIndex ( void ) { return &sigIndex; } |
| 217 |
|
| 218 |
|
| 219 |
const AxiomVec& getModule ( void ) const { return Module; } |
| 220 |
|
| 221 |
const TSignature& getSignature ( void ) const { return sig; } |
| 222 |
|
| 223 |
LocalityChecker* getLocalityChecker ( void ) { return Checker; } |
| 224 |
|
| 225 |
unsigned long long getNChecks ( void ) const { return nChecks; } |
| 226 |
|
| 227 |
unsigned long long getNNonLocal ( void ) const { return nNonLocal; } |
| 228 |
}; |
| 229 |
|
| 230 |
#endif |