| 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 | #ifndef REASONERNOM_H |
| 21 | #define REASONERNOM_H |
| 22 | |
| 23 | #include "Reasoner.h" |
| 24 | |
| 25 | class NominalReasoner: public DlSatTester |
| 26 | { |
| 27 | protected: // type definition |
| 28 | /// vector of singletons |
| 29 | typedef TBox::SingletonVector SingletonVector; |
| 30 | |
| 31 | protected: // members |
| 32 | /// all nominals defined in TBox |
| 33 | SingletonVector Nominals; |
| 34 | |
| 35 | protected: // methods |
| 36 | /// prepare reasoning |
| 37 | void prepareReasoner ( void ) override; |
| 38 | /// there are nominals |
| 39 | bool hasNominals ( void ) const override { return true; } |
| 40 | |
| 41 | //----------------------------------------------------------------------------- |
| 42 | //-- internal nominal reasoning interface |
| 43 | //----------------------------------------------------------------------------- |
| 44 | |
| 45 | /// init vector of nominals defined in TBox |
| 46 | void initNominalVector ( void ); |
| 47 | |
| 48 | /// create cache entry for given singleton |
| 49 | void registerNominalCache ( const TIndividual* p ) |
| 50 | { DLHeap.setCache ( p->pName, createModelCache(p->node->resolvePBlocker()) ); } |
| 51 | /// init single nominal node |
| 52 | bool initNominalNode ( const TIndividual* nom ) |
| 53 | { |
| 54 | DlCompletionTree* node = CGraph.getNewNode(); |
| 55 | node->setNominalLevel(); |
| 56 | const_cast<TIndividual*>(nom)->node = node; // init nominal with associated node |
| 57 | return initNewNode ( node, DepSet(), nom->pName ); // ABox is inconsistent |
| 58 | } |
| 59 | /// create nominal nodes for all individuals in TBox |
| 60 | bool initNominalCloud ( void ); |
| 61 | /// make an R-edge between related nominals |
| 62 | bool initRelatedNominals ( const TRelated* rel ); |
| 63 | /// use classification information for the nominal P |
| 64 | void updateClassifiedSingleton ( TIndividual* p ) |
| 65 | { |
| 66 | registerNominalCache(p); |
| 67 | if ( unlikely(p->node->isPBlocked()) ) |
| 68 | { |
| 69 | // BP of the individual P is merged to |
| 70 | BipolarPointer bp = p->node->getBlocker()->label().begin_sc()->bp(); |
| 71 | TIndividual* blocker = (TIndividual*)DLHeap[bp].getConcept(); |
| 72 | fpp_assert ( blocker->node == p->node->getBlocker() ); |
| 73 | tBox.SameI[p] = std::make_pair ( blocker, p->node->getPurgeDep().empty() ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Checks whether a completion graph node has references to individuals (nominals) |
| 78 | bool hasIndividuals(const DlCompletionTree* node); |
| 79 | |
| 80 | // Returns a list of individuals referenced by a completion graph node |
| 81 | std::vector<TIndividual*> getIndividuals(const DlCompletionTree* node); |
| 82 | |
| 83 | // Populates individual relations using the completion graph and the automaton transitions of a complex role |
| 84 | void followTransition(std::map<TIndividual*, std::map<const TRole*, std::set<TIndividual*>>>& role_map, const DlCompletionTree* first, const TRole* role, const DlCompletionTree* current, const RoleAutomaton& automaton, RAState state, std::map<std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>>, std::map<RAState, std::set<const DlCompletionTree*>>>& visited, std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>> blocker_stack); |
| 85 | |
| 86 | // Creates cache of individual relations based on the completion graph and roles automata (for faster but potentially incomplete queries) |
| 87 | void precacheRelated(); |
| 88 | |
| 89 | public: |
| 90 | /// c'tor |
| 91 | explicit NominalReasoner ( TBox& tbox ) |
| 92 | : DlSatTester(tbox) |
| 93 | { |
| 94 | initNominalVector(); |
| 95 | } |
| 96 | |
| 97 | /// check whether ontology with nominals is consistent |
| 98 | bool consistentNominalCloud ( void ); |
| 99 | }; // NominalReasoner |
| 100 | |
| 101 | //----------------------------------------------------------------------------- |
| 102 | //-- implementation of nominal reasoner-related parts of TBox |
| 103 | //----------------------------------------------------------------------------- |
| 104 | |
| 105 | inline void |
| 106 | TBox :: initReasoner ( void ) |
| 107 | { |
| 108 | fpp_assert ( !reasonersInited() ); // do init only once |
| 109 | stdReasoner = new DlSatTester(*this); |
| 110 | |
| 111 | if ( NCFeatures.hasSingletons() ) |
| 112 | { |
| 113 | nomReasoner = new NominalReasoner(*this); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #endif |