| 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 <iostream> |
| 21 | |
| 22 | #include "KnowledgeExplorer.h" |
| 23 | #include "dlCompletionTree.h" |
| 24 | #include "dlTBox.h" |
| 25 | |
| 26 | /// init c'tor |
| 27 | KnowledgeExplorer :: KnowledgeExplorer ( const TBox* box, TExpressionManager* pEM ) |
| 28 | : D2I ( box->getDag(), pEM ) |
| 29 | { |
| 30 | // init all concepts |
| 31 | for ( TBox::c_const_iterator c = box->c_begin(), c_end = box->c_end(); c != c_end; ++c ) |
| 32 | addE ( Cs, *c ); |
| 33 | // init all individuals |
| 34 | for ( TBox::i_const_iterator i = box->i_begin(), i_end = box->i_end(); i != i_end; ++i ) |
| 35 | addE ( Is, *i ); |
| 36 | // init all object roles |
| 37 | for ( const auto& R: box->getORM() ) |
| 38 | { |
| 39 | addE ( ORs, R ); |
| 40 | for ( const auto& sup: R->ancestors() ) |
| 41 | ORs.add ( R, sup ); |
| 42 | } |
| 43 | // init all data roles |
| 44 | for ( const auto& R: box->getDRM() ) |
| 45 | { |
| 46 | addE ( DRs, R ); |
| 47 | for ( const auto& sup: R->ancestors() ) |
| 48 | DRs.add ( R, sup ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// @return true if the entry does not have correspondent entity |
| 53 | template <typename Entity> |
| 54 | bool |
| 55 | KnowledgeExplorer::EE2Map<Entity> :: check ( const TNamedEntry* e ) const |
| 56 | { |
| 57 | if ( e->getEntity() != nullptr ) |
| 58 | return false; |
| 59 | std::cerr << "No entity found for '" << e->getName() << "'\n"; |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /// add concept-like entity E (possibly with synonyms) to CONCEPTS |
| 64 | void |
| 65 | KnowledgeExplorer :: addC ( const TDLExpression* e ) |
| 66 | { |
| 67 | // check named concepts |
| 68 | const TDLConceptName* C = dynamic_cast<const TDLConceptName*>(e); |
| 69 | if ( C != nullptr ) |
| 70 | { |
| 71 | for ( EE2Map<TDLConceptName>::iterator p = Cs.begin(C), p_end = Cs.end(C); p != p_end; ++p ) |
| 72 | if ( unlikely(*p == nullptr) ) |
| 73 | std::cerr << "Null found while processing class " << C->getName() << "\n"; |
| 74 | else |
| 75 | Concepts.push_back(*p); |
| 76 | return; |
| 77 | } |
| 78 | // check named individuals |
| 79 | const TDLIndividualName* I = dynamic_cast<const TDLIndividualName*>(e); |
| 80 | if ( I != nullptr ) |
| 81 | { |
| 82 | for ( EE2Map<TDLIndividualName>::iterator p = Is.begin(I), p_end = Is.end(I); p != p_end; ++p ) |
| 83 | if ( unlikely(*p == nullptr) ) |
| 84 | std::cerr << "Null found while processing individual " << I->getName() << "\n"; |
| 85 | else |
| 86 | Concepts.push_back(*p); |
| 87 | return; |
| 88 | } |
| 89 | Concepts.push_back(e); |
| 90 | } |
| 91 | |
| 92 | const KnowledgeExplorer::TCGRoleSet& |
| 93 | KnowledgeExplorer :: getDataRoles ( const TCGNode* node, bool onlyDet ) |
| 94 | { |
| 95 | Roles.clear(); |
| 96 | for ( const DlCompletionTreeArc* edge : *node ) |
| 97 | if ( likely(!edge->isIBlocked()) && edge->getArcEnd()->isDataNode() && (!onlyDet || edge->getDep().empty()) ) |
| 98 | for (const auto* role: DRs.get(edge->getRole()->getEntity())) |
| 99 | Roles.insert(role); |
| 100 | return Roles; |
| 101 | } |
| 102 | /// build the set of object neighbours of a NODE; incoming edges are counted iff NEEDINCOMING is true |
| 103 | const KnowledgeExplorer::TCGRoleSet& |
| 104 | KnowledgeExplorer :: getObjectRoles ( const TCGNode* node, bool onlyDet, bool needIncoming ) |
| 105 | { |
| 106 | Roles.clear(); |
| 107 | for ( const DlCompletionTreeArc* edge : *node ) |
| 108 | if ( likely(!edge->isIBlocked()) && !edge->getArcEnd()->isDataNode() && (!onlyDet || edge->getDep().empty()) && (needIncoming || edge->isSuccEdge() ) ) |
| 109 | for (const auto* role: ORs.get(edge->getRole()->getEntity())) |
| 110 | Roles.insert(role); |
| 111 | return Roles; |
| 112 | } |
| 113 | /// build the set of neighbours of a NODE via role ROLE; put the resulting list into RESULT |
| 114 | const KnowledgeExplorer::TCGNodeVec& |
| 115 | KnowledgeExplorer :: getNeighbours ( const TCGNode* node, const TRole* R ) |
| 116 | { |
| 117 | Nodes.clear(); |
| 118 | for ( const DlCompletionTreeArc* edge : *node ) |
| 119 | if ( likely(!edge->isIBlocked()) && edge->isNeighbour(R) ) |
| 120 | Nodes.push_back(edge->getArcEnd()); |
| 121 | return Nodes; |
| 122 | } |
| 123 | /// put into RESULT all the data expressions from the NODE label |
| 124 | const KnowledgeExplorer::TCGItemVec& |
| 125 | KnowledgeExplorer :: getLabel ( const TCGNode* node, bool onlyDet ) |
| 126 | { |
| 127 | // prepare D2I translator |
| 128 | D2I.ensureDagSize(); |
| 129 | DlCompletionTree::const_label_iterator p, p_end; |
| 130 | bool data = node->isDataNode(); |
| 131 | Concepts.clear(); |
| 132 | for ( p = node->beginl_sc(), p_end = node->endl_sc(); p != p_end; ++p ) |
| 133 | if ( !onlyDet || p->getDep().empty() ) |
| 134 | addC(D2I.getExpr(p->bp(),data)); |
| 135 | for ( p = node->beginl_cc(), p_end = node->endl_cc(); p != p_end; ++p ) |
| 136 | if ( !onlyDet || p->getDep().empty() ) |
| 137 | addC(D2I.getExpr(p->bp(),data)); |
| 138 | return Concepts; |
| 139 | } |
| 140 | |
| 141 | /// @return blocker of a blocked node NODE or NULL if node is not blocked |
| 142 | const KnowledgeExplorer::TCGNode* |
| 143 | KnowledgeExplorer :: getBlocker ( const TCGNode* node ) const { return node->getBlocker(); } |