1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2010-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TEXPRESSIONTRANSLATOR_H
#define TEXPRESSIONTRANSLATOR_H
#include "tDLExpression.h"
#include "tDataTypeManager.h"
#include "dlTBox.h"
class TExpressionTranslator: public DLExpressionVisitor
{
protected: // members
/// tree corresponding to a processing expression
DLTree* tree;
/// TBox to get access to the named entities
TBox& KB;
/// signature of non-trivial entities; used in semantic locality checkers only
const TSignature* sig;
#define THROW_UNSUPPORTED(name) \
throw EFaCTPlusPlus("Unsupported expression '" name "' in transformation")
protected: // methods
/// create DLTree of given TAG and named ENTRY; set the entry's ENTITY if necessary
TNamedEntry* matchEntry ( TNamedEntry* entry, const TNamedEntity* entity )
{
entry->setEntity(entity);
const_cast<TNamedEntity*>(entity)->setEntry(entry);
return entry;
}
/// @return true iff ENTRY is not in signature
bool nc ( const TNamedEntity* entity ) const { return unlikely(sig != NULL) && !sig->contains(entity); }
public: // interface
/// empty c'tor
TExpressionTranslator ( TBox& kb ) : tree(NULL), KB(kb), sig(NULL) {}
/// empty d'tor
virtual ~TExpressionTranslator ( void ) { deleteTree(tree); }
/// get (single) access to the tree
operator DLTree* ( void ) { DLTree* ret = tree; tree = NULL; return ret; }
/// set internal signature to a given signature S
void setSignature ( const TSignature* s ) { sig = s; }
public: // visitor interface
// concept expressions
virtual void visit ( const TDLConceptTop& ) { tree = createTop(); }
virtual void visit ( const TDLConceptBottom& ) { tree = createBottom(); }
virtual void visit ( const TDLConceptName& expr )
{
if ( nc(&expr) )
tree = sig->topCLocal() ? createTop() : createBottom();
else
{
TNamedEntry* entry = expr.getEntry();
if ( entry == NULL )
entry = matchEntry ( KB.getConcept(expr.getName()), &expr );
tree = createEntry(CNAME,entry);
}
}
virtual void visit ( const TDLConceptNot& expr ) { expr.getC()->accept(*this); tree = createSNFNot(*this); }
virtual void visit ( const TDLConceptAnd& expr )
{
DLTree* acc = createTop();
for ( TDLConceptAnd::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
{
(*p)->accept(*this);
acc = createSNFAnd ( acc, *this );
}
tree = acc;
}
virtual void visit ( const TDLConceptOr& expr )
{
DLTree* acc = createBottom();
for ( TDLConceptOr::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
{
(*p)->accept(*this);
acc = createSNFOr ( acc, *this );
}
tree = acc;
}
virtual void visit ( const TDLConceptOneOf& expr )
{
DLTree* acc = createBottom();
for ( TDLConceptOneOf::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
{
(*p)->accept(*this);
acc = createSNFOr ( acc, *this );
}
tree = acc;
}
virtual void visit ( const TDLConceptObjectSelf& expr )
{
expr.getOR()->accept(*this);
tree = createSNFSelf(*this);
}
virtual void visit ( const TDLConceptObjectValue& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getI()->accept(*this);
tree = createSNFExists ( R, *this );
}
virtual void visit ( const TDLConceptObjectExists& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getC()->accept(*this);
tree = createSNFExists ( R, *this );
}
virtual void visit ( const TDLConceptObjectForall& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getC()->accept(*this);
tree = createSNFForall ( R, *this );
}
virtual void visit ( const TDLConceptObjectMinCardinality& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getC()->accept(*this);
tree = createSNFGE ( expr.getNumber(), R, *this );
}
virtual void visit ( const TDLConceptObjectMaxCardinality& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getC()->accept(*this);
tree = createSNFLE ( expr.getNumber(), R, *this );
}
virtual void visit ( const TDLConceptObjectExactCardinality& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getC()->accept(*this);
DLTree* C = *this;
DLTree* LE = createSNFLE ( expr.getNumber(), clone(R), clone(C) );
DLTree* GE = createSNFGE ( expr.getNumber(), R, C );
tree = createSNFAnd ( GE, LE );
}
virtual void visit ( const TDLConceptDataValue& expr )
{
expr.getDR()->accept(*this);
DLTree* R = *this;
expr.getExpr()->accept(*this);
tree = createSNFExists ( R, *this );
}
virtual void visit ( const TDLConceptDataExists& expr )
{
expr.getDR()->accept(*this);
DLTree* R = *this;
expr.getExpr()->accept(*this);
tree = createSNFExists ( R, *this );
}
virtual void visit ( const TDLConceptDataForall& expr )
{
expr.getDR()->accept(*this);
DLTree* R = *this;
expr.getExpr()->accept(*this);
tree = createSNFForall ( R, *this );
}
virtual void visit ( const TDLConceptDataMinCardinality& expr )
{
expr.getDR()->accept(*this);
DLTree* R = *this;
expr.getExpr()->accept(*this);
tree = createSNFGE ( expr.getNumber(), R, *this );
}
virtual void visit ( const TDLConceptDataMaxCardinality& expr )
{
expr.getDR()->accept(*this);
DLTree* R = *this;
expr.getExpr()->accept(*this);
tree = createSNFLE ( expr.getNumber(), R, *this );
}
virtual void visit ( const TDLConceptDataExactCardinality& expr )
{
expr.getDR()->accept(*this);
DLTree* R = *this;
expr.getExpr()->accept(*this);
DLTree* C = *this;
DLTree* LE = createSNFLE ( expr.getNumber(), clone(R), clone(C) );
DLTree* GE = createSNFGE ( expr.getNumber(), R, C );
tree = createSNFAnd ( GE, LE );
}
// individual expressions
virtual void visit ( const TDLIndividualName& expr )
{
TNamedEntry* entry = expr.getEntry();
if ( entry == NULL )
entry = matchEntry ( KB.getIndividual(expr.getName()), &expr );
tree = createEntry(INAME,entry);
}
// object role expressions
virtual void visit ( const TDLObjectRoleTop& ) { THROW_UNSUPPORTED("top object role"); }
virtual void visit ( const TDLObjectRoleBottom& ) { THROW_UNSUPPORTED("bottom object role"); }
virtual void visit ( const TDLObjectRoleName& expr )
{
RoleMaster* RM = KB.getORM();
TNamedEntry* role;
if ( nc(&expr) )
role = sig->topRLocal() ? RM->getTopRole() : RM->getBotRole();
else
{
role = expr.getEntry();
if ( role == NULL )
role = matchEntry ( RM->ensureRoleName(expr.getName()), &expr );
}
tree = createEntry(RNAME,role);
}
virtual void visit ( const TDLObjectRoleInverse& expr ) { expr.getOR()->accept(*this); tree = createInverse(*this); }
virtual void visit ( const TDLObjectRoleChain& expr )
{
TDLObjectRoleChain::iterator p = expr.begin(), p_end = expr.end();
if ( p == p_end )
THROW_UNSUPPORTED("empty role chain");
(*p)->accept(*this);
DLTree* acc = *this;
while ( ++p != p_end )
{
(*p)->accept(*this);
acc = new DLTree ( TLexeme(RCOMPOSITION), acc, *this );
}
tree = acc;
}
virtual void visit ( const TDLObjectRoleProjectionFrom& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getC()->accept(*this);
DLTree* C = *this;
tree = new DLTree ( TLexeme(PROJFROM), R, C );
}
virtual void visit ( const TDLObjectRoleProjectionInto& expr )
{
expr.getOR()->accept(*this);
DLTree* R = *this;
expr.getC()->accept(*this);
DLTree* C = *this;
tree = new DLTree ( TLexeme(PROJINTO), R, C );
}
// data role expressions
virtual void visit ( const TDLDataRoleTop& ) { THROW_UNSUPPORTED("top data role"); }
virtual void visit ( const TDLDataRoleBottom& ) { THROW_UNSUPPORTED("bottom data role"); }
virtual void visit ( const TDLDataRoleName& expr )
{
RoleMaster* RM = KB.getDRM();
TNamedEntry* role;
if ( nc(&expr) )
role = sig->topRLocal() ? RM->getTopRole() : RM->getBotRole();
else
{
role = expr.getEntry();
if ( role == NULL )
role = matchEntry ( RM->ensureRoleName(expr.getName()), &expr );
}
tree = createEntry(DNAME,role);
}
// data expressions
virtual void visit ( const TDLDataTop& ) { tree = createTop(); }
virtual void visit ( const TDLDataBottom& ) { tree = createBottom(); }
virtual void visit ( const TDLDataTypeName& expr )
{
DataTypeCenter& DTC = KB.getDataTypeCenter();
if ( isStrDataType(&expr) )
tree = DTC.getStringType();
else if ( isIntDataType(&expr) )
tree = DTC.getNumberType();
else if ( isRealDataType(&expr) )
tree = DTC.getRealType();
else if ( isBoolDataType(&expr) )
tree = DTC.getBoolType(); // get-by-name("bool")??
else if ( isTimeDataType(&expr) )
tree = DTC.getTimeType();
else
THROW_UNSUPPORTED("data type name");
}
virtual void visit ( const TDLDataTypeRestriction& expr )
{
DLTree* acc = createTop();
for ( TDLDataTypeRestriction::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
{
(*p)->accept(*this);
acc = createSNFAnd ( acc, *this );
}
tree = acc;
}
virtual void visit ( const TDLDataValue& expr )
{
expr.getExpr()->accept(*this); // process type
DLTree* type = *this;
tree = KB.getDataTypeCenter().getDataValue(expr.getName(),type);
deleteTree(type);
}
virtual void visit ( const TDLDataNot& expr ) { expr.getExpr()->accept(*this); tree = createSNFNot(*this); }
virtual void visit ( const TDLDataAnd& expr )
{
DLTree* acc = createTop();
for ( TDLDataAnd::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
{
(*p)->accept(*this);
acc = createSNFAnd ( acc, *this );
}
tree = acc;
}
virtual void visit ( const TDLDataOr& expr )
{
DLTree* acc = createBottom();
for ( TDLDataOr::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
{
(*p)->accept(*this);
acc = createSNFOr ( acc, *this );
}
tree = acc;
}
virtual void visit ( const TDLDataOneOf& expr )
{
DLTree* acc = createBottom();
for ( TDLDataOneOf::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
{
(*p)->accept(*this);
acc = createSNFOr ( acc, *this );
}
tree = acc;
}
// facets
virtual void visit ( const TDLFacetMinInclusive& expr )
{
expr.getExpr()->accept(*this);
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, /*min=*/true, /*excl=*/false );
}
virtual void visit ( const TDLFacetMinExclusive& expr )
{
expr.getExpr()->accept(*this);
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, /*min=*/true, /*excl=*/true );
}
virtual void visit ( const TDLFacetMaxInclusive& expr )
{
expr.getExpr()->accept(*this);
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, /*min=*/false, /*excl=*/false );
}
virtual void visit ( const TDLFacetMaxExclusive& expr )
{
expr.getExpr()->accept(*this);
tree = KB.getDataTypeCenter().getIntervalFacetExpr ( tree, /*min=*/false, /*excl=*/true );
}
#undef THROW_UNSUPPORTED
}; // TExpressionTranslator
#endif
|