| 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" |
| 23 | #include "logging.h" |
| 24 | #include "ProgressIndicatorInterface.h" |
| 25 | |
| 26 | //#define RKG_DEBUG_AD |
| 27 | |
| 28 | /// d'tor |
| 29 | AtomicDecomposer :: ~AtomicDecomposer() |
| 30 | { |
| 31 | delete AOS; |
| 32 | delete PI; |
| 33 | } |
| 34 | |
| 35 | /// remove tautologies (axioms that are always local) from the ontology temporarily |
| 36 | void |
| 37 | AtomicDecomposer :: removeTautologies ( TOntology* O ) |
| 38 | { |
| 39 | // we might use it for another decomposition |
| 40 | Tautologies.clear(); |
| 41 | unsigned long nAx = 0; |
| 42 | for ( TDLAxiom* axiom : *O ) |
| 43 | if ( likely(axiom->isUsed()) ) |
| 44 | { |
| 45 | if ( unlikely(pModularizer->isTautology(axiom, type)) ) |
| 46 | { |
| 47 | Tautologies.push_back(axiom); |
| 48 | axiom->setUsed(false); |
| 49 | } |
| 50 | else |
| 51 | ++nAx; |
| 52 | } |
| 53 | if ( PI ) |
| 54 | PI->setLimit(nAx); |
| 55 | } |
| 56 | |
| 57 | /// build a module for given axiom AX; use parent atom's module as a base for the module search |
| 58 | TOntologyAtom* |
| 59 | AtomicDecomposer :: buildModule ( const TSignature& sig, TOntologyAtom* parent ) |
| 60 | { |
| 61 | // build a module for a given signature |
| 62 | pModularizer->extract ( parent->getModule().begin(), parent->getModule().end(), sig, type ); |
| 63 | const AxiomVec& Module = pModularizer->getModule(); |
| 64 | // if module is empty (empty bottom atom) -- do nothing |
| 65 | if ( Module.empty() ) |
| 66 | return nullptr; |
| 67 | // here the module is created; report it |
| 68 | if ( PI ) |
| 69 | PI->incIndicator(); |
| 70 | // check if the module corresponds to a PARENT one; modules are the same iff their sizes are the same |
| 71 | if ( parent != rootAtom && Module.size() == parent->getModule().size() ) // same module means same atom |
| 72 | return parent; |
| 73 | // create new atom with that module |
| 74 | TOntologyAtom* atom = AOS->newAtom(); |
| 75 | atom->setModule(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 | TOntologyAtom* |
| 81 | AtomicDecomposer :: createAtom ( TDLAxiom* ax, TOntologyAtom* parent ) |
| 82 | { |
| 83 | // check whether axiom already has an atom |
| 84 | if ( ax->getAtom() != nullptr ) |
| 85 | return const_cast<TOntologyAtom*>(ax->getAtom()); |
| 86 | // build an atom: use a module to find atomic dependencies |
| 87 | TOntologyAtom* atom = buildModule( ax->getSignature(), parent ); |
| 88 | // no empty modules should be here |
| 89 | fpp_assert ( atom != nullptr ); |
| 90 | // register axiom as a part of an atom |
| 91 | atom->addAxiom(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->getModule() ) |
| 103 | #endif |
| 104 | if ( likely ( axiom != ax ) ) |
| 105 | atom->addDepAtom ( createAtom ( axiom, atom ) ); |
| 106 | return atom; |
| 107 | } |
| 108 | |
| 109 | /// get the atomic structure for given module type T |
| 110 | AOStructure* |
| 111 | AtomicDecomposer :: getAOS ( TOntology* O, ModuleType t ) |
| 112 | { |
| 113 | // remember the type of the module |
| 114 | type = t; |
| 115 | |
| 116 | // prepare a new AO structure |
| 117 | delete AOS; |
| 118 | AOS = new AOStructure(); |
| 119 | |
| 120 | // init semantic locality checker |
| 121 | pModularizer->preprocessOntology(O->getAxioms()); |
| 122 | |
| 123 | // we don't need tautologies here |
| 124 | removeTautologies(O); |
| 125 | |
| 126 | // init the root atom |
| 127 | rootAtom = new TOntologyAtom(); |
| 128 | rootAtom -> setModule ( TOntologyAtom::AxiomSet ( O->begin(), O->end() ) ); |
| 129 | |
| 130 | // build the "bottom" atom for an empty signature |
| 131 | TOntologyAtom* BottomAtom = buildModule ( TSignature(), rootAtom ); |
| 132 | if ( BottomAtom ) |
| 133 | for ( const auto& axiom: BottomAtom->getModule() ) |
| 134 | BottomAtom->addAxiom(axiom); |
| 135 | |
| 136 | // create atoms for all the axioms in the ontology |
| 137 | for ( TDLAxiom* axiom : *O ) |
| 138 | if ( axiom->isUsed() && axiom->getAtom() == nullptr ) |
| 139 | createAtom ( axiom, rootAtom ); |
| 140 | |
| 141 | // restore tautologies in the ontology |
| 142 | restoreTautologies(); |
| 143 | |
| 144 | if ( LLM.isWritable(llAlways) ) |
| 145 | LL << "\nThere were " << pModularizer->getNNonLocal() << " non-local axioms out of " << pModularizer->getNChecks() << " totally checked\n"; |
| 146 | |
| 147 | // clear the root atom |
| 148 | delete rootAtom; |
| 149 | |
| 150 | // reduce graph |
| 151 | AOS->reduceGraph(); |
| 152 | |
| 153 | return AOS; |
| 154 | } |