1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TROLE_H
#define TROLE_H
#include <set>
#include <vector>
#include "globaldef.h" // sorted reasoning
#include "BiPointer.h"
#include "dltree.h"
#include "taxNamEntry.h"
#include "tLabeller.h"
#include "RAutomaton.h"
#include "eFPPNonSimpleRole.h"
#include "eFPPCycleInRIA.h"
#ifdef RKG_USE_SORTED_REASONING
# include "mergableLabel.h"
#endif
class TBox;
class RoleMaster;
class Taxonomy;
class SaveLoadManager;
/// Define class with all information about DL role
class TRole: public ClassifiableEntry
{
friend class RoleMaster;
private: // no copy
/// no empty c'tor
TRole ();
/// no copy c'tor
TRole ( const TRole& );
/// no assignment
TRole& operator = ( const TRole& );
protected: // types
/// Class for values that can change wrt ontology
class TKnownValue
{
protected: // members
/// flag value
bool value;
/// whether flag set or not
bool known;
public: // interface
/// init c'tor
TKnownValue ( bool val = false ) : value(val), known(false) {}
/// empty d'tor
~TKnownValue ( void ) {}
/// @return true iff the value is known to be set
bool isKnown ( void ) const { return known; }
/// @return the value
bool getValue ( void ) const { return value; }
/// set the value; it is now known
void setValue ( bool val ) { value = val; known = true; }
}; // TKnownValue
public: // types
/// vector of roles
typedef std::vector<TRole*> TRoleVec;
/// RO iterator over role vector
typedef TRoleVec::const_iterator const_iterator;
/// set of roles
typedef std::set<TRole*> TRoleSet;
/// bitmap for roles
typedef std::vector<bool> TRoleBitMap;
protected: // members
/// role that are inverse of given one
TRole* Inverse;
/// Domain of role as a concept description; default NULL
DLTree* pDomain;
/// Domain of role as a concept description; default NULL
DLTree* pSpecialDomain;
/// Domain of role as a pointer to DAG entry
BipolarPointer bpDomain;
/// domain in the form AR.Range for the complex roles
BipolarPointer bpSpecialDomain;
/// pointer to role's functional definition DAG entry (or just TOP)
BipolarPointer Functional;
/// is role relevant to current query
TLabeller::LabelType rel;
#ifdef RKG_USE_SORTED_REASONING
/// label of a domain (inverse role is used for a range label)
mergableLabel domLabel;
#endif
// for later filling
// FIXME!! const was removed for Relevance setting
TRoleVec Ancestor, Descendant;
/// set of the most functional super-roles
TRoleVec TopFunc;
/// set of the roles that are disjoint with a given one
TRoleSet Disjoint;
/// all compositions in the form R1*R2*\ldots*Rn [= R
std::vector<TRoleVec> subCompositions;
/// bit-vector of all parents
TRoleBitMap AncMap;
/// bit-vector of all roles disjoint with current
TRoleBitMap DJRoles;
/// automaton for role
RoleAutomaton A;
/// value for functionality
TKnownValue Functionality;
/// value for symmetry
TKnownValue Symmetry;
/// value for asymmetricity
TKnownValue Asymmetry;
/// value for transitivity
TKnownValue Transitivity;
/// value for reflexivity
TKnownValue Reflexivity;
/// value for reflexivity
TKnownValue Irreflexivity;
/// flag to show that this role needs special R&D processing
bool SpecialDomain;
protected: // methods
// support for Anc/Desc filling and such
/// check if the role is REALLY topmost-functional (internal-use only)
bool isRealTopFunc ( void ) const;
/// set up TopFunc member properly (internal-use only)
void initTopFunc ( void );
/// init map of all disjoint roles
void initDJMap ( void );
/// eliminate told role cycle, carrying aux arrays of processed roles and synonyms
TRole* eliminateToldCycles ( TRoleSet& RInProcess, TRoleVec& ToldSynonyms );
// support for automaton construction
/// complete role automaton; keep track of processed roles in RINPROCESS
void completeAutomaton ( TRoleSet& RInProcess );
/// replace RoR [= R with Trans(R), replace synonyms in RS
void preprocessComposition ( TRoleVec& RS );
/// add transition to automaton with the role
void addTrivialTransition ( const TRole* r )
{ A.addTransitionSafe ( A.initial(), new RATransition ( A.final(), r ) ); }
/// add automaton of a sub-role to a given one
void addSubRoleAutomaton ( const TRole* R )
{
if ( this != R ) // non-trivial addition
A.addRA(R->getAutomaton());
}
/// get an automaton by a (possibly synonymical) role
const RoleAutomaton& completeAutomatonByRole ( TRole* R, TRoleSet& RInProcess ) const
{
fpp_assert ( !R->isSynonym() ); // no synonyms here
fpp_assert ( R != this ); // no case ...*S*... [= S
R->completeAutomaton(RInProcess);
return R->getAutomaton();
}
/// add automaton for a role composition; simplify composition
void addSubCompositionAutomaton ( TRoleVec& RS, TRoleSet& RInProcess );
/// check (and correct) case whether R != S for R [= S
void checkHierarchicalDisjoint ( TRole* R );
/// check whether there is a sub-property with special domain; propagate this
void checkSpecialDomain ( void )
{
if ( hasSpecialDomain() )
return;
for ( const_iterator p = begin_desc(), p_end = end_desc(); p != p_end; ++p )
if ( (*p)->hasSpecialDomain() )
{
SpecialDomain = true;
return;
}
}
public: // interface
/// the only c'tor
TRole ( const std::string& name );
/// d'tor
virtual ~TRole ( void );
/// get (unsigned) unique index of the role
unsigned int getIndex ( void ) const
{
int i = 2*getId();
return i > 0
? (unsigned int)i
: (unsigned int)(1-i);
}
// synonym operations
/// copy role information (like transitivity, functionality, R&D etc) to synonym
void addFeaturesToSynonym ( void );
// inverse of the role
/// get inverse of given role (non-const version)
TRole* inverse ( void ) { fpp_assert (Inverse != NULL); return resolveSynonym(Inverse); }
/// get inverse of given role (const version)
const TRole* inverse ( void ) const { fpp_assert (Inverse != NULL); return resolveSynonym(Inverse); }
/// get real inverse of a role (RO)
const TRole* realInverse ( void ) const { fpp_assert (Inverse != NULL); return Inverse; }
/// set inverse to given role
void setInverse ( TRole* p ) { fpp_assert (Inverse == NULL); Inverse = p; }
// different flags
/// distinguish data- and non-data role
FPP_ADD_FLAG(DataRole,0x10);
// simple
// @return true iff the role is simple
bool isSimple ( void ) const { return A.isSimple(); }
// functionality
/// test if role is functional (ie, have some functional ancestors)
bool isFunctional ( void ) const { return Functionality.getValue(); }
/// check whether the functionality of a role is known
bool isFunctionalityKnown ( void ) const { return Functionality.isKnown(); }
/// check if the role is topmost-functional (ie, has no functional ancestors).
bool isTopFunc ( void ) const
{ // check for emptyness is here due to case where a role is determined to be a functional
return !TopFunc.empty() && *TopFunc.begin() == this;
}
/// set role functionality value
void setFunctional ( bool value ) { Functionality.setValue(value); }
/// mark role (topmost) functional
void setFunctional ( void )
{
if ( TopFunc.empty() )
TopFunc.push_back(this);
setFunctional(true);
}
/// set functional attribute to given value (functional DAG vertex)
void setFunctional ( BipolarPointer fNode ) { Functional = fNode; }
/// get the Functional DAG vertex
BipolarPointer getFunctional ( void ) const { return Functional; }
// transitivity
/// check whether the role is transitive
bool isTransitive ( void ) const { return Transitivity.getValue(); }
/// check whether the transitivity of a role is known
bool isTransitivityKnown ( void ) const { return Transitivity.isKnown(); }
/// set the transitivity of both role and it's inverse
void setTransitive ( bool value = true ) { Transitivity.setValue(value); inverse()->Transitivity.setValue(value); }
// symmetry
/// check whether the role is symmetric
bool isSymmetric ( void ) const { return Symmetry.getValue(); }
/// check whether the symmetry of a role is known
bool isSymmetryKnown ( void ) const { return Symmetry.isKnown(); }
/// set the symmetry of both role and it's inverse
void setSymmetric ( bool value = true ) { Symmetry.setValue(value); inverse()->Symmetry.setValue(value); }
// asymmetry
/// check whether the role is asymmetric
bool isAsymmetric ( void ) const { return Asymmetry.getValue(); }
/// check whether the asymmetry of a role is known
bool isAsymmetryKnown ( void ) const { return Asymmetry.isKnown(); }
/// set the asymmetry of both role and it's inverse
void setAsymmetric ( bool value = true ) { Asymmetry.setValue(value); inverse()->Asymmetry.setValue(value); }
// reflexivity
/// check whether the role is reflexive
bool isReflexive ( void ) const { return Reflexivity.getValue(); }
/// check whether the reflexivity of a role is known
bool isReflexivityKnown ( void ) const { return Reflexivity.isKnown(); }
/// set the reflexivity of both role and it's inverse
void setReflexive ( bool value = true ) { Reflexivity.setValue(value); inverse()->Reflexivity.setValue(value); }
// irreflexivity
/// check whether the role is irreflexive
bool isIrreflexive ( void ) const { return Irreflexivity.getValue(); }
/// check whether the irreflexivity of a role is known
bool isIrreflexivityKnown ( void ) const { return Irreflexivity.isKnown(); }
/// set the irreflexivity of both role and it's inverse
void setIrreflexive ( bool value = true ) { Irreflexivity.setValue(value); inverse()->Irreflexivity.setValue(value); }
// relevance
/// is given role relevant to given Labeller's state
bool isRelevant ( const TLabeller& lab ) const { return lab.isLabelled(rel); }
/// make given role relevant to given Labeller's state
void setRelevant ( const TLabeller& lab ) { lab.set(rel); }
#ifdef RKG_USE_SORTED_REASONING
// Sorted reasoning interface
/// get label of the role's domain
mergableLabel& getDomainLabel ( void ) { return domLabel; }
/// get label of the role's range
mergableLabel& getRangeLabel ( void ) { return inverse()->getDomainLabel(); }
/// merge label of given role and all its super-roles
void mergeSupersDomain ( void );
#endif
// domain and range
/// add p to domain of the role
void setDomain ( DLTree* p )
{
if ( equalTrees ( pDomain, p ) ) // not just a CName
deleteTree(p); // usual case when you have a name for inverse role
else if ( isFunctionalExpr ( p, this ) )
{
setFunctional();
deleteTree(p); // functional restriction in the role domain means the role is functional
}
else
pDomain = createSNFReducedAnd ( pDomain, p );
}
/// add p to range of the role
void setRange ( DLTree* p ) { inverse()->setDomain(p); }
/// get domain-as-a-tree of the role
DLTree* getTDomain ( void ) const { return pDomain; }
/// get range-as-a-tree of the role
DLTree* getTRange ( void ) const { return inverse()->pDomain; }
/// get special-domain-as-a-tree
DLTree* getTSpecialDomain ( void ) { return pSpecialDomain; }
#ifdef RKG_UPDATE_RND_FROM_SUPERROLES
/// merge to Domain all domains from super-roles
void collectDomainFromSupers ( void )
{
for ( iterator p = begin_anc(); p != end_anc(); ++p )
setDomain ( clone((*p)->getTDomain()) );
}
#endif
/// set domain-as-a-bipointer to a role
void setBPDomain ( BipolarPointer p ) { bpDomain = p; }
/// get domain-as-a-bipointer of the role
BipolarPointer getBPDomain ( void ) const { return bpDomain; }
/// get range-as-a-bipointer of the role
BipolarPointer getBPRange ( void ) const { return inverse()->bpDomain; }
/// @return true iff role has a special domain
bool hasSpecialDomain ( void ) const { return SpecialDomain; }
/// init special domain; call this only after *ALL* the domains are known
void initSpecialDomain ( void )
{
if ( !hasSpecialDomain() || getTRange() == NULL )
pSpecialDomain = createTop();
else
pSpecialDomain = createSNFForall ( createRole(this), clone(getTRange()) );
}
/// set the special domain value
void setSpecialDomain ( BipolarPointer bp ) { bpSpecialDomain = bp; }
/// get special domain value
BipolarPointer getSpecialDomain ( void ) const { return bpSpecialDomain; }
/// get special range value
BipolarPointer getSpecialRange ( void ) const { return inverse()->bpSpecialDomain; }
// disjoint roles
/// set R and THIS as a disjoint; use it after Anc/Desc are determined
void addDisjointRole ( TRole* R )
{
Disjoint.insert(R);
for ( const_iterator p = R->begin_desc(), p_end = R->end_desc(); p != p_end; ++p )
{
Disjoint.insert(*p);
(*p)->Disjoint.insert(this);
}
}
/// check (and correct) case whether R != S for R [= S
void checkHierarchicalDisjoint ( void )
{
checkHierarchicalDisjoint(this);
if ( isReflexive() ) // for reflexive roles check for R^- is necessary
checkHierarchicalDisjoint(inverse());
}
/// check whether a role is disjoint with anything
bool isDisjoint ( void ) const { return !Disjoint.empty(); }
/// check whether a role is disjoint with R
bool isDisjoint ( const TRole* r ) const { return DJRoles[r->getIndex()]; }
// role relations checking
/// two roles are the same iff thy are synonyms of the same role
bool operator == ( const TRole& r ) const { return this == &r; }
/// check if role is a strict sub-role of R
bool operator < ( const TRole& r ) const { return (isDataRole() == r.isDataRole()) && AncMap[r.getIndex()]; }
/// check if role is a non-strict sub-role of R
bool operator <= ( const TRole& r ) const { return (*this == r) || (*this < r); }
/// check if role is a strict super-role of R
bool operator > ( const TRole& r ) const { return r < *this; }
/// check if role is a non-strict super-role of R
bool operator >= ( const TRole& r ) const { return (*this == r) || (*this > r); }
// iterators
/// get access to all super-roles via iterator
const_iterator begin_anc ( void ) const { return Ancestor.begin(); }
const_iterator end_anc ( void ) const { return Ancestor.end(); }
/// get access to all sub-roles via iterator
const_iterator begin_desc ( void ) const { return Descendant.begin(); }
const_iterator end_desc ( void ) const { return Descendant.end(); }
/// get access to the func super-roles w/o func parents via iterator
const_iterator begin_topfunc ( void ) const { return TopFunc.begin(); }
const_iterator end_topfunc ( void ) const { return TopFunc.end(); }
/// fills BITMAP with the role's ancestors
void addAncestorsToBitMap ( TRoleBitMap& bitmap ) const
{
fpp_assert ( !bitmap.empty() ); // use only after the size is known
for ( const_iterator p = begin_anc(), p_end = end_anc(); p != p_end; ++p )
bitmap[(*p)->getIndex()] = true;
}
// automaton construction
/// add composition to a role
void addComposition ( const DLTree* tree )
{
TRoleVec RS;
fillsComposition ( RS, tree );
subCompositions.push_back(RS);
}
/// get access to a RA for the role
const RoleAutomaton& getAutomaton ( void ) const { return A; }
// completing internal constructions
/// eliminate told role cycle
TRole* eliminateToldCycles ( void )
{
TRoleSet RInProcess;
TRoleVec ToldSynonyms;
return eliminateToldCycles ( RInProcess, ToldSynonyms );
}
/// init ancestors and descendants using Taxonomy
void initADbyTaxonomy ( Taxonomy* pTax, size_t ADMapSize );
/// init other fields that requires Anc/Desc for all roles
void postProcess ( void );
/// fills role composition by given TREE
void fillsComposition ( TRoleVec& Composition, const DLTree* tree ) const;
/// complete role automaton
void completeAutomaton ( size_t nRoles )
{
TRoleSet RInProcess;
completeAutomaton(RInProcess);
A.setup ( nRoles, isDataRole() );
}
/// check whether role description is consistent
void consistent ( void ) const
{
if ( isSimple() ) // all simple roles are consistent
return;
if ( isFunctional() ) // non-simple role can not be functional
throw EFPPNonSimpleRole(getName());
if ( isDataRole() ) // data role can not be non-simple
throw EFPPNonSimpleRole(getName());
if ( isDisjoint() ) // non-simple role can not be disjoint with any role
throw EFPPNonSimpleRole(getName());
}
// output
/// print role to given stream
void Print ( std::ostream& o ) const;
// save/load interface; implementation is in SaveLoad.cpp
/// save entry
virtual void Save ( SaveLoadManager& m ) const;
/// load entry
virtual void Load ( SaveLoadManager& m );
}; // TRole
/// @return R or -R for T in the form (inv ... (inv R)...)
inline
TRole*
resolveRoleHelper ( const DLTree* t )
{
if ( t == NULL ) // empty tree -- error
throw EFaCTPlusPlus("Role expression expected");
switch ( t->Element().getToken() )
{
case RNAME: // role name
case DNAME: // data role name
return static_cast<TRole*>(t->Element().getNE());
case INV: // inversion
return resolveRoleHelper(t->Left())->inverse();
default: // error
throw EFaCTPlusPlus("Invalid role expression");
}
}
/// @return R or -R for T in the form (inv ... (inv R)...); remove synonyms
inline TRole* resolveRole ( const DLTree* t ) { return resolveSynonym(resolveRoleHelper(t)); }
//--------------------------------------------------
// TRole implementation
//--------------------------------------------------
inline TRole :: TRole ( const std::string& name )
: ClassifiableEntry(name)
, Inverse(NULL)
, pDomain(NULL)
, pSpecialDomain(NULL)
, bpDomain(bpINVALID)
, bpSpecialDomain(bpINVALID)
, Functional(bpINVALID)
, rel(0)
, SpecialDomain(false)
{
setCompletelyDefined (true); // role hierarchy is completely defined by it's parents
addTrivialTransition (this);
}
inline TRole :: ~TRole ( void )
{
deleteTree(pDomain);
deleteTree(pSpecialDomain);
if ( Inverse != NULL && Inverse != this )
{
Inverse->Inverse = NULL;
delete Inverse;
}
}
//--------------------------------------------------
// RAStateTransitions implementation depending on TRole
//--------------------------------------------------
/// check whether one of the transitions accept R
inline bool
RAStateTransitions :: recognise ( const TRole* R ) const { return R != NULL && R->isDataRole() == DataRole && ApplicableRoles.in(R->getIndex()); }
#endif
|