Debian Codegraph @

FaCT++.Python/lib/Kernel/tRole.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 <set>
21 #include <fstream>
22 #include <algorithm>
23
24 #include "tRole.h"
25 #include "Taxonomy.h"
26
27 void TRole :: fillsComposition ( TRoleVec& Composition, const DLTree* tree ) const
28 {
29 if ( tree->Element() == RCOMPOSITION )
30 {
31 fillsComposition ( Composition, tree->Left() );
32 fillsComposition ( Composition, tree->Right() );
33 }
34 else
35 Composition.push_back(resolveRole(tree));
36 }
37
38 /// copy role information (like transitivity, functionality, R&D etc) to synonym
39 void TRole :: addFeaturesToSynonym ( void )
40 {
41 if ( !isSynonym() )
42 return;
43
44 TRole* syn = resolveSynonym(this);
45
46 // don't copy parents: they are already copied during ToldSubsumers processing
47
48 // copy functionality
49 if ( isFunctional() && !syn->isFunctional() )
50 syn->setFunctional();
51
52 // copy transitivity
53 if ( isTransitive() )
54 syn->setTransitive ();
55
56 // copy reflexivity
57 if ( isReflexive() )
58 syn->setReflexive();
59
60 // copy data type
61 if ( isDataRole() )
62 syn->setDataRole();
63
64 // copy R&D
65 if ( pDomain != nullptr )
66 syn->setDomain (clone(pDomain));
67
68 // copy disjoint
69 if ( isDisjoint() )
70 syn->Disjoint.insert ( Disjoint.begin(), Disjoint.end() );
71
72 // copy subCompositions
73 syn->subCompositions.insert ( syn->subCompositions.end(),
74 subCompositions.begin(),
75 subCompositions.end() );
76
77 // syn should be the only parent for synonym
78 toldSubsumers.clear();
79 addParent(syn);
80 }
81
82 // compare 2 TRoles wrt order of synonyms
83 class TRoleCompare
84 {
85 public:
86 bool operator() ( TRole* p, TRole* q ) const
87 {
88 int n = p->getId(), m = q->getId();
89 if ( n > 0 && m < 0 )
90 return true;
91 if ( n < 0 && m > 0 )
92 return false;
93 return abs(n) < abs(m);
94 }
95 }; // TRoleCompare
96
97 TRole* TRole :: eliminateToldCycles ( TRoleSet& RInProcess, TRoleVec& ToldSynonyms )
98 {
99 // skip synonyms
100 if ( isSynonym() )
101 return nullptr;
102
103 // if we found a cycle...
104 if ( RInProcess.find(this) != RInProcess.end() )
105 {
106 ToldSynonyms.push_back(this);
107 return this;
108 }
109
110 TRole* ret = nullptr;
111
112 // start processing role
113 RInProcess.insert(this);
114
115 // ensure that parents does not contain synonyms
116 removeSynonymsFromParents();
117
118 // not involved in cycle -- check all told subsumers
119 for ( auto r: told() )
120 // if cycle was detected
121 if ( (ret = static_cast<TRole*>(r)->eliminateToldCycles ( RInProcess, ToldSynonyms )) != nullptr )
122 {
123 if ( ret == this )
124 {
125 std::sort ( ToldSynonyms.begin(), ToldSynonyms.end(), TRoleCompare() );
126 // now first element is representative; save it as RET
127 ret = *ToldSynonyms.begin();
128
129 // make all others synonyms of RET
130 for ( std::vector<TRole*>::iterator
131 p = ToldSynonyms.begin()+1, p_end = ToldSynonyms.end(); p < p_end; ++p )
132 {
133 (*p)->setSynonym(ret);
134 ret->addParents((*p)->told());
135 }
136
137 ToldSynonyms.clear();
138 // restart search for the representative
139 RInProcess.erase(this);
140 return ret->eliminateToldCycles ( RInProcess, ToldSynonyms );
141 }
142 else // some role inside a cycle: save it and return
143 {
144 ToldSynonyms.push_back(this);
145 break;
146 }
147 }
148
149 // finish processing role
150 RInProcess.erase(this);
151 return ret;
152 }
153
154 void TRole :: Print ( std::ostream& o ) const
155 {
156 o << "Role \"" << getName() << "\"(" << getId() << ")";
157
158 //FIXME!! while it's not necessary
159 // o << " [" << r.nUsageFreq << "]";
160
161 // transitivity
162 if ( isTransitive() )
163 o << "T";
164
165 // reflexivity
166 if ( isReflexive() )
167 o << "R";
168
169 // functionality
170 if ( isTopFunc() )
171 o << "t";
172 if ( isFunctional() )
173 o << "F";
174
175 // data role
176 if ( isDataRole() )
177 o << "D";
178
179 if ( isSynonym() )
180 {
181 o << " = \"" << getSynonym()->getName() << "\"\n";
182 return;
183 }
184
185 if ( !toldSubsumers.empty() )
186 {
187 ClassifiableEntry::linkSet::const_iterator q = toldSubsumers.begin();
188
189 o << " parents={\"" << (*q)->getName();
190
191 for ( ++q; q != toldSubsumers.end(); ++q )
192 o << "\", \"" << (*q)->getName();
193
194 o << "\"}";
195 }
196
197 if ( !Disjoint.empty() )
198 {
199 TRoleSet::const_iterator p = Disjoint.begin(), p_end = Disjoint.end();
200
201 o << " disjoint with {\"" << (*p)->getName();
202
203 for ( ++p; p != p_end; ++p )
204 o << "\", \"" << (*p)->getName();
205
206 o << "\"}";
207 }
208
209 // range/domain
210 if ( getTDomain() != nullptr )
211 o << " Domain=(" << getBPDomain() << ")=" << getTDomain();
212 if ( getTRange() != nullptr )
213 o << " Range=(" << getBPRange() << ")=" << getTRange();
214
215 if ( !isDataRole() )
216 {
217 o << "\nAutomaton (size " << A.size() << "): " << ( A.isISafe() ? "I" : "i" ) << ( A.isOSafe() ? "O" : "o" );
218 A.Print(o);
219 }
220 o << "\n";
221 }
222
223 // actor to fill vector by traversing taxonomy in a proper direction
224 class AddRoleActor: public WalkerInterface
225 {
226 protected:
227 TRole::TRoleVec& rset;
228 public:
229 explicit AddRoleActor ( TRole::TRoleVec& v ) : rset(v) {}
230 bool apply ( const TaxonomyVertex& v ) override
231 {
232 if ( v.getPrimer()->getId() == 0 )
233 return false;
234 rset.push_back(const_cast<TRole*>(static_cast<const TRole*>(v.getPrimer())));
235 return true;
236 }
237 }; // AddRoleActor
238
239 /// init ancestors and descendants using Taxonomy
240 void
241 TRole :: initADbyTaxonomy ( Taxonomy* pTax, size_t nRoles )
242 {
243 fpp_assert ( isClassified() ); // safety check
244 fpp_assert ( Ancestor.empty() && Descendant.empty() );
245
246 // Note that Top/Bottom are not connected to taxonomy yet.
247
248 // fills ancestors by the taxonomy
249 AddRoleActor anc(Ancestor);
250 pTax->getRelativesInfo</*needCurrent=*/false, /*onlyDirect=*/false, /*upDirection=*/true> ( getTaxVertex(), anc );
251 // fills descendants by the taxonomy
252 AddRoleActor desc(Descendant);
253 pTax->getRelativesInfo</*needCurrent=*/false, /*onlyDirect=*/false, /*upDirection=*/false> ( getTaxVertex(), desc );
254
255 // resize maps for fast access
256 DJRoles.resize(nRoles);
257 AncMap.resize(nRoles);
258 // init map for fast Anc/Desc access
259 addAncestorsToBitMap(AncMap);
260 }
261
262 void TRole :: postProcess ( void )
263 {
264 // set Topmost-Functional field
265 initTopFunc();
266 // init DJ roles map
267 if ( isDisjoint() )
268 initDJMap();
269 }
270
271 /// check if the role is topmost-functional (internal-use only)
272 // not very efficient, but good enough
273 bool TRole :: isRealTopFunc ( void ) const
274 {
275 if ( !isFunctional() ) // all REAL top-funcs have their self-ref in TopFunc already
276 return false;
277 // if any of the parent is self-proclaimed top-func -- this one is not top-func
278 for ( const auto& sup: ancestors() )
279 if ( sup->isTopFunc() )
280 return false;
281
282 // functional role with no functional parents is top-most functional
283 return true;
284 }
285
286 /// set up TopFunc member (internal-use only)
287 // not very efficient, but good enough
288 void TRole :: initTopFunc ( void )
289 {
290 if ( isRealTopFunc() ) // TF already set up -- nothing to do
291 return;
292
293 if ( isTopFunc() ) // self-proclaimed TF but not real -- need to be updated
294 TopFunc.clear();
295
296 // register all real TFs
297 for ( const auto& sup: ancestors() )
298 if ( sup->isRealTopFunc() )
299 TopFunc.push_back(sup);
300
301 if ( !TopFunc.empty() )
302 Functionality.setValue(true);
303 }
304
305 // disjoint-related implementation
306
307 /// check (and correct) case whether R != S for R [= S
308 void TRole :: checkHierarchicalDisjoint ( TRole* R )
309 {
310 // if element is disjoint with itself -- the role is empty
311 if ( Disjoint.count(R) )
312 {
313 setDomain(createBottom());
314 Disjoint.clear();
315 return;
316 }
317
318 // check whether a sub-role is disjoint with the given one
319 for ( auto& sub: R->descendants() )
320 if ( Disjoint.count(sub) )
321 {
322 sub->setDomain(createBottom());
323 Disjoint.erase(sub);
324 sub->Disjoint.clear();
325 }
326 }
327
328 /// init map of all disjoint roles (N is a size of a bitmap)
329 void TRole :: initDJMap ( void )
330 {
331 // role R is disjoint with every role S' [= S such that R != S
332 for ( const TRole* role : Disjoint )
333 DJRoles[role->getIndex()] = true;
334 }
335
336 // automaton-related implementation
337
338 void
339 TRole :: preprocessComposition ( TRoleVec& RS )
340 {
341 bool same = false;
342 size_t last = RS.size()-1;
343 size_t i = 0; // current element of the composition
344
345 for ( TRoleVec::iterator p = RS.begin(), p_end = RS.end(); p != p_end; ++p, ++i )
346 {
347 TRole* R = resolveSynonym(*p);
348
349 if ( R->isBottom() ) // empty role in composition -- nothing to do
350 {
351 RS.clear();
352 return;
353 }
354 if ( R == this ) // found R in composition
355 {
356 if ( i != 0 && i != last ) // R in the middle of the composition
357 throw EFPPCycleInRIA(getName());
358 if ( same ) // second one
359 {
360 if ( last == 1 ) // transitivity
361 {
362 RS.clear();
363 setTransitive();
364 return;
365 }
366 else // wrong (undecidable) axiom
367 throw EFPPCycleInRIA(getName());
368 }
369 else // first one
370 same = true;
371 }
372
373 *p = R; // replace possible synonyms
374 }
375 }
376
377 /// complete role automaton
378 void TRole :: completeAutomaton ( TRoleSet& RInProcess )
379 {
380 // check whether RA is already complete
381 if ( A.isCompleted() )
382 return;
383
384 // if we found a cycle...
385 if ( RInProcess.find(this) != RInProcess.end() )
386 throw EFPPCycleInRIA(getName());
387
388 // start processing role
389 RInProcess.insert(this);
390
391 // make sure that all sub-roles already have completed automata
392 for ( auto& sub: descendants() )
393 sub->completeAutomaton(RInProcess);
394
395 // add automata for complex role inclusions
396 for ( auto& subComp: subCompositions )
397 addSubCompositionAutomaton ( subComp, RInProcess );
398
399 // check for the transitivity
400 if ( isTransitive() )
401 A.addTransitionSafe ( A.final(), RATransition(A.initial()) );
402
403 // here automaton is complete
404 A.setCompleted();
405
406 if ( likely(!isBottom()) ) // FIXME!! for now; need better Top/Bot synonyms processing
407 for ( auto& p: told() )
408 {
409 TRole* R = resolveSynonym(static_cast<TRole*>(p));
410 if ( R->isTop() ) // do not propagate anything to a top-role
411 continue;
412 R->addSubRoleAutomaton(this);
413 if ( hasSpecialDomain() )
414 R->SpecialDomain = true;
415 }
416
417 // finish processing role
418 RInProcess.erase(this);
419 }
420
421 /// add automaton for a role composition
422 void
423 TRole :: addSubCompositionAutomaton ( TRoleVec& RS, TRoleSet& RInProcess )
424 {
425 // first preprocess the role chain
426 preprocessComposition(RS);
427
428 if ( RS.empty() ) // fallout from transitivity axiom
429 return;
430
431 // here we need a special treatment for R&D
432 SpecialDomain = true;
433
434 // tune iterators and states
435 auto p = RS.begin(), p_last = RS.end() - 1;
436 RAState from = A.initial(), to = A.final();
437
438 if ( RS.front() == this )
439 {
440 ++p;
441 from = A.final();
442 }
443 else if ( RS.back() == this )
444 {
445 --p_last;
446 to = A.initial();
447 }
448
449 // make sure the role chain contain at least one element
450 fpp_assert ( p <= p_last );
451
452 // create a chain
453 bool oSafe = false; // we couldn't assume that the current role automaton is i- or o-safe
454 A.initChain(from);
455 for ( ; p != p_last; ++p )
456 oSafe = A.addToChain ( completeAutomatonByRole ( *p, RInProcess ), oSafe );
457
458 // add the last automaton to chain
459 A.addToChain ( completeAutomatonByRole ( *p, RInProcess ), oSafe, to );
460 }