| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2011-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 | #include <ostream> |
| 21 | |
| 22 | #include "AtomicDecomposer.h"cxx . . $ `<file>/Kernel/AtomicDecomposer.h`/ |
| 23 | #include "logging.h"cxx . . $ `<file>/Kernel/logging.h`/ |
| 24 | #include "ProgressIndicatorInterface.h"cxx . . $ `<file>/Kernel/ProgressIndicatorInterface.h`/ |
| 25 | |
| 26 | //#define RKG_DEBUG_AD |
| 27 | |
| 28 | /// d'tor |
| 29 | AtomicDecomposercxx . . $ AtomicDecomposer# :: ~cxx . . $ AtomicDecomposer#`~AtomicDecomposer`(49f6e7a06ebc5aa8).AtomicDecomposercxx . . $ AtomicDecomposer#() |
| 30 | { |
| 31 | delete AOScxx . . $ AtomicDecomposer#AOS.; |
| 32 | delete PIcxx . . $ AtomicDecomposer#PI.; |
| 33 | } |
| 34 | |
| 35 | /// remove tautologies (axioms that are always local) from the ontology temporarily |
| 36 | void |
| 37 | AtomicDecomposercxx . . $ AtomicDecomposer# :: removeTautologiescxx . . $ AtomicDecomposer#removeTautologies(33fca0ffe8b07fe8). ( TOntologycxx . . $ TOntology#* O ) |
| 38 | { |
| 39 | // we might use it for another decomposition |
| 40 | Tautologiescxx . . $ AtomicDecomposer#Tautologies..clearcxx . . $ std/vector#clear(49f6e7a06ebc5aa8).(); |
| 41 | unsigned long nAx = 0; |
| 42 | for ( TDLAxiomcxx . . $ TDLAxiom#* axiom : *O ) |
| 43 | if ( likelycxx . . $ `Kernel/globaldef.h:28:10`!(axiom->isUsed()) ) |
| 44 | { |
| 45 | if ( unlikelycxx . . $ `Kernel/globaldef.h:29:10`!(pModularizer->isTautology(axiom, type)) ) |
| 46 | { |
| 47 | Tautologiescxx . . $ AtomicDecomposer#Tautologies..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(axiom); |
| 48 | axiom->setUsedcxx . . $ TDLAxiom#setUsed(b45ddcbc8423892c).(false); |
| 49 | } |
| 50 | else |
| 51 | ++nAx; |
| 52 | } |
| 53 | if ( PIcxx . . $ AtomicDecomposer#PI. ) |
| 54 | PIcxx . . $ AtomicDecomposer#PI.->setLimitcxx . . $ ProgressIndicatorInterface#setLimit(2c44cd906ae4e829).(nAx); |
| 55 | } |
| 56 | |
| 57 | /// build a module for given axiom AX; use parent atom's module as a base for the module search |
| 58 | TOntologyAtomcxx . . $ TOntologyAtom#* |
| 59 | AtomicDecomposercxx . . $ AtomicDecomposer# :: buildModulecxx . . $ AtomicDecomposer#buildModule(7d21f2e6c4bdd0a1). ( const TSignaturecxx . . $ TSignature#& sig, TOntologyAtomcxx . . $ TOntologyAtom#* parent ) |
| 60 | { |
| 61 | // build a module for a given signature |
| 62 | pModularizercxx . . $ AtomicDecomposer#pModularizer.->extractcxx . . $ TModularizer#extract(67bca874b954bf38). ( parent->getModulecxx . . $ TOntologyAtom#getModule(73e4577bb59fbb07).().begincxx . . $ std/vector#begin(359ca5030082c9f).(), parent->getModulecxx . . $ TOntologyAtom#getModule(73e4577bb59fbb07).().endcxx . . $ std/vector#end(359ca5030082c9f).(), sig, typecxx . . $ AtomicDecomposer#type. ); |
| 63 | const AxiomVeccxx . . $ AxiomVec#& Module = pModularizercxx . . $ AtomicDecomposer#pModularizer.->getModulecxx . . $ TModularizer#getModule(73e4577bb59fbb07).(); |
| 64 | // if module is empty (empty bottom atom) -- do nothing |
| 65 | if ( Module.emptycxx . . $ std/vector#empty(50ce9a9e25b4a850).() ) |
| 66 | return nullptr; |
| 67 | // here the module is created; report it |
| 68 | if ( PIcxx . . $ AtomicDecomposer#PI. ) |
| 69 | PIcxx . . $ AtomicDecomposer#PI.->incIndicatorcxx . . $ ProgressIndicatorInterface#incIndicator(2c44cd906ae4e829).(); |
| 70 | // check if the module corresponds to a PARENT one; modules are the same iff their sizes are the same |
| 71 | if ( parent != rootAtomcxx . . $ AtomicDecomposer#rootAtom. && Module.sizecxx . . $ std/vector#size(fe4d2d262d4164a8).() == parent->getModulecxx . . $ TOntologyAtom#getModule(73e4577bb59fbb07).().sizecxx . . $ std/vector#size(fe4d2d262d4164a8).() ) // same module means same atom |
| 72 | return parent; |
| 73 | // create new atom with that module |
| 74 | TOntologyAtomcxx . . $ TOntologyAtom#* atom = AOScxx . . $ AtomicDecomposer#AOS.->newAtomcxx . . $ AOStructure#newAtom(cdca29e2a9ec24ff).(); |
| 75 | atom->setModulecxx . . $ TOntologyAtom#setModule(b963b3dd7533a896).(Module); |
| 76 | return atom; |
| 77 | } |
| 78 | |
| 79 | /// create atom for given axiom AX; use parent atom's module as a base for the module search |
| 80 | TOntologyAtomcxx . . $ TOntologyAtom#* |
| 81 | AtomicDecomposercxx . . $ AtomicDecomposer# :: createAtomcxx . . $ AtomicDecomposer#createAtom(3f90616d3a2486ad). ( TDLAxiomcxx . . $ TDLAxiom#* ax, TOntologyAtomcxx . . $ TOntologyAtom#* parent ) |
| 82 | { |
| 83 | // check whether axiom already has an atom |
| 84 | if ( ax->getAtomcxx . . $ TDLAxiom#getAtom(a636214afeaaa94e).() != nullptr ) |
| 85 | return const_cast<TOntologyAtomcxx . . $ TOntologyAtom#*>(ax->getAtomcxx . . $ TDLAxiom#getAtom(a636214afeaaa94e).()); |
| 86 | // build an atom: use a module to find atomic dependencies |
| 87 | TOntologyAtomcxx . . $ TOntologyAtom#* atom = buildModulecxx . . $ AtomicDecomposer#buildModule(7d21f2e6c4bdd0a1).( ax->getSignaturecxx . . $ TDLAxiom#getSignature(67d28bb768a72444).(), parent ); |
| 88 | // no empty modules should be here |
| 89 | fpp_assertcxx . . $ `Kernel/fpp_assert.h:46:10`! ( atom != nullptr ); |
| 90 | // register axiom as a part of an atom |
| 91 | atom->addAxiomcxx . . $ TOntologyAtom#addAxiom(ade74f2f8b540a23).(ax); |
| 92 | // if atom is the same as parent -- nothing more to do |
| 93 | if ( atom == parent ) |
| 94 | return parent; |
| 95 | // not the same as parent: for all atom's axioms check their atoms and make ATOM depend on them |
| 96 | #ifdef RKG_DEBUG_AD |
| 97 | // do cycle via set to keep the order |
| 98 | typedef std::set<TDLAxiom*> AxSet; |
| 99 | const AxSet M ( atom->getModule().begin(), atom->getModule().end() ); |
| 100 | for ( const auto& axiom: M ) |
| 101 | #else |
| 102 | for ( const auto& axiom: atom->getModulecxx . . $ TOntologyAtom#getModule(73e4577bb59fbb07).() ) |
| 103 | #endif |
| 104 | if ( likelycxx . . $ `Kernel/globaldef.h:28:10`! ( axiom != ax ) ) |
| 105 | atom->addDepAtomcxx . . $ TOntologyAtom#addDepAtom(1edcf2cc03520493). ( createAtomcxx . . $ AtomicDecomposer#createAtom(3f90616d3a2486ad). ( axiom, atom ) ); |
| 106 | return atom; |
| 107 | } |
| 108 | |
| 109 | /// get the atomic structure for given module type T |
| 110 | AOStructurecxx . . $ AOStructure#* |
| 111 | AtomicDecomposercxx . . $ AtomicDecomposer# :: getAOScxx . . $ AtomicDecomposer#getAOS(54e527a6df0aab25). ( TOntologycxx . . $ TOntology#* O, ModuleTypecxx . . $ ModuleType# t ) |
| 112 | { |
| 113 | // remember the type of the module |
| 114 | typecxx . . $ AtomicDecomposer#type. = t; |
| 115 | |
| 116 | // prepare a new AO structure |
| 117 | delete AOScxx . . $ AtomicDecomposer#AOS.; |
| 118 | AOScxx . . $ AtomicDecomposer#AOS. = new AOStructurecxx . . $ AOStructure#(); |
| 119 | |
| 120 | // init semantic locality checker |
| 121 | pModularizercxx . . $ AtomicDecomposer#pModularizer.->preprocessOntologycxx . . $ TModularizer#preprocessOntology(b963b3dd7533a896).(O->getAxiomscxx . . $ TOntology#getAxioms(791b20285504d2d7).()); |
| 122 | |
| 123 | // we don't need tautologies here |
| 124 | removeTautologiescxx . . $ AtomicDecomposer#removeTautologies(33fca0ffe8b07fe8).(O); |
| 125 | |
| 126 | // init the root atom |
| 127 | rootAtomcxx . . $ AtomicDecomposer#rootAtom. = new TOntologyAtomcxx . . $ TOntologyAtom#(); |
| 128 | rootAtomcxx . . $ AtomicDecomposer#rootAtom. -> setModulecxx . . $ TOntologyAtom#setModule(b963b3dd7533a896). ( TOntologyAtomcxx . . $ std/vector#vector(13e0640cd3842f28).::AxiomSetcxx . . $ TOntologyAtom#AxiomSet# ( O->begincxx . . $ TOntology#begin(37aced936b943211).(), O->endcxx . . $ TOntology#end(37aced936b943211).() ) ); |
| 129 | |
| 130 | // build the "bottom" atom for an empty signature |
| 131 | TOntologyAtomcxx . . $ TOntologyAtom#* BottomAtom = buildModulecxx . . $ AtomicDecomposer#buildModule(7d21f2e6c4bdd0a1). ( TSignaturecxx . . $ TSignature#(), rootAtomcxx . . $ AtomicDecomposer#rootAtom. ); |
| 132 | if ( BottomAtom ) |
| 133 | for ( const auto& axiom: BottomAtom->getModulecxx . . $ TOntologyAtom#getModule(73e4577bb59fbb07).() ) |
| 134 | BottomAtom->addAxiomcxx . . $ TOntologyAtom#addAxiom(ade74f2f8b540a23).(axiom); |
| 135 | |
| 136 | // create atoms for all the axioms in the ontology |
| 137 | for ( TDLAxiomcxx . . $ TDLAxiom#* axiom : *O ) |
| 138 | if ( axiom->isUsedcxx . . $ TDLAxiom#isUsed(50ce9a9e25b4a850).() && axiom->getAtomcxx . . $ TDLAxiom#getAtom(a636214afeaaa94e).() == nullptr ) |
| 139 | createAtomcxx . . $ AtomicDecomposer#createAtom(3f90616d3a2486ad). ( axiom, rootAtomcxx . . $ AtomicDecomposer#rootAtom. ); |
| 140 | |
| 141 | // restore tautologies in the ontology |
| 142 | restoreTautologiescxx . . $ AtomicDecomposer#restoreTautologies(49f6e7a06ebc5aa8).(); |
| 143 | |
| 144 | if ( LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llAlwayscxx . . $ llAlways.) ) |
| 145 | LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\nThere were " <<cxx . . $ std/basic_ostream#`operator<<`(a2e659b753987ede). pModularizercxx . . $ AtomicDecomposer#pModularizer.->getNNonLocalcxx . . $ TModularizer#getNNonLocal(1c5fc1fd41a99ae7).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " non-local axioms out of " <<cxx . . $ std/basic_ostream#`operator<<`(a2e659b753987ede). pModularizercxx . . $ AtomicDecomposer#pModularizer.->getNCheckscxx . . $ TModularizer#getNChecks(1c5fc1fd41a99ae7).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " totally checked\n"; |
| 146 | |
| 147 | // clear the root atom |
| 148 | delete rootAtomcxx . . $ AtomicDecomposer#rootAtom.; |
| 149 | |
| 150 | // reduce graph |
| 151 | AOScxx . . $ AtomicDecomposer#AOS.->reduceGraphcxx . . $ AOStructure#reduceGraph(49f6e7a06ebc5aa8).(); |
| 152 | |
| 153 | return AOScxx . . $ AtomicDecomposer#AOS.; |
| 154 | } |