| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TEXPRESSIONTRANSLATOR_H |
| 21 |
#define TEXPRESSIONTRANSLATOR_H |
| 22 |
|
| 23 |
#include "tDLExpression.h" |
| 24 |
#include "tDataTypeManager.h" |
| 25 |
#include "dlTBox.h" |
| 26 |
|
| 27 |
class TExpressionTranslator: public DLExpressionVisitor |
| 28 |
{ |
| 29 |
protected: |
| 30 |
|
| 31 |
DLTree* tree = nullptr; |
| 32 |
|
| 33 |
TBox& KB; |
| 34 |
|
| 35 |
const TSignature* sig = nullptr; |
| 36 |
|
| 37 |
#define THROW_UNSUPPORTED(name) \ |
| 38 |
throw EFaCTPlusPlus("Unsupported expression '" name "' in transformation") |
| 39 |
|
| 40 |
protected: |
| 41 |
|
| 42 |
TNamedEntry* matchEntry ( TNamedEntry* entry, const TNamedEntity* entity ) |
| 43 |
{ |
| 44 |
entry->setEntity(entity); |
| 45 |
const_cast<TNamedEntity*>(entity)->setEntry(entry); |
| 46 |
return entry; |
| 47 |
} |
| 48 |
|
| 49 |
bool nc ( const TNamedEntity* entity ) const { return unlikely(sig != nullptr) && !sig->contains(entity); } |
| 50 |
|
| 51 |
TNamedEntry* getRoleEntry ( RoleMaster& RM, const TNamedEntity* entity ) |
| 52 |
{ |
| 53 |
|
| 54 |
if ( nc(entity) ) |
| 55 |
return sig->topRLocal() ? RM.getTopRole() : RM.getBotRole(); |
| 56 |
|
| 57 |
auto role = entity->getEntry(); |
| 58 |
if ( role == nullptr ) |
| 59 |
role = matchEntry ( RM.ensureRoleName(entity->getName()), entity ); |
| 60 |
return role; |
| 61 |
} |
| 62 |
|
| 63 |
public: |
| 64 |
|
| 65 |
explicit TExpressionTranslator ( TBox& kb ) : KB(kb) {} |
| 66 |
|
| 67 |
~TExpressionTranslator() override { deleteTree(tree); } |
| 68 |
|
| 69 |
|
| 70 |
operator DLTree* ( void ) { DLTree* ret = tree; tree = nullptr; return ret; } |
| 71 |
|
| 72 |
void setSignature ( const TSignature* s ) { sig = s; } |
| 73 |
|
| 74 |
public: |
| 75 |
|
| 76 |
void visit ( const TDLConceptTop& ) override { tree = createTop(); } |
| 77 |
void visit ( const TDLConceptBottom& ) override { tree = createBottom(); } |
| 78 |
void visit ( const TDLConceptName& expr ) override |
| 79 |
{ |
| 80 |
if ( nc(&expr) ) |
| 81 |
tree = sig->topCLocal() ? createTop() : createBottom(); |
| 82 |
else |
| 83 |
{ |
| 84 |
TNamedEntry* entry = expr.getEntry(); |
| 85 |
if ( entry == nullptr ) |
| 86 |
entry = matchEntry ( KB.getConcept(expr.getName()), &expr ); |
| 87 |
tree = createEntry(CNAME,entry); |
| 88 |
} |
| 89 |
} |
| 90 |
void visit ( const TDLConceptNot& expr ) override { expr.getC()->accept(*this); tree = createSNFNot(*this); } |
| 91 |
void visit ( const TDLConceptAnd& expr ) override |
| 92 |
{ |
| 93 |
DLTree* acc = createTop(); |
| 94 |
|
| 95 |
for ( const auto* arg : expr ) |
| 96 |
{ |
| 97 |
arg->accept(*this); |
| 98 |
acc = createSNFAnd ( acc, *this ); |
| 99 |
} |
| 100 |
|
| 101 |
tree = acc; |
| 102 |
} |
| 103 |
void visit ( const TDLConceptOr& expr ) override |
| 104 |
{ |
| 105 |
DLTree* acc = createBottom(); |
| 106 |
|
| 107 |
for ( const auto* arg : expr ) |
| 108 |
{ |
| 109 |
arg->accept(*this); |
| 110 |
acc = createSNFOr ( acc, *this ); |
| 111 |
} |
| 112 |
|
| 113 |
tree = acc; |
| 114 |
} |
| 115 |
void visit ( const TDLConceptOneOf& expr ) override |
| 116 |
{ |
| 117 |
DLTree* acc = createBottom(); |
| 118 |
|
| 119 |
for ( const auto* arg : expr ) |
| 120 |
{ |
| 121 |
arg->accept(*this); |
| 122 |
acc = createSNFOr ( acc, *this ); |
| 123 |
} |
| 124 |
|
| 125 |
tree = acc; |
| 126 |
} |
| 127 |
void visit ( const TDLConceptObjectSelf& expr ) override |
| 128 |
{ |
| 129 |
expr.getOR()->accept(*this); |
| 130 |
tree = createSNFSelf(*this); |
| 131 |
} |
| 132 |
void visit ( const TDLConceptObjectValue& expr ) override |
| 133 |
{ |
| 134 |
expr.getOR()->accept(*this); |
| 135 |
DLTree* R = *this; |
| 136 |
expr.getI()->accept(*this); |
| 137 |
tree = createSNFExists ( R, *this ); |
| 138 |
} |
| 139 |
void visit ( const TDLConceptObjectExists& expr ) override |
| 140 |
{ |
| 141 |
expr.getOR()->accept(*this); |
| 142 |
DLTree* R = *this; |
| 143 |
expr.getC()->accept(*this); |
| 144 |
tree = createSNFExists ( R, *this ); |
| 145 |
} |
| 146 |
void visit ( const TDLConceptObjectForall& expr ) override |
| 147 |
{ |
| 148 |
expr.getOR()->accept(*this); |
| 149 |
DLTree* R = *this; |
| 150 |
expr.getC()->accept(*this); |
| 151 |
tree = createSNFForall ( R, *this ); |
| 152 |
} |
| 153 |
void visit ( const TDLConceptObjectMinCardinality& expr ) override |
| 154 |
{ |
| 155 |
expr.getOR()->accept(*this); |
| 156 |
DLTree* R = *this; |
| 157 |
expr.getC()->accept(*this); |
| 158 |
tree = createSNFGE ( expr.getNumber(), R, *this ); |
| 159 |
} |
| 160 |
void visit ( const TDLConceptObjectMaxCardinality& expr ) override |
| 161 |
{ |
| 162 |
expr.getOR()->accept(*this); |
| 163 |
DLTree* R = *this; |
| 164 |
expr.getC()->accept(*this); |
| 165 |
tree = createSNFLE ( expr.getNumber(), R, *this ); |
| 166 |
} |
| 167 |
void visit ( const TDLConceptObjectExactCardinality& expr ) override |
| 168 |
{ |
| 169 |
expr.getOR()->accept(*this); |
| 170 |
DLTree* R = *this; |
| 171 |
expr.getC()->accept(*this); |
| 172 |
DLTree* C = *this; |
| 173 |
DLTree* LE = createSNFLE ( expr.getNumber(), clone(R), clone(C) ); |
| 174 |
DLTree* GE = createSNFGE ( expr.getNumber(), R, C ); |
| 175 |
tree = createSNFAnd ( GE, LE ); |
| 176 |
} |
| 177 |
void visit ( const TDLConceptDataValue& expr ) override |
| 178 |
{ |
| 179 |
expr.getDR()->accept(*this); |
| 180 |
DLTree* R = *this; |
| 181 |
expr.getExpr()->accept(*this); |
| 182 |
tree = createSNFExists ( R, *this ); |
| 183 |
} |
| 184 |
void visit ( const TDLConceptDataExists& expr ) override |
| 185 |
{ |
| 186 |
expr.getDR()->accept(*this); |
| 187 |
DLTree* R = *this; |
| 188 |
expr.getExpr()->accept(*this); |
| 189 |
tree = createSNFExists ( R, *this ); |
| 190 |
} |
| 191 |
void visit ( const TDLConceptDataForall& expr ) override |
| 192 |
{ |
| 193 |
expr.getDR()->accept(*this); |
| 194 |
DLTree* R = *this; |
| 195 |
expr.getExpr()->accept(*this); |
| 196 |
tree = createSNFForall ( R, *this ); |
| 197 |
} |
| 198 |
void visit ( const TDLConceptDataMinCardinality& expr ) override |
| 199 |
{ |
| 200 |
expr.getDR()->accept(*this); |
| 201 |
DLTree* R = *this; |
| 202 |
expr.getExpr()->accept(*this); |
| 203 |
tree = createSNFGE ( expr.getNumber(), R, *this ); |
| 204 |
} |
| 205 |
void visit ( const TDLConceptDataMaxCardinality& expr ) override |
| 206 |
{ |
| 207 |
expr.getDR()->accept(*this); |
| 208 |
DLTree* R = *this; |
| 209 |
expr.getExpr()->accept(*this); |
| 210 |
tree = createSNFLE ( expr.getNumber(), R, *this ); |
| 211 |
} |
| 212 |
void visit ( const TDLConceptDataExactCardinality& expr ) override |
| 213 |
{ |
| 214 |
expr.getDR()->accept(*this); |
| 215 |
DLTree* R = *this; |
| 216 |
expr.getExpr()->accept(*this); |
| 217 |
DLTree* C = *this; |
| 218 |
DLTree* LE = createSNFLE ( expr.getNumber(), clone(R), clone(C) ); |
| 219 |
DLTree* GE = createSNFGE ( expr.getNumber(), R, C ); |
| 220 |
tree = createSNFAnd ( GE, LE ); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
void visit ( const TDLIndividualName& expr ) override |
| 225 |
{ |
| 226 |
TNamedEntry* entry = expr.getEntry(); |
| 227 |
if ( entry == nullptr ) |
| 228 |
entry = matchEntry ( KB.getIndividual(expr.getName()), &expr ); |
| 229 |
tree = createEntry(INAME,entry); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
void visit ( const TDLObjectRoleTop& ) override { THROW_UNSUPPORTED("top object role"); } |
| 234 |
void visit ( const TDLObjectRoleBottom& ) override { THROW_UNSUPPORTED("bottom object role"); } |
| 235 |
void visit ( const TDLObjectRoleName& expr ) override |
| 236 |
{ |
| 237 |
auto role = getRoleEntry ( KB.getORM(), &expr ); |
| 238 |
tree = createEntry(RNAME,role); |
| 239 |
} |
| 240 |
void visit ( const TDLObjectRoleInverse& expr ) override { expr.getOR()->accept(*this); tree = createInverse(*this); } |
| 241 |
void visit ( const TDLObjectRoleChain& expr ) override |
| 242 |
{ |
| 243 |
TDLObjectRoleChain::iterator p = expr.begin(), p_end = expr.end(); |
| 244 |
if ( p == p_end ) |
| 245 |
THROW_UNSUPPORTED("empty role chain"); |
| 246 |
|
| 247 |
(*p)->accept(*this); |
| 248 |
DLTree* acc = *this; |
| 249 |
|
| 250 |
while ( ++p != p_end ) |
| 251 |
{ |
| 252 |
(*p)->accept(*this); |
| 253 |
acc = new DLTree ( TLexeme(RCOMPOSITION), acc, *this ); |
| 254 |
} |
| 255 |
|
| 256 |
tree = acc; |
| 257 |
} |
| 258 |
void visit ( const TDLObjectRoleProjectionFrom& expr ) override |
| 259 |
{ |
| 260 |
expr.getOR()->accept(*this); |
| 261 |
DLTree* R = *this; |
| 262 |
expr.getC()->accept(*this); |
| 263 |
DLTree* C = *this; |
| 264 |
tree = new DLTree ( TLexeme(PROJFROM), R, C ); |
| 265 |
} |
| 266 |
void visit ( const TDLObjectRoleProjectionInto& expr ) override |
| 267 |
{ |
| 268 |
expr.getOR()->accept(*this); |
| 269 |
DLTree* R = *this; |
| 270 |
expr.getC()->accept(*this); |
| 271 |
DLTree* C = *this; |
| 272 |
tree = new DLTree ( TLexeme(PROJINTO), R, C ); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
void visit ( const TDLDataRoleTop& ) override { THROW_UNSUPPORTED("top data role"); } |
| 277 |
void visit ( const TDLDataRoleBottom& ) override { THROW_UNSUPPORTED("bottom data role"); } |
| 278 |
void visit ( const TDLDataRoleName& expr ) override |
| 279 |
{ |
| 280 |
auto role = getRoleEntry ( KB.getDRM(), &expr ); |
| 281 |
tree = createEntry(DNAME,role); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
void visit ( const TDLDataTop& ) override { tree = createTop(); } |
| 286 |
void visit ( const TDLDataBottom& ) override { tree = createBottom(); } |
| 287 |
void visit ( const TDLDataTypeName& expr ) override |
| 288 |
{ |
| 289 |
DataTypeCenter& DTC = KB.getDataTypeCenter(); |
| 290 |
if ( isStrDataType(&expr) ) |
| 291 |
tree = DTC.getStringType(); |
| 292 |
else if ( isIntDataType(&expr) ) |
| 293 |
tree = DTC.getNumberType(); |
| 294 |
else if ( isRealDataType(&expr) ) |
| 295 |
tree = DTC.getRealType(); |
| 296 |
else if ( isBoolDataType(&expr) ) |
| 297 |
tree = DTC.getBoolType(); |
| 298 |
else if ( isTimeDataType(&expr) ) |
| 299 |
tree = DTC.getTimeType(); |
| 300 |
else |
| 301 |
tree = DTC.getStringType(); |
| 302 |
|
| 303 |
} |
| 304 |
void visit ( const TDLDataTypeRestriction& expr ) override |
| 305 |
{ |
| 306 |
DLTree* acc = createTop(); |
| 307 |
|
| 308 |
for ( const auto* arg : expr ) |
| 309 |
{ |
| 310 |
arg->accept(*this); |
| 311 |
acc = createSNFAnd ( acc, *this ); |
| 312 |
} |
| 313 |
|
| 314 |
tree = acc; |
| 315 |
} |
| 316 |
void visit ( const TDLDataValue& expr ) override |
| 317 |
{ |
| 318 |
expr.getExpr()->accept(*this); |
| 319 |
DLTree* type = *this; |
| 320 |
tree = KB.getDataTypeCenter().getDataValue(expr.getName(),type); |
| 321 |
deleteTree(type); |
| 322 |
} |
| 323 |
void visit ( const TDLDataNot& expr ) override { expr.getExpr()->accept(*this); tree = createSNFNot(*this); } |
| 324 |
void visit ( const TDLDataAnd& expr ) override |
| 325 |
{ |
| 326 |
DLTree* acc = createTop(); |
| 327 |
|
| 328 |
for ( const auto* arg : expr ) |
| 329 |
{ |
| 330 |
arg->accept(*this); |
| 331 |
acc = createSNFAnd ( acc, *this ); |
| 332 |
} |
| 333 |
|
| 334 |
tree = acc; |
| 335 |
} |
| 336 |
void visit ( const TDLDataOr& expr ) override |
| 337 |
{ |
| 338 |
DLTree* acc = createBottom(); |
| 339 |
|
| 340 |
for ( const auto* arg : expr ) |
| 341 |
{ |
| 342 |
arg->accept(*this); |
| 343 |
acc = createSNFOr ( acc, *this ); |
| 344 |
} |
| 345 |
|
| 346 |
tree = acc; |
| 347 |
} |
| 348 |
void visit ( const TDLDataOneOf& expr ) override |
| 349 |
{ |
| 350 |
DLTree* acc = createBottom(); |
| 351 |
|
| 352 |
for ( const auto* arg : expr ) |
| 353 |
{ |
| 354 |
arg->accept(*this); |
| 355 |
acc = createSNFOr ( acc, *this ); |
| 356 |
} |
| 357 |
|
| 358 |
tree = acc; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
void visit ( const TDLFacetMinInclusive& expr ) override |
| 363 |
{ |
| 364 |
expr.getExpr()->accept(*this); |
| 365 |
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, true, false ); |
| 366 |
} |
| 367 |
void visit ( const TDLFacetMinExclusive& expr ) override |
| 368 |
{ |
| 369 |
expr.getExpr()->accept(*this); |
| 370 |
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, true, true ); |
| 371 |
} |
| 372 |
void visit ( const TDLFacetMaxInclusive& expr ) override |
| 373 |
{ |
| 374 |
expr.getExpr()->accept(*this); |
| 375 |
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, false, false ); |
| 376 |
} |
| 377 |
void visit ( const TDLFacetMaxExclusive& expr ) override |
| 378 |
{ |
| 379 |
expr.getExpr()->accept(*this); |
| 380 |
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, false, true ); |
| 381 |
} |
| 382 |
|
| 383 |
#undef THROW_UNSUPPORTED |
| 384 |
}; |
| 385 |
|
| 386 |
#endif |