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
|
/* 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
*/
#include "RoleMaster.h"
#include "eFPPInconsistentKB.h"
#include "TaxonomyCreator.h"
RoleMaster :: RoleMaster ( bool dataRoles, const std::string& TopRoleName, const std::string& BotRoleName )
: newRoleId(1)
, emptyRole(BotRoleName == "" ? "emptyRole" : BotRoleName)
, universalRole(TopRoleName == "" ? "universalRole" : TopRoleName)
, roleNS()
, pTax(NULL)
, DataRoles(dataRoles)
, useUndefinedNames(true)
{
// no zero-named roles allowed
Roles.push_back(NULL);
Roles.push_back(NULL);
// setup empty role
emptyRole.setId(0);
emptyRole.setInverse(&emptyRole);
emptyRole.setDataRole(dataRoles);
emptyRole.setBPDomain(bpBOTTOM);
emptyRole.setBottom();
// setup universal role
universalRole.setId(0);
universalRole.setInverse(&universalRole);
universalRole.setDataRole(dataRoles);
universalRole.setBPDomain(bpTOP);
universalRole.setTop();
// FIXME!! now it is not transitive => simple
const_cast<RoleAutomaton&>(universalRole.getAutomaton()).setCompleted();
}
/// register TRole and it's inverse in RoleBox
void
RoleMaster :: registerRole ( TRole* r )
{
fpp_assert ( r != NULL && r->Inverse == NULL ); // sanity check
fpp_assert ( r->getId() == 0 ); // only call it for the new roles
if ( DataRoles )
r->setDataRole();
Roles.push_back (r);
r->setId (newRoleId);
// create new role which would be inverse of R
std::string iname ("-");
iname += r->getName();
TRole* ri = new TRole(iname);
// set up inverse
r->setInverse(ri);
ri->setInverse(r);
Roles.push_back (ri);
ri->setId (-newRoleId);
++newRoleId;
}
TRole*
RoleMaster :: ensureRoleName ( const std::string& name )
{
// check for the Top/Bottom names
if ( name == emptyRole.getName() )
return &emptyRole;
if ( name == universalRole.getName() )
return &universalRole;
// new name from NS
TRole* p = roleNS.insert(name);
// check what happens
if ( p == NULL ) // role registration attempt failed
throw EFPPCantRegName ( name, DataRoles ? "data role" : "role" );
if ( isRegisteredRole(p) ) // registered role
return p;
if ( p->getId() != 0 || // not registered but has non-null ID
!useUndefinedNames ) // new names are disallowed
throw EFPPCantRegName ( name, DataRoles ? "data role" : "role" );
registerRole(p);
return p;
}
// inverse the role composition
DLTree* inverseComposition ( const DLTree* tree )
{
if ( tree->Element() == RCOMPOSITION )
return new DLTree ( TLexeme(RCOMPOSITION),
inverseComposition(tree->Right()),
inverseComposition(tree->Left()) );
else
return createEntry ( RNAME, resolveRole(tree)->inverse() );
}
void
RoleMaster :: addRoleParent ( DLTree* tree, TRole* parent ) const
{
if ( !tree ) // nothing to do
return;
if ( tree->Element() == RCOMPOSITION )
{
parent->addComposition(tree);
DLTree* inv = inverseComposition(tree);
parent->inverse()->addComposition(inv);
deleteTree(inv);
}
else if ( tree->Element() == PROJINTO )
{
// here -R->C became -PARENT->
// encode this as PROJFROM(R-,PROJINTO(PARENT-,C)),
// added to the range of R
TRole* R = resolveRole(tree->Left());
// can't do anything ATM for the data roles
if ( R->isDataRole() )
throw EFaCTPlusPlus("Projection into not implemented for the data role");
DLTree* C = clone(tree->Right());
DLTree* InvP = createEntry ( RNAME, parent->inverse() );
DLTree* InvR = createEntry ( RNAME, R->inverse() );
// C = PROJINTO(PARENT-,C)
C = new DLTree ( TLexeme(PROJINTO), InvP, C );
// C = PROJFROM(R-,PROJINTO(PARENT-,C))
C = new DLTree ( TLexeme(PROJFROM), InvR, C );
R->setRange(C);
}
else if ( tree->Element() == PROJFROM )
{
// here C-R-> became -PARENT->
// encode this as PROJFROM(R,PROJINTO(PARENT,C)),
// added to the domain of R
TRole* R = resolveRole(tree->Left());
DLTree* C = clone(tree->Right());
DLTree* P = createEntry ( RNAME, parent );
// C = PROJINTO(PARENT,C)
C = new DLTree ( TLexeme(PROJINTO), P, C );
// C = PROJFROM(R,PROJINTO(PARENT,C))
C = new DLTree ( TLexeme(PROJFROM), clone(tree->Left()), C );
R->setDomain(C);
}
else
addRoleParent ( resolveRole(tree), parent );
deleteTree(tree);
}
/// add parent for the input role
void
RoleMaster :: addRoleParentProper ( TRole* role, TRole* parent ) const
{
fpp_assert ( !role->isSynonym() && !parent->isSynonym() );
if ( role == parent ) // nothing to do
return;
if ( role->isDataRole() != parent->isDataRole() )
throw EFaCTPlusPlus("Mixed object and data roles in role subsumption axiom");
// check the inconsistency case *UROLE* [= *EROLE*
if ( unlikely(role->isTop() && parent->isBottom()) )
throw EFPPInconsistentKB();
// *UROLE* [= R means R (and R-) are synonym of *UROLE*
if ( unlikely(role->isTop()) )
{
parent->setSynonym(role);
parent->inverse()->setSynonym(role);
return;
}
// R [= *EROLE* means R (and R-) are synonyms of *EROLE*
if ( unlikely(parent->isBottom()) )
{
role->setSynonym(parent);
role->inverse()->setSynonym(parent);
return;
}
role->addParent(parent);
role->inverse()->addParent(parent->inverse());
}
void RoleMaster :: initAncDesc ( void )
{
iterator p, p_begin = begin(), p_end = end();
size_t nRoles = Roles.size();
// stage 0.1: eliminate told cycles
for ( p = p_begin; p != p_end; ++p )
(*p)->eliminateToldCycles(); // not VERY efficient: quadratic vs (possible) linear
// setting up all synonyms
for ( p = p_begin; p != p_end; ++p )
if ( (*p)->isSynonym() )
{
(*p)->canonicaliseSynonym();
(*p)->addFeaturesToSynonym();
}
// change all parents that are synonyms to their primers
for ( p = p_begin; p != p_end; ++p )
if ( !(*p)->isSynonym() )
(*p)->removeSynonymsFromParents();
// here TOP-role has no children yet, so it's safe to complete the automaton
universalRole.completeAutomaton(nRoles);
// make all roles w/o told subsumers have Role TOP instead
for ( p = p_begin; p < p_end; ++p )
if ( !(*p)->isSynonym() && !(*p)->hasToldSubsumers() )
(*p)->addParent(&universalRole);
// stage 2: perform classification
// create roles taxonomy
pTax = new Taxonomy ( &universalRole, &emptyRole );
TaxonomyCreator TaxCreator(pTax);
TaxCreator.setCompletelyDefined(true);
for ( p = p_begin; p != p_end; ++p )
if ( !(*p)->isClassified() )
TaxCreator.classifyEntry(*p);
// stage 3: fills ancestor/descendants using taxonomy
for ( p = p_begin; p != p_end; ++p )
if ( !(*p)->isSynonym() )
(*p)->initADbyTaxonomy ( pTax, nRoles );
// complete role automaton's info
for ( p = p_begin; p != p_end; ++p )
if ( !(*p)->isSynonym() )
(*p)->completeAutomaton(nRoles);
// now all usual roles has their own automata, set up Bottom's automata
emptyRole.completeAutomaton(nRoles);
// prepare taxonomy to the real usage
pTax->finalise();
// stage 3.5: apply Disjoint axioms to roles; check and correct disjoints in hierarchy
if ( !DJRolesA.empty() )
{
for ( iterator q = DJRolesA.begin(), q_end = DJRolesA.end(), r = DJRolesB.begin();
q != q_end; ++q, ++r )
{
TRole* R = resolveSynonym(*q);
TRole* S = resolveSynonym(*r);
R->addDisjointRole(S);
S->addDisjointRole(R);
R->inverse()->addDisjointRole(S->inverse());
S->inverse()->addDisjointRole(R->inverse());
}
for ( p = p_begin; p != p_end; ++p )
if ( !(*p)->isSynonym() && (*p)->isDisjoint() )
(*p)->checkHierarchicalDisjoint();
}
// stage 4: init other fields for the roles. The whole hierarchy is known here
for ( p = p_begin; p != p_end; ++p )
if ( !(*p)->isSynonym() )
(*p)->postProcess();
// the last stage: check whether all roles are consistent
for ( p = p_begin; p != p_end; ++p )
if ( !(*p)->isSynonym() )
(*p)->consistent();
}
|