| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TAXIOM_H |
| 21 |
#define TAXIOM_H |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifdef RKG_DEBUG_ABSORPTION |
| 27 |
# include <iostream> |
| 28 |
#else |
| 29 |
# include <iosfwd> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include "globaldef.h" |
| 35 |
#include "dltree.h" |
| 36 |
#include "tConcept.h" |
| 37 |
#include "tRole.h" |
| 38 |
#include "counter.h" |
| 39 |
|
| 40 |
class TBox; |
| 41 |
|
| 42 |
|
| 43 |
namespace Stat |
| 44 |
{ |
| 45 |
class SAbsRepCN: public counter<SAbsRepCN> {}; |
| 46 |
class SAbsRepForall: public counter<SAbsRepForall> {}; |
| 47 |
class SAbsSplit: public counter<SAbsSplit> {}; |
| 48 |
class SAbsBApply: public counter<SAbsBApply> {}; |
| 49 |
class SAbsTApply: public counter<SAbsTApply> {}; |
| 50 |
class SAbsCApply: public counter<SAbsCApply> {}; |
| 51 |
class SAbsCAttempt: public counter<SAbsCAttempt> {}; |
| 52 |
class SAbsNApply: public counter<SAbsNApply> {}; |
| 53 |
class SAbsNAttempt: public counter<SAbsNAttempt> {}; |
| 54 |
class SAbsRApply: public counter<SAbsRApply> {}; |
| 55 |
class SAbsRAttempt: public counter<SAbsRAttempt> {}; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
namespace InAx |
| 60 |
{ |
| 61 |
|
| 62 |
inline TConcept* getConcept ( DLTree* p ) |
| 63 |
{ return static_cast<TConcept*>(p->Element().getNE()); } |
| 64 |
|
| 65 |
inline const TConcept* getConcept ( const DLTree* p ) |
| 66 |
{ return static_cast<const TConcept*>(p->Element().getNE()); } |
| 67 |
|
| 68 |
bool isNP ( const TConcept* C, TBox& KB ); |
| 69 |
|
| 70 |
|
| 71 |
inline bool isTop ( const DLTree* p ) { return p->Element() == BOTTOM; } |
| 72 |
|
| 73 |
inline bool isBot ( const DLTree* p ) { return p->Element() == TOP; } |
| 74 |
|
| 75 |
inline bool isPosCN ( const DLTree* p ) { return p->Element() == NOT && isName(p->Left()); } |
| 76 |
|
| 77 |
inline bool isPosNP ( const DLTree* p, TBox& KB ) |
| 78 |
{ return isPosCN(p) && isNP(getConcept(p->Left()),KB); } |
| 79 |
|
| 80 |
inline bool isPosPC ( const DLTree* p ) |
| 81 |
{ return isPosCN(p) && getConcept(p->Left())->isPrimitive(); } |
| 82 |
|
| 83 |
inline bool isNegCN ( const DLTree* p ) { return isName(p); } |
| 84 |
|
| 85 |
inline bool isNegNP ( const DLTree* p, TBox& KB ) |
| 86 |
{ return isNegCN(p) && isNP(getConcept(p),KB); } |
| 87 |
|
| 88 |
inline bool isNegPC ( const DLTree* p ) |
| 89 |
{ return isNegCN(p) && getConcept(p)->isPrimitive(); } |
| 90 |
|
| 91 |
inline bool isAnd ( const DLTree* p ) { return p->Element() == NOT && p->Left()->Element() == AND; } |
| 92 |
|
| 93 |
inline bool isOr ( const DLTree* p ) { return p->Element() == AND; } |
| 94 |
|
| 95 |
inline bool isForall ( const DLTree* p ) { return p->Element() == NOT && p->Left()->Element() == FORALL; } |
| 96 |
|
| 97 |
inline bool isOForall ( const DLTree* p ) { return isForall(p) && !resolveRole(p->Left()->Left())->isDataRole(); } |
| 98 |
|
| 99 |
inline bool isAbsForall ( const DLTree* p ) |
| 100 |
{ |
| 101 |
if ( !isOForall(p) ) |
| 102 |
return false; |
| 103 |
const DLTree* C = p->Left()->Right(); |
| 104 |
if ( isTop(C) ) |
| 105 |
return false; |
| 106 |
return !isName(C) || !getConcept(C)->isSystem(); |
| 107 |
} |
| 108 |
|
| 109 |
inline bool isSimpleForall ( const DLTree* p ) |
| 110 |
{ |
| 111 |
if ( !isAbsForall(p) ) |
| 112 |
return false; |
| 113 |
const DLTree* C = p->Left()->Right(); |
| 114 |
|
| 115 |
return isName(C) && (getConcept(C)->Description == nullptr); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
class TAxiom |
| 120 |
{ |
| 121 |
protected: |
| 122 |
|
| 123 |
typedef std::vector<DLTree*> absorptionSet; |
| 124 |
|
| 125 |
typedef absorptionSet::iterator iterator; |
| 126 |
|
| 127 |
typedef absorptionSet::const_iterator const_iterator; |
| 128 |
|
| 129 |
typedef absorptionSet WorkSet; |
| 130 |
|
| 131 |
protected: |
| 132 |
|
| 133 |
absorptionSet Disjuncts; |
| 134 |
|
| 135 |
const TAxiom* origin; |
| 136 |
|
| 137 |
protected: |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
iterator begin ( void ) { return Disjuncts.begin(); } |
| 142 |
|
| 143 |
iterator end ( void ) { return Disjuncts.end(); } |
| 144 |
|
| 145 |
const_iterator begin ( void ) const { return Disjuncts.begin(); } |
| 146 |
|
| 147 |
const_iterator end ( void ) const { return Disjuncts.end(); } |
| 148 |
|
| 149 |
|
| 150 |
TAxiom* copy ( const DLTree* skip ) const |
| 151 |
{ |
| 152 |
TAxiom* ret = new TAxiom(this); |
| 153 |
for ( const auto& C: Disjuncts ) |
| 154 |
if ( C != skip ) |
| 155 |
ret->Disjuncts.push_back(clone(C)); |
| 156 |
return ret; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
TAxiom* simplifyPosNP ( const DLTree* rep ) const |
| 163 |
{ |
| 164 |
Stat::SAbsRepCN(); |
| 165 |
TAxiom* ret = copy(rep); |
| 166 |
ret->add(createSNFNot(clone(InAx::getConcept(rep->Left())->Description))); |
| 167 |
# ifdef RKG_DEBUG_ABSORPTION |
| 168 |
std::cout << " simplify CN expression for" << rep->Left(); |
| 169 |
# endif |
| 170 |
return ret; |
| 171 |
} |
| 172 |
|
| 173 |
TAxiom* simplifyNegNP ( const DLTree* rep ) const |
| 174 |
{ |
| 175 |
Stat::SAbsRepCN(); |
| 176 |
TAxiom* ret = copy(rep); |
| 177 |
ret->add(clone(InAx::getConcept(rep)->Description)); |
| 178 |
# ifdef RKG_DEBUG_ABSORPTION |
| 179 |
std::cout << " simplify ~CN expression for" << rep; |
| 180 |
# endif |
| 181 |
return ret; |
| 182 |
} |
| 183 |
|
| 184 |
TAxiom* simplifyForall ( const DLTree* rep, TBox& KB ) const; |
| 185 |
|
| 186 |
void split ( std::vector<TAxiom*>& acc, const DLTree* rep, DLTree* pAnd ) const |
| 187 |
{ |
| 188 |
if ( pAnd->Element().getToken() == AND ) |
| 189 |
{ |
| 190 |
split ( acc, rep, pAnd->Left() ); |
| 191 |
split ( acc, rep, pAnd->Right() ); |
| 192 |
} |
| 193 |
else |
| 194 |
{ |
| 195 |
TAxiom* ret = copy(rep); |
| 196 |
ret->add(createSNFNot(clone(pAnd))); |
| 197 |
acc.push_back(ret); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
DLTree* createAnAxiom ( const DLTree* skip ) const; |
| 202 |
|
| 203 |
public: |
| 204 |
|
| 205 |
explicit TAxiom ( const TAxiom* parent ) : origin(parent) {} |
| 206 |
|
| 207 |
TAxiom ( const TAxiom& ) = delete; |
| 208 |
|
| 209 |
TAxiom& operator = ( const TAxiom& ) = delete; |
| 210 |
|
| 211 |
~TAxiom() |
| 212 |
{ |
| 213 |
for ( auto& p: Disjuncts ) |
| 214 |
deleteTree(p); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
void add ( DLTree* p ); |
| 219 |
|
| 220 |
bool operator == ( const TAxiom& ax ) const |
| 221 |
{ |
| 222 |
if ( Disjuncts.size() != ax.Disjuncts.size() ) |
| 223 |
return false; |
| 224 |
auto p = begin(), q = ax.begin(), p_end = end(); |
| 225 |
for ( ; p != p_end; ++p, ++q ) |
| 226 |
if ( !equalTrees(*p,*q) ) |
| 227 |
return false; |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
TAxiom* simplifyCN ( TBox& KB ) const; |
| 233 |
|
| 234 |
TAxiom* simplifyForall ( TBox& KB ) const; |
| 235 |
|
| 236 |
TAxiom* simplifySForall ( TBox& KB ) const; |
| 237 |
|
| 238 |
bool split ( std::vector<TAxiom*>& acc ) const |
| 239 |
{ |
| 240 |
acc.clear(); |
| 241 |
for ( const auto& C: Disjuncts ) |
| 242 |
if ( InAx::isAnd(C) ) |
| 243 |
{ |
| 244 |
Stat::SAbsSplit(); |
| 245 |
# ifdef RKG_DEBUG_ABSORPTION |
| 246 |
std::cout << " split AND expression" << C->Left(); |
| 247 |
# endif |
| 248 |
split ( acc, C, C->Left() ); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
bool isCyclic ( void ) const |
| 261 |
{ |
| 262 |
for ( auto p = origin; p; p = p->origin ) |
| 263 |
if ( *p == *this ) |
| 264 |
{ |
| 265 |
# ifdef RKG_DEBUG_ABSORPTION |
| 266 |
std::cout << " same as ancestor"; |
| 267 |
# endif |
| 268 |
return true; |
| 269 |
} |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
bool absorbIntoBottom ( void ) const; |
| 274 |
|
| 275 |
bool absorbIntoTop ( TBox& KB ) const; |
| 276 |
|
| 277 |
bool absorbIntoConcept ( TBox& KB ) const; |
| 278 |
|
| 279 |
bool absorbIntoNegConcept ( TBox& KB ) const; |
| 280 |
|
| 281 |
bool absorbIntoDomain ( void ) const; |
| 282 |
|
| 283 |
DLTree* createAnAxiom ( void ) const { return createAnAxiom(nullptr); } |
| 284 |
|
| 285 |
#ifdef RKG_DEBUG_ABSORPTION |
| 286 |
|
| 287 |
void dump ( std::ostream& o ) const; |
| 288 |
#endif |
| 289 |
}; |
| 290 |
|
| 291 |
#endif |