Debian Codegraph @

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

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2013-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 EXTENDEDSYNTACTICLOCALITYCHECKER_H
21 #define EXTENDEDSYNTACTICLOCALITYCHECKER_H
22
23 // uncomment the following line to debug the locality checker
24 //#define FPP_DEBUG_EXTENDED_LOCALITY
25
26 // debug also the general locality checker
27 #ifdef FPP_DEBUG_EXTENDED_LOCALITY
28 # define FPP_DEBUG_LOCALITY
29 #endif
30
31 #include "GeneralSyntacticLocalityChecker.h"
32
33 #ifdef FPP_DEBUG_EXTENDED_LOCALITY
34 # include "tExpressionPrinterLISP.h"
35 #endif
36
37 // forward declarations
38 class UpperBoundDirectEvaluator;
39 class LowerBoundDirectEvaluator;
40 class UpperBoundComplementEvaluator;
41 class LowerBoundComplementEvaluator;
42
43 class CardinalityEvaluatorBase: protected SigAccessor, public DLExpressionVisitorEmpty
44 {
45 protected: // members
46 #ifdef FPP_DEBUG_EXTENDED_LOCALITY
47 TLISPExpressionPrinter lp;
48 const char* name;
49 #endif
50
51 UpperBoundDirectEvaluator* UBD = nullptr;
52 LowerBoundDirectEvaluator* LBD = nullptr;
53 UpperBoundComplementEvaluator* UBC = nullptr;
54 LowerBoundComplementEvaluator* LBC = nullptr;
55
56 /// keep the value here
57 int value = 0;
58
59 protected: // methods to
60 /// main method to use
61 int getValue ( const TDLExpression& expr )
62 {
63 expr.accept(*this);
64 return value;
65 }
66
67 void dumpValue ( const TDLExpression& expr ATTR_UNUSED )
68 {
69 # ifdef FPP_DEBUG_EXTENDED_LOCALITY
70 std::cout << name << ": value for";
71 expr.accept(lp);
72 std::cout << " is " << value << std::endl;
73 # endif
74 }
75
76 /// implementation of evaluation
77 int getUpperBoundDirect ( const TDLExpression& expr );
78 /// implementation of evaluation
79 int getUpperBoundComplement ( const TDLExpression& expr );
80 /// implementation of evaluation
81 int getLowerBoundDirect ( const TDLExpression& expr );
82 /// implementation of evaluation
83 int getLowerBoundComplement ( const TDLExpression& expr );
84
85 protected: // visitor helpers
86 /// define a special value for concepts that are not in C[C}^{<= n}
87 int noUpperValue ( void ) const { return -1; }
88 /// define a special value for concepts that are in C[C]^{<= n} for all n
89 int anyUpperValue ( void ) const { return 0; }
90 /// return all or none values depending on the condition
91 int getAllNoneUpper ( bool condition ) const { return condition ? anyUpperValue() : noUpperValue(); }
92 /// define a special value for concepts that are not in C[C]^{>= n}
93 int noLowerValue ( void ) const { return 0; }
94 /// define a special value for concepts that are in C[C]^{>= n} for all n
95 int anyLowerValue ( void ) const { return -1; }
96 /// return 1 or none values depending on the condition
97 int getOneNoneLower ( bool condition ) const { return condition ? 1 : noLowerValue(); }
98
99 /// @return true if given upper VALUE is less than M
100 bool isUpperLT ( int value, unsigned int m ) const
101 {
102 if ( value == noUpperValue() )
103 return false;
104 return value == anyUpperValue() || (unsigned int) value < m;
105 }
106 /// @return true if given upper VALUE is less than or equal to M
107 bool isUpperLE ( int value, unsigned int m ) const { return isUpperLT ( value, m+1 ); }
108 /// @return true if given lower VALUE is greater than or equal to M
109 bool isLowerGE ( int value, unsigned int m ) const
110 {
111 if ( value == noLowerValue() )
112 return false;
113 return value == anyLowerValue() || (unsigned int) value >= m;
114 }
115 /// @return true if given upper VALUE is greater than M
116 bool isLowerGT ( int value, unsigned int m ) const { return isLowerGE ( value, m+1 ); }
117
118 bool isBotEquivalent ( const TDLExpression* expr ) { return isUpperLE ( getUpperBoundDirect(*expr), 0 ); }
119 bool isTopEquivalent ( const TDLExpression* expr ) { return isUpperLE ( getUpperBoundComplement(*expr), 0 ); }
120
121 // helpers for and/or
122
123 /// return minimal of the two Upper Bounds
124 int minUpperValue ( int uv1, int uv2 )
125 {
126 // noUpperValue is a maximal element
127 if ( uv1 == noUpperValue() )
128 return uv2;
129 if ( uv2 == noUpperValue() )
130 return uv1;
131 // now return the smallest value, with anyUpperValue being
132 // the smallest and deal with automatically
133 return std::min(uv1,uv2);
134 }
135 /* /// return maximal of the two Lower Bounds
136 int maxLowerValue ( int lv1, int lv2 )
137 {
138 // noLowerValue is a maximal element
139 if ( lv1 == noLowerValue() || lv2 == noLowerValue() )
140 return noLowerValue();
141 // we are looking for the maximal value here; ANY need to be special-cased
142 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
143 {
144 int n = getLowerBoundDirect(*p);
145 if ( n == anyLowerValue() )
146 return anyLowerValue();
147 max = std::max ( max, n );
148 }
149 return max;
150
151 }
152 */
153 // tunable for every case
154
155 /// helper for entities
156 virtual int getEntityValue ( const TNamedEntity* entity ) = 0;
157 /// helper for All
158 virtual int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) = 0;
159 /// helper for things like >= m R.C
160 virtual int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) = 0;
161 /// helper for things like <= m R.C
162 virtual int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) = 0;
163 /// helper for things like = m R.C
164 virtual int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) = 0;
165
166 public: // interface
167 /// init c'tor
168 CardinalityEvaluatorBase ( const TSignature* s, const char* n ATTR_UNUSED )
169 : SigAccessor(s)
170 # ifdef FPP_DEBUG_EXTENDED_LOCALITY
171 , lp(std::cout)
172 , name(n)
173 # endif
174 {}
175
176 /// set all other evaluators
177 void setEvaluators ( UpperBoundDirectEvaluator* pUD, LowerBoundDirectEvaluator* pLD, UpperBoundComplementEvaluator* pUC, LowerBoundComplementEvaluator* pLC )
178 {
179 UBD = pUD;
180 LBD = pLD;
181 UBC = pUC;
182 LBC = pLC;
183 fpp_assert ( (void*)UBD == this || (void*)LBD == this || (void*)UBC == this || (void*)LBC == this );
184 }
185
186 /// implementation of evaluation
187 int getUpperBoundDirect ( const TDLExpression* expr ) { return getUpperBoundDirect(*expr); }
188 /// implementation of evaluation
189 int getUpperBoundComplement ( const TDLExpression* expr ) { return getUpperBoundComplement(*expr); }
190 /// implementation of evaluation
191 int getLowerBoundDirect ( const TDLExpression* expr ) { return getLowerBoundDirect(*expr); }
192 /// implementation of evaluation
193 int getLowerBoundComplement ( const TDLExpression* expr ) { return getLowerBoundComplement(*expr); }
194
195 public: // visitor implementation: common cases
196 // concept expressions
197 void visit ( const TDLConceptName& expr ) override
198 { value = getEntityValue(expr.getEntity()); }
199 void visit ( const TDLConceptObjectExists& expr ) override
200 { value = getMinValue ( 1, expr.getOR(), expr.getC() ); dumpValue(expr); }
201 void visit ( const TDLConceptObjectForall& expr ) override
202 { value = getForallValue ( expr.getOR(), expr.getC() ); }
203 void visit ( const TDLConceptObjectMinCardinality& expr ) override
204 { value = getMinValue ( expr.getNumber(), expr.getOR(), expr.getC() ); }
205 void visit ( const TDLConceptObjectMaxCardinality& expr ) override
206 { value = getMaxValue ( expr.getNumber(), expr.getOR(), expr.getC() ); }
207 void visit ( const TDLConceptObjectExactCardinality& expr ) override
208 { value = getExactValue ( expr.getNumber(), expr.getOR(), expr.getC() ); }
209 void visit ( const TDLConceptDataExists& expr ) override
210 { value = getMinValue ( 1, expr.getDR(), expr.getExpr() ); }
211 void visit ( const TDLConceptDataForall& expr ) override
212 { value = getForallValue ( expr.getDR(), expr.getExpr() ); }
213 void visit ( const TDLConceptDataMinCardinality& expr ) override
214 { value = getMinValue ( expr.getNumber(), expr.getDR(), expr.getExpr() ); }
215 void visit ( const TDLConceptDataMaxCardinality& expr ) override
216 { value = getMaxValue ( expr.getNumber(), expr.getDR(), expr.getExpr() ); }
217 void visit ( const TDLConceptDataExactCardinality& expr ) override
218 { value = getExactValue ( expr.getNumber(), expr.getDR(), expr.getExpr() ); }
219
220 // object role expressions
221 void visit ( const TDLObjectRoleName& expr ) override
222 { value = getEntityValue(expr.getEntity()); }
223 // FaCT++ extension: equivalent to R(x,y) and C(x), so copy behaviour from ER.X
224 void visit ( const TDLObjectRoleProjectionFrom& expr ) override
225 { value = getMinValue ( 1, expr.getOR(), expr.getC() ); }
226 // FaCT++ extension: equivalent to R(x,y) and C(y), so copy behaviour from ER.X
227 void visit ( const TDLObjectRoleProjectionInto& expr ) override
228 { value = getMinValue ( 1, expr.getOR(), expr.getC() ); }
229
230 // data role expressions
231 void visit ( const TDLDataRoleName& expr ) override
232 { value = getEntityValue(expr.getEntity()); }
233 }; // CardinalityEvaluatorBase
234
235 /// determine how many instances can an expression have;
236 /// all methods return minimal n such that expr\in C^{<= n}, n >= 0
237 class UpperBoundDirectEvaluator: public CardinalityEvaluatorBase
238 {
239 protected: // methods
240 /// helper for entities TODO: checks only C top-locality, not R
241 int getEntityValue ( const TNamedEntity* entity ) override
242 { return getAllNoneUpper ( botCLocal() && nc(entity) ); }
243 /// helper for All
244 int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override
245 { return getAllNoneUpper ( isTopEquivalent(R) && isLowerGE ( getLowerBoundComplement(C), 1 ) ); }
246 /// helper for things like >= m R.C
247 int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
248 {
249 // m > 0 and...
250 if ( m <= 0 )
251 return noUpperValue();
252 // R = \bot or...
253 if ( isBotEquivalent(R) )
254 return anyUpperValue();
255 // C \in C^{<= m-1}
256 return getAllNoneUpper ( isUpperLT ( getUpperBoundDirect(C), m ) );
257 }
258 /// helper for things like <= m R.C
259 int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
260 {
261 // R = \top and...
262 if ( !isTopEquivalent(R) )
263 return noUpperValue();
264 // C\in C^{>= m+1}
265 return getAllNoneUpper ( isLowerGT ( getLowerBoundDirect(C), m ) );
266 }
267 /// helper for = m R.C
268 int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
269 {
270 // conjunction of Min and Max values
271 return minUpperValue ( getMinValue ( m, R, C ), getMaxValue ( m, R, C ) );
272 }
273
274 /// helper for And
275 template <typename C>
276 int getAndValue ( const TDLNAryExpression<C>& expr )
277 {
278 // noUpperValue is a maximal element
279 int min = noUpperValue();
280 // we are looking for the minimal value here, use an appropriate helper
281 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
282 min = minUpperValue ( min, getUpperBoundDirect(*p) );
283 return min;
284 }
285 /// helper for Or
286 template <typename C>
287 int getOrValue ( const TDLNAryExpression<C>& expr )
288 {
289 int sum = 0;
290 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
291 {
292 int n = getUpperBoundDirect(*p);
293 if ( n == noUpperValue() )
294 return noUpperValue();
295 sum += n;
296 }
297 return sum;
298 }
299
300 public: // interface
301 /// init c'tor
302 explicit UpperBoundDirectEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "UpperBoundDirect" ) {}
303
304 public: // visitor implementation
305 // make all other visit() methods from the base implementation visible
306 using CardinalityEvaluatorBase::visit;
307 // concept expressions
308 void visit ( const TDLConceptTop& ) override { value = noUpperValue(); }
309 void visit ( const TDLConceptBottom& ) override { value = anyUpperValue(); }
310 void visit ( const TDLConceptNot& expr ) override { value = getUpperBoundComplement(expr.getC()); }
311 void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); }
312 void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); }
313 void visit ( const TDLConceptOneOf& expr ) override { value = int(expr.size()); }
314 void visit ( const TDLConceptObjectSelf& expr ) override { value = getAllNoneUpper(isBotEquivalent(expr.getOR())); }
315 void visit ( const TDLConceptObjectValue& expr ) override { value = getAllNoneUpper(isBotEquivalent(expr.getOR())); }
316 void visit ( const TDLConceptDataValue& expr ) override { value = getAllNoneUpper(isBotEquivalent(expr.getDR())); }
317
318 // object role expressions
319 // TODO!! properly process roles that are isTop/isBot
320 void visit ( const TDLObjectRoleTop& ) override { value = noUpperValue(); }
321 void visit ( const TDLObjectRoleBottom& ) override { value = anyUpperValue(); }
322 void visit ( const TDLObjectRoleInverse& expr ) override { value = getUpperBoundDirect(expr.getOR()); }
323 void visit ( const TDLObjectRoleChain& expr ) override
324 {
325 for ( const auto* arg : expr )
326 if ( isBotEquivalent(arg) )
327 {
328 value = anyUpperValue();
329 return;
330 }
331 value = noUpperValue();
332 }
333
334 // data role expressions
335 void visit ( const TDLDataRoleTop& ) override { value = noUpperValue(); }
336 void visit ( const TDLDataRoleBottom& ) override { value = anyUpperValue(); }
337
338 // data expressions
339 void visit ( const TDLDataTop& ) override { value = noUpperValue(); }
340 void visit ( const TDLDataBottom& ) override { value = anyUpperValue(); }
341 // FIXME!! not ready
342 //void visit ( const TDLDataTypeName& ) override { isBotEq = false; }
343 // FIXME!! not ready
344 //void visit ( const TDLDataTypeRestriction& ) override { isBotEq = false; }
345 void visit ( const TDLDataValue& ) override { value = 1; }
346 void visit ( const TDLDataNot& expr ) override { value = getUpperBoundComplement(expr.getExpr()); }
347 void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); }
348 void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); }
349 void visit ( const TDLDataOneOf& expr ) override { value = (int)expr.size(); }
350 }; // UpperBoundDirectEvaluator
351
352 /// determine how many instances can a complement of expression have;
353 /// all methods return minimal n such that expr\in CC^{<= n}, n >= 0
354 class UpperBoundComplementEvaluator: public CardinalityEvaluatorBase
355 {
356 protected: // methods
357 /// helper for entities TODO: checks only C top-locality, not R
358 int getEntityValue ( const TNamedEntity* entity ) override
359 { return getAllNoneUpper ( topCLocal() && nc(entity) ); }
360 /// helper for All
361 int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override
362 { return getAllNoneUpper ( isBotEquivalent(R) || isUpperLE ( getUpperBoundComplement(C), 0 ) ); }
363 /// helper for things like >= m R.C
364 int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
365 {
366 // m == 0 or...
367 if ( m == 0 )
368 return anyUpperValue();
369 // R = \top and...
370 if ( !isTopEquivalent(R) )
371 return noUpperValue();
372 // C \in C^{>= m}
373 return getAllNoneUpper ( isLowerGE ( getLowerBoundDirect(C), m ) );
374 }
375 /// helper for things like <= m R.C
376 int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
377 {
378 // R = \bot or...
379 if ( isBotEquivalent(R) )
380 return anyUpperValue();
381 // C\in C^{<= m}
382 return getAllNoneUpper ( isUpperLE ( getUpperBoundDirect(C), m ) );
383 }
384 /// helper for things like = m R.C
385 int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
386 {
387 // here the minimal value between Mix and Max is an answer. The -1 case will be dealt with automagically
388 return std::min ( getMinValue ( m, R, C ), getMaxValue ( m, R, C ) );
389 }
390
391 /// helper for And
392 template <typename C>
393 int getAndValue ( const TDLNAryExpression<C>& expr )
394 {
395 int sum = 0;
396 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
397 {
398 int n = getUpperBoundComplement(*p);
399 if ( n == noUpperValue() )
400 return noUpperValue();
401 sum += n;
402 }
403 return sum;
404 }
405 /// helper for Or
406 template <typename C>
407 int getOrValue ( const TDLNAryExpression<C>& expr )
408 {
409 // noUpperValue is a maximal element
410 int min = noUpperValue();
411 // we are looking for the minimal value here, use an appropriate helper
412 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
413 min = minUpperValue ( min, getUpperBoundDirect(*p) );
414 return min;
415 }
416
417 public: // interface
418 /// init c'tor
419 explicit UpperBoundComplementEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "UpperBoundCompliment" ) {}
420
421 public: // visitor interface
422 // make all other visit() methods from the base implementation visible
423 using CardinalityEvaluatorBase::visit;
424 // concept expressions
425 void visit ( const TDLConceptTop& ) override { value = anyUpperValue(); }
426 void visit ( const TDLConceptBottom& ) override { value = noUpperValue(); }
427 void visit ( const TDLConceptNot& expr ) override { value = getUpperBoundDirect(expr.getC()); }
428 void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); }
429 void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); }
430 void visit ( const TDLConceptOneOf& ) override { value = noUpperValue(); }
431 void visit ( const TDLConceptObjectSelf& expr ) override { value = getAllNoneUpper(isTopEquivalent(expr.getOR())); }
432 void visit ( const TDLConceptObjectValue& expr ) override { value = getAllNoneUpper(isTopEquivalent(expr.getOR())); }
433 void visit ( const TDLConceptDataValue& expr ) override { value = getAllNoneUpper(isTopEquivalent(expr.getDR())); }
434
435 // object role expressions
436 void visit ( const TDLObjectRoleTop& ) override { value = anyUpperValue(); }
437 void visit ( const TDLObjectRoleBottom& ) override { value = noUpperValue(); }
438 void visit ( const TDLObjectRoleInverse& expr ) override { value = getUpperBoundComplement(expr.getOR()); }
439 void visit ( const TDLObjectRoleChain& expr ) override
440 {
441 for ( const auto* arg : expr )
442 if ( !isTopEquivalent(arg) )
443 {
444 value = noUpperValue();
445 return;
446 }
447 value = anyUpperValue();
448 }
449
450 // data role expressions
451 void visit ( const TDLDataRoleTop& ) override { value = anyUpperValue(); }
452 void visit ( const TDLDataRoleBottom& ) override { value = noUpperValue(); }
453
454 // data expressions
455 void visit ( const TDLDataTop& ) override { value = anyUpperValue(); }
456 void visit ( const TDLDataBottom& ) override { value = noUpperValue(); }
457 // negated datatype is a union of all other DTs that are infinite
458 void visit ( const TDLDataTypeName& ) override { value = noUpperValue(); }
459 // negated restriction include negated DT
460 void visit ( const TDLDataTypeRestriction& ) override { value = noUpperValue(); }
461 void visit ( const TDLDataValue& ) override { value = noUpperValue(); }
462 void visit ( const TDLDataNot& expr ) override { value = getUpperBoundDirect(expr.getExpr()); }
463 void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); }
464 void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); }
465 void visit ( const TDLDataOneOf& ) override { value = noUpperValue(); }
466 }; // UpperBoundComplementEvaluator
467
468 /// determine how many instances can an expression have;
469 /// all methods return maximal n such that expr\in C^{>= n}, n >= 1
470 class LowerBoundDirectEvaluator: public CardinalityEvaluatorBase
471 {
472 protected: // methods
473 /// helper for entities TODO: checks only C top-locality, not R
474 int getEntityValue ( const TNamedEntity* entity ) override
475 { return getOneNoneLower ( topCLocal() && nc(entity) ); }
476 /// helper for All
477 int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override
478 { return getOneNoneLower ( isBotEquivalent(R) || isUpperLE ( getUpperBoundComplement(C), 0 ) ); }
479 /// helper for things like >= m R.C
480 int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
481 {
482 // m == 0 or...
483 if ( m == 0 )
484 return anyLowerValue();
485 // R = \top and...
486 if ( !isTopEquivalent(R) )
487 return noLowerValue();
488 // C \in C^{>= m}
489 return isLowerGE ( getLowerBoundDirect(C), m ) ? (int)m : noLowerValue();
490 }
491 /// helper for things like <= m R.C
492 int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
493 {
494 // R = \bot or...
495 if ( isBotEquivalent(R) )
496 return 1;
497 // C\in C^{<= m}
498 return getOneNoneLower ( isUpperLE ( getUpperBoundDirect(C), m ) );
499 }
500 /// helper for things like = m R.C
501 int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
502 {
503 int min = getMinValue ( m, R, C ), max = getMaxValue ( m, R, C );
504 // we need to take the lowest value
505 if ( min == noLowerValue() || max == noLowerValue() )
506 return noLowerValue();
507 if ( min == anyLowerValue() )
508 return max;
509 if ( max == anyLowerValue() )
510 return min;
511 return std::min ( min, max );
512 }
513
514 /// helper for And
515 // FIXME!! not done yet
516 template <typename C>
517 int getAndValue ( const TDLNAryExpression<C>& expr )
518 {
519 // return m - sumK, where
520 bool foundC = false; // true if found a conjunct that is in C^{>=}
521 int foundM = 0;
522 int mMax = 0, kMax = 0; // the m- and k- values for the C_j with max m+k
523 int sumK = 0; // sum of all known k
524 // 1st pass: check for none-case, deals with deterministic cases
525 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
526 {
527 int m = getLowerBoundDirect(*p); // C_j \in C^{>= m}
528 int k = getUpperBoundComplement(*p); // C_j \in CC^{<= k}
529 // note bound flip for K
530 // case 0: if both aren't known then we don't know
531 if ( m == noLowerValue() && k == noUpperValue() )
532 return noLowerValue();
533 // if only k exists then add it to k
534 if ( m == noLowerValue() )
535 {
536 // if ( k == getAllValue() ) // we don't have any bound then
537 // return getNoneValue();
538 sumK += k;
539 continue;
540 }
541 // if only m exists then set it to m
542 if ( k == noUpperValue() )
543 {
544 if ( foundC ) // should not have 2 elements in C
545 return noLowerValue();
546 foundC = true;
547 foundM = m;
548 continue;
549 }
550 // here both k and m are values
551 sumK += k; // count k for the
552 if ( k+m > kMax + mMax )
553 {
554 kMax = k;
555 mMax = m;
556 }
557 }
558 // here we know the best candidate for M, and only need to set it up
559 if ( foundC ) // found during the deterministic case
560 {
561 foundM -= sumK;
562 return foundM > 0 ? foundM : noLowerValue();
563 }
564 else // no deterministic option; choose the best one
565 {
566 sumK -= kMax;
567 mMax -= sumK;
568 return mMax > 0 ? mMax : noLowerValue();
569 }
570 }
571 /// helper for Or
572 template <typename C>
573 int getOrValue ( const TDLNAryExpression<C>& expr )
574 {
575 int max = noLowerValue();
576 // we are looking for the maximal value here; ANY need to be special-cased
577 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
578 {
579 int n = getLowerBoundDirect(*p);
580 if ( n == anyLowerValue() )
581 return anyLowerValue();
582 max = std::max ( max, n );
583 }
584 return max;
585 }
586
587 public: // interface
588 /// init c'tor
589 explicit LowerBoundDirectEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "LowerBoundDirect" ) {}
590
591 public: // visitor interface
592 // make all other visit() methods from the base implementation visible
593 using CardinalityEvaluatorBase::visit;
594 // concept expressions
595 void visit ( const TDLConceptTop& ) override { value = 1; }
596 void visit ( const TDLConceptBottom& ) override { value = noLowerValue(); }
597 void visit ( const TDLConceptNot& expr ) override { value = getLowerBoundComplement(expr.getC()); }
598 void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); }
599 void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); }
600 void visit ( const TDLConceptOneOf& expr ) override { value = getOneNoneLower(expr.size() > 0); }
601 void visit ( const TDLConceptObjectSelf& expr ) override { value = getOneNoneLower(isTopEquivalent(expr.getOR())); }
602 // FIXME!! differ from the paper
603 void visit ( const TDLConceptObjectValue& expr ) override { value = getOneNoneLower(isTopEquivalent(expr.getOR())); }
604 void visit ( const TDLConceptDataValue& expr ) override { value = getOneNoneLower(isTopEquivalent(expr.getDR())); }
605
606 // object role expressions
607 void visit ( const TDLObjectRoleTop& ) override { value = anyLowerValue(); }
608 void visit ( const TDLObjectRoleBottom& ) override { value = noLowerValue(); }
609 void visit ( const TDLObjectRoleInverse& expr ) override { value = getLowerBoundDirect(expr.getOR()); }
610 void visit ( const TDLObjectRoleChain& expr ) override
611 {
612 for ( const auto* arg : expr )
613 if ( !isTopEquivalent(arg) )
614 {
615 value = noLowerValue();
616 return;
617 }
618 value = anyLowerValue();
619 }
620
621 // data role expressions
622 void visit ( const TDLDataRoleTop& ) override { value = anyLowerValue(); }
623 void visit ( const TDLDataRoleBottom& ) override { value = noLowerValue(); }
624
625 // data expressions
626 void visit ( const TDLDataTop& ) override { value = anyLowerValue(); }
627 void visit ( const TDLDataBottom& ) override { value = noLowerValue(); }
628 // negated datatype is a union of all other DTs that are infinite
629 void visit ( const TDLDataTypeName& ) override { value = noLowerValue(); }
630 // negated restriction include negated DT
631 void visit ( const TDLDataTypeRestriction& ) override { value = noLowerValue(); }
632 void visit ( const TDLDataValue& ) override { value = noLowerValue(); }
633 void visit ( const TDLDataNot& expr ) override { value = getLowerBoundComplement(expr.getExpr()); }
634 void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); }
635 void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); }
636 void visit ( const TDLDataOneOf& expr ) override { value = getOneNoneLower(expr.size() > 0); }
637 }; // LowerBoundDirectEvaluator
638
639 /// determine how many instances can an expression have;
640 /// all methods return maximal n such that expr\in CC^{>= n}, n >= 1
641 class LowerBoundComplementEvaluator: public CardinalityEvaluatorBase
642 {
643 protected: // methods
644 /// helper for entities TODO: checks only C top-locality, not R
645 int getEntityValue ( const TNamedEntity* entity ) override
646 { return getOneNoneLower ( botCLocal() && nc(entity) ); }
647 /// helper for All
648 int getForallValue ( const TDLRoleExpression* R, const TDLExpression* C ) override
649 { return getOneNoneLower ( isTopEquivalent(R) && isLowerGE ( getLowerBoundComplement(C), 1 ) ); }
650 /// helper for things like >= m R.C
651 int getMinValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
652 {
653 // m > 0 and...
654 if ( m <= 0 )
655 return noLowerValue();
656 // R = \bot or...
657 if ( isBotEquivalent(R) )
658 return 1;
659 // C \in C^{<= m-1}
660 return getOneNoneLower ( isUpperLT ( getUpperBoundDirect(C), m ) );
661 }
662 /// helper for things like <= m R.C
663 int getMaxValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
664 {
665 // R = \top and...
666 if ( !isTopEquivalent(R) )
667 return noLowerValue();
668 // C\in C^{>= m+1}
669 if ( isLowerGT ( getLowerBoundDirect(C), m ) )
670 return (int)m+1;
671 else
672 return noLowerValue();
673 }
674 /// helper for things like = m R.C
675 int getExactValue ( unsigned int m, const TDLRoleExpression* R, const TDLExpression* C ) override
676 {
677 // here the maximal value between Mix and Max is an answer. The -1 case will be dealt with automagically
678 // because both min and max are between 0 and m+1
679 return std::max ( getMinValue ( m, R, C ), getMaxValue ( m, R, C ) );
680 }
681
682 /// helper for And
683 template <typename C>
684 int getAndValue ( const TDLNAryExpression<C>& expr )
685 {
686 int max = noLowerValue();
687 // we are looking for the maximal value here; ANY need to be special-cased
688 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
689 {
690 int n = getLowerBoundComplement(*p);
691 if ( n == anyLowerValue() )
692 return anyLowerValue();
693 max = std::max ( max, n );
694 }
695 return max;
696 }
697 /// helper for Or
698 template <typename C>
699 int getOrValue ( const TDLNAryExpression<C>& expr )
700 {
701 // return m - sumK, where
702 bool foundC = false; // true if found a conjunct that is in C^{>=}
703 int foundM = 0;
704 int mMax = 0, kMax = 0; // the m- and k- values for the C_j with max m+k
705 int sumK = 0; // sum of all known k
706 // 1st pass: check for none-case, deals with deterministic cases
707 for ( typename TDLNAryExpression<C>::iterator p = expr.begin(), p_end = expr.end(); p != p_end; ++p )
708 {
709 int m = getLowerBoundComplement(*p); // C_j \in CC^{>= m}
710 int k = getUpperBoundDirect(*p); // C_j \in C^{<= k}
711 // note bound flip for K
712 // case 0: if both aren't known then we don't know
713 if ( m == noLowerValue() && k == noUpperValue() )
714 return noLowerValue();
715 // if only k exists then add it to k
716 if ( m == noLowerValue() )
717 {
718 // if ( k == getAllValue() ) // we don't have any bound then
719 // return getNoneValue();
720 sumK += k;
721 continue;
722 }
723 // if only m exists then set it to m
724 if ( k == noUpperValue() )
725 {
726 if ( foundC ) // should not have 2 elements in C
727 return noLowerValue();
728 foundC = true;
729 foundM = m;
730 continue;
731 }
732 // here both k and m are values
733 sumK += k; // count k for the
734 if ( k+m > kMax + mMax )
735 {
736 kMax = k;
737 mMax = m;
738 }
739 }
740 // here we know the best candidate for M, and only need to set it up
741 if ( foundC ) // found during the deterministic case
742 {
743 foundM -= sumK;
744 return foundM > 0 ? foundM : noLowerValue();
745 }
746 else // no deterministic option; choose the best one
747 {
748 sumK -= kMax;
749 mMax -= sumK;
750 return mMax > 0 ? mMax : noLowerValue();
751 }
752 }
753
754 public: // interface
755 /// init c'tor
756 explicit LowerBoundComplementEvaluator ( const TSignature* s ) : CardinalityEvaluatorBase ( s, "LowerBoundComplement" ) {}
757
758 public: // visitor implementation
759 // make all other visit() methods from the base implementation visible
760 using CardinalityEvaluatorBase::visit;
761 // concept expressions
762 void visit ( const TDLConceptTop& ) override { value = noLowerValue(); }
763 void visit ( const TDLConceptBottom& ) override { value = 1; }
764 void visit ( const TDLConceptNot& expr ) override { value = getLowerBoundDirect(expr.getC()); }
765 void visit ( const TDLConceptAnd& expr ) override { value = getAndValue(expr); dumpValue(expr); }
766 void visit ( const TDLConceptOr& expr ) override { value = getOrValue(expr); }
767 void visit ( const TDLConceptOneOf& ) override { value = noLowerValue(); }
768 void visit ( const TDLConceptObjectSelf& expr ) override { value = getOneNoneLower(isBotEquivalent(expr.getOR())); }
769 void visit ( const TDLConceptObjectValue& expr ) override { value = getOneNoneLower(isBotEquivalent(expr.getOR())); }
770 void visit ( const TDLConceptDataValue& expr ) override { value = getOneNoneLower(isBotEquivalent(expr.getDR())); }
771
772 // object role expressions
773 void visit ( const TDLObjectRoleTop& ) override { value = noLowerValue(); }
774 void visit ( const TDLObjectRoleBottom& ) override { value = anyLowerValue(); }
775 void visit ( const TDLObjectRoleInverse& expr ) override { value = getLowerBoundComplement(expr.getOR()); }
776 void visit ( const TDLObjectRoleChain& expr ) override
777 {
778 for ( const auto* arg : expr )
779 if ( isBotEquivalent(arg) )
780 {
781 value = anyLowerValue();
782 return;
783 }
784 value = noLowerValue();
785 }
786
787 // data role expressions
788 void visit ( const TDLDataRoleTop& ) override { value = noLowerValue(); }
789 void visit ( const TDLDataRoleBottom& ) override { value = anyLowerValue(); }
790
791 // data expressions
792 void visit ( const TDLDataTop& ) override { value = noLowerValue(); }
793 void visit ( const TDLDataBottom& ) override { value = anyLowerValue(); }
794 // FIXME!! not ready
795 //void visit ( const TDLDataTypeName& ) override { isBotEq = false; }
796 // FIXME!! not ready
797 //void visit ( const TDLDataTypeRestriction& ) override { isBotEq = false; }
798 void visit ( const TDLDataValue& ) override { value = 1; }
799 void visit ( const TDLDataNot& expr ) override { value = getLowerBoundDirect(expr.getExpr()); }
800 void visit ( const TDLDataAnd& expr ) override { value = getAndValue(expr); }
801 void visit ( const TDLDataOr& expr ) override { value = getOrValue(expr); }
802 void visit ( const TDLDataOneOf& ) override { value = noLowerValue(); }
803 }; // LowerBoundComplementEvaluator
804
805 /// implementation of evaluation
806 inline int
807 CardinalityEvaluatorBase :: getUpperBoundDirect ( const TDLExpression& expr ) { return UBD->getValue(expr); }
808 /// implementation of evaluation
809 inline int
810 CardinalityEvaluatorBase :: getUpperBoundComplement ( const TDLExpression& expr ) { return UBC->getValue(expr); }
811 /// implementation of evaluation
812 inline int
813 CardinalityEvaluatorBase :: getLowerBoundDirect ( const TDLExpression& expr ) { return LBD->getValue(expr); }
814 /// implementation of evaluation
815 inline int
816 CardinalityEvaluatorBase :: getLowerBoundComplement ( const TDLExpression& expr ) { return LBC->getValue(expr); }
817
818 /// syntactic locality checker for DL axioms
819 class ExtendedSyntacticLocalityChecker: public GeneralSyntacticLocalityChecker
820 {
821 protected: // members
822 UpperBoundDirectEvaluator UBD;
823 LowerBoundDirectEvaluator LBD;
824 UpperBoundComplementEvaluator UBC;
825 LowerBoundComplementEvaluator LBC;
826 #ifdef FPP_DEBUG_EXTENDED_LOCALITY
827 TLISPExpressionPrinter lp;
828 #endif
829
830 protected: // methods
831 /// @return true iff EXPR is top equivalent
832 bool isTopEquivalent ( const TDLExpression* expr ) override {
833 # ifdef FPP_DEBUG_EXTENDED_LOCALITY
834 std::cout << "Checking top locality of";
835 expr->accept(lp);
836 std::cout << "\n";
837 # endif
838 bool ret = UBC.getUpperBoundComplement(expr) == 0;
839 # ifdef FPP_DEBUG_EXTENDED_LOCALITY
840 std::cout << "top loc: " << ret << "\n";
841 # endif
842 return ret;
843 }
844 /// @return true iff EXPR is bottom equivalent
845 bool isBotEquivalent ( const TDLExpression* expr ) override {
846 # ifdef FPP_DEBUG_EXTENDED_LOCALITY
847 std::cout << "Checking bot locality of";
848 expr->accept(lp);
849 std::cout << "\n";
850 # endif
851 bool ret = UBD.getUpperBoundDirect(expr) == 0;
852 # ifdef FPP_DEBUG_EXTENDED_LOCALITY
853 std::cout << "bot loc: " << ret << "\n";
854 # endif
855 return ret;
856 }
857
858 public: // interface
859 /// init c'tor
860 explicit ExtendedSyntacticLocalityChecker ( const TSignature* s )
861 : GeneralSyntacticLocalityChecker(s)
862 , UBD(s)
863 , LBD(s)
864 , UBC(s)
865 , LBC(s)
866 # ifdef FPP_DEBUG_EXTENDED_LOCALITY
867 , lp(std::cout)
868 # endif
869 {
870 UBD.setEvaluators ( &UBD, &LBD, &UBC, &LBC );
871 LBD.setEvaluators ( &UBD, &LBD, &UBC, &LBC );
872 UBC.setEvaluators ( &UBD, &LBD, &UBC, &LBC );
873 LBC.setEvaluators ( &UBD, &LBD, &UBC, &LBC );
874 }
875 }; // ExtendedSyntacticLocalityChecker
876
877 #endif