| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TEXPRESSIONMANAGER_H |
| 21 |
#define TEXPRESSIONMANAGER_H |
| 22 |
|
| 23 |
#include "tDLExpression.h" |
| 24 |
#include "tNameSet.h" |
| 25 |
#include "tNAryQueue.h" |
| 26 |
#include "tDataTypeManager.h" |
| 27 |
#include "tHeadTailCache.h" |
| 28 |
|
| 29 |
|
| 30 |
class TExpressionManager |
| 31 |
{ |
| 32 |
protected: |
| 33 |
|
| 34 |
class TOneOfCache: public THeadTailCache<TDLConceptExpression, const TDLIndividualExpression> |
| 35 |
{ |
| 36 |
protected: |
| 37 |
|
| 38 |
TExpressionManager* pManager; |
| 39 |
|
| 40 |
protected: |
| 41 |
|
| 42 |
TDLConceptExpression* build ( const TDLIndividualExpression* tail ) override; |
| 43 |
|
| 44 |
public: |
| 45 |
|
| 46 |
explicit TOneOfCache ( TExpressionManager* p ) : THeadTailCache<TDLConceptExpression, const TDLIndividualExpression>(), pManager(p) {} |
| 47 |
|
| 48 |
|
| 49 |
void clear ( void ) { Map.clear(); } |
| 50 |
}; |
| 51 |
|
| 52 |
class TInverseRoleCache : public THeadTailCache<TDLObjectRoleExpression, const TDLObjectRoleExpression> |
| 53 |
{ |
| 54 |
protected: |
| 55 |
|
| 56 |
TExpressionManager* pManager; |
| 57 |
|
| 58 |
protected: |
| 59 |
|
| 60 |
TDLObjectRoleExpression* build ( const TDLObjectRoleExpression* tail ) override; |
| 61 |
|
| 62 |
public: |
| 63 |
|
| 64 |
explicit TInverseRoleCache ( TExpressionManager* p ) : THeadTailCache<TDLObjectRoleExpression, const TDLObjectRoleExpression>(), pManager(p) {} |
| 65 |
|
| 66 |
|
| 67 |
void clear ( void ) { Map.clear(); } |
| 68 |
}; |
| 69 |
|
| 70 |
protected: |
| 71 |
|
| 72 |
TNameSet<TDLConceptName> NS_C; |
| 73 |
|
| 74 |
TNameSet<TDLIndividualName> NS_I; |
| 75 |
|
| 76 |
TNameSet<TDLObjectRoleName> NS_OR; |
| 77 |
|
| 78 |
TNameSet<TDLDataRoleName> NS_DR; |
| 79 |
|
| 80 |
TDataTypeManager NS_DT; |
| 81 |
|
| 82 |
|
| 83 |
TNAryQueue<const TDLExpression> ArgQueue; |
| 84 |
|
| 85 |
|
| 86 |
TDLConceptTop* CTop; |
| 87 |
|
| 88 |
TDLConceptBottom* CBottom; |
| 89 |
|
| 90 |
TDLDataTop* DTop; |
| 91 |
|
| 92 |
TDLDataBottom* DBottom; |
| 93 |
|
| 94 |
TDLObjectRoleExpression* ORTop; |
| 95 |
|
| 96 |
TDLObjectRoleExpression* ORBottom; |
| 97 |
|
| 98 |
TDLDataRoleExpression* DRTop; |
| 99 |
|
| 100 |
TDLDataRoleExpression* DRBottom; |
| 101 |
|
| 102 |
|
| 103 |
std::vector<TDLExpression*> RefRecorder; |
| 104 |
|
| 105 |
|
| 106 |
TInverseRoleCache InverseRoleCache; |
| 107 |
|
| 108 |
TOneOfCache OneOfCache; |
| 109 |
|
| 110 |
protected: |
| 111 |
|
| 112 |
template <typename T> |
| 113 |
T* record ( T* arg ) { RefRecorder.push_back(arg); return arg; } |
| 114 |
|
| 115 |
public: |
| 116 |
|
| 117 |
TExpressionManager ( void ); |
| 118 |
|
| 119 |
~TExpressionManager(); |
| 120 |
|
| 121 |
|
| 122 |
void clear ( void ); |
| 123 |
|
| 124 |
void clearNameCache ( void ); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
void setTopBottomRoles ( const char* topORoleName, const char* botORoleName, const char* topDRoleName, const char* botDRoleName ) |
| 130 |
{ |
| 131 |
delete ORTop; |
| 132 |
ORTop = new TDLObjectRoleName(topORoleName); |
| 133 |
delete ORBottom; |
| 134 |
ORBottom = new TDLObjectRoleName(botORoleName); |
| 135 |
delete DRTop; |
| 136 |
DRTop = new TDLDataRoleName(topDRoleName); |
| 137 |
delete DRBottom; |
| 138 |
DRBottom = new TDLDataRoleName(botDRoleName); |
| 139 |
} |
| 140 |
|
| 141 |
bool isUniversalRole ( const TDLObjectRoleExpression* R ) const { return R == ORTop; } |
| 142 |
|
| 143 |
bool isUniversalRole ( const TDLDataRoleExpression* R ) const { return R == DRTop; } |
| 144 |
|
| 145 |
bool isEmptyRole ( const TDLObjectRoleExpression* R ) const { return R == ORBottom; } |
| 146 |
|
| 147 |
bool isEmptyRole ( const TDLDataRoleExpression* R ) const { return R == DRBottom; } |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
size_t nConcepts ( void ) const { return NS_C.size(); } |
| 153 |
|
| 154 |
size_t nIndividuals ( void ) const { return NS_I.size(); } |
| 155 |
|
| 156 |
size_t nORoles ( void ) const { return NS_OR.size(); } |
| 157 |
|
| 158 |
size_t nDRoles ( void ) const { return NS_DR.size(); } |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
void newArgList ( void ) { ArgQueue.openArgList(); } |
| 164 |
|
| 165 |
void addArg ( const TDLExpression* arg ) { ArgQueue.addArg(arg); } |
| 166 |
|
| 167 |
const std::vector<const TDLExpression*>& getArgList ( void ) { return ArgQueue.getLastArgList(); } |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
TDLConceptTop* Top ( void ) const { return CTop; } |
| 175 |
|
| 176 |
TDLConceptBottom* Bottom ( void ) const { return CBottom; } |
| 177 |
|
| 178 |
TDLConceptName* Concept ( const std::string& name ) { return NS_C.insert(name); } |
| 179 |
|
| 180 |
TDLConceptExpression* Not ( const TDLConceptExpression* C ) { return record(new TDLConceptNot(C)); } |
| 181 |
|
| 182 |
TDLConceptExpression* And ( void ) { return record(new TDLConceptAnd(getArgList())); } |
| 183 |
|
| 184 |
TDLConceptExpression* And ( const TDLConceptExpression* C, const TDLConceptExpression* D ) |
| 185 |
{ newArgList(); addArg(C); addArg(D); return And(); } |
| 186 |
|
| 187 |
TDLConceptExpression* Or ( void ) { return record(new TDLConceptOr(getArgList())); } |
| 188 |
|
| 189 |
TDLConceptExpression* Or ( const TDLConceptExpression* C, const TDLConceptExpression* D ) |
| 190 |
{ newArgList(); addArg(C); addArg(D); return Or(); } |
| 191 |
|
| 192 |
TDLConceptExpression* OneOf ( void ) |
| 193 |
{ |
| 194 |
auto& v = getArgList(); |
| 195 |
if ( v.size() == 1 ) |
| 196 |
return OneOfCache.get(static_cast<const TDLIndividualExpression*>(v.front())); |
| 197 |
return record(new TDLConceptOneOf(v)); |
| 198 |
} |
| 199 |
|
| 200 |
TDLConceptExpression* OneOf ( const TDLIndividualExpression* I ) { return OneOfCache.get(I); } |
| 201 |
|
| 202 |
|
| 203 |
TDLConceptExpression* SelfReference ( const TDLObjectRoleExpression* R ) { return record(new TDLConceptObjectSelf(R)); } |
| 204 |
|
| 205 |
TDLConceptExpression* Value ( const TDLObjectRoleExpression* R, const TDLIndividualExpression* I ) |
| 206 |
{ return record(new TDLConceptObjectValue(R,I)); } |
| 207 |
|
| 208 |
TDLConceptExpression* Exists ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 209 |
{ return record(new TDLConceptObjectExists(R,C)); } |
| 210 |
|
| 211 |
TDLConceptExpression* Forall ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 212 |
{ return record(new TDLConceptObjectForall(R,C)); } |
| 213 |
|
| 214 |
TDLConceptExpression* MinCardinality ( unsigned int n, const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 215 |
{ return record(new TDLConceptObjectMinCardinality(n,R,C)); } |
| 216 |
|
| 217 |
TDLConceptExpression* MaxCardinality ( unsigned int n, const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 218 |
{ return record(new TDLConceptObjectMaxCardinality(n,R,C)); } |
| 219 |
|
| 220 |
TDLConceptExpression* Cardinality ( unsigned int n, const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 221 |
{ return record(new TDLConceptObjectExactCardinality(n,R,C)); } |
| 222 |
|
| 223 |
|
| 224 |
TDLConceptExpression* Value ( const TDLDataRoleExpression* R, const TDLDataValue* V ) |
| 225 |
{ return record(new TDLConceptDataValue(R,V)); } |
| 226 |
|
| 227 |
TDLConceptExpression* Exists ( const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 228 |
{ return record(new TDLConceptDataExists(R,E)); } |
| 229 |
|
| 230 |
TDLConceptExpression* Forall ( const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 231 |
{ return record(new TDLConceptDataForall(R,E)); } |
| 232 |
|
| 233 |
TDLConceptExpression* MinCardinality ( unsigned int n, const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 234 |
{ return record(new TDLConceptDataMinCardinality(n,R,E)); } |
| 235 |
|
| 236 |
TDLConceptExpression* MaxCardinality ( unsigned int n, const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 237 |
{ return record(new TDLConceptDataMaxCardinality(n,R,E)); } |
| 238 |
|
| 239 |
TDLConceptExpression* Cardinality ( unsigned int n, const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 240 |
{ return record(new TDLConceptDataExactCardinality(n,R,E)); } |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
TDLIndividualName* Individual ( const std::string& name ) { return NS_I.insert(name); } |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
TDLObjectRoleExpression* ObjectRoleTop ( void ) const { return ORTop; } |
| 251 |
|
| 252 |
TDLObjectRoleExpression* ObjectRoleBottom ( void ) const { return ORBottom; } |
| 253 |
|
| 254 |
TDLObjectRoleName* ObjectRole ( const std::string& name ) { return NS_OR.insert(name); } |
| 255 |
|
| 256 |
TDLObjectRoleExpression* Inverse ( const TDLObjectRoleExpression* R ) { return InverseRoleCache.get(R); } |
| 257 |
|
| 258 |
TDLObjectRoleComplexExpression* Compose ( void ) { return record(new TDLObjectRoleChain(getArgList())); } |
| 259 |
|
| 260 |
TDLObjectRoleComplexExpression* ProjectFrom ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 261 |
{ return record(new TDLObjectRoleProjectionFrom(R,C)); } |
| 262 |
|
| 263 |
TDLObjectRoleComplexExpression* ProjectInto ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 264 |
{ return record(new TDLObjectRoleProjectionInto(R,C)); } |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
TDLDataRoleExpression* DataRoleTop ( void ) const { return DRTop; } |
| 270 |
|
| 271 |
TDLDataRoleExpression* DataRoleBottom ( void ) const { return DRBottom; } |
| 272 |
|
| 273 |
TDLDataRoleName* DataRole ( const std::string& name ) { return NS_DR.insert(name); } |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
TDLDataExpression* DataTop ( void ) const { return DTop; } |
| 279 |
|
| 280 |
TDLDataExpression* DataBottom ( void ) const { return DBottom; } |
| 281 |
|
| 282 |
|
| 283 |
TDLDataTypeName* DataType ( const std::string& name ) { return NS_DT.insert(name); } |
| 284 |
|
| 285 |
TDLDataTypeName* getStrDataType ( void ) { return DataType(TDataTypeManager::getStrTypeName()); } |
| 286 |
|
| 287 |
TDLDataTypeName* getIntDataType ( void ) { return DataType(TDataTypeManager::getIntTypeName()); } |
| 288 |
|
| 289 |
TDLDataTypeName* getRealDataType ( void ) { return DataType(TDataTypeManager::getRealTypeName()); } |
| 290 |
|
| 291 |
TDLDataTypeName* getBoolDataType ( void ) { return DataType(TDataTypeManager::getBoolTypeName()); } |
| 292 |
|
| 293 |
TDLDataTypeName* getTimeDataType ( void ) { return DataType(TDataTypeManager::getTimeTypeName()); } |
| 294 |
|
| 295 |
|
| 296 |
TDLDataTypeRestriction* RestrictedType ( TDLDataTypeExpression* type, const TDLFacetExpression* facet ) |
| 297 |
{ |
| 298 |
TDLDataTypeRestriction* ret = dynamic_cast<TDLDataTypeRestriction*>(type); |
| 299 |
if ( ret == nullptr ) |
| 300 |
{ |
| 301 |
TDLDataTypeName* hostType = dynamic_cast<TDLDataTypeName*>(type); |
| 302 |
fpp_assert ( hostType != nullptr ); |
| 303 |
ret = record(new TDLDataTypeRestriction(hostType)); |
| 304 |
} |
| 305 |
ret->add(facet); |
| 306 |
return ret; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
const TDLDataValue* DataValue ( const std::string& value, TDLDataTypeExpression* type ) { return getBasicDataType(type)->getValue(value); } |
| 313 |
|
| 314 |
TDLDataExpression* DataNot ( const TDLDataExpression* E ) { return record(new TDLDataNot(E)); } |
| 315 |
|
| 316 |
TDLDataExpression* DataAnd ( void ) { return record(new TDLDataAnd(getArgList())); } |
| 317 |
|
| 318 |
TDLDataExpression* DataOr ( void ) { return record(new TDLDataOr(getArgList())); } |
| 319 |
|
| 320 |
TDLDataExpression* DataOneOf ( void ) { return record(new TDLDataOneOf(getArgList())); } |
| 321 |
|
| 322 |
|
| 323 |
const TDLFacetExpression* FacetMinInclusive ( const TDLDataValue* V ) { return record(new TDLFacetMinInclusive(V)); } |
| 324 |
|
| 325 |
const TDLFacetExpression* FacetMinExclusive ( const TDLDataValue* V ) { return record(new TDLFacetMinExclusive(V)); } |
| 326 |
|
| 327 |
const TDLFacetExpression* FacetMaxInclusive ( const TDLDataValue* V ) { return record(new TDLFacetMaxInclusive(V)); } |
| 328 |
|
| 329 |
const TDLFacetExpression* FacetMaxExclusive ( const TDLDataValue* V ) { return record(new TDLFacetMaxExclusive(V)); } |
| 330 |
|
| 331 |
}; |
| 332 |
|
| 333 |
inline TDLObjectRoleExpression* |
| 334 |
TExpressionManager::TInverseRoleCache::build ( const TDLObjectRoleExpression* tail ) |
| 335 |
{ |
| 336 |
return pManager->record(new TDLObjectRoleInverse(tail)); |
| 337 |
} |
| 338 |
|
| 339 |
inline TDLConceptExpression* |
| 340 |
TExpressionManager::TOneOfCache::build ( const TDLIndividualExpression* tail ) |
| 341 |
{ |
| 342 |
pManager->newArgList(); |
| 343 |
pManager->addArg(tail); |
| 344 |
return pManager->record(new TDLConceptOneOf(pManager->getArgList())); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
#endif |