| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 35 |
class TRelatedMap |
| 36 |
{ |
| 37 |
public: |
| 38 |
|
| 39 |
typedef std::vector<const TIndividual*> CIVec; |
| 40 |
|
| 41 |
protected: |
| 42 |
|
| 43 |
typedef std::map<const TRole*,CIVec> BaseType; |
| 44 |
|
| 45 |
protected: |
| 46 |
|
| 47 |
BaseType Base; |
| 48 |
|
| 49 |
public: |
| 50 |
|
| 51 |
TRelatedMap() = default; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
bool hasRole ( const TRole* R ) const { return Base.find(R) != Base.end(); } |
| 57 |
|
| 58 |
const CIVec& getRelated ( const TRole* R ) const |
| 59 |
{ |
| 60 |
fpp_assert ( hasRole(R) ); |
| 61 |
return Base.find(R)->second; |
| 62 |
} |
| 63 |
|
| 64 |
void setRelated ( const TRole* R, const CIVec& v ) |
| 65 |
{ |
| 66 |
fpp_assert ( !hasRole(R) ); |
| 67 |
Base[R] = v; |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
class TDataValueMap { |
| 73 |
public: |
| 74 |
|
| 75 |
typedef std::vector<const TDataEntry*> DEVec; |
| 76 |
|
| 77 |
protected: |
| 78 |
|
| 79 |
typedef std::map<const TRole*, DEVec> BaseType; |
| 80 |
|
| 81 |
protected: |
| 82 |
|
| 83 |
BaseType Base; |
| 84 |
|
| 85 |
public: |
| 86 |
|
| 87 |
TDataValueMap() = default; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
bool hasRole(const TRole* R) const |
| 93 |
{ |
| 94 |
return Base.find(R) != Base.end(); |
| 95 |
} |
| 96 |
|
| 97 |
const DEVec& getDataValues(const TRole* R) const |
| 98 |
{ |
| 99 |
fpp_assert(hasRole(R)); |
| 100 |
return Base.find(R)->second; |
| 101 |
} |
| 102 |
|
| 103 |
const std::map<const TRole*, DEVec>& getAllDataValues() const { return Base; } |
| 104 |
|
| 105 |
void setDataValues(const TRole* R, const DEVec& v) |
| 106 |
{ |
| 107 |
fpp_assert(!hasRole(R)); |
| 108 |
Base[R] = v; |
| 109 |
} |
| 110 |
|
| 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 |
}; |
| 119 |
|
| 120 |
|
| 121 |
class TIndividual: public TConcept |
| 122 |
{ |
| 123 |
public: |
| 124 |
|
| 125 |
typedef std::vector<TRelated*> RelatedSet; |
| 126 |
|
| 127 |
typedef TRelatedMap::CIVec CIVec; |
| 128 |
|
| 129 |
public: |
| 130 |
|
| 131 |
DlCompletionTree* node = nullptr; |
| 132 |
|
| 133 |
|
| 134 |
RelatedSet RelatedIndex; |
| 135 |
|
| 136 |
TRelatedMap* pRelatedMap; |
| 137 |
|
| 138 |
TDataValueMap* pDataValueMap; |
| 139 |
|
| 140 |
public: |
| 141 |
|
| 142 |
explicit TIndividual ( const std::string& name ) |
| 143 |
: TConcept(name) |
| 144 |
, pRelatedMap(new TRelatedMap()) |
| 145 |
, pDataValueMap(new TDataValueMap()) |
| 146 |
{ |
| 147 |
setSingleton(true); |
| 148 |
} |
| 149 |
|
| 150 |
TIndividual ( const TIndividual& ) = delete; |
| 151 |
|
| 152 |
TIndividual& operator = ( const TIndividual& ) = delete; |
| 153 |
|
| 154 |
~TIndividual() override |
| 155 |
{ |
| 156 |
delete pRelatedMap; |
| 157 |
delete pDataValueMap; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
void initToldSubsumers ( void ) override |
| 162 |
{ |
| 163 |
toldSubsumers.clear(); |
| 164 |
clearHasSP(); |
| 165 |
if ( isRelated() ) |
| 166 |
updateToldFromRelated(); |
| 167 |
|
| 168 |
if ( isPrimitive() && Description && Description->Element() == TOP ) |
| 169 |
removeDescription(); |
| 170 |
|
| 171 |
|
| 172 |
bool CD = !hasExtraRules() && isPrimitive() && !isRelated(); |
| 173 |
if ( Description != nullptr || hasToldSubsumers() ) |
| 174 |
CD &= TConcept::initToldSubsumers(Description); |
| 175 |
setCompletelyDefined(CD); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 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 |
|
| 188 |
void updateToldFromRelated ( void ); |
| 189 |
|
| 190 |
bool isRelated ( void ) const { return !RelatedIndex.empty(); } |
| 191 |
|
| 192 |
void addRelated ( TRelated* p ) { RelatedIndex.push_back(p); } |
| 193 |
|
| 194 |
void addRelated ( TIndividual* p ) |
| 195 |
{ RelatedIndex.insert ( RelatedIndex.end(), p->RelatedIndex.begin(), p->RelatedIndex.end() ); } |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
bool hasRelatedCache ( const TRole* R ) const { return pRelatedMap->hasRole(R); } |
| 201 |
|
| 202 |
const CIVec& getRelatedCache ( const TRole* R ) const { return pRelatedMap->getRelated(R); } |
| 203 |
|
| 204 |
void setRelatedCache ( const TRole* R, const CIVec& v ) { pRelatedMap->setRelated ( R, v ); } |
| 205 |
|
| 206 |
void clearRelatedMap ( void ) { delete pRelatedMap; pRelatedMap = new TRelatedMap(); } |
| 207 |
|
| 208 |
|
| 209 |
bool hasDataValueCache ( const TRole* R ) const { return pDataValueMap->hasRole(R); } |
| 210 |
|
| 211 |
const TDataValueMap::DEVec& getDataValueCache ( const TRole* R ) const { return pDataValueMap->getDataValues(R); } |
| 212 |
|
| 213 |
void setDataValueCache ( const TRole* R, const TDataValueMap::DEVec& v ) { pDataValueMap->setDataValues ( R, v ); } |
| 214 |
|
| 215 |
void addDataValueCache(const TRole* R, const TDataEntry* v) { pDataValueMap->addDataValue(R, v); } |
| 216 |
|
| 217 |
const TDataValueMap* getDataValueMap() const { return pDataValueMap; } |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
void Save ( SaveLoadManager& ) const override; |
| 224 |
|
| 225 |
void Load ( SaveLoadManager& ) override; |
| 226 |
}; |
| 227 |
|
| 228 |
#endif |
| 229 |
|