Debian Codegraph @

Kernel/RoleMaster.cpp

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "RoleMaster.h"
21 #include "eFPPInconsistentKB.h"
22 #include "TaxonomyCreator.h"
23
24 RoleMaster :: RoleMaster ( bool dataRoles, const std::string& TopRoleName, const std::string& BotRoleName )
25 : emptyRole(BotRoleName == "" ? "emptyRole" : BotRoleName)
26 , universalRole(TopRoleName == "" ? "universalRole" : TopRoleName)
27 , roleNS()
28 , DataRoles(dataRoles)
29 {
30 // no zero-named roles allowed
31 Roles.push_back(nullptr);
32 Roles.push_back(nullptr);
33 // setup empty role
34 emptyRole.setId(0);
35 emptyRole.setInverse(&emptyRole);
36 emptyRole.setDataRole(dataRoles);
37 emptyRole.setBPDomain(bpBOTTOM);
38 emptyRole.setBottom();
39 // setup universal role
40 universalRole.setId(0);
41 universalRole.setInverse(&universalRole);
42 universalRole.setDataRole(dataRoles);
43 universalRole.setBPDomain(bpTOP);
44 universalRole.setTop();
45 // FIXME!! now it is not transitive => simple
46 const_cast<RoleAutomaton&>(universalRole.getAutomaton()).setCompleted();
47 }
48
49 /// register TRole and it's inverse in RoleBox
50 void
51 RoleMaster :: registerRole ( TRole* r )
52 {
53 fpp_assert ( r != nullptr && r->Inverse == nullptr ); // sanity check
54 fpp_assert ( r->getId() == 0 ); // only call it for the new roles
55
56 if ( DataRoles )
57 r->setDataRole();
58
59 Roles.push_back (r);
60 r->setId (newRoleId);
61
62 // create new role which would be inverse of R
63 std::string iname ("-");
64 iname += r->getName();
65 TRole* ri = new TRole(iname);
66
67 // set up inverse
68 r->setInverse(ri);
69 ri->setInverse(r);
70
71 Roles.push_back (ri);
72 ri->setId (-newRoleId);
73 ++newRoleId;
74 }
75
76 TRole*
77 RoleMaster :: ensureRoleName ( const std::string& name )
78 {
79 // check for the Top/Bottom names
80 if ( name == emptyRole.getName() )
81 return &emptyRole;
82 if ( name == universalRole.getName() )
83 return &universalRole;
84
85 // new name from NS
86 TRole* p = roleNS.insert(name);
87 // check what happens
88 if ( p == nullptr ) // role registration attempt failed
89 throw EFPPCantRegName ( name, DataRoles ? "data role" : "role" );
90
91 if ( isRegisteredRole(p) ) // registered role
92 return p;
93 if ( p->getId() != 0 || // not registered but has non-null ID
94 !useUndefinedNames ) // new names are disallowed
95 throw EFPPCantRegName ( name, DataRoles ? "data role" : "role" );
96
97 registerRole(p);
98 return p;
99 }
100
101 // inverse the role composition
102 DLTree* inverseComposition ( const DLTree* tree )
103 {
104 if ( tree->Element() == RCOMPOSITION )
105 return new DLTree ( TLexeme(RCOMPOSITION),
106 inverseComposition(tree->Right()),
107 inverseComposition(tree->Left()) );
108 else
109 return createEntry ( RNAME, resolveRole(tree)->inverse() );
110 }
111
112 void
113 RoleMaster :: addRoleParent ( DLTree* tree, TRole* parent ) const
114 {
115 if ( !tree ) // nothing to do
116 return;
117 if ( tree->Element() == RCOMPOSITION )
118 {
119 parent->addComposition(tree);
120 DLTree* inv = inverseComposition(tree);
121 parent->inverse()->addComposition(inv);
122 deleteTree(inv);
123 }
124 else if ( tree->Element() == PROJINTO )
125 {
126 // here -R->C became -PARENT->
127 // encode this as PROJFROM(R-,PROJINTO(PARENT-,C)),
128 // added to the range of R
129 TRole* R = resolveRole(tree->Left());
130 // can't do anything ATM for the data roles
131 if ( R->isDataRole() )
132 throw EFaCTPlusPlus("Projection into not implemented for the data role");
133 DLTree* C = clone(tree->Right());
134 DLTree* InvP = createEntry ( RNAME, parent->inverse() );
135 DLTree* InvR = createEntry ( RNAME, R->inverse() );
136 // C = PROJINTO(PARENT-,C)
137 C = new DLTree ( TLexeme(PROJINTO), InvP, C );
138 // C = PROJFROM(R-,PROJINTO(PARENT-,C))
139 C = new DLTree ( TLexeme(PROJFROM), InvR, C );
140 R->setRange(C);
141 }
142 else if ( tree->Element() == PROJFROM )
143 {
144 // here C-R-> became -PARENT->
145 // encode this as PROJFROM(R,PROJINTO(PARENT,C)),
146 // added to the domain of R
147 TRole* R = resolveRole(tree->Left());
148 DLTree* C = clone(tree->Right());
149 DLTree* P = createEntry ( RNAME, parent );
150 // C = PROJINTO(PARENT,C)
151 C = new DLTree ( TLexeme(PROJINTO), P, C );
152 // C = PROJFROM(R,PROJINTO(PARENT,C))
153 C = new DLTree ( TLexeme(PROJFROM), clone(tree->Left()), C );
154 R->setDomain(C);
155 }
156 else
157 addRoleParent ( resolveRole(tree), parent );
158 deleteTree(tree);
159 }
160
161 /// add parent for the input role
162 void
163 RoleMaster :: addRoleParentProper ( TRole* role, TRole* parent ) const
164 {
165 fpp_assert ( !role->isSynonym() && !parent->isSynonym() );
166
167 if ( role == parent ) // nothing to do
168 return;
169
170 if ( role->isDataRole() != parent->isDataRole() )
171 throw EFaCTPlusPlus("Mixed object and data roles in role subsumption axiom");
172
173 // check the inconsistency case *UROLE* [= *EROLE*
174 if ( unlikely(role->isTop() && parent->isBottom()) )
175 throw EFPPInconsistentKB();
176
177 // *UROLE* [= R means R (and R-) are synonym of *UROLE*
178 if ( unlikely(role->isTop()) )
179 {
180 parent->setSynonym(role);
181 parent->inverse()->setSynonym(role);
182 return;
183 }
184
185 // R [= *EROLE* means R (and R-) are synonyms of *EROLE*
186 if ( unlikely(parent->isBottom()) )
187 {
188 role->setSynonym(parent);
189 role->inverse()->setSynonym(parent);
190 return;
191 }
192
193 role->addParent(parent);
194 role->inverse()->addParent(parent->inverse());
195 }
196
197 void RoleMaster :: initAncDesc ( void )
198 {
199 iterator p, p_begin = begin(), p_end = end();
200 size_t nRoles = Roles.size();
201
202 // stage 0.1: eliminate told cycles
203 for ( p = p_begin; p != p_end; ++p )
204 (*p)->eliminateToldCycles(); // not VERY efficient: quadratic vs (possible) linear
205
206 // setting up all synonyms
207 for ( p = p_begin; p != p_end; ++p )
208 if ( (*p)->isSynonym() )
209 {
210 (*p)->canonicaliseSynonym();
211 (*p)->addFeaturesToSynonym();
212 }
213
214 // change all parents that are synonyms to their primers
215 for ( p = p_begin; p != p_end; ++p )
216 if ( !(*p)->isSynonym() )
217 (*p)->removeSynonymsFromParents();
218
219 // here TOP-role has no children yet, so it's safe to complete the automaton
220 universalRole.completeAutomaton(nRoles);
221
222 // make all roles w/o told subsumers have Role TOP instead
223 for ( p = p_begin; p < p_end; ++p )
224 if ( !(*p)->isSynonym() && !(*p)->hasToldSubsumers() )
225 (*p)->addParent(&universalRole);
226
227 // stage 2: perform classification
228
229 // create roles taxonomy
230 pTax = new Taxonomy ( &universalRole, &emptyRole );
231 TaxonomyCreator TaxCreator(pTax);
232 TaxCreator.setCompletelyDefined(true);
233
234 for ( p = p_begin; p != p_end; ++p )
235 if ( !(*p)->isClassified() )
236 TaxCreator.classifyEntry(*p);
237
238 // stage 3: fills ancestor/descendants using taxonomy
239 for ( p = p_begin; p != p_end; ++p )
240 if ( !(*p)->isSynonym() )
241 (*p)->initADbyTaxonomy ( pTax, nRoles );
242
243 // complete role automaton's info
244 for ( p = p_begin; p != p_end; ++p )
245 if ( !(*p)->isSynonym() )
246 (*p)->completeAutomaton(nRoles);
247
248 // now all usual roles has their own automata, set up Bottom's automata
249 emptyRole.completeAutomaton(nRoles);
250
251 // prepare taxonomy to the real usage
252 pTax->finalise();
253
254 // stage 3.5: apply Disjoint axioms to roles; check and correct disjoints in hierarchy
255 if ( !DJRolesA.empty() )
256 {
257 for ( iterator q = DJRolesA.begin(), q_end = DJRolesA.end(), r = DJRolesB.begin();
258 q != q_end; ++q, ++r )
259 {
260 TRole* R = resolveSynonym(*q);
261 TRole* S = resolveSynonym(*r);
262 R->addDisjointRole(S);
263 S->addDisjointRole(R);
264 R->inverse()->addDisjointRole(S->inverse());
265 S->inverse()->addDisjointRole(R->inverse());
266 }
267
268 for ( p = p_begin; p != p_end; ++p )
269 if ( !(*p)->isSynonym() && (*p)->isDisjoint() )
270 (*p)->checkHierarchicalDisjoint();
271 }
272
273 // stage 4: init other fields for the roles. The whole hierarchy is known here
274 for ( p = p_begin; p != p_end; ++p )
275 if ( !(*p)->isSynonym() )
276 (*p)->postProcess();
277
278 // the last stage: check whether all roles are consistent
279 for ( p = p_begin; p != p_end; ++p )
280 if ( !(*p)->isSynonym() )
281 (*p)->consistent();
282 }