Debian Codegraph @

FaCT++.Python/lib/Kernel/tRole.h

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