| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef EXTENDEDSYNTACTICLOCALITYCHECKER_H |
| 21 |
#define EXTENDEDSYNTACTICLOCALITYCHECKER_H |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 28 |
# define FPP_DEBUG_LOCALITY |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "GeneralSyntacticLocalityChecker.h" |
| 32 |
|
| 33 |
#ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 34 |
# include "tExpressionPrinterLISP.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
class UpperBoundDirectEvaluator; |
| 39 |
class LowerBoundDirectEvaluator; |
| 40 |
class UpperBoundComplementEvaluator; |
| 41 |
class LowerBoundComplementEvaluator; |
| 42 |
|
| 43 |
class CardinalityEvaluatorBase: protected SigAccessor, public DLExpressionVisitorEmpty |
| 44 |
{ |
| 45 |
protected: |
| 46 |
#ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 47 |
TLISPExpressionPrinter lp; |
| 48 |
const char* name; |
| 49 |
#endif |
| 50 |
|
| 51 |
UpperBoundDirectEvaluator* UBD = nullptr; |
| 52 |
LowerBoundDirectEvaluator* LBD = nullptr; |
| 53 |
UpperBoundComplementEvaluator* UBC = nullptr; |
| 54 |
LowerBoundComplementEvaluator* LBC = nullptr; |
| 55 |
|
| 56 |
|
| 57 |
int value = 0; |
| 58 |
|
| 59 |
protected: |
| 60 |
|
| 61 |
int getValue ( const TDLExpression& expr ) |
| 62 |
{ |
| 63 |
expr.accept(*this); |
| 64 |
return value; |
| 65 |
} |
| 66 |
|
| 67 |
void dumpValue ( const TDLExpression& expr ATTR_UNUSED ) |
| 68 |
{ |
| 69 |
# ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 70 |
std::cout << name << ": value for"; |
| 71 |
expr.accept(lp); |
| 72 |
std::cout << " is " << value << std::endl; |
| 73 |
# endif |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
int getUpperBoundDirect ( const TDLExpression& expr ); |
| 78 |
|
| 79 |
int getUpperBoundComplement ( const TDLExpression& expr ); |
| 80 |
|
| 81 |
int getLowerBoundDirect ( const TDLExpression& expr ); |
| 82 |
|
| 83 |
int getLowerBoundComplement ( const TDLExpression& expr ); |
| 84 |
|
| 85 |
protected: |
| 86 |
|
| 87 |
int noUpperValue ( void ) const { return -1; } |
| 88 |
|
| 89 |
int anyUpperValue ( void ) const { return 0; } |
| 90 |
|
| 91 |
int getAllNoneUpper ( bool condition ) const { return condition ? anyUpperValue() : noUpperValue(); } |
| 92 |
|
| 93 |
int noLowerValue ( void ) const { return 0; } |
| 94 |
|
| 95 |
int anyLowerValue ( void ) const { return -1; } |
| 96 |
|
| 97 |
int getOneNoneLower ( bool condition ) const { return condition ? 1 : noLowerValue(); } |
| 98 |
|
| 99 |
|
| 100 |
bool isUpperLT ( int value, unsigned int m ) const |
| 101 |
{ |
| 102 |
if ( value == noUpperValue() ) |
| 103 |
return false; |
| 104 |
return value == anyUpperValue() || (unsigned int) value < m; |
| 105 |
} |
| 106 |
|
| 107 |
bool isUpperLE ( int value, unsigned int m ) const { return isUpperLT ( value, m+1 ); } |
| 108 |
|
| 109 |
bool isLowerGE ( int value, unsigned int m ) const |
| 110 |
{ |
| 111 |
if ( value == noLowerValue() ) |
| 112 |
return false; |
| 113 |
return value == anyLowerValue() || (unsigned int) value >= m; |
| 114 |
} |
| 115 |
|
| 116 |
bool isLowerGT ( int value, unsigned int m ) const { return isLowerGE ( value, m+1 ); } |
| 117 |
|
| 118 |
bool isBotEquivalent ( const TDLExpression* expr ) { return isUpperLE ( getUpperBoundDirect(*expr), 0 ); } |
| 119 |
bool isTopEquivalent ( const TDLExpression* expr ) { return isUpperLE ( getUpperBoundComplement(*expr), 0 ); } |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
int minUpperValue ( int uv1, int uv2 ) |
| 125 |
{ |
| 126 |
|
| 127 |
if ( uv1 == noUpperValue() ) |
| 128 |
return uv2; |
| 129 |
if ( uv2 == noUpperValue() ) |
| 130 |
return uv1; |
| 131 |
|
| 132 |
|
| 133 |
return std::min(uv1,uv2); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
virtual int getEntityValue ( const TNamedEntity* entity ) = 0; |
| 157 |
|
| 158 |
virtual int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) = 0; |
| 159 |
|
| 160 |
virtual int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) = 0; |
| 161 |
|
| 162 |
virtual int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) = 0; |
| 163 |
|
| 164 |
virtual int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) = 0; |
| 165 |
|
| 166 |
public: |
| 167 |
|
| 168 |
CardinalityEvaluatorBase ( const TSignature* s, const char* n ATTR_UNUSED ) |
| 169 |
: SigAccessor(s) |
| 170 |
# ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 171 |
, lp(std::cout) |
| 172 |
, name(n) |
| 173 |
# endif |
| 174 |
{} |
| 175 |
|
| 176 |
|
| 177 |
void setEvaluators ( UpperBoundDirectEvaluator* pUD, LowerBoundDirectEvaluator* pLD, UpperBoundComplementEvaluator* pUC, LowerBoundComplementEvaluator* pLC ) |
| 178 |
{ |
| 179 |
UBD = pUD; |
| 180 |
LBD = pLD; |
| 181 |
UBC = pUC; |
| 182 |
LBC = pLC; |
| 183 |
fpp_assert ( (void*)UBD == this || (void*)LBD == this || (void*)UBC == this || (void*)LBC == this ); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
int getUpperBoundDirect ( const TDLExpression* expr ) { return getUpperBoundDirect(*expr); } |
| 188 |
|
| 189 |
int getUpperBoundComplement ( const TDLExpression* expr ) { return getUpperBoundComplement(*expr); } |
| 190 |
|
| 191 |
int getLowerBoundDirect ( const TDLExpression* expr ) { return getLowerBoundDirect(*expr); } |
| 192 |
|
| 193 |
int getLowerBoundComplement ( const TDLExpression* expr ) { return getLowerBoundComplement(*expr); } |
| 194 |
|
| 195 |
public: |
| 196 |
|
| 197 |
void visit ( const TDLConceptName& expr ) override |
| 198 |
{ value = getEntityValue(expr.getEntity()); } |
| 199 |
void visit ( const TDLConceptObjectExists& expr ) override |
| 200 |
{ value = getMinValue ( 1, expr.getOR(), expr.getC() ); dumpValue(expr); } |
| 201 |
void visit ( const TDLConceptObjectForall& expr ) override |
| 202 |
{ value = getForallValue ( expr.getOR(), expr.getC() ); } |
| 203 |
void visit ( const TDLConceptObjectMinCardinality& expr ) override |
| 204 |
{ value = getMinValue ( expr.getNumber(), expr.getOR(), expr.getC() ); } |
| 205 |
void visit ( const TDLConceptObjectMaxCardinality& expr ) override |
| 206 |
{ value = getMaxValue ( expr.getNumber(), expr.getOR(), expr.getC() ); } |
| 207 |
void visit ( const TDLConceptObjectExactCardinality& expr ) override |
| 208 |
{ value = getExactValue ( expr.getNumber(), expr.getOR(), expr.getC() ); } |
| 209 |
void visit ( const TDLConceptDataExists& expr ) override |
| 210 |
{ value = getMinValue ( 1, expr.getDR(), expr.getExpr() ); } |
| 211 |
void visit ( const TDLConceptDataForall& expr ) override |
| 212 |
{ value = getForallValue ( expr.getDR(), expr.getExpr() ); } |
| 213 |
void visit ( const TDLConceptDataMinCardinality& expr ) override |
| 214 |
{ value = getMinValue ( expr.getNumber(), expr.getDR(), expr.getExpr() ); } |
| 215 |
void visit ( const TDLConceptDataMaxCardinality& expr ) override |
| 216 |
{ value = getMaxValue ( expr.getNumber(), expr.getDR(), expr.getExpr() ); } |
| 217 |
void visit ( const TDLConceptDataExactCardinality& expr ) override |
| 218 |
{ value = getExactValue ( expr.getNumber(), expr.getDR(), expr.getExpr() ); } |
| 219 |
|
| 220 |
|
| 221 |
void visit ( const TDLObjectRoleName& expr ) override |
| 222 |
{ value = getEntityValue(expr.getEntity()); } |
| 223 |
|
| 224 |
void visit ( const TDLObjectRoleProjectionFrom& expr ) override |
| 225 |
{ value = getMinValue ( 1, expr.getOR(), expr.getC() ); } |
| 226 |
|
| 227 |
void visit ( const TDLObjectRoleProjectionInto& expr ) override |
| 228 |
{ value = getMinValue ( 1, expr.getOR(), expr.getC() ); } |
| 229 |
|
| 230 |
|
| 231 |
void visit ( const TDLDataRoleName& expr ) override |
| 232 |
{ value = getEntityValue(expr.getEntity()); } |
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
class UpperBoundDirectEvaluator: public CardinalityEvaluatorBase |
| 238 |
{ |
| 239 |
protected: |
| 240 |
|
| 241 |
int getEntityValue ( const TNamedEntity* entity ) override |
| 242 |
{ return getAllNoneUpper ( botCLocal() && nc(entity) ); } |
| 243 |
|
| 244 |
int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override |
| 245 |
{ return getAllNoneUpper ( isTopEquivalent(R) && isLowerGE ( getLowerBoundComplement(C), 1 ) ); } |
| 246 |
|
| 247 |
int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 248 |
{ |
| 249 |
|
| 250 |
if ( m <= 0 ) |
| 251 |
return noUpperValue(); |
| 252 |
|
| 253 |
if ( isBotEquivalent(R) ) |
| 254 |
return anyUpperValue(); |
| 255 |
|
| 256 |
return getAllNoneUpper ( isUpperLT ( getUpperBoundDirect(C), m ) ); |
| 257 |
} |
| 258 |
|
| 259 |
int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 260 |
{ |
| 261 |
|
| 262 |
if ( !isTopEquivalent(R) ) |
| 263 |
return noUpperValue(); |
| 264 |
|
| 265 |
return getAllNoneUpper ( isLowerGT ( getLowerBoundDirect(C), m ) ); |
| 266 |
} |
| 267 |
|
| 268 |
int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 269 |
{ |
| 270 |
|
| 271 |
return minUpperValue ( getMinValue ( m, R, C ), getMaxValue ( m, R, C ) ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
template <typename C> |
| 276 |
int getAndValue ( const TDLNAryExpression<C>& expr ) |
| 277 |
{ |
| 278 |
|
| 279 |
int min = noUpperValue(); |
| 280 |
|
| 281 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 282 |
min = minUpperValue ( min, getUpperBoundDirect(*p) ); |
| 283 |
return min; |
| 284 |
} |
| 285 |
|
| 286 |
template <typename C> |
| 287 |
int getOrValue ( const TDLNAryExpression<C>& expr ) |
| 288 |
{ |
| 289 |
int sum = 0; |
| 290 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 291 |
{ |
| 292 |
int n = getUpperBoundDirect(*p); |
| 293 |
if ( n == noUpperValue() ) |
| 294 |
return noUpperValue(); |
| 295 |
sum += n; |
| 296 |
} |
| 297 |
return sum; |
| 298 |
} |
| 299 |
|
| 300 |
public: |
| 301 |
|
| 302 |
explicit UpperBoundDirectEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "UpperBoundDirect" ) {} |
| 303 |
|
| 304 |
public: |
| 305 |
|
| 306 |
using CardinalityEvaluatorBase::visit; |
| 307 |
|
| 308 |
void visit ( const TDLConceptTop& ) override { value = noUpperValue(); } |
| 309 |
void visit ( const TDLConceptBottom& ) override { value = anyUpperValue(); } |
| 310 |
void visit ( const TDLConceptNot& expr ) override { value = getUpperBoundComplement(expr.getC()); } |
| 311 |
void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); } |
| 312 |
void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); } |
| 313 |
void visit ( const TDLConceptOneOf& expr ) override { value = int(expr.size()); } |
| 314 |
void visit ( const TDLConceptObjectSelf& expr ) override { value = getAllNoneUpper(isBotEquivalent(expr.getOR())); } |
| 315 |
void visit ( const TDLConceptObjectValue& expr ) override { value = getAllNoneUpper(isBotEquivalent(expr.getOR())); } |
| 316 |
void visit ( const TDLConceptDataValue& expr ) override { value = getAllNoneUpper(isBotEquivalent(expr.getDR())); } |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
void visit ( const TDLObjectRoleTop& ) override { value = noUpperValue(); } |
| 321 |
void visit ( const TDLObjectRoleBottom& ) override { value = anyUpperValue(); } |
| 322 |
void visit ( const TDLObjectRoleInverse& expr ) override { value = getUpperBoundDirect(expr.getOR()); } |
| 323 |
void visit ( const TDLObjectRoleChain& expr ) override |
| 324 |
{ |
| 325 |
for ( const auto* arg : expr ) |
| 326 |
if ( isBotEquivalent(arg) ) |
| 327 |
{ |
| 328 |
value = anyUpperValue(); |
| 329 |
return; |
| 330 |
} |
| 331 |
value = noUpperValue(); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
void visit ( const TDLDataRoleTop& ) override { value = noUpperValue(); } |
| 336 |
void visit ( const TDLDataRoleBottom& ) override { value = anyUpperValue(); } |
| 337 |
|
| 338 |
|
| 339 |
void visit ( const TDLDataTop& ) override { value = noUpperValue(); } |
| 340 |
void visit ( const TDLDataBottom& ) override { value = anyUpperValue(); } |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
void visit ( const TDLDataValue& ) override { value = 1; } |
| 346 |
void visit ( const TDLDataNot& expr ) override { value = getUpperBoundComplement(expr.getExpr()); } |
| 347 |
void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); } |
| 348 |
void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); } |
| 349 |
void visit ( const TDLDataOneOf& expr ) override { value = (int)expr.size(); } |
| 350 |
}; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
class UpperBoundComplementEvaluator: public CardinalityEvaluatorBase |
| 355 |
{ |
| 356 |
protected: |
| 357 |
|
| 358 |
int getEntityValue ( const TNamedEntity* entity ) override |
| 359 |
{ return getAllNoneUpper ( topCLocal() && nc(entity) ); } |
| 360 |
|
| 361 |
int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override |
| 362 |
{ return getAllNoneUpper ( isBotEquivalent(R) || isUpperLE ( getUpperBoundComplement(C), 0 ) ); } |
| 363 |
|
| 364 |
int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 365 |
{ |
| 366 |
|
| 367 |
if ( m == 0 ) |
| 368 |
return anyUpperValue(); |
| 369 |
|
| 370 |
if ( !isTopEquivalent(R) ) |
| 371 |
return noUpperValue(); |
| 372 |
|
| 373 |
return getAllNoneUpper ( isLowerGE ( getLowerBoundDirect(C), m ) ); |
| 374 |
} |
| 375 |
|
| 376 |
int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 377 |
{ |
| 378 |
|
| 379 |
if ( isBotEquivalent(R) ) |
| 380 |
return anyUpperValue(); |
| 381 |
|
| 382 |
return getAllNoneUpper ( isUpperLE ( getUpperBoundDirect(C), m ) ); |
| 383 |
} |
| 384 |
|
| 385 |
int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 386 |
{ |
| 387 |
|
| 388 |
return std::min ( getMinValue ( m, R, C ), getMaxValue ( m, R, C ) ); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
template <typename C> |
| 393 |
int getAndValue ( const TDLNAryExpression<C>& expr ) |
| 394 |
{ |
| 395 |
int sum = 0; |
| 396 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 397 |
{ |
| 398 |
int n = getUpperBoundComplement(*p); |
| 399 |
if ( n == noUpperValue() ) |
| 400 |
return noUpperValue(); |
| 401 |
sum += n; |
| 402 |
} |
| 403 |
return sum; |
| 404 |
} |
| 405 |
|
| 406 |
template <typename C> |
| 407 |
int getOrValue ( const TDLNAryExpression<C>& expr ) |
| 408 |
{ |
| 409 |
|
| 410 |
int min = noUpperValue(); |
| 411 |
|
| 412 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 413 |
min = minUpperValue ( min, getUpperBoundDirect(*p) ); |
| 414 |
return min; |
| 415 |
} |
| 416 |
|
| 417 |
public: |
| 418 |
|
| 419 |
explicit UpperBoundComplementEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "UpperBoundCompliment" ) {} |
| 420 |
|
| 421 |
public: |
| 422 |
|
| 423 |
using CardinalityEvaluatorBase::visit; |
| 424 |
|
| 425 |
void visit ( const TDLConceptTop& ) override { value = anyUpperValue(); } |
| 426 |
void visit ( const TDLConceptBottom& ) override { value = noUpperValue(); } |
| 427 |
void visit ( const TDLConceptNot& expr ) override { value = getUpperBoundDirect(expr.getC()); } |
| 428 |
void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); } |
| 429 |
void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); } |
| 430 |
void visit ( const TDLConceptOneOf& ) override { value = noUpperValue(); } |
| 431 |
void visit ( const TDLConceptObjectSelf& expr ) override { value = getAllNoneUpper(isTopEquivalent(expr.getOR())); } |
| 432 |
void visit ( const TDLConceptObjectValue& expr ) override { value = getAllNoneUpper(isTopEquivalent(expr.getOR())); } |
| 433 |
void visit ( const TDLConceptDataValue& expr ) override { value = getAllNoneUpper(isTopEquivalent(expr.getDR())); } |
| 434 |
|
| 435 |
|
| 436 |
void visit ( const TDLObjectRoleTop& ) override { value = anyUpperValue(); } |
| 437 |
void visit ( const TDLObjectRoleBottom& ) override { value = noUpperValue(); } |
| 438 |
void visit ( const TDLObjectRoleInverse& expr ) override { value = getUpperBoundComplement(expr.getOR()); } |
| 439 |
void visit ( const TDLObjectRoleChain& expr ) override |
| 440 |
{ |
| 441 |
for ( const auto* arg : expr ) |
| 442 |
if ( !isTopEquivalent(arg) ) |
| 443 |
{ |
| 444 |
value = noUpperValue(); |
| 445 |
return; |
| 446 |
} |
| 447 |
value = anyUpperValue(); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
void visit ( const TDLDataRoleTop& ) override { value = anyUpperValue(); } |
| 452 |
void visit ( const TDLDataRoleBottom& ) override { value = noUpperValue(); } |
| 453 |
|
| 454 |
|
| 455 |
void visit ( const TDLDataTop& ) override { value = anyUpperValue(); } |
| 456 |
void visit ( const TDLDataBottom& ) override { value = noUpperValue(); } |
| 457 |
|
| 458 |
void visit ( const TDLDataTypeName& ) override { value = noUpperValue(); } |
| 459 |
|
| 460 |
void visit ( const TDLDataTypeRestriction& ) override { value = noUpperValue(); } |
| 461 |
void visit ( const TDLDataValue& ) override { value = noUpperValue(); } |
| 462 |
void visit ( const TDLDataNot& expr ) override { value = getUpperBoundDirect(expr.getExpr()); } |
| 463 |
void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); } |
| 464 |
void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); } |
| 465 |
void visit ( const TDLDataOneOf& ) override { value = noUpperValue(); } |
| 466 |
}; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
class LowerBoundDirectEvaluator: public CardinalityEvaluatorBase |
| 471 |
{ |
| 472 |
protected: |
| 473 |
|
| 474 |
int getEntityValue ( const TNamedEntity* entity ) override |
| 475 |
{ return getOneNoneLower ( topCLocal() && nc(entity) ); } |
| 476 |
|
| 477 |
int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override |
| 478 |
{ return getOneNoneLower ( isBotEquivalent(R) || isUpperLE ( getUpperBoundComplement(C), 0 ) ); } |
| 479 |
|
| 480 |
int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 481 |
{ |
| 482 |
|
| 483 |
if ( m == 0 ) |
| 484 |
return anyLowerValue(); |
| 485 |
|
| 486 |
if ( !isTopEquivalent(R) ) |
| 487 |
return noLowerValue(); |
| 488 |
|
| 489 |
return isLowerGE ( getLowerBoundDirect(C), m ) ? (int)m : noLowerValue(); |
| 490 |
} |
| 491 |
|
| 492 |
int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 493 |
{ |
| 494 |
|
| 495 |
if ( isBotEquivalent(R) ) |
| 496 |
return 1; |
| 497 |
|
| 498 |
return getOneNoneLower ( isUpperLE ( getUpperBoundDirect(C), m ) ); |
| 499 |
} |
| 500 |
|
| 501 |
int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 502 |
{ |
| 503 |
int min = getMinValue ( m, R, C ), max = getMaxValue ( m, R, C ); |
| 504 |
|
| 505 |
if ( min == noLowerValue() || max == noLowerValue() ) |
| 506 |
return noLowerValue(); |
| 507 |
if ( min == anyLowerValue() ) |
| 508 |
return max; |
| 509 |
if ( max == anyLowerValue() ) |
| 510 |
return min; |
| 511 |
return std::min ( min, max ); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
template <typename C> |
| 517 |
int getAndValue ( const TDLNAryExpression<C>& expr ) |
| 518 |
{ |
| 519 |
|
| 520 |
bool foundC = false; |
| 521 |
int foundM = 0; |
| 522 |
int mMax = 0, kMax = 0; |
| 523 |
int sumK = 0; |
| 524 |
|
| 525 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 526 |
{ |
| 527 |
int m = getLowerBoundDirect(*p); |
| 528 |
int k = getUpperBoundComplement(*p); |
| 529 |
|
| 530 |
|
| 531 |
if ( m == noLowerValue() && k == noUpperValue() ) |
| 532 |
return noLowerValue(); |
| 533 |
|
| 534 |
if ( m == noLowerValue() ) |
| 535 |
{ |
| 536 |
|
| 537 |
|
| 538 |
sumK += k; |
| 539 |
continue; |
| 540 |
} |
| 541 |
|
| 542 |
if ( k == noUpperValue() ) |
| 543 |
{ |
| 544 |
if ( foundC ) |
| 545 |
return noLowerValue(); |
| 546 |
foundC = true; |
| 547 |
foundM = m; |
| 548 |
continue; |
| 549 |
} |
| 550 |
|
| 551 |
sumK += k; |
| 552 |
if ( k+m > kMax + mMax ) |
| 553 |
{ |
| 554 |
kMax = k; |
| 555 |
mMax = m; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
if ( foundC ) |
| 560 |
{ |
| 561 |
foundM -= sumK; |
| 562 |
return foundM > 0 ? foundM : noLowerValue(); |
| 563 |
} |
| 564 |
else |
| 565 |
{ |
| 566 |
sumK -= kMax; |
| 567 |
mMax -= sumK; |
| 568 |
return mMax > 0 ? mMax : noLowerValue(); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
template <typename C> |
| 573 |
int getOrValue ( const TDLNAryExpression<C>& expr ) |
| 574 |
{ |
| 575 |
int max = noLowerValue(); |
| 576 |
|
| 577 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 578 |
{ |
| 579 |
int n = getLowerBoundDirect(*p); |
| 580 |
if ( n == anyLowerValue() ) |
| 581 |
return anyLowerValue(); |
| 582 |
max = std::max ( max, n ); |
| 583 |
} |
| 584 |
return max; |
| 585 |
} |
| 586 |
|
| 587 |
public: |
| 588 |
|
| 589 |
explicit LowerBoundDirectEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "LowerBoundDirect" ) {} |
| 590 |
|
| 591 |
public: |
| 592 |
|
| 593 |
using CardinalityEvaluatorBase::visit; |
| 594 |
|
| 595 |
void visit ( const TDLConceptTop& ) override { value = 1; } |
| 596 |
void visit ( const TDLConceptBottom& ) override { value = noLowerValue(); } |
| 597 |
void visit ( const TDLConceptNot& expr ) override { value = getLowerBoundComplement(expr.getC()); } |
| 598 |
void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); } |
| 599 |
void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); } |
| 600 |
void visit ( const TDLConceptOneOf& expr ) override { value = getOneNoneLower(expr.size() > 0); } |
| 601 |
void visit ( const TDLConceptObjectSelf& expr ) override { value = getOneNoneLower(isTopEquivalent(expr.getOR())); } |
| 602 |
|
| 603 |
void visit ( const TDLConceptObjectValue& expr ) override { value = getOneNoneLower(isTopEquivalent(expr.getOR())); } |
| 604 |
void visit ( const TDLConceptDataValue& expr ) override { value = getOneNoneLower(isTopEquivalent(expr.getDR())); } |
| 605 |
|
| 606 |
|
| 607 |
void visit ( const TDLObjectRoleTop& ) override { value = anyLowerValue(); } |
| 608 |
void visit ( const TDLObjectRoleBottom& ) override { value = noLowerValue(); } |
| 609 |
void visit ( const TDLObjectRoleInverse& expr ) override { value = getLowerBoundDirect(expr.getOR()); } |
| 610 |
void visit ( const TDLObjectRoleChain& expr ) override |
| 611 |
{ |
| 612 |
for ( const auto* arg : expr ) |
| 613 |
if ( !isTopEquivalent(arg) ) |
| 614 |
{ |
| 615 |
value = noLowerValue(); |
| 616 |
return; |
| 617 |
} |
| 618 |
value = anyLowerValue(); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
void visit ( const TDLDataRoleTop& ) override { value = anyLowerValue(); } |
| 623 |
void visit ( const TDLDataRoleBottom& ) override { value = noLowerValue(); } |
| 624 |
|
| 625 |
|
| 626 |
void visit ( const TDLDataTop& ) override { value = anyLowerValue(); } |
| 627 |
void visit ( const TDLDataBottom& ) override { value = noLowerValue(); } |
| 628 |
|
| 629 |
void visit ( const TDLDataTypeName& ) override { value = noLowerValue(); } |
| 630 |
|
| 631 |
void visit ( const TDLDataTypeRestriction& ) override { value = noLowerValue(); } |
| 632 |
void visit ( const TDLDataValue& ) override { value = noLowerValue(); } |
| 633 |
void visit ( const TDLDataNot& expr ) override { value = getLowerBoundComplement(expr.getExpr()); } |
| 634 |
void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); } |
| 635 |
void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); } |
| 636 |
void visit ( const TDLDataOneOf& expr ) override { value = getOneNoneLower(expr.size() > 0); } |
| 637 |
}; |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
class LowerBoundComplementEvaluator: public CardinalityEvaluatorBase |
| 642 |
{ |
| 643 |
protected: |
| 644 |
|
| 645 |
int getEntityValue ( const TNamedEntity* entity ) override |
| 646 |
{ return getOneNoneLower ( botCLocal() && nc(entity) ); } |
| 647 |
|
| 648 |
int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override |
| 649 |
{ return getOneNoneLower ( isTopEquivalent(R) && isLowerGE ( getLowerBoundComplement(C), 1 ) ); } |
| 650 |
|
| 651 |
int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 652 |
{ |
| 653 |
|
| 654 |
if ( m <= 0 ) |
| 655 |
return noLowerValue(); |
| 656 |
|
| 657 |
if ( isBotEquivalent(R) ) |
| 658 |
return 1; |
| 659 |
|
| 660 |
return getOneNoneLower ( isUpperLT ( getUpperBoundDirect(C), m ) ); |
| 661 |
} |
| 662 |
|
| 663 |
int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 664 |
{ |
| 665 |
|
| 666 |
if ( !isTopEquivalent(R) ) |
| 667 |
return noLowerValue(); |
| 668 |
|
| 669 |
if ( isLowerGT ( getLowerBoundDirect(C), m ) ) |
| 670 |
return (int)m+1; |
| 671 |
else |
| 672 |
return noLowerValue(); |
| 673 |
} |
| 674 |
|
| 675 |
int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override |
| 676 |
{ |
| 677 |
|
| 678 |
|
| 679 |
return std::max ( getMinValue ( m, R, C ), getMaxValue ( m, R, C ) ); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
template <typename C> |
| 684 |
int getAndValue ( const TDLNAryExpression<C>& expr ) |
| 685 |
{ |
| 686 |
int max = noLowerValue(); |
| 687 |
|
| 688 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 689 |
{ |
| 690 |
int n = getLowerBoundComplement(*p); |
| 691 |
if ( n == anyLowerValue() ) |
| 692 |
return anyLowerValue(); |
| 693 |
max = std::max ( max, n ); |
| 694 |
} |
| 695 |
return max; |
| 696 |
} |
| 697 |
|
| 698 |
template <typename C> |
| 699 |
int getOrValue ( const TDLNAryExpression<C>& expr ) |
| 700 |
{ |
| 701 |
|
| 702 |
bool foundC = false; |
| 703 |
int foundM = 0; |
| 704 |
int mMax = 0, kMax = 0; |
| 705 |
int sumK = 0; |
| 706 |
|
| 707 |
for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p ) |
| 708 |
{ |
| 709 |
int m = getLowerBoundComplement(*p); |
| 710 |
int k = getUpperBoundDirect(*p); |
| 711 |
|
| 712 |
|
| 713 |
if ( m == noLowerValue() && k == noUpperValue() ) |
| 714 |
return noLowerValue(); |
| 715 |
|
| 716 |
if ( m == noLowerValue() ) |
| 717 |
{ |
| 718 |
|
| 719 |
|
| 720 |
sumK += k; |
| 721 |
continue; |
| 722 |
} |
| 723 |
|
| 724 |
if ( k == noUpperValue() ) |
| 725 |
{ |
| 726 |
if ( foundC ) |
| 727 |
return noLowerValue(); |
| 728 |
foundC = true; |
| 729 |
foundM = m; |
| 730 |
continue; |
| 731 |
} |
| 732 |
|
| 733 |
sumK += k; |
| 734 |
if ( k+m > kMax + mMax ) |
| 735 |
{ |
| 736 |
kMax = k; |
| 737 |
mMax = m; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
if ( foundC ) |
| 742 |
{ |
| 743 |
foundM -= sumK; |
| 744 |
return foundM > 0 ? foundM : noLowerValue(); |
| 745 |
} |
| 746 |
else |
| 747 |
{ |
| 748 |
sumK -= kMax; |
| 749 |
mMax -= sumK; |
| 750 |
return mMax > 0 ? mMax : noLowerValue(); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
public: |
| 755 |
|
| 756 |
explicit LowerBoundComplementEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "LowerBoundComplement" ) {} |
| 757 |
|
| 758 |
public: |
| 759 |
|
| 760 |
using CardinalityEvaluatorBase::visit; |
| 761 |
|
| 762 |
void visit ( const TDLConceptTop& ) override { value = noLowerValue(); } |
| 763 |
void visit ( const TDLConceptBottom& ) override { value = 1; } |
| 764 |
void visit ( const TDLConceptNot& expr ) override { value = getLowerBoundDirect(expr.getC()); } |
| 765 |
void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); } |
| 766 |
void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); } |
| 767 |
void visit ( const TDLConceptOneOf& ) override { value = noLowerValue(); } |
| 768 |
void visit ( const TDLConceptObjectSelf& expr ) override { value = getOneNoneLower(isBotEquivalent(expr.getOR())); } |
| 769 |
void visit ( const TDLConceptObjectValue& expr ) override { value = getOneNoneLower(isBotEquivalent(expr.getOR())); } |
| 770 |
void visit ( const TDLConceptDataValue& expr ) override { value = getOneNoneLower(isBotEquivalent(expr.getDR())); } |
| 771 |
|
| 772 |
|
| 773 |
void visit ( const TDLObjectRoleTop& ) override { value = noLowerValue(); } |
| 774 |
void visit ( const TDLObjectRoleBottom& ) override { value = anyLowerValue(); } |
| 775 |
void visit ( const TDLObjectRoleInverse& expr ) override { value = getLowerBoundComplement(expr.getOR()); } |
| 776 |
void visit ( const TDLObjectRoleChain& expr ) override |
| 777 |
{ |
| 778 |
for ( const auto* arg : expr ) |
| 779 |
if ( isBotEquivalent(arg) ) |
| 780 |
{ |
| 781 |
value = anyLowerValue(); |
| 782 |
return; |
| 783 |
} |
| 784 |
value = noLowerValue(); |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
void visit ( const TDLDataRoleTop& ) override { value = noLowerValue(); } |
| 789 |
void visit ( const TDLDataRoleBottom& ) override { value = anyLowerValue(); } |
| 790 |
|
| 791 |
|
| 792 |
void visit ( const TDLDataTop& ) override { value = noLowerValue(); } |
| 793 |
void visit ( const TDLDataBottom& ) override { value = anyLowerValue(); } |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
void visit ( const TDLDataValue& ) override { value = 1; } |
| 799 |
void visit ( const TDLDataNot& expr ) override { value = getLowerBoundDirect(expr.getExpr()); } |
| 800 |
void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); } |
| 801 |
void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); } |
| 802 |
void visit ( const TDLDataOneOf& ) override { value = noLowerValue(); } |
| 803 |
}; |
| 804 |
|
| 805 |
|
| 806 |
inline int |
| 807 |
CardinalityEvaluatorBase :: getUpperBoundDirect ( const TDLExpression& expr ) { return UBD->getValue(expr); } |
| 808 |
|
| 809 |
inline int |
| 810 |
CardinalityEvaluatorBase :: getUpperBoundComplement ( const TDLExpression& expr ) { return UBC->getValue(expr); } |
| 811 |
|
| 812 |
inline int |
| 813 |
CardinalityEvaluatorBase :: getLowerBoundDirect ( const TDLExpression& expr ) { return LBD->getValue(expr); } |
| 814 |
|
| 815 |
inline int |
| 816 |
CardinalityEvaluatorBase :: getLowerBoundComplement ( const TDLExpression& expr ) { return LBC->getValue(expr); } |
| 817 |
|
| 818 |
|
| 819 |
class ExtendedSyntacticLocalityChecker: public GeneralSyntacticLocalityChecker |
| 820 |
{ |
| 821 |
protected: |
| 822 |
UpperBoundDirectEvaluator UBD; |
| 823 |
LowerBoundDirectEvaluator LBD; |
| 824 |
UpperBoundComplementEvaluator UBC; |
| 825 |
LowerBoundComplementEvaluator LBC; |
| 826 |
#ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 827 |
TLISPExpressionPrinter lp; |
| 828 |
#endif |
| 829 |
|
| 830 |
protected: |
| 831 |
|
| 832 |
bool isTopEquivalent ( const TDLExpression* expr ) override { |
| 833 |
# ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 834 |
std::cout << "Checking top locality of"; |
| 835 |
expr->accept(lp); |
| 836 |
std::cout << "\n"; |
| 837 |
# endif |
| 838 |
bool ret = UBC.getUpperBoundComplement(expr) == 0; |
| 839 |
# ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 840 |
std::cout << "top loc: " << ret << "\n"; |
| 841 |
# endif |
| 842 |
return ret; |
| 843 |
} |
| 844 |
|
| 845 |
bool isBotEquivalent ( const TDLExpression* expr ) override { |
| 846 |
# ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 847 |
std::cout << "Checking bot locality of"; |
| 848 |
expr->accept(lp); |
| 849 |
std::cout << "\n"; |
| 850 |
# endif |
| 851 |
bool ret = UBD.getUpperBoundDirect(expr) == 0; |
| 852 |
# ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 853 |
std::cout << "bot loc: " << ret << "\n"; |
| 854 |
# endif |
| 855 |
return ret; |
| 856 |
} |
| 857 |
|
| 858 |
public: |
| 859 |
|
| 860 |
explicit ExtendedSyntacticLocalityChecker ( const TSignature* s ) |
| 861 |
: GeneralSyntacticLocalityChecker(s) |
| 862 |
, UBD(s) |
| 863 |
, LBD(s) |
| 864 |
, UBC(s) |
| 865 |
, LBC(s) |
| 866 |
# ifdef FPP_DEBUG_EXTENDED_LOCALITY |
| 867 |
, lp(std::cout) |
| 868 |
# endif |
| 869 |
{ |
| 870 |
UBD.setEvaluators ( &UBD, &LBD, &UBC, &LBC ); |
| 871 |
LBD.setEvaluators ( &UBD, &LBD, &UBC, &LBC ); |
| 872 |
UBC.setEvaluators ( &UBD, &LBD, &UBC, &LBC ); |
| 873 |
LBC.setEvaluators ( &UBD, &LBD, &UBC, &LBC ); |
| 874 |
} |
| 875 |
}; |
| 876 |
|
| 877 |
#endif |