| 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 | #ifndef TDAG2INTERFACE_H |
| 21 | #define TDAG2INTERFACE_H |
| 22 | |
| 23 | #include "tDLExpression.h" |
| 24 | #include "tExpressionManager.h" |
| 25 | #include "dlDag.h" |
| 26 | |
| 27 | /// class to translate DAG entities into the TDL* expressions |
| 28 | class TDag2Interface |
| 29 | { |
| 30 | protected: // members |
| 31 | /// DAG to be translated |
| 32 | const DLDag& Dag; |
| 33 | /// expression manager |
| 34 | TExpressionManager* Manager; |
| 35 | /// vector of cached concept expressions |
| 36 | std::vector<const TDLConceptExpression*> TransC; |
| 37 | /// vector of cached data expressions |
| 38 | std::vector<const TDLDataExpression*> TransD; |
| 39 | |
| 40 | protected: // methods |
| 41 | /// build concept expression by a vertex V |
| 42 | const TDLConceptExpression* buildCExpr ( const DLVertex& v ); |
| 43 | /// build data expression by a vertex V |
| 44 | const TDLDataExpression* buildDExpr ( const DLVertex& v ); |
| 45 | /// create concept name by named entry |
| 46 | static const TDLConceptName* CName ( const TNamedEntry* p ) { return dynamic_cast<const TDLConceptName*>(p->getEntity()); } |
| 47 | /// create individual name by named entry |
| 48 | static const TDLIndividualName* IName ( const TNamedEntry* p ) { return dynamic_cast<const TDLIndividualName*>(p->getEntity()); } |
| 49 | /// create object role name by named entry |
| 50 | static const TDLObjectRoleName* ORName ( const TNamedEntry* p ) { return dynamic_cast<const TDLObjectRoleName*>(p->getEntity()); } |
| 51 | /// create data role name by named entry |
| 52 | static const TDLDataRoleName* DRName ( const TNamedEntry* p ) { return dynamic_cast<const TDLDataRoleName*>(p->getEntity()); } |
| 53 | |
| 54 | public: // interface |
| 55 | /// init c'tor |
| 56 | TDag2Interface ( const DLDag& dag, TExpressionManager* manager ) |
| 57 | : Dag(dag) |
| 58 | , Manager(manager) |
| 59 | , TransC(dag.size(),nullptr) |
| 60 | , TransD(dag.size(),nullptr) |
| 61 | {} |
| 62 | |
| 63 | /// make sure that size of expression cache is the same as the size of a DAG |
| 64 | void ensureDagSize ( void ) |
| 65 | { |
| 66 | size_t ds = Dag.size(), ts = TransC.size(); |
| 67 | if ( likely(ds == ts) ) |
| 68 | return; |
| 69 | TransC.resize(ds); |
| 70 | TransD.resize(ds); |
| 71 | if ( unlikely(ds>ts) ) |
| 72 | for ( ; ts != ds; ++ts ) |
| 73 | { |
| 74 | TransC[ts] = nullptr; |
| 75 | TransD[ts] = nullptr; |
| 76 | } |
| 77 | } |
| 78 | /// get concept expression corresponding index of vertex |
| 79 | const TDLConceptExpression* getCExpr ( BipolarPointer p ) |
| 80 | { |
| 81 | if ( isNegative(p) ) |
| 82 | return Manager->Not(getCExpr(inverse(p))); |
| 83 | unsigned int i = (unsigned int)p; |
| 84 | if ( TransC[i] == nullptr ) |
| 85 | TransC[i] = buildCExpr(Dag[p]); |
| 86 | return TransC[i]; |
| 87 | } |
| 88 | /// get data expression corresponding index of vertex |
| 89 | const TDLDataExpression* getDExpr ( BipolarPointer p ) |
| 90 | { |
| 91 | if ( isNegative(p) ) |
| 92 | return Manager->DataNot(getDExpr(inverse(p))); |
| 93 | unsigned int i = (unsigned int)p; |
| 94 | if ( TransD[i] == nullptr ) |
| 95 | TransD[i] = buildDExpr(Dag[p]); |
| 96 | return TransD[i]; |
| 97 | } |
| 98 | /// get expression corresponding index of vertex given the DATA flag |
| 99 | const TDLExpression* getExpr ( BipolarPointer p, bool data ) |
| 100 | { |
| 101 | if ( data ) |
| 102 | return getDExpr(p); |
| 103 | else |
| 104 | return getCExpr(p); |
| 105 | } |
| 106 | }; // TDag2Interface |
| 107 | |
| 108 | #endif |