| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-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 TINDIVIDUAL_H |
| 21 | #define TINDIVIDUAL_H |
| 22 | |
| 23 | #include <map> |
| 24 | |
| 25 | #include "tConcept.h" |
| 26 | #include "tDataEntry.h" |
| 27 | |
| 28 | class DlCompletionTree; |
| 29 | class TRelated; |
| 30 | class SaveLoadManager; |
| 31 | |
| 32 | class TIndividual; |
| 33 | |
| 34 | /// class to map roles to set of individuals |
| 35 | class TRelatedMap |
| 36 | { |
| 37 | public: // interface types |
| 38 | /// vector of individuals |
| 39 | typedef std::vector<const TIndividual*> CIVec; |
| 40 | |
| 41 | protected: // types |
| 42 | /// base class |
| 43 | typedef std::map<const TRole*,CIVec> BaseType; |
| 44 | |
| 45 | protected: // members |
| 46 | /// base that contains all info |
| 47 | BaseType Base; |
| 48 | |
| 49 | public: // interface |
| 50 | /// empty c'tor |
| 51 | TRelatedMap() = default; |
| 52 | |
| 53 | // access |
| 54 | |
| 55 | /// check whether role is in map |
| 56 | bool hasRole ( const TRole* R ) const { return Base.find(R) != Base.end(); } |
| 57 | /// get array related to role |
| 58 | const CIVec& getRelated ( const TRole* R ) const |
| 59 | { |
| 60 | fpp_assert ( hasRole(R) ); |
| 61 | return Base.find(R)->second; |
| 62 | } |
| 63 | /// add related wrt role |
| 64 | void setRelated ( const TRole* R, const CIVec& v ) |
| 65 | { |
| 66 | fpp_assert ( !hasRole(R) ); |
| 67 | Base[R] = v; |
| 68 | } |
| 69 | }; // TRelatedMap |
| 70 | |
| 71 | /// class to map roles to set of data values |
| 72 | class TDataValueMap { |
| 73 | public: // interface types |
| 74 | /// vector of data values |
| 75 | typedef std::vector<const TDataEntry*> DEVec; |
| 76 | |
| 77 | protected: // types |
| 78 | /// base class |
| 79 | typedef std::map<const TRole*, DEVec> BaseType; |
| 80 | |
| 81 | protected: // members |
| 82 | /// base that contains all info |
| 83 | BaseType Base; |
| 84 | |
| 85 | public: // interface |
| 86 | /// empty c'tor |
| 87 | TDataValueMap() = default; |
| 88 | |
| 89 | // access |
| 90 | |
| 91 | /// check whether role is in map |
| 92 | bool hasRole(const TRole* R) const |
| 93 | { |
| 94 | return Base.find(R) != Base.end(); |
| 95 | } |
| 96 | /// get array of data values for a role |
| 97 | const DEVec& getDataValues(const TRole* R) const |
| 98 | { |
| 99 | fpp_assert(hasRole(R)); |
| 100 | return Base.find(R)->second; |
| 101 | } |
| 102 | /// get array of data values for all roles |
| 103 | const std::map<const TRole*, DEVec>& getAllDataValues() const { return Base; } |
| 104 | /// add data values wrt role |
| 105 | void setDataValues(const TRole* R, const DEVec& v) |
| 106 | { |
| 107 | fpp_assert(!hasRole(R)); |
| 108 | Base[R] = v; |
| 109 | } |
| 110 | /// add a single data value wrt role |
| 111 | void addDataValue(const TRole* R, const TDataEntry* v) |
| 112 | { |
| 113 | if (Base.count(R) == 0) |
| 114 | Base[R] = DEVec(); |
| 115 | |
| 116 | Base[R].push_back(v); |
| 117 | } |
| 118 | }; // TRelatedMap |
| 119 | |
| 120 | /// class to represent individuals |
| 121 | class TIndividual: public TConcept |
| 122 | { |
| 123 | public: // types |
| 124 | /// pointers to RELATED constructors |
| 125 | typedef std::vector<TRelated*> RelatedSet; |
| 126 | /// vector of individuals |
| 127 | typedef TRelatedMap::CIVec CIVec; |
| 128 | |
| 129 | public: // members |
| 130 | /// pointer to nominal node (works for singletons only) |
| 131 | DlCompletionTree* node = nullptr; |
| 132 | |
| 133 | /// index for axioms <this,C>:R |
| 134 | RelatedSet RelatedIndex; |
| 135 | /// map for the related individuals: Map[R]={i:R(this,i)} |
| 136 | TRelatedMap* pRelatedMap; |
| 137 | /// map for the related individuals: Map[R]={i:R(this,i)} |
| 138 | TDataValueMap* pDataValueMap; |
| 139 | |
| 140 | public: // interface |
| 141 | /// the only c'tor |
| 142 | explicit TIndividual ( const std::string& name ) |
| 143 | : TConcept(name) |
| 144 | , pRelatedMap(new TRelatedMap()) |
| 145 | , pDataValueMap(new TDataValueMap()) |
| 146 | { |
| 147 | setSingleton(true); |
| 148 | } |
| 149 | /// no copy c'tor |
| 150 | TIndividual ( const TIndividual& ) = delete; |
| 151 | /// no assignment |
| 152 | TIndividual& operator = ( const TIndividual& ) = delete; |
| 153 | /// empty d'tor |
| 154 | ~TIndividual() override |
| 155 | { |
| 156 | delete pRelatedMap; |
| 157 | delete pDataValueMap; |
| 158 | } |
| 159 | |
| 160 | /// init told subsumers of the individual by it's description |
| 161 | void initToldSubsumers ( void ) override |
| 162 | { |
| 163 | toldSubsumers.clear(); |
| 164 | clearHasSP(); |
| 165 | if ( isRelated() ) // check if domain and range of RELATED axioms affects TS |
| 166 | updateToldFromRelated(); |
| 167 | // normalise description if the only parent is TOP |
| 168 | if ( isPrimitive() && Description && Description->Element() == TOP ) |
| 169 | removeDescription(); |
| 170 | |
| 171 | // not a completely defined if there are extra rules or related individuals |
| 172 | bool CD = !hasExtraRules() && isPrimitive() && !isRelated(); |
| 173 | if ( Description != nullptr || hasToldSubsumers() ) |
| 174 | CD &= TConcept::initToldSubsumers(Description); |
| 175 | setCompletelyDefined(CD); |
| 176 | } |
| 177 | |
| 178 | // related things |
| 179 | |
| 180 | /// update told subsumers from the RELATED axioms in a given range |
| 181 | template <typename Iterator> |
| 182 | void updateTold ( Iterator begin, Iterator end, RoleSSet& RolesProcessed ) |
| 183 | { |
| 184 | for ( Iterator p = begin; p < end; ++p ) |
| 185 | SearchTSbyRoleAndSupers ( (*p)->getRole(), RolesProcessed); |
| 186 | } |
| 187 | /// update told subsumers from all relevant RELATED axioms |
| 188 | void updateToldFromRelated ( void ); |
| 189 | /// check if individual connected to something with RELATED statement |
| 190 | bool isRelated ( void ) const { return !RelatedIndex.empty(); } |
| 191 | /// set individual related |
| 192 | void addRelated ( TRelated* p ) { RelatedIndex.push_back(p); } |
| 193 | /// add all the related elements from the given P |
| 194 | void addRelated ( TIndividual* p ) |
| 195 | { RelatedIndex.insert ( RelatedIndex.end(), p->RelatedIndex.begin(), p->RelatedIndex.end() ); } |
| 196 | |
| 197 | // related map access |
| 198 | |
| 199 | /// @return true if has cache for related individuals via role R |
| 200 | bool hasRelatedCache ( const TRole* R ) const { return pRelatedMap->hasRole(R); } |
| 201 | /// get set of individuals related to THIS via R |
| 202 | const CIVec& getRelatedCache ( const TRole* R ) const { return pRelatedMap->getRelated(R); } |
| 203 | /// set the cache of individuals related to THIS via R |
| 204 | void setRelatedCache ( const TRole* R, const CIVec& v ) { pRelatedMap->setRelated ( R, v ); } |
| 205 | /// clear RM |
| 206 | void clearRelatedMap ( void ) { delete pRelatedMap; pRelatedMap = new TRelatedMap(); } |
| 207 | |
| 208 | /// @return true if has cache for data values via role R |
| 209 | bool hasDataValueCache ( const TRole* R ) const { return pDataValueMap->hasRole(R); } |
| 210 | /// get set of data values of THIS via R |
| 211 | const TDataValueMap::DEVec& getDataValueCache ( const TRole* R ) const { return pDataValueMap->getDataValues(R); } |
| 212 | /// set the cache of data values of THIS via R |
| 213 | void setDataValueCache ( const TRole* R, const TDataValueMap::DEVec& v ) { pDataValueMap->setDataValues ( R, v ); } |
| 214 | /// add a specific data value for a specific role to the cache |
| 215 | void addDataValueCache(const TRole* R, const TDataEntry* v) { pDataValueMap->addDataValue(R, v); } |
| 216 | /// get the whole data value map |
| 217 | const TDataValueMap* getDataValueMap() const { return pDataValueMap; } |
| 218 | |
| 219 | |
| 220 | // save/load interface; implementation is in SaveLoad.cpp |
| 221 | |
| 222 | /// save entry |
| 223 | void Save ( SaveLoadManager& ) const override; |
| 224 | /// load entry |
| 225 | void Load ( SaveLoadManager& ) override; |
| 226 | }; // TIndividual |
| 227 | |
| 228 | #endif |
| 229 |