| 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 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 | // This file contains methods for creating DAG representation of KB |
| 21 | |
| 22 | #include "dlTBox.h" |
| 23 | |
| 24 | void TBox :: buildDAG ( void ) |
| 25 | { |
| 26 | nNominalReferences = 0; |
| 27 | |
| 28 | // init concept indexing |
| 29 | nC = 1; // start with 1 to make index 0 an indicator of "not processed" |
| 30 | ConceptMap.push_back(nullptr); |
| 31 | |
| 32 | // make fresh concept and datatype |
| 33 | concept2dag(pTemp); |
| 34 | DLTree* freshDT = DTCenter.getFreshDataType(); |
| 35 | addDataExprToHeap ( static_cast<TDataEntry*>(freshDT->Element().getNE()) ); |
| 36 | deleteTree(freshDT); |
| 37 | |
| 38 | for ( c_const_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 39 | concept2dag(*pc); |
| 40 | for ( i_const_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 41 | concept2dag(*pi); |
| 42 | |
| 43 | // init heads of simple rules |
| 44 | for ( TSimpleRules::iterator q = SimpleRules.begin(), q_end = SimpleRules.end(); q < q_end; ++q ) |
| 45 | (*q)->bpHead = tree2dag((*q)->tHead); |
| 46 | |
| 47 | // builds Roles range and domain |
| 48 | initRangeDomain(ORM); |
| 49 | initRangeDomain(DRM); |
| 50 | |
| 51 | RoleMaster::iterator p, p_end; |
| 52 | |
| 53 | // build all GCIs |
| 54 | DLTree* GCI = Axioms.getGCI(); |
| 55 | |
| 56 | // add special domains to the GCIs |
| 57 | if ( likely(useSpecialDomains) ) |
| 58 | for ( p = ORM.begin(), p_end = ORM.end(); p < p_end; ++p ) |
| 59 | if ( !(*p)->isSynonym() && (*p)->hasSpecialDomain() ) |
| 60 | GCI = createSNFAnd ( GCI, clone((*p)->getTSpecialDomain()) ); |
| 61 | |
| 62 | // take chains that lead to Bot role into account |
| 63 | if ( !ORM.getBotRole()->isSimple() ) |
| 64 | GCI = createSNFAnd ( GCI, |
| 65 | new DLTree ( TLexeme(FORALL), createRole(ORM.getBotRole()), createBottom() ) ); |
| 66 | |
| 67 | T_G = tree2dag(GCI); |
| 68 | deleteTree(GCI); |
| 69 | |
| 70 | // mark GCI flags |
| 71 | GCIs.setGCI(T_G != bpTOP); |
| 72 | GCIs.setReflexive(ORM.hasReflexiveRoles()); |
| 73 | |
| 74 | // builds functional labels for roles |
| 75 | for ( p = ORM.begin(), p_end = ORM.end(); p < p_end; ++p ) |
| 76 | if ( !(*p)->isSynonym() && (*p)->isTopFunc() ) |
| 77 | (*p)->setFunctional ( atmost2dag ( 1, *p, bpTOP ) ); |
| 78 | for ( p = DRM.begin(), p_end = DRM.end(); p < p_end; ++p ) |
| 79 | if ( !(*p)->isSynonym() && (*p)->isTopFunc() ) |
| 80 | (*p)->setFunctional ( atmost2dag ( 1, *p, bpTOP ) ); |
| 81 | |
| 82 | // check the type of the ontology |
| 83 | if ( nNominalReferences > 0 ) |
| 84 | { |
| 85 | unsigned int nInd = (unsigned int) (i_end() - i_begin()); |
| 86 | if ( nInd > 100 && nNominalReferences > nInd ) |
| 87 | isLikeWINE = true; |
| 88 | } |
| 89 | |
| 90 | // here DAG is complete; set its final size |
| 91 | DLHeap.setFinalSize(); |
| 92 | } |
| 93 | |
| 94 | void TBox :: initRangeDomain ( RoleMaster& RM ) |
| 95 | { |
| 96 | for ( auto& R: RM ) |
| 97 | if ( !R->isSynonym() ) |
| 98 | { |
| 99 | // add R&D from super-roles (do it AFTER axioms are transformed into R&D) |
| 100 | if ( RKG_UPDATE_RND_FROM_SUPERROLES ) |
| 101 | R->collectDomainFromSupers(); |
| 102 | |
| 103 | DLTree* dom = R->getTDomain(); |
| 104 | BipolarPointer bp = bpTOP; |
| 105 | if ( dom ) |
| 106 | { |
| 107 | bp = tree2dag(dom); |
| 108 | GCIs.setRnD(); |
| 109 | } |
| 110 | |
| 111 | R->setBPDomain(bp); |
| 112 | |
| 113 | // special domain for R is AR.Range |
| 114 | R->initSpecialDomain(); |
| 115 | if ( R->hasSpecialDomain() ) |
| 116 | R->setSpecialDomain(tree2dag(R->getTSpecialDomain())); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// register data expression in the DAG |
| 121 | BipolarPointer TBox :: addDataExprToHeap ( TDataEntry* p ) |
| 122 | { |
| 123 | if ( isValid(p->getBP()) ) // already registered value |
| 124 | return p->getBP(); |
| 125 | |
| 126 | // determine the type of an entry |
| 127 | DagTag dt = p->isBasicDataType() ? dtDataType : p->isDataValue() ? dtDataValue : dtDataExpr; |
| 128 | BipolarPointer hostBP = bpTOP; |
| 129 | |
| 130 | // register host type first (if any) |
| 131 | if ( p->getType() != nullptr ) |
| 132 | hostBP = addDataExprToHeap(const_cast<TDataEntry*>(p->getType())); |
| 133 | |
| 134 | // create new DAG entry for the data value |
| 135 | DLVertex* ver = new DLVertex ( dt, hostBP ); |
| 136 | ver->setConcept(p); |
| 137 | p->setBP(DLHeap.directAdd(ver)); |
| 138 | |
| 139 | return p->getBP(); |
| 140 | } |
| 141 | |
| 142 | void TBox :: addConceptToHeap ( TConcept* pConcept ) |
| 143 | { |
| 144 | // choose proper tag by concept |
| 145 | DagTag tag = pConcept->isPrimitive() ? |
| 146 | (pConcept->isSingleton() ? dtPSingleton : dtPConcept): |
| 147 | (pConcept->isSingleton() ? dtNSingleton : dtNConcept); |
| 148 | |
| 149 | // NSingleton is a nominal |
| 150 | if ( tag == dtNSingleton && !pConcept->isSynonym() ) |
| 151 | static_cast<TIndividual*>(pConcept)->setNominal(); |
| 152 | |
| 153 | // new concept's addition |
| 154 | DLVertex* ver = new DLVertex(tag); |
| 155 | ver->setConcept(pConcept); |
| 156 | pConcept->pName = DLHeap.directAdd(ver); |
| 157 | |
| 158 | BipolarPointer desc = bpTOP; |
| 159 | |
| 160 | // translate body of a concept |
| 161 | if ( pConcept->Description != nullptr ) // complex concept |
| 162 | desc = tree2dag(pConcept->Description); |
| 163 | else // only primitive concepts here |
| 164 | fpp_assert ( pConcept->isPrimitive() ); |
| 165 | |
| 166 | // update concept's entry |
| 167 | pConcept->pBody = desc; |
| 168 | ver->setChild(desc); |
| 169 | if ( !pConcept->isSynonym() && pConcept->index() == 0 ) |
| 170 | setConceptIndex(pConcept); |
| 171 | } |
| 172 | |
| 173 | BipolarPointer TBox :: tree2dag ( const DLTree* t ) |
| 174 | { |
| 175 | if ( t == nullptr ) |
| 176 | return bpINVALID; // invalid value |
| 177 | |
| 178 | const TLexeme& cur = t->Element(); |
| 179 | BipolarPointer ret = bpINVALID; |
| 180 | |
| 181 | switch ( cur.getToken() ) |
| 182 | { |
| 183 | case BOTTOM: // it is just !top |
| 184 | ret = bpBOTTOM; |
| 185 | break; |
| 186 | case TOP: // the 1st node |
| 187 | ret = bpTOP; |
| 188 | break; |
| 189 | case DATAEXPR: // data-related expression |
| 190 | ret = addDataExprToHeap ( static_cast<TDataEntry*>(cur.getNE()) ); |
| 191 | break; |
| 192 | case CNAME: // concept name |
| 193 | ret = concept2dag(toConcept(cur.getNE())); |
| 194 | break; |
| 195 | case INAME: // individual name |
| 196 | { |
| 197 | ++nNominalReferences; // definitely a nominal |
| 198 | TIndividual* ind = toIndividual(cur.getNE()); |
| 199 | ind->setNominal(); |
| 200 | ret = concept2dag(ind); |
| 201 | break; |
| 202 | } |
| 203 | |
| 204 | case NOT: |
| 205 | ret = inverse ( tree2dag ( t->Left() ) ); |
| 206 | break; |
| 207 | case AND: |
| 208 | ret = and2dag(t); |
| 209 | break; |
| 210 | case FORALL: |
| 211 | ret = forall2dag ( resolveRole(t->Left()), tree2dag(t->Right()) ); |
| 212 | break; |
| 213 | case SELF: |
| 214 | ret = reflexive2dag(resolveRole(t->Left())); |
| 215 | break; |
| 216 | case LE: |
| 217 | ret = atmost2dag ( cur.getData(), resolveRole(t->Left()), tree2dag(t->Right()) ); |
| 218 | break; |
| 219 | case PROJFROM: // note: no PROJINTO as already unified |
| 220 | ret = DLHeap.directAdd ( new DLVertex ( resolveRole(t->Left()), tree2dag(t->Right()->Right()), resolveRole(t->Right()->Left()) ) ); |
| 221 | break; |
| 222 | default: |
| 223 | fpp_assert ( isSNF(t) ); // safety check |
| 224 | fpp_unreachable(); // extra safety check ;) |
| 225 | } |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | /// fills AND-like vertex V with an AND-like expression T; process result |
| 231 | BipolarPointer |
| 232 | TBox :: and2dag ( const DLTree* t ) |
| 233 | { |
| 234 | BipolarPointer ret = bpBOTTOM; |
| 235 | DLVertex* v = new DLVertex(dtAnd); |
| 236 | |
| 237 | if ( fillANDVertex ( v, t ) ) // clash found |
| 238 | delete v; |
| 239 | else // AND vertex |
| 240 | switch ( v->end() - v->begin() ) |
| 241 | { |
| 242 | case 0: // and(TOP) = TOP |
| 243 | delete v; |
| 244 | return bpTOP; |
| 245 | case 1: // and(C) = C |
| 246 | ret = *v->begin(); |
| 247 | delete v; |
| 248 | break; |
| 249 | default: |
| 250 | ret = DLHeap.add(v); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | BipolarPointer TBox :: forall2dag ( const TRole* R, BipolarPointer C ) |
| 258 | { |
| 259 | if ( R->isDataRole() ) |
| 260 | return dataForall2dag(R,C); |
| 261 | |
| 262 | // create \all R.C == \all R{0}.C |
| 263 | BipolarPointer ret = DLHeap.add ( new DLVertex ( dtForall, 0, R, C ) ); |
| 264 | |
| 265 | if ( R->isSimple() ) // don't care about the rest |
| 266 | return ret; |
| 267 | |
| 268 | // check if the concept is not last |
| 269 | if ( !DLHeap.isLast(ret) ) |
| 270 | return ret; // all sub-roles were added before |
| 271 | |
| 272 | // have appropriate concepts for all the automaton states |
| 273 | for ( unsigned int i = 1; i < R->getAutomaton().size(); ++i ) |
| 274 | DLHeap.directAddAndCache ( new DLVertex ( dtForall, i, R, C ) ); |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | BipolarPointer TBox :: atmost2dag ( unsigned int n, const TRole* R, BipolarPointer C ) |
| 280 | { |
| 281 | // input check: only simple roles are allowed in the (non-trivial) NR |
| 282 | if ( !R->isSimple() ) |
| 283 | throw EFPPNonSimpleRole(R->getName()); |
| 284 | |
| 285 | if ( R->isDataRole() ) |
| 286 | return dataAtMost2dag(n,R,C); |
| 287 | |
| 288 | if ( unlikely ( C == bpBOTTOM ) ) // can happen as A & ~A |
| 289 | return bpTOP; |
| 290 | |
| 291 | BipolarPointer ret = DLHeap.add ( new DLVertex ( dtLE, n, R, C ) ); |
| 292 | |
| 293 | // check if the concept is not last |
| 294 | if ( !DLHeap.isLast(ret) ) |
| 295 | return ret; // all elements were added before |
| 296 | |
| 297 | // create entries for the transitive sub-roles |
| 298 | for ( unsigned int m = n-1; m > 0; --m ) |
| 299 | DLHeap.directAddAndCache ( new DLVertex ( dtLE, m, R, C ) ); |
| 300 | |
| 301 | // create a blocker for the NN-rule |
| 302 | DLHeap.directAddAndCache(new DLVertex(dtNN)); |
| 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | bool TBox :: fillANDVertex ( DLVertex* v, const DLTree* t ) |
| 308 | { |
| 309 | if ( t->Element().getToken() == AND ) |
| 310 | return fillANDVertex ( v, t->Left() ) || fillANDVertex ( v, t->Right() ); |
| 311 | else |
| 312 | return v->addChild ( tree2dag(t) ); |
| 313 | } |