| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License |
| 7 | as published by the Free Software Foundation; either version 2 |
| 8 | of the License, or (at your option) any later version. |
| 9 | |
| 10 | This program 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 |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | // methods to work with Atomic Decomposition |
| 21 | |
| 22 | #include <fstream> |
| 23 | |
| 24 | #include "AtomicDecomposer.h" |
| 25 | #include "tOntologyPrinterLISP.h" // AD prints |
| 26 | #include "procTimer.h" |
| 27 | #include "cppi.h" |
| 28 | |
| 29 | // defined in FaCT.cpp |
| 30 | extern std::ofstream Out; |
| 31 | |
| 32 | // print axioms of an atom |
| 33 | static void |
| 34 | printAtomAxioms ( const TOntologyAtom::AxiomSet& Axioms ) |
| 35 | { |
| 36 | static TLISPOntologyPrinter LP(Out); |
| 37 | // do cycle via set to keep the order |
| 38 | typedef std::set<TDLAxiom*> AxSet; |
| 39 | const AxSet M ( Axioms.begin(), Axioms.end() ); |
| 40 | for ( const TDLAxiom* axiom : M ) |
| 41 | axiom->accept(LP); |
| 42 | } |
| 43 | |
| 44 | /// print dependencies of an atom |
| 45 | static void |
| 46 | printAtomDeps ( const TOntologyAtom::AtomSet& Dep ) |
| 47 | { |
| 48 | if ( unlikely(Dep.empty()) ) |
| 49 | Out << "Ground"; |
| 50 | else |
| 51 | Out << "Depends on:"; |
| 52 | for ( const TOntologyAtom* atom : Dep ) |
| 53 | Out << " " << atom->getId(); |
| 54 | Out << "\n"; |
| 55 | } |
| 56 | |
| 57 | /// print the atom with an index INDEX of the AD |
| 58 | static void |
| 59 | printADAtom ( const TOntologyAtom* atom ) |
| 60 | { |
| 61 | const TOntologyAtom::AxiomSet& Axioms = atom->getAtomAxioms(); |
| 62 | Out << "Atom " << atom->getId() << " (size " << Axioms.size() << ", module size " << atom->getModule().size() << "):\n"; |
| 63 | printAtomAxioms(Axioms); |
| 64 | printAtomDeps(atom->getDepAtoms()); |
| 65 | } |
| 66 | |
| 67 | /// @return all the axioms in the AD |
| 68 | static size_t |
| 69 | sizeAD ( AOStructure* AOS ) |
| 70 | { |
| 71 | size_t ret = 0; |
| 72 | for ( const TOntologyAtom* atom : *AOS ) |
| 73 | ret += atom->getAtomAxioms().size(); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | CreateAD ( TOntology* Ontology, ModuleMethod moduleMethod ) |
| 79 | { |
| 80 | std::cerr << "\n"; |
| 81 | // do the atomic decomposition |
| 82 | TsProcTimer timer; |
| 83 | timer.Start(); |
| 84 | TModularizer mod(moduleMethod); |
| 85 | AtomicDecomposer* AD = new AtomicDecomposer(&mod); |
| 86 | AD->setProgressIndicator(new CPPI()); |
| 87 | AOStructure* AOS = AD->getAOS ( Ontology, M_BOT ); |
| 88 | timer.Stop(); |
| 89 | Out << "Atomic structure built in " << timer << " seconds\n"; |
| 90 | size_t sz = sizeAD(AOS); |
| 91 | Out << "Atomic structure (" << sz << " axioms in " << AOS->size() << " atoms; " << Ontology->size()-sz << " tautologies):\n"; |
| 92 | for ( const TOntologyAtom* atom : *AOS ) |
| 93 | printADAtom(atom); |
| 94 | delete AD; |
| 95 | } |