| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2010-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 | #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 | /// manager to work with all DL expressions in the kernel |
| 30 | class TExpressionManager |
| 31 | { |
| 32 | protected: // types |
| 33 | /// cache for the one-of expressions |
| 34 | class TOneOfCache: public THeadTailCache<TDLConceptExpression, const TDLIndividualExpression> |
| 35 | { |
| 36 | protected: // members |
| 37 | /// host expression manager |
| 38 | TExpressionManager* pManager; |
| 39 | |
| 40 | protected: // methods |
| 41 | /// the way to create an object by a given tail |
| 42 | TDLConceptExpression* build ( const TDLIndividualExpression* tail ) override; |
| 43 | |
| 44 | public: // interface |
| 45 | /// empty c'tor |
| 46 | explicit TOneOfCache ( TExpressionManager* p ) : THeadTailCache<TDLConceptExpression, const TDLIndividualExpression>(), pManager(p) {} |
| 47 | |
| 48 | /// clear the cache |
| 49 | void clear ( void ) { Map.clear(); } |
| 50 | }; // TOneOfCache |
| 51 | /// Cache for the inverse roles |
| 52 | class TInverseRoleCache : public THeadTailCache<TDLObjectRoleExpression, const TDLObjectRoleExpression> |
| 53 | { |
| 54 | protected: // members |
| 55 | /// host expression manager |
| 56 | TExpressionManager* pManager; |
| 57 | |
| 58 | protected: // methods |
| 59 | /// the way to create an object by a given tail |
| 60 | TDLObjectRoleExpression* build ( const TDLObjectRoleExpression* tail ) override; |
| 61 | |
| 62 | public: // interface |
| 63 | /// empty c'tor |
| 64 | explicit TInverseRoleCache ( TExpressionManager* p ) : THeadTailCache<TDLObjectRoleExpression, const TDLObjectRoleExpression>(), pManager(p) {} |
| 65 | |
| 66 | /// clear the cache |
| 67 | void clear ( void ) { Map.clear(); } |
| 68 | }; // TInverseRoleCache |
| 69 | |
| 70 | protected: // members |
| 71 | /// nameset for concepts |
| 72 | TNameSet<TDLConceptName> NS_C; |
| 73 | /// nameset for individuals |
| 74 | TNameSet<TDLIndividualName> NS_I; |
| 75 | /// nameset for object roles |
| 76 | TNameSet<TDLObjectRoleName> NS_OR; |
| 77 | /// nameset for data roles |
| 78 | TNameSet<TDLDataRoleName> NS_DR; |
| 79 | /// nameset for data types |
| 80 | TDataTypeManager NS_DT; |
| 81 | |
| 82 | /// n-ary queue for arguments |
| 83 | TNAryQueue<const TDLExpression> ArgQueue; |
| 84 | |
| 85 | /// TOP concept |
| 86 | TDLConceptTop* CTop; |
| 87 | /// BOTTOM concept |
| 88 | TDLConceptBottom* CBottom; |
| 89 | /// TOP data element |
| 90 | TDLDataTop* DTop; |
| 91 | /// BOTTOM data element |
| 92 | TDLDataBottom* DBottom; |
| 93 | /// TOP object role |
| 94 | TDLObjectRoleExpression* ORTop; |
| 95 | /// BOTTOM object role |
| 96 | TDLObjectRoleExpression* ORBottom; |
| 97 | /// TOP data role |
| 98 | TDLDataRoleExpression* DRTop; |
| 99 | /// BOTTOM data role |
| 100 | TDLDataRoleExpression* DRBottom; |
| 101 | |
| 102 | /// record all the references |
| 103 | std::vector<TDLExpression*> RefRecorder; |
| 104 | |
| 105 | /// cache for the role inverses |
| 106 | TInverseRoleCache InverseRoleCache; |
| 107 | /// cache for the one-of singletons |
| 108 | TOneOfCache OneOfCache; |
| 109 | |
| 110 | protected: // methods |
| 111 | /// record the reference; @return the argument |
| 112 | template <typename T> |
| 113 | T* record ( T* arg ) { RefRecorder.push_back(arg); return arg; } |
| 114 | |
| 115 | public: // interface |
| 116 | /// empty c'tor |
| 117 | TExpressionManager ( void ); |
| 118 | /// d'tor |
| 119 | ~TExpressionManager(); |
| 120 | |
| 121 | /// clear the ontology |
| 122 | void clear ( void ); |
| 123 | /// clear the TNamedEntry cache for all elements of all name-sets |
| 124 | void clearNameCache ( void ); |
| 125 | |
| 126 | // top/bottom roles |
| 127 | |
| 128 | /// set Top/Bot properties |
| 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 | /// @return true iff R is a top object role |
| 141 | bool isUniversalRole ( const TDLObjectRoleExpression* R ) const { return R == ORTop; } |
| 142 | /// @return true iff R is a top data role |
| 143 | bool isUniversalRole ( const TDLDataRoleExpression* R ) const { return R == DRTop; } |
| 144 | /// @return true iff R is a bottom object role |
| 145 | bool isEmptyRole ( const TDLObjectRoleExpression* R ) const { return R == ORBottom; } |
| 146 | /// @return true iff R is a bottom data role |
| 147 | bool isEmptyRole ( const TDLDataRoleExpression* R ) const { return R == DRBottom; } |
| 148 | |
| 149 | // entries count |
| 150 | |
| 151 | /// get number of registered concepts |
| 152 | size_t nConcepts ( void ) const { return NS_C.size(); } |
| 153 | /// get number of registered individuals |
| 154 | size_t nIndividuals ( void ) const { return NS_I.size(); } |
| 155 | /// get number of registered object roles |
| 156 | size_t nORoles ( void ) const { return NS_OR.size(); } |
| 157 | /// get number of registered data roles |
| 158 | size_t nDRoles ( void ) const { return NS_DR.size(); } |
| 159 | |
| 160 | // argument lists |
| 161 | |
| 162 | /// opens new argument list |
| 163 | void newArgList ( void ) { ArgQueue.openArgList(); } |
| 164 | /// add argument ARG to the current argument list |
| 165 | void addArg ( const TDLExpression* arg ) { ArgQueue.addArg(arg); } |
| 166 | /// get the latest argument list |
| 167 | const std::vector<const TDLExpression*>& getArgList ( void ) { return ArgQueue.getLastArgList(); } |
| 168 | |
| 169 | // create expressions methods |
| 170 | |
| 171 | // concepts |
| 172 | |
| 173 | /// get TOP concept |
| 174 | TDLConceptTop* Top ( void ) const { return CTop; } |
| 175 | /// get BOTTOM concept |
| 176 | TDLConceptBottom* Bottom ( void ) const { return CBottom; } |
| 177 | /// get named concept |
| 178 | TDLConceptName* Concept ( const std::string& name ) { return NS_C.insert(name); } |
| 179 | /// get negation of a concept C |
| 180 | TDLConceptExpression* Not ( const TDLConceptExpression* C ) { return record(new TDLConceptNot(C)); } |
| 181 | /// get an n-ary conjunction expression; take the arguments from the last argument list |
| 182 | TDLConceptExpression* And ( void ) { return record(new TDLConceptAnd(getArgList())); } |
| 183 | /// @return C and D |
| 184 | TDLConceptExpression* And ( const TDLConceptExpression* C, const TDLConceptExpression* D ) |
| 185 | { newArgList(); addArg(C); addArg(D); return And(); } |
| 186 | /// get an n-ary disjunction expression; take the arguments from the last argument list |
| 187 | TDLConceptExpression* Or ( void ) { return record(new TDLConceptOr(getArgList())); } |
| 188 | /// @return C or D |
| 189 | TDLConceptExpression* Or ( const TDLConceptExpression* C, const TDLConceptExpression* D ) |
| 190 | { newArgList(); addArg(C); addArg(D); return Or(); } |
| 191 | /// get an n-ary one-of expression; take the arguments from the last argument list |
| 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 | /// @return concept {I} for the individual I |
| 200 | TDLConceptExpression* OneOf ( const TDLIndividualExpression* I ) { return OneOfCache.get(I); } |
| 201 | |
| 202 | /// get self-reference restriction of an object role R |
| 203 | TDLConceptExpression* SelfReference ( const TDLObjectRoleExpression* R ) { return record(new TDLConceptObjectSelf(R)); } |
| 204 | /// get value restriction wrt an object role R and an individual I |
| 205 | TDLConceptExpression* Value ( const TDLObjectRoleExpression* R, const TDLIndividualExpression* I ) |
| 206 | { return record(new TDLConceptObjectValue(R,I)); } |
| 207 | /// get existential restriction wrt an object role R and a concept C |
| 208 | TDLConceptExpression* Exists ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 209 | { return record(new TDLConceptObjectExists(R,C)); } |
| 210 | /// get universal restriction wrt an object role R and a concept C |
| 211 | TDLConceptExpression* Forall ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 212 | { return record(new TDLConceptObjectForall(R,C)); } |
| 213 | /// get min cardinality restriction wrt number N, an object role R and a concept C |
| 214 | TDLConceptExpression* MinCardinality ( unsigned int n, const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 215 | { return record(new TDLConceptObjectMinCardinality(n,R,C)); } |
| 216 | /// get max cardinality restriction wrt number N, an object role R and a concept C |
| 217 | TDLConceptExpression* MaxCardinality ( unsigned int n, const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 218 | { return record(new TDLConceptObjectMaxCardinality(n,R,C)); } |
| 219 | /// get exact cardinality restriction wrt number N, an object role R and a concept C |
| 220 | TDLConceptExpression* Cardinality ( unsigned int n, const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 221 | { return record(new TDLConceptObjectExactCardinality(n,R,C)); } |
| 222 | |
| 223 | /// get value restriction wrt a data role R and a data value V |
| 224 | TDLConceptExpression* Value ( const TDLDataRoleExpression* R, const TDLDataValue* V ) |
| 225 | { return record(new TDLConceptDataValue(R,V)); } |
| 226 | /// get existential restriction wrt a data role R and a data expression E |
| 227 | TDLConceptExpression* Exists ( const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 228 | { return record(new TDLConceptDataExists(R,E)); } |
| 229 | /// get universal restriction wrt a data role R and a data expression E |
| 230 | TDLConceptExpression* Forall ( const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 231 | { return record(new TDLConceptDataForall(R,E)); } |
| 232 | /// get min cardinality restriction wrt number N, a data role R and a data expression E |
| 233 | TDLConceptExpression* MinCardinality ( unsigned int n, const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 234 | { return record(new TDLConceptDataMinCardinality(n,R,E)); } |
| 235 | /// get max cardinality restriction wrt number N, a data role R and a data expression E |
| 236 | TDLConceptExpression* MaxCardinality ( unsigned int n, const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 237 | { return record(new TDLConceptDataMaxCardinality(n,R,E)); } |
| 238 | /// get exact cardinality restriction wrt number N, a data role R and a data expression E |
| 239 | TDLConceptExpression* Cardinality ( unsigned int n, const TDLDataRoleExpression* R, const TDLDataExpression* E ) |
| 240 | { return record(new TDLConceptDataExactCardinality(n,R,E)); } |
| 241 | |
| 242 | // individuals |
| 243 | |
| 244 | /// get named individual |
| 245 | TDLIndividualName* Individual ( const std::string& name ) { return NS_I.insert(name); } |
| 246 | |
| 247 | // object roles |
| 248 | |
| 249 | /// get TOP object role |
| 250 | TDLObjectRoleExpression* ObjectRoleTop ( void ) const { return ORTop; } |
| 251 | /// get BOTTOM object role |
| 252 | TDLObjectRoleExpression* ObjectRoleBottom ( void ) const { return ORBottom; } |
| 253 | /// get named object role |
| 254 | TDLObjectRoleName* ObjectRole ( const std::string& name ) { return NS_OR.insert(name); } |
| 255 | /// get an inverse of a given object role expression R |
| 256 | TDLObjectRoleExpression* Inverse ( const TDLObjectRoleExpression* R ) { return InverseRoleCache.get(R); } |
| 257 | /// get a role chain corresponding to R1 o ... o Rn; take the arguments from the last argument list |
| 258 | TDLObjectRoleComplexExpression* Compose ( void ) { return record(new TDLObjectRoleChain(getArgList())); } |
| 259 | /// get a expression corresponding to R projected from C |
| 260 | TDLObjectRoleComplexExpression* ProjectFrom ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 261 | { return record(new TDLObjectRoleProjectionFrom(R,C)); } |
| 262 | /// get a expression corresponding to R projected into C |
| 263 | TDLObjectRoleComplexExpression* ProjectInto ( const TDLObjectRoleExpression* R, const TDLConceptExpression* C ) |
| 264 | { return record(new TDLObjectRoleProjectionInto(R,C)); } |
| 265 | |
| 266 | // data roles |
| 267 | |
| 268 | /// get TOP data role |
| 269 | TDLDataRoleExpression* DataRoleTop ( void ) const { return DRTop; } |
| 270 | /// get BOTTOM data role |
| 271 | TDLDataRoleExpression* DataRoleBottom ( void ) const { return DRBottom; } |
| 272 | /// get named data role |
| 273 | TDLDataRoleName* DataRole ( const std::string& name ) { return NS_DR.insert(name); } |
| 274 | |
| 275 | // data expressions |
| 276 | |
| 277 | /// get TOP data element |
| 278 | TDLDataExpression* DataTop ( void ) const { return DTop; } |
| 279 | /// get BOTTOM data element |
| 280 | TDLDataExpression* DataBottom ( void ) const { return DBottom; } |
| 281 | |
| 282 | /// get named data type |
| 283 | TDLDataTypeName* DataType ( const std::string& name ) { return NS_DT.insert(name); } |
| 284 | /// get basic string data type |
| 285 | TDLDataTypeName* getStrDataType ( void ) { return DataType(TDataTypeManager::getStrTypeName()); } |
| 286 | /// get basic integer data type |
| 287 | TDLDataTypeName* getIntDataType ( void ) { return DataType(TDataTypeManager::getIntTypeName()); } |
| 288 | /// get basic floating point data type |
| 289 | TDLDataTypeName* getRealDataType ( void ) { return DataType(TDataTypeManager::getRealTypeName()); } |
| 290 | /// get basic boolean data type |
| 291 | TDLDataTypeName* getBoolDataType ( void ) { return DataType(TDataTypeManager::getBoolTypeName()); } |
| 292 | /// get basic date-time data type |
| 293 | TDLDataTypeName* getTimeDataType ( void ) { return DataType(TDataTypeManager::getTimeTypeName()); } |
| 294 | |
| 295 | /// get basic boolean data type |
| 296 | TDLDataTypeRestriction* RestrictedType ( TDLDataTypeExpression* type, const TDLFacetExpression* facet ) |
| 297 | { |
| 298 | TDLDataTypeRestriction* ret = dynamic_cast<TDLDataTypeRestriction*>(type); |
| 299 | if ( ret == nullptr ) |
| 300 | { // get a type and build an appropriate restriction of it |
| 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 | /// get data value with given VALUE and TYPE; |
| 310 | // FIXME!! now change the type to the basic type of the given one |
| 311 | // That is, value of a type positiveInteger will be of a type Integer |
| 312 | const TDLDataValue* DataValue ( const std::string& value, TDLDataTypeExpression* type ) { return getBasicDataType(type)->getValue(value); } |
| 313 | /// get negation of a data expression E |
| 314 | TDLDataExpression* DataNot ( const TDLDataExpression* E ) { return record(new TDLDataNot(E)); } |
| 315 | /// get an n-ary data conjunction expression; take the arguments from the last argument list |
| 316 | TDLDataExpression* DataAnd ( void ) { return record(new TDLDataAnd(getArgList())); } |
| 317 | /// get an n-ary data disjunction expression; take the arguments from the last argument list |
| 318 | TDLDataExpression* DataOr ( void ) { return record(new TDLDataOr(getArgList())); } |
| 319 | /// get an n-ary data one-of expression; take the arguments from the last argument list |
| 320 | TDLDataExpression* DataOneOf ( void ) { return record(new TDLDataOneOf(getArgList())); } |
| 321 | |
| 322 | /// get minInclusive facet with a given VALUE |
| 323 | const TDLFacetExpression* FacetMinInclusive ( const TDLDataValue* V ) { return record(new TDLFacetMinInclusive(V)); } |
| 324 | /// get minExclusive facet with a given VALUE |
| 325 | const TDLFacetExpression* FacetMinExclusive ( const TDLDataValue* V ) { return record(new TDLFacetMinExclusive(V)); } |
| 326 | /// get maxInclusive facet with a given VALUE |
| 327 | const TDLFacetExpression* FacetMaxInclusive ( const TDLDataValue* V ) { return record(new TDLFacetMaxInclusive(V)); } |
| 328 | /// get maxExclusive facet with a given VALUE |
| 329 | const TDLFacetExpression* FacetMaxExclusive ( const TDLDataValue* V ) { return record(new TDLFacetMaxExclusive(V)); } |
| 330 | |
| 331 | }; // TExpressionManager |
| 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 |