| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef DLVERTEX_H |
| 21 |
#define DLVERTEX_H |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
#include <cstring> |
| 25 |
#include <iosfwd> |
| 26 |
|
| 27 |
#include "globaldef.h" |
| 28 |
#include "BiPointer.h" |
| 29 |
#include "modelCacheInterface.h" |
| 30 |
#include "mergeableLabel.h" |
| 31 |
|
| 32 |
class DLDag; |
| 33 |
class TRole; |
| 34 |
class TNamedEntry; |
| 35 |
class SaveLoadManager; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
enum DagTag { |
| 57 |
|
| 58 |
dtBad = 0, |
| 59 |
|
| 60 |
dtTop, |
| 61 |
dtAnd, |
| 62 |
dtForall, |
| 63 |
dtLE, |
| 64 |
dtIrr, |
| 65 |
dtProj, |
| 66 |
dtNN, |
| 67 |
dtChoose, |
| 68 |
|
| 69 |
|
| 70 |
dtPConcept, |
| 71 |
dtNConcept, |
| 72 |
dtPSingleton, |
| 73 |
dtNSingleton, |
| 74 |
dtDataType, |
| 75 |
dtDataValue, |
| 76 |
dtDataExpr, |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
inline bool isPNameTag ( DagTag tag ) { return (tag == dtPConcept || tag == dtPSingleton); } |
| 81 |
|
| 82 |
inline bool isNNameTag ( DagTag tag ) { return (tag == dtNConcept || tag == dtNSingleton); } |
| 83 |
|
| 84 |
inline bool isCNameTag ( DagTag tag ) { return isPNameTag(tag) || isNNameTag(tag); } |
| 85 |
|
| 86 |
|
| 87 |
#define dtConcept dtPConcept: case dtNConcept |
| 88 |
#define dtSingleton dtPSingleton: case dtNSingleton |
| 89 |
#define dtName dtConcept: case dtSingleton |
| 90 |
#define dtData dtDataType: case dtDataValue: case dtDataExpr |
| 91 |
|
| 92 |
|
| 93 |
class DLVertexCache |
| 94 |
{ |
| 95 |
protected: |
| 96 |
|
| 97 |
const modelCacheInterface* pCache = nullptr; |
| 98 |
|
| 99 |
const modelCacheInterface* nCache = nullptr; |
| 100 |
|
| 101 |
public: |
| 102 |
|
| 103 |
virtual ~DLVertexCache() { delete pCache; delete nCache; } |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
const modelCacheInterface* getCache ( bool pos ) const { return pos ? pCache : nCache; } |
| 109 |
|
| 110 |
void setCache ( bool pos, const modelCacheInterface* p ) |
| 111 |
{ |
| 112 |
if ( pos ) |
| 113 |
pCache = p; |
| 114 |
else |
| 115 |
nCache = p; |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
class DLVertexStatistic |
| 120 |
{ |
| 121 |
public: |
| 122 |
|
| 123 |
typedef unsigned short int StatType; |
| 124 |
|
| 125 |
protected: |
| 126 |
|
| 127 |
StatType stat[10]; |
| 128 |
|
| 129 |
public: |
| 130 |
|
| 131 |
static unsigned int getStatIndexDepth ( bool pos ) { return (pos ? 0 : 1); } |
| 132 |
|
| 133 |
static unsigned int getStatIndexSize ( bool pos ) { return (pos ? 2 : 3); } |
| 134 |
|
| 135 |
static unsigned int getStatIndexBranch ( bool pos ) { return (pos ? 4 : 5); } |
| 136 |
|
| 137 |
static unsigned int getStatIndexGener ( bool pos ) { return (pos ? 6 : 7); } |
| 138 |
|
| 139 |
static unsigned int getStatIndexFreq ( bool pos ) { return (pos ? 8 : 9); } |
| 140 |
|
| 141 |
public: |
| 142 |
|
| 143 |
DLVertexStatistic ( void ) { std::memset ( stat, 0, sizeof(stat) ); } |
| 144 |
|
| 145 |
virtual ~DLVertexStatistic() = default; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
void updateStatValues ( StatType d, StatType s, StatType b, StatType g, bool pos ) |
| 151 |
{ |
| 152 |
stat[getStatIndexSize(pos)] += s; |
| 153 |
stat[getStatIndexBranch(pos)] += b; |
| 154 |
stat[getStatIndexGener(pos)] += g; |
| 155 |
if ( d > stat[getStatIndexDepth(pos)] ) |
| 156 |
stat[getStatIndexDepth(pos)] = d; |
| 157 |
} |
| 158 |
|
| 159 |
void updateStatValues ( const DLVertexStatistic& v, bool posV, bool pos ) |
| 160 |
{ updateStatValues ( v.getDepth(posV), v.getSize(posV), v.getBranch(posV), v.getGener(posV), pos ); } |
| 161 |
|
| 162 |
void incFreqValue ( bool pos ) { ++stat[getStatIndexFreq(pos)]; } |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
StatType getStat ( unsigned int i ) const { return stat[i]; } |
| 168 |
|
| 169 |
StatType getDepth ( bool pos ) const { return stat[getStatIndexDepth(pos)]; } |
| 170 |
|
| 171 |
StatType getSize ( bool pos ) const { return stat[getStatIndexSize(pos)]; } |
| 172 |
|
| 173 |
StatType getBranch ( bool pos ) const { return stat[getStatIndexBranch(pos)]; } |
| 174 |
|
| 175 |
StatType getGener ( bool pos ) const { return stat[getStatIndexGener(pos)]; } |
| 176 |
|
| 177 |
StatType getFreq ( bool pos ) const { return stat[getStatIndexFreq(pos)]; } |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
class DLVertexTagDFS |
| 182 |
{ |
| 183 |
protected: |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
DagTag Op : 6; |
| 188 |
|
| 189 |
bool VisitedPos : 1; |
| 190 |
|
| 191 |
bool ProcessedPos : 1; |
| 192 |
|
| 193 |
bool inCyclePos : 1; |
| 194 |
|
| 195 |
bool VisitedNeg : 1; |
| 196 |
|
| 197 |
bool ProcessedNeg : 1; |
| 198 |
|
| 199 |
bool inCycleNeg : 1; |
| 200 |
|
| 201 |
unsigned unused : 4; |
| 202 |
|
| 203 |
public: |
| 204 |
|
| 205 |
explicit DLVertexTagDFS ( DagTag op ) |
| 206 |
: Op(op) |
| 207 |
, VisitedPos(false) |
| 208 |
, ProcessedPos(false) |
| 209 |
, inCyclePos(false) |
| 210 |
, VisitedNeg(false) |
| 211 |
, ProcessedNeg(false) |
| 212 |
, inCycleNeg(false) |
| 213 |
{} |
| 214 |
|
| 215 |
virtual ~DLVertexTagDFS() = default; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
DagTag Type ( void ) const { return Op; } |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
bool isVisited ( bool pos ) const { return (pos ? VisitedPos : VisitedNeg); } |
| 226 |
|
| 227 |
bool isProcessed ( bool pos ) const { return (pos ? ProcessedPos : ProcessedNeg); } |
| 228 |
|
| 229 |
void setVisited ( bool pos ) { if ( pos ) VisitedPos = true; else VisitedNeg = true; } |
| 230 |
|
| 231 |
void setProcessed ( bool pos ) |
| 232 |
{ |
| 233 |
if ( pos ) |
| 234 |
{ |
| 235 |
ProcessedPos = true; |
| 236 |
VisitedPos = false; |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
ProcessedNeg = true; |
| 241 |
VisitedNeg = false; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
void clearDFS ( void ) { ProcessedPos = VisitedPos = ProcessedNeg = VisitedNeg = false; } |
| 246 |
|
| 247 |
bool isInCycle ( bool pos ) const { return (pos ? inCyclePos : inCycleNeg); } |
| 248 |
|
| 249 |
void setInCycle ( bool pos ) { if ( pos ) inCyclePos = true; else inCycleNeg = true; } |
| 250 |
}; |
| 251 |
|
| 252 |
|
| 253 |
class DLVertexUsage |
| 254 |
{ |
| 255 |
public: |
| 256 |
|
| 257 |
typedef unsigned long UsageType; |
| 258 |
|
| 259 |
protected: |
| 260 |
|
| 261 |
UsageType posUsage = 0; |
| 262 |
UsageType negUsage = 0; |
| 263 |
|
| 264 |
public: |
| 265 |
|
| 266 |
virtual ~DLVertexUsage() = default; |
| 267 |
|
| 268 |
|
| 269 |
UsageType getUsage ( bool pos ) const { return pos ? posUsage : negUsage; } |
| 270 |
|
| 271 |
void incUsage ( bool pos ) { if ( pos ) ++posUsage; else ++negUsage; } |
| 272 |
}; |
| 273 |
|
| 274 |
class DLVertexSort |
| 275 |
{ |
| 276 |
protected: |
| 277 |
|
| 278 |
mergeableLabel Sort; |
| 279 |
|
| 280 |
public: |
| 281 |
|
| 282 |
virtual ~DLVertexSort() = default; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
mergeableLabel& getSort ( void ) { return Sort; } |
| 288 |
|
| 289 |
const mergeableLabel& getSort ( void ) const { return Sort; } |
| 290 |
|
| 291 |
void merge ( mergeableLabel& label ) { Sort.merge(label); } |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
class DLVertex |
| 296 |
: public DLVertexCache |
| 297 |
, public DLVertexStatistic |
| 298 |
#ifdef RKG_PRINT_DAG_USAGE |
| 299 |
, public DLVertexUsage |
| 300 |
#endif |
| 301 |
, public DLVertexTagDFS |
| 302 |
#ifdef RKG_USE_SORTED_REASONING |
| 303 |
, public DLVertexSort |
| 304 |
#endif |
| 305 |
{ |
| 306 |
protected: |
| 307 |
|
| 308 |
typedef std::vector<BipolarPointer> BaseType; |
| 309 |
|
| 310 |
public: |
| 311 |
|
| 312 |
typedef BaseType::const_iterator const_iterator; |
| 313 |
|
| 314 |
typedef BaseType::const_reverse_iterator const_reverse_iterator; |
| 315 |
|
| 316 |
protected: |
| 317 |
|
| 318 |
BaseType Child; |
| 319 |
|
| 320 |
TNamedEntry* Concept = nullptr; |
| 321 |
|
| 322 |
const TRole* Role = nullptr; |
| 323 |
|
| 324 |
const TRole* ProjRole = nullptr; |
| 325 |
|
| 326 |
BipolarPointer C = bpINVALID; |
| 327 |
|
| 328 |
unsigned int n = 0; |
| 329 |
|
| 330 |
public: |
| 331 |
|
| 332 |
explicit DLVertex ( DagTag op ) |
| 333 |
: DLVertexTagDFS(op) |
| 334 |
{} |
| 335 |
|
| 336 |
DLVertex ( DagTag op, const TRole* R ) |
| 337 |
: DLVertexTagDFS(op) |
| 338 |
, Role(R) |
| 339 |
{} |
| 340 |
|
| 341 |
DLVertex ( DagTag op, BipolarPointer c ) |
| 342 |
: DLVertexTagDFS(op) |
| 343 |
, C(c) |
| 344 |
{} |
| 345 |
|
| 346 |
DLVertex ( DagTag op, unsigned int m, const TRole* R, BipolarPointer c ) |
| 347 |
: DLVertexTagDFS(op) |
| 348 |
, Role(R) |
| 349 |
, C(c) |
| 350 |
, n(m) |
| 351 |
{} |
| 352 |
|
| 353 |
DLVertex ( const TRole* R, BipolarPointer c, const TRole* ProjR ) |
| 354 |
: DLVertexTagDFS(dtProj) |
| 355 |
, Role(R) |
| 356 |
, ProjRole(ProjR) |
| 357 |
, C(c) |
| 358 |
{} |
| 359 |
|
| 360 |
DLVertex ( const DLVertex& ) = delete; |
| 361 |
|
| 362 |
DLVertex& operator = ( const DLVertex& ) = delete; |
| 363 |
|
| 364 |
|
| 365 |
bool operator == ( const DLVertex& v ) const |
| 366 |
{ |
| 367 |
return (Type() == v.Type()) && |
| 368 |
(Role == v.Role) && |
| 369 |
(ProjRole == v.ProjRole) && |
| 370 |
(C == v.C) && |
| 371 |
(n == v.n) && |
| 372 |
(Child == v.Child); |
| 373 |
} |
| 374 |
|
| 375 |
bool operator != ( const DLVertex& v ) const { return !(*this == v); } |
| 376 |
|
| 377 |
BipolarPointer getC ( void ) const { return C; } |
| 378 |
|
| 379 |
unsigned int getNumberLE ( void ) const { return n; } |
| 380 |
|
| 381 |
unsigned int getNumberGE ( void ) const { return n+1; } |
| 382 |
|
| 383 |
unsigned int getState ( void ) const { return n; } |
| 384 |
|
| 385 |
|
| 386 |
const_iterator begin ( void ) const { return Child.begin(); } |
| 387 |
|
| 388 |
const_iterator end ( void ) const { return Child.end(); } |
| 389 |
|
| 390 |
|
| 391 |
const_reverse_iterator rbegin ( void ) const { return Child.rbegin(); } |
| 392 |
|
| 393 |
const_reverse_iterator rend ( void ) const { return Child.rend(); } |
| 394 |
|
| 395 |
|
| 396 |
const TRole* getRole ( void ) const { return Role; } |
| 397 |
|
| 398 |
const TRole* getProjRole ( void ) const { return ProjRole; } |
| 399 |
|
| 400 |
TNamedEntry* getConcept ( void ) { return Concept; } |
| 401 |
|
| 402 |
const TNamedEntry* getConcept ( void ) const { return Concept; } |
| 403 |
|
| 404 |
|
| 405 |
void setConcept ( TNamedEntry* p ) { Concept = p; } |
| 406 |
|
| 407 |
void setChild ( BipolarPointer p ) { C = p; } |
| 408 |
|
| 409 |
bool addChild ( BipolarPointer p ); |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
bool omitStat ( bool pos ) const; |
| 415 |
|
| 416 |
void sortEntry ( const DLDag& dag ); |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
const char* getTagName ( void ) const; |
| 422 |
|
| 423 |
void Print ( std::ostream& o ) const; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
void Save ( SaveLoadManager& m ) const; |
| 429 |
|
| 430 |
void Load ( SaveLoadManager& m ); |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
inline bool |
| 436 |
DLVertex :: omitStat ( bool pos ) const |
| 437 |
{ |
| 438 |
switch ( Type() ) |
| 439 |
{ |
| 440 |
case dtDataType: |
| 441 |
case dtDataValue: |
| 442 |
case dtDataExpr: |
| 443 |
case dtNN: |
| 444 |
case dtChoose: |
| 445 |
case dtBad: |
| 446 |
case dtTop: |
| 447 |
return true; |
| 448 |
case dtPConcept: |
| 449 |
case dtPSingleton: |
| 450 |
case dtProj: |
| 451 |
return !pos; |
| 452 |
default: |
| 453 |
return false; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
inline bool mayClashNR ( unsigned int geNR, unsigned int leNR ) |
| 464 |
{ |
| 465 |
return geNR >= leNR; |
| 466 |
} |
| 467 |
|
| 468 |
#endif |