| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2013-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | // incremental reasoning implementation |
| 21 | |
| 22 | #include "Kernel.h" |
| 23 | #include "OntologyBasedModularizer.h" |
| 24 | #include "tOntologyPrinterLISP.h" |
| 25 | #include "procTimer.h" |
| 26 | #include "SaveLoadManager.h" // for saving/restoring ontology |
| 27 | |
| 28 | TsProcTimer moduleTimer, subCheckTimer; |
| 29 | int nModule = 0; |
| 30 | |
| 31 | /// setup Name2Sig for a given name C |
| 32 | void |
| 33 | ReasoningKernel :: setupSig ( const TNamedEntity* entity, const AxiomVec& Module ) |
| 34 | { |
| 35 | // do nothing if entity doesn't exist |
| 36 | if ( entity == nullptr ) |
| 37 | return; |
| 38 | |
| 39 | moduleTimer.Start(); |
| 40 | // prepare a place to update |
| 41 | TSignature sig; |
| 42 | NameSigMap::iterator insert = Name2Sig.find(entity); |
| 43 | if ( insert == Name2Sig.end() ) |
| 44 | insert = Name2Sig.insert(std::make_pair(entity,&sig)).first; |
| 45 | else |
| 46 | delete insert->second; |
| 47 | |
| 48 | // calculate a module |
| 49 | sig.add(entity); |
| 50 | getModExtractor(SYN_LOC_STD)->getModule(Module,sig,M_BOT); |
| 51 | ++nModule; |
| 52 | |
| 53 | // perform update |
| 54 | insert->second = new TSignature(getModExtractor(SYN_LOC_STD)->getModularizer()->getSignature()); |
| 55 | |
| 56 | moduleTimer.Stop(); |
| 57 | } |
| 58 | |
| 59 | /// build signature for ENTITY and all dependent entities from toProcess; look for modules in Module; |
| 60 | void |
| 61 | ReasoningKernel :: buildSignature ( const TNamedEntity* entity, const AxiomVec& Module, std::set<const TNamedEntity*>& toProcess ) |
| 62 | { |
| 63 | toProcess.erase(entity); |
| 64 | setupSig ( entity, Module ); |
| 65 | const AxiomVec NewModule = getModExtractor(SYN_LOC_STD)->getModularizer()->getModule(); |
| 66 | if ( Module.size() == NewModule.size() ) // the same module |
| 67 | return; |
| 68 | // smaller module: recurse |
| 69 | for ( const TNamedEntity* e : getModExtractor(SYN_LOC_STD)->getModularizer()->getSignature() ) |
| 70 | if ( toProcess.count(e) > 0 ) // need to process |
| 71 | buildSignature ( e, NewModule, toProcess ); |
| 72 | } |
| 73 | |
| 74 | /// initialise the incremental bits on full reload |
| 75 | void |
| 76 | ReasoningKernel :: initIncremental ( void ) |
| 77 | { |
| 78 | delete ModSyn; |
| 79 | ModSyn = nullptr; |
| 80 | // fill the module signatures of the concepts |
| 81 | Name2Sig.clear(); |
| 82 | // found all entities |
| 83 | std::set<const TNamedEntity*> toProcess; |
| 84 | for ( TBox::c_const_iterator p = getTBox()->c_begin(), p_end = getTBox()->c_end(); p != p_end; ++p ) |
| 85 | toProcess.insert((*p)->getEntity()); |
| 86 | // process all entries recursively |
| 87 | while ( !toProcess.empty() ) |
| 88 | buildSignature ( *toProcess.begin(), Ontology.getAxioms(), toProcess ); |
| 89 | |
| 90 | |
| 91 | getTBox()->setNameSigMap(&Name2Sig); |
| 92 | // fill in ontology signature |
| 93 | OntoSig = Ontology.getSignature(); |
| 94 | std::cout << "Init modules (" << nModule << ") time: " << moduleTimer << " sec" << std::endl; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | ReasoningKernel :: doIncremental ( void ) |
| 99 | { |
| 100 | TsProcTimer total; |
| 101 | total.Start(); |
| 102 | std::cout << "Incremental!\n"; |
| 103 | // re-set the modularizer to use updated ontology |
| 104 | delete ModSyn; |
| 105 | ModSyn = nullptr; |
| 106 | |
| 107 | std::set<const TNamedEntity*> MPlus, MMinus; |
| 108 | std::set<const TNamedEntry*> excluded; |
| 109 | |
| 110 | // detect new- and old- signature elements |
| 111 | TSignature NewSig = Ontology.getSignature(); |
| 112 | TSignature::BaseType RemovedEntities, AddedEntities; |
| 113 | std::set_difference(OntoSig.begin(), OntoSig.end(), NewSig.begin(), NewSig.end(), inserter(RemovedEntities, RemovedEntities.begin())); |
| 114 | std::set_difference(NewSig.begin(), NewSig.end(), OntoSig.begin(), OntoSig.end(), inserter(AddedEntities, AddedEntities.begin())); |
| 115 | |
| 116 | Taxonomy* tax = getCTaxonomy(); |
| 117 | // std::cout << "Original Taxonomy:"; |
| 118 | // tax->print(std::cout); |
| 119 | |
| 120 | // deal with removed concepts |
| 121 | TSignature::BaseType::iterator e, e_end; |
| 122 | for ( e = RemovedEntities.begin(), e_end = RemovedEntities.end(); e != e_end; ++e ) |
| 123 | if ( const TConcept* C = dynamic_cast<const TConcept*>((*e)->getEntry()) ) |
| 124 | { |
| 125 | excluded.insert(C); |
| 126 | // remove all links |
| 127 | C->getTaxVertex()->remove(); |
| 128 | // update Name2Sig |
| 129 | delete Name2Sig[*e]; |
| 130 | Name2Sig.erase(*e); |
| 131 | } |
| 132 | |
| 133 | // deal with added concepts |
| 134 | tax->deFinalise(); |
| 135 | for ( e = AddedEntities.begin(), e_end = AddedEntities.end(); e != e_end; ++e ) |
| 136 | if ( const TDLConceptName* cName = dynamic_cast<const TDLConceptName*>(*e) ) |
| 137 | { |
| 138 | // register the name in TBox |
| 139 | TreeDeleter TD(this->e(cName)); |
| 140 | TConcept* C = dynamic_cast<TConcept*>(cName->getEntry()); |
| 141 | // create sig for it |
| 142 | setupSig(cName); |
| 143 | // init the taxonomy element |
| 144 | TaxonomyVertex* cur = tax->getCurrent(); |
| 145 | cur->clear(); |
| 146 | cur->setSample(C); |
| 147 | cur->addNeighbour ( /*upDirection=*/true, tax->getTopVertex() ); |
| 148 | tax->finishCurrentNode(); |
| 149 | // std::cout << "Insert " << C->getName() << std::endl; |
| 150 | } |
| 151 | OntoSig = NewSig; |
| 152 | |
| 153 | // fill in M^+ and M^- sets |
| 154 | TsProcTimer t; |
| 155 | t.Start(); |
| 156 | LocalityChecker* lc = getModExtractor(SYN_LOC_STD)->getModularizer()->getLocalityChecker(); |
| 157 | TOntology::iterator nb = Ontology.beginUnprocessed(), ne = Ontology.end(), rb = Ontology.beginRetracted(), re = Ontology.endRetracted(); |
| 158 | // TLISPOntologyPrinter pr(std::cout); |
| 159 | // TOntology::iterator q; |
| 160 | // for ( q = nb; q != ne; ++q ) |
| 161 | // { |
| 162 | // std::cout << "Add:"; |
| 163 | // (*q)->accept(pr); |
| 164 | // } |
| 165 | // for ( q = rb; q != re; ++q ) |
| 166 | // { |
| 167 | // std::cout << "Del:"; |
| 168 | // (*q)->accept(pr); |
| 169 | // } |
| 170 | for ( NameSigMap::iterator p = Name2Sig.begin(), p_end = Name2Sig.end(); p != p_end; ++p ) |
| 171 | { |
| 172 | lc->setSignatureValue(*p->second); |
| 173 | for ( TOntology::iterator notProcessed = nb; notProcessed != ne; ++notProcessed ) |
| 174 | if ( !lc->local(*notProcessed) ) |
| 175 | { |
| 176 | MPlus.insert(p->first); |
| 177 | // std::cout << "Non-local NP axiom "; |
| 178 | // (*notProcessed)->accept(pr); |
| 179 | // std::cout << " wrt " << p->first->getName() << std::endl; |
| 180 | break; |
| 181 | } |
| 182 | for ( TOntology::iterator retracted = rb; retracted != re; retracted++ ) |
| 183 | if ( !lc->local(*retracted) ) |
| 184 | { |
| 185 | MMinus.insert(p->first); |
| 186 | // FIXME!! only concepts for now |
| 187 | TaxonomyVertex* v = dynamic_cast<const ClassifiableEntry*>(p->first->getEntry())->getTaxVertex(); |
| 188 | if ( v->noNeighbours(true) ) |
| 189 | { |
| 190 | v->addNeighbour(true,tax->getTopVertex()); |
| 191 | tax->getTopVertex()->addNeighbour(false,v); |
| 192 | } |
| 193 | // std::cout << "Non-local RT axiom "; |
| 194 | // (*retracted)->accept(pr); |
| 195 | // std::cout << " wrt " << p->first->getName() << std::endl; |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | t.Stop(); |
| 200 | std::cout << "Determine concepts that need reclassification: done in " << t << std::endl; |
| 201 | |
| 202 | // build changed modules |
| 203 | std::set<const TNamedEntity*> toProcess(MPlus); |
| 204 | toProcess.insert ( MMinus.begin(), MMinus.end() ); |
| 205 | // process all entries recursively |
| 206 | while ( !toProcess.empty() ) |
| 207 | buildSignature ( *toProcess.begin(), Ontology.getAxioms(), toProcess ); |
| 208 | |
| 209 | tax->finalise(); |
| 210 | // std::cout << "Adjusted Taxonomy:"; |
| 211 | // tax->print(std::cout); |
| 212 | |
| 213 | t.Reset(); |
| 214 | t.Start(); |
| 215 | // save taxonomy |
| 216 | SaveLoadManager SLManager("Incremental"); |
| 217 | SLManager.prepare(/*input=*/false); |
| 218 | // FIXME!! for now |
| 219 | excluded.clear(); |
| 220 | getTBox()->SaveTaxonomy(SLManager,excluded); |
| 221 | |
| 222 | // do actual change |
| 223 | useIncrementalReasoning = false; |
| 224 | forceReload(); |
| 225 | pTBox->setNameSigMap(&Name2Sig); |
| 226 | pTBox->isConsistent(); |
| 227 | useIncrementalReasoning = true; |
| 228 | |
| 229 | // load the taxonomy |
| 230 | SLManager.prepare(/*input=*/true); |
| 231 | getTBox()->LoadTaxonomy(SLManager); |
| 232 | t.Stop(); |
| 233 | |
| 234 | std::cout << "Reloading ontology: done in " << t << std::endl; |
| 235 | |
| 236 | tax = getCTaxonomy(); |
| 237 | // std::cout << "Reloaded Taxonomy:"; |
| 238 | // tax->print(std::cout); |
| 239 | // std::cout.flush(); |
| 240 | |
| 241 | subCheckTimer.Start(); |
| 242 | getTBox()->reclassify ( MPlus, MMinus ); |
| 243 | subCheckTimer.Stop(); |
| 244 | Ontology.setProcessed(); |
| 245 | total.Stop(); |
| 246 | std::cout << "Total modularization (" << nModule << ") time: " << moduleTimer << " sec\nTotal reasoning time: " << subCheckTimer |
| 247 | << " sec\nTotal reclassification time: " << total << " sec" << std::endl; |
| 248 | } |
| 249 | |
| 250 | std::ostream& |
| 251 | operator << ( std::ostream& o, const TSignature& sig ) |
| 252 | { |
| 253 | o << "["; |
| 254 | for ( const TNamedEntity* entity : sig ) |
| 255 | o << entity->getName() << " "; |
| 256 | o << "]" << std::endl; |
| 257 | return o; |
| 258 | } |