| 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 | #include <iostream> |
| 21 | |
| 22 | #include "dltree.h" |
| 23 | #include "fpp_assert.h" |
| 24 | #include "tDataEntry.h" |
| 25 | #include "tRole.h" |
| 26 | |
| 27 | /// create entry for the role R |
| 28 | DLTree* |
| 29 | createRole ( TRole* R ) |
| 30 | { |
| 31 | return createEntry ( R->isDataRole() ? DNAME : RNAME, R ); |
| 32 | } |
| 33 | /// create inverse of role R |
| 34 | DLTree* createInverse ( DLTree* R ) |
| 35 | { |
| 36 | fpp_assert ( R != nullptr ); // sanity check |
| 37 | switch ( R->Element().getToken() ) |
| 38 | { |
| 39 | case INV: // R-- = R |
| 40 | { |
| 41 | DLTree* p = clone(R->Left()); |
| 42 | deleteTree(R); |
| 43 | return p; |
| 44 | } |
| 45 | case RNAME: // object role name |
| 46 | if ( unlikely(isTopRole(R)) || unlikely(isBotRole(R)) ) |
| 47 | return R; // top/bottom roles are inverses of themselves |
| 48 | return new DLTree ( TLexeme(INV), R ); |
| 49 | default: // no other elements can have inverses |
| 50 | fpp_unreachable(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// create negation of given formula |
| 55 | DLTree* createSNFNot ( DLTree* C ) |
| 56 | { |
| 57 | fpp_assert ( C != nullptr ); // sanity check |
| 58 | if ( C->Element() == BOTTOM ) |
| 59 | { // \not F = T |
| 60 | deleteTree(C); |
| 61 | return createTop(); |
| 62 | } |
| 63 | if ( C->Element() == TOP ) |
| 64 | { // \not T = F |
| 65 | deleteTree(C); |
| 66 | return createBottom(); |
| 67 | } |
| 68 | if ( C->Element () == NOT ) |
| 69 | { // \not\not C = C |
| 70 | DLTree* p = clone(C->Left()); |
| 71 | deleteTree(C); |
| 72 | return p; |
| 73 | } |
| 74 | |
| 75 | // general case |
| 76 | return new DLTree ( TLexeme(NOT), C ); |
| 77 | } |
| 78 | |
| 79 | /// create conjunction of given formulas |
| 80 | DLTree* createSNFAnd ( DLTree* C, DLTree* D ) |
| 81 | { |
| 82 | // try to simplify conjunction |
| 83 | if ( C == nullptr ) // single element |
| 84 | return D; |
| 85 | if ( D == nullptr ) |
| 86 | return C; |
| 87 | |
| 88 | if ( C->Element() == TOP || // T\and D = D |
| 89 | D->Element() == BOTTOM ) // C\and F = F |
| 90 | { |
| 91 | deleteTree(C); |
| 92 | return D; |
| 93 | } |
| 94 | |
| 95 | if ( D->Element() == TOP || // C\and T = C |
| 96 | C->Element() == BOTTOM ) // F\and D = F |
| 97 | { |
| 98 | deleteTree(D); |
| 99 | return C; |
| 100 | } |
| 101 | |
| 102 | // no simplification possible -- return actual conjunction |
| 103 | return new DLTree ( TLexeme(AND), C, D ); |
| 104 | } |
| 105 | |
| 106 | static bool |
| 107 | containsC ( DLTree* C, DLTree* D ) |
| 108 | { |
| 109 | switch ( C->Element().getToken() ) |
| 110 | { |
| 111 | case CNAME: |
| 112 | return equalTrees ( C, D ); |
| 113 | case AND: |
| 114 | return containsC ( C->Left(), D ) || containsC ( C->Right(), D ); |
| 115 | default: |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | DLTree* createSNFReducedAnd ( DLTree* C, DLTree* D ) |
| 121 | { |
| 122 | if ( C == nullptr || D == nullptr ) |
| 123 | return createSNFAnd ( C, D ); |
| 124 | |
| 125 | if ( D->Element().getToken() == CNAME && containsC ( C, D ) ) |
| 126 | { |
| 127 | deleteTree(D); |
| 128 | return C; |
| 129 | } |
| 130 | else if ( D->Element().getToken() == AND ) |
| 131 | { |
| 132 | C = createSNFReducedAnd ( C, clone(D->Left()) ); |
| 133 | C = createSNFReducedAnd ( C, clone(D->Right()) ); |
| 134 | deleteTree(D); |
| 135 | return C; |
| 136 | } |
| 137 | else // can't optimise |
| 138 | return createSNFAnd ( C, D ); |
| 139 | } |
| 140 | |
| 141 | // Semantic Locality checking support. DO NOT used in usual reasoning |
| 142 | |
| 143 | /// @return true iff a data range DR is semantically equivalent to TOP. FIXME!! good approximation for now |
| 144 | static bool |
| 145 | isSemanticallyDataTop ( DLTree* dr ) { return dr->Element().getToken() == TOP; } |
| 146 | |
| 147 | /// @return true iff a data range DR is semantically equivalent to BOTTOM. FIXME!! good approximation for now |
| 148 | static bool |
| 149 | isSemanticallyDataBottom ( DLTree* dr ) { return dr->Element().getToken() == BOTTOM; } |
| 150 | |
| 151 | /// @return true iff the cardinality of a given data range DR is greater than N. FIXME!! good approximation for now |
| 152 | static bool |
| 153 | isDataRangeBigEnough ( DLTree*, unsigned int ) { return true; } |
| 154 | |
| 155 | /// simplify universal restriction with top data role |
| 156 | static DLTree* |
| 157 | simplifyDataTopForall ( DLTree* dr ) |
| 158 | { |
| 159 | TreeDeleter td(dr); |
| 160 | // if the filler (dr) is TOP (syntactically or semantically), then the forall is top |
| 161 | if ( isSemanticallyDataTop(dr) ) |
| 162 | return createTop(); |
| 163 | // in any other case the attempt to restrict the data domain will fail |
| 164 | return createBottom(); |
| 165 | } |
| 166 | |
| 167 | /// simplify minimal cardinality restriction with top data role |
| 168 | static DLTree* |
| 169 | simplifyDataTopLE ( unsigned int n, DLTree* dr ) |
| 170 | { |
| 171 | TreeDeleter td(dr); |
| 172 | // if the filler (dr) is BOTTOM (syntactically or semantically), then the LE is top |
| 173 | if ( isSemanticallyDataBottom(dr) ) |
| 174 | return createTop(); |
| 175 | // if the size of a filler is smaller than the cardinality, then it's always possible to make a restriction |
| 176 | if ( !isDataRangeBigEnough ( dr, n ) ) |
| 177 | return createTop(); |
| 178 | // in any other case the attempt to restrict the data domain will fail |
| 179 | return createBottom(); |
| 180 | } |
| 181 | |
| 182 | /// create universal restriction of given formulas (\AR.C) |
| 183 | DLTree* createSNFForall ( DLTree* R, DLTree* C ) |
| 184 | { |
| 185 | if ( C->Element() == TOP ) // \AR.T = T |
| 186 | { |
| 187 | deleteTree(R); |
| 188 | return C; |
| 189 | } |
| 190 | if ( unlikely(isBotRole(R)) ) |
| 191 | { // \A Bot.C = T |
| 192 | deleteTree(R); |
| 193 | deleteTree(C); |
| 194 | return createTop(); |
| 195 | } |
| 196 | if ( unlikely(isTopRole(R)) && resolveRole(R)->isDataRole() ) |
| 197 | { |
| 198 | deleteTree(R); |
| 199 | return simplifyDataTopForall(C); |
| 200 | } |
| 201 | // no simplification possible |
| 202 | return new DLTree ( TLexeme(FORALL), R, C ); |
| 203 | } |
| 204 | /// create at-most (LE) restriction of given formulas (<= n R.C) |
| 205 | DLTree* createSNFLE ( unsigned int n, DLTree* R, DLTree* C ) |
| 206 | { |
| 207 | if ( C->Element() == BOTTOM ) |
| 208 | { // <= n R.F -> T; |
| 209 | deleteTree(R); |
| 210 | deleteTree(C); |
| 211 | return createTop(); |
| 212 | } |
| 213 | if ( n == 0 ) // <= 0 R.C -> \AR.\not C |
| 214 | return createSNFForall ( R, createSNFNot(C) ); |
| 215 | if ( unlikely(isBotRole(R)) ) |
| 216 | { // <=n Bot.C = T |
| 217 | deleteTree(R); |
| 218 | deleteTree(C); |
| 219 | return createTop(); |
| 220 | } |
| 221 | if ( unlikely(isTopRole(R)) && resolveRole(R)->isDataRole() ) |
| 222 | { |
| 223 | deleteTree(R); |
| 224 | return simplifyDataTopLE ( n, C ); |
| 225 | } |
| 226 | return new DLTree ( TLexeme ( LE, n ), R, C ); |
| 227 | } |
| 228 | /// create at-least (GE) restriction of given formulas (>= n R.C) |
| 229 | DLTree* createSNFGE ( unsigned int n, DLTree* R, DLTree* C ) |
| 230 | { |
| 231 | if ( n == 0 ) |
| 232 | { // >= 0 R.C -> T |
| 233 | deleteTree(R); |
| 234 | deleteTree(C); |
| 235 | return createTop(); |
| 236 | } |
| 237 | if ( C->Element() == BOTTOM ) |
| 238 | { // >=n R.F -> F |
| 239 | deleteTree(R); |
| 240 | return C; |
| 241 | } |
| 242 | else // >= n R.C -> !<= (n-1) R.C |
| 243 | return createSNFNot ( createSNFLE ( n-1 , R, C ) ); |
| 244 | } |
| 245 | |
| 246 | //******************************************************************************************** |
| 247 | //** equalTrees implementation |
| 248 | //******************************************************************************************** |
| 249 | bool equalTrees ( const DLTree* t1, const DLTree* t2 ) |
| 250 | { |
| 251 | // empty trees are equal |
| 252 | if ( t1 == nullptr && t2 == nullptr ) |
| 253 | return true; |
| 254 | |
| 255 | // empty and non-empty trees are not equal |
| 256 | if ( t1 == nullptr || t2 == nullptr ) |
| 257 | return false; |
| 258 | |
| 259 | // non-empty trees are checked recursively |
| 260 | return ( t1->Element() == t2->Element() ) && |
| 261 | equalTrees ( t1->Left(), t2->Left() ) && |
| 262 | equalTrees ( t1->Right(), t2->Right() ); |
| 263 | } |
| 264 | |
| 265 | bool isSubTree ( const DLTree* t1, const DLTree* t2 ) |
| 266 | { |
| 267 | if ( t1 == nullptr || t1->Element() == TOP ) |
| 268 | return true; |
| 269 | if ( t2 == nullptr ) |
| 270 | return false; |
| 271 | if ( t1->Element() == AND ) |
| 272 | return isSubTree ( t1->Left(), t2 ) && isSubTree ( t1->Right(), t2 ); |
| 273 | // t1 is a single elem, t2 is a (probably) AND-tree |
| 274 | if ( t2->Element() == AND ) |
| 275 | return isSubTree ( t1, t2->Left() ) || isSubTree ( t1, t2->Right() ); |
| 276 | // t1 and t2 are non-single elements |
| 277 | return equalTrees(t1,t2); |
| 278 | } |
| 279 | //******************************************************************************************** |
| 280 | //** OnlySNF realization |
| 281 | //******************************************************************************************** |
| 282 | bool isSNF ( const DLTree* t ) |
| 283 | { |
| 284 | if ( t == nullptr ) |
| 285 | return true; |
| 286 | |
| 287 | switch ( t -> Element (). getToken () ) |
| 288 | { |
| 289 | case TOP: |
| 290 | case BOTTOM: |
| 291 | case NAME: |
| 292 | case DATAEXPR: |
| 293 | case NOT: |
| 294 | case INV: |
| 295 | case AND: |
| 296 | case FORALL: |
| 297 | case LE: |
| 298 | case SELF: |
| 299 | case RCOMPOSITION: |
| 300 | case PROJFROM: |
| 301 | case PROJINTO: |
| 302 | return ( isSNF (t->Left()) && isSNF (t->Right()) ); |
| 303 | default: |
| 304 | return false; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | //******************************************************************************************** |
| 309 | const char* TokenName ( Token t ) |
| 310 | { |
| 311 | switch ( t ) |
| 312 | { |
| 313 | case TOP: return "*TOP*"; |
| 314 | case BOTTOM: return "*BOTTOM*"; |
| 315 | case CNAME: return "cname"; |
| 316 | case INAME: return "iname"; |
| 317 | case RNAME: return "rname"; |
| 318 | case DNAME: return "dname"; |
| 319 | case DATAEXPR: return "dataexpr"; |
| 320 | case INV: return "inv"; |
| 321 | case OR: return "or"; |
| 322 | case AND: return "and"; |
| 323 | case NOT: return "not"; |
| 324 | case EXISTS: return "some"; |
| 325 | case FORALL: return "all"; |
| 326 | case GE: return "at-least"; |
| 327 | case LE: return "at-most"; |
| 328 | case RCOMPOSITION: return "compose"; |
| 329 | case SELF: return "self-ref"; |
| 330 | case PROJINTO: return "project_into"; |
| 331 | case PROJFROM: return "project_from"; |
| 332 | default: std::cerr << "token " << t << "has no name"; |
| 333 | fpp_unreachable(); |
| 334 | }; |
| 335 | } |
| 336 | |
| 337 | std::ostream& operator << ( std::ostream& o, const DLTree *form ) |
| 338 | { |
| 339 | if ( form == nullptr ) |
| 340 | return o; |
| 341 | |
| 342 | const TLexeme& lex = form->Element(); |
| 343 | switch ( lex.getToken() ) |
| 344 | { |
| 345 | case TOP: |
| 346 | case BOTTOM: |
| 347 | o << ' ' << TokenName(lex.getToken()); |
| 348 | break; |
| 349 | case RNAME: |
| 350 | case DNAME: |
| 351 | case CNAME: |
| 352 | o << ' ' << lex.getName(); |
| 353 | break; |
| 354 | case INAME: |
| 355 | o << " (one-of " << lex.getName() << ')'; |
| 356 | break; |
| 357 | |
| 358 | case DATAEXPR: |
| 359 | static_cast<TDataEntry*>(lex.getNE())->printLISP(o); |
| 360 | break; |
| 361 | |
| 362 | case NOT: |
| 363 | case INV: |
| 364 | case SELF: |
| 365 | o << " (" << TokenName (lex.getToken()) << form->Left() << ')'; |
| 366 | break; |
| 367 | |
| 368 | case AND: |
| 369 | case OR: |
| 370 | case EXISTS: |
| 371 | case FORALL: |
| 372 | case RCOMPOSITION: |
| 373 | case PROJINTO: |
| 374 | case PROJFROM: |
| 375 | o << " (" << TokenName (lex.getToken()) << form->Left() << form->Right() << ')'; |
| 376 | break; |
| 377 | |
| 378 | case GE: |
| 379 | case LE: |
| 380 | o << " (" << TokenName (lex.getToken()) << ' ' << lex.getData() |
| 381 | << form->Left() << form->Right() << ')'; |
| 382 | break; |
| 383 | |
| 384 | default: |
| 385 | break; |
| 386 | } |
| 387 | return o; |
| 388 | } |
| 389 | |
| 390 | //******************************************************************************************** |