Debian Codegraph @

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

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2011-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 SYNLOCCHECKER_H
21 #define SYNLOCCHECKER_H
22
23 #include "GeneralSyntacticLocalityChecker.h"
24
25 // forward declarations
26 class BotEquivalenceEvaluator;
27 class TopEquivalenceEvaluator;
28
29 /// check whether class expressions are equivalent to bottom wrt given locality class
30 class BotEquivalenceEvaluator: protected SigAccessor, public DLExpressionVisitorEmpty
31 {
32 protected: // members
33 /// corresponding top evaluator
34 TopEquivalenceEvaluator* TopEval;
35 /// keep the value here
36 bool isBotEq = false;
37
38 protected: // methods
39 /// check whether the expression is top-equivalent
40 bool isTopEquivalent ( const TDLExpression& expr );
41 /// convenience helper
42 bool isTopEquivalent ( const TDLExpression* expr ) { return isTopEquivalent(*expr); }
43
44 // non-empty Concept/Data expression
45
46 /// @return true iff C^I is non-empty
47 bool isBotDistinct ( const TDLExpression* C )
48 {
49 // TOP is non-empty
50 if ( isTopEquivalent(C) )
51 return true;
52 // built-in DT are non-empty
53 if ( dynamic_cast<const TDLDataTypeName*>(C) )
54 return true;
55 // FIXME!! that's it for now
56 return false;
57 }
58
59 // cardinality of a concept/data expression interpretation
60
61 /// @return true if #C^I > n
62 bool isCardLargerThan ( const TDLExpression* C, unsigned int n )
63 {
64 if ( n == 0 ) // non-empty is enough
65 return isBotDistinct(C);
66 // data top is infinite
67 if ( dynamic_cast<const TDLDataExpression*>(C) && isTopEquivalent(C) )
68 return true;
69 if ( const TDLDataTypeName* namedDT = dynamic_cast<const TDLDataTypeName*>(C) )
70 { // string/time are infinite DT
71 std::string name = namedDT->getName();
72 if ( name == TDataTypeManager::getStrTypeName() || name == TDataTypeManager::getTimeTypeName() )
73 return true;
74 }
75 // FIXME!! try to be more precise
76 return false;
77 }
78
79 // QCRs
80
81 /// @return true iff (>= n R.C) is botEq
82 bool isMinBotEquivalent ( unsigned int n, const TDLRoleExpression* R, const TDLExpression* C )
83 { return (n > 0) && (isBotEquivalent(R) || isBotEquivalent(C)); }
84 /// @return true iff (<= n R.C) is botEq
85 bool isMaxBotEquivalent ( unsigned int n, const TDLRoleExpression* R, const TDLExpression* C )
86 { return isTopEquivalent(R) && isCardLargerThan ( C, n ); }
87
88 public: // interface
89 /// init c'tor
90 explicit BotEquivalenceEvaluator ( const TSignature* s ) : SigAccessor(s) {}
91
92 // set fields
93
94 /// set the corresponding top evaluator
95 void setTopEval ( TopEquivalenceEvaluator* eval ) { TopEval = eval; }
96 /// @return true iff an EXPRession is equivalent to bottom wrt defined policy
97 bool isBotEquivalent ( const TDLExpression& expr )
98 {
99 expr.accept(*this);
100 return isBotEq;
101 }
102 /// @return true iff an EXPRession is equivalent to bottom wrt defined policy
103 bool isBotEquivalent ( const TDLExpression* expr ) { return isBotEquivalent(*expr); }
104
105 public: // visitor interface
106 // concept expressions
107 void visit ( const TDLConceptTop& ) override { isBotEq = false; }
108 void visit ( const TDLConceptBottom& ) override { isBotEq = true; }
109 void visit ( const TDLConceptName& expr ) override { isBotEq = !topCLocal() && nc(expr.getEntity()); }
110 void visit ( const TDLConceptNot& expr ) override { isBotEq = isTopEquivalent(expr.getC()); }
111 void visit ( const TDLConceptAnd& expr ) override
112 {
113 for ( const auto* arg : expr )
114 if ( isBotEquivalent(arg) ) // here isBotEq is true, so just return
115 return;
116 isBotEq = false;
117 }
118 void visit ( const TDLConceptOr& expr ) override
119 {
120 for ( const auto* arg : expr )
121 if ( !isBotEquivalent(arg) ) // here isBotEq is false, so just return
122 return;
123 isBotEq = true;
124 }
125 void visit ( const TDLConceptOneOf& expr ) override { isBotEq = expr.empty(); }
126 void visit ( const TDLConceptObjectSelf& expr ) override { isBotEq = isBotEquivalent(expr.getOR()); }
127 void visit ( const TDLConceptObjectValue& expr ) override { isBotEq = isBotEquivalent(expr.getOR()); }
128 void visit ( const TDLConceptObjectExists& expr ) override
129 { isBotEq = isMinBotEquivalent ( 1, expr.getOR(), expr.getC() ); }
130 void visit ( const TDLConceptObjectForall& expr ) override
131 { isBotEq = isTopEquivalent(expr.getOR()) && isBotEquivalent(expr.getC()); }
132 void visit ( const TDLConceptObjectMinCardinality& expr ) override
133 { isBotEq = isMinBotEquivalent ( expr.getNumber(), expr.getOR(), expr.getC() ); }
134 void visit ( const TDLConceptObjectMaxCardinality& expr ) override
135 { isBotEq = isMaxBotEquivalent ( expr.getNumber(), expr.getOR(), expr.getC() ); }
136 void visit ( const TDLConceptObjectExactCardinality& expr ) override
137 {
138 unsigned int n = expr.getNumber();
139 const TDLObjectRoleExpression* R = expr.getOR();
140 const TDLConceptExpression* C = expr.getC();
141 isBotEq = isMinBotEquivalent ( n, R, C ) || isMaxBotEquivalent ( n, R, C );
142 }
143 void visit ( const TDLConceptDataValue& expr ) override
144 { isBotEq = isBotEquivalent(expr.getDR()); }
145 void visit ( const TDLConceptDataExists& expr ) override
146 { isBotEq = isMinBotEquivalent ( 1, expr.getDR(), expr.getExpr() ); }
147 void visit ( const TDLConceptDataForall& expr ) override
148 { isBotEq = isTopEquivalent(expr.getDR()) && !isTopEquivalent(expr.getExpr()); }
149 void visit ( const TDLConceptDataMinCardinality& expr ) override
150 { isBotEq = isMinBotEquivalent ( expr.getNumber(), expr.getDR(), expr.getExpr() ); }
151 void visit ( const TDLConceptDataMaxCardinality& expr ) override
152 { isBotEq = isMaxBotEquivalent ( expr.getNumber(), expr.getDR(), expr.getExpr() ); }
153 void visit ( const TDLConceptDataExactCardinality& expr ) override
154 {
155 unsigned int n = expr.getNumber();
156 const TDLDataRoleExpression* R = expr.getDR();
157 const TDLDataExpression* D = expr.getExpr();
158 isBotEq = isMinBotEquivalent ( n, R, D ) || isMaxBotEquivalent ( n, R, D );
159 }
160
161 // object role expressions
162 void visit ( const TDLObjectRoleTop& ) override { isBotEq = false; }
163 void visit ( const TDLObjectRoleBottom& ) override { isBotEq = true; }
164 void visit ( const TDLObjectRoleName& expr ) override { isBotEq = !topRLocal() && nc(expr.getEntity()); }
165 void visit ( const TDLObjectRoleInverse& expr ) override { isBotEq = isBotEquivalent(expr.getOR()); }
166 void visit ( const TDLObjectRoleChain& expr ) override
167 {
168 isBotEq = true;
169 for ( const auto* arg : expr )
170 if ( isBotEquivalent(arg) ) // isBotEq is true here
171 return;
172 isBotEq = false;
173 }
174 // FaCT++ extension: equivalent to R(x,y) and C(x), so copy behaviour from ER.X
175 void visit ( const TDLObjectRoleProjectionFrom& expr ) override
176 { isBotEq = isMinBotEquivalent ( 1, expr.getOR(), expr.getC() ); }
177 // FaCT++ extension: equivalent to R(x,y) and C(y), so copy behaviour from ER.X
178 void visit ( const TDLObjectRoleProjectionInto& expr ) override
179 { isBotEq = isMinBotEquivalent ( 1, expr.getOR(), expr.getC() ); }
180
181 // data role expressions
182 void visit ( const TDLDataRoleTop& ) override { isBotEq = false; }
183 void visit ( const TDLDataRoleBottom& ) override { isBotEq = true; }
184 void visit ( const TDLDataRoleName& expr ) override { isBotEq = !topRLocal() && nc(expr.getEntity()); }
185
186 // data expressions
187 void visit ( const TDLDataTop& ) override { isBotEq = false; }
188 void visit ( const TDLDataBottom& ) override { isBotEq = true; }
189 void visit ( const TDLDataTypeName& ) override { isBotEq = false; }
190 void visit ( const TDLDataTypeRestriction& ) override { isBotEq = false; }
191 void visit ( const TDLDataValue& ) override { isBotEq = false; }
192 void visit ( const TDLDataNot& expr ) override { isBotEq = isTopEquivalent(expr.getExpr()); }
193 void visit ( const TDLDataAnd& expr ) override
194 {
195 for ( const auto* arg : expr )
196 if ( isBotEquivalent(arg) ) // here isBotEq is true, so just return
197 return;
198 isBotEq = false;
199 }
200 void visit ( const TDLDataOr& expr ) override
201 {
202 for ( const auto* arg : expr )
203 if ( !isBotEquivalent(arg) ) // here isBotEq is false, so just return
204 return;
205 isBotEq = true;
206 }
207 void visit ( const TDLDataOneOf& expr ) override { isBotEq = expr.empty(); }
208 }; // BotEquivalenceEvaluator
209
210 /// check whether class expressions are equivalent to top wrt given locality class
211 class TopEquivalenceEvaluator: protected SigAccessor, public DLExpressionVisitorEmpty
212 {
213 protected: // members
214 /// corresponding bottom evaluator
215 BotEquivalenceEvaluator* BotEval;
216 /// keep the value here
217 bool isTopEq = false;
218
219 protected: // methods
220 /// check whether the expression is top-equivalent
221 bool isBotEquivalent ( const TDLExpression& expr ) { return BotEval->isBotEquivalent(expr); }
222 /// convenience helper
223 bool isBotEquivalent ( const TDLExpression* expr ) { return isBotEquivalent(*expr); }
224
225 // non-empty Concept/Data expression
226
227 /// @return true iff C^I is non-empty
228 bool isBotDistinct ( const TDLExpression* C )
229 {
230 // TOP is non-empty
231 if ( isTopEquivalent(C) )
232 return true;
233 // built-in DT are non-empty
234 if ( dynamic_cast<const TDLDataTypeName*>(C) )
235 return true;
236 // FIXME!! that's it for now
237 return false;
238 }
239
240 // cardinality of a concept/data expression interpretation
241
242 /// @return true if #C^I > n
243 bool isCardLargerThan ( const TDLExpression* C, unsigned int n )
244 {
245 if ( n == 0 ) // non-empty is enough
246 return isBotDistinct(C);
247 // data top is infinite
248 if ( dynamic_cast<const TDLDataExpression*>(C) && isTopEquivalent(C) )
249 return true;
250 if ( const TDLDataTypeName* namedDT = dynamic_cast<const TDLDataTypeName*>(C) )
251 { // string/time are infinite DT
252 std::string name = namedDT->getName();
253 if ( name == TDataTypeManager::getStrTypeName() || name == TDataTypeManager::getTimeTypeName() )
254 return true;
255 }
256 // FIXME!! try to be more precise
257 return false;
258 }
259
260 // QCRs
261
262 /// @return true iff (>= n R.C) is topEq
263 bool isMinTopEquivalent ( unsigned int n, const TDLRoleExpression* R, const TDLExpression* C )
264 { return (n == 0) || ( isTopEquivalent(R) && isCardLargerThan ( C, n-1 ) ); }
265 /// @return true iff (<= n R.C) is topEq
266 bool isMaxTopEquivalent ( unsigned int, const TDLRoleExpression* R, const TDLExpression* C )
267 { return isBotEquivalent(R) || isBotEquivalent(C); }
268
269 public: // interface
270 /// init c'tor
271 explicit TopEquivalenceEvaluator ( const TSignature* s ) : SigAccessor(s) {}
272
273 // set fields
274
275 /// set the corresponding bottom evaluator
276 void setBotEval ( BotEquivalenceEvaluator* eval ) { BotEval = eval; }
277 /// @return true iff an EXPRession is equivalent to top wrt defined policy
278 bool isTopEquivalent ( const TDLExpression& expr )
279 {
280 expr.accept(*this);
281 return isTopEq;
282 }
283 /// @return true iff an EXPRession is equivalent to top wrt defined policy
284 bool isTopEquivalent ( const TDLExpression* expr ) { return isTopEquivalent(*expr); }
285
286 public: // visitor interface
287 // concept expressions
288 void visit ( const TDLConceptTop& ) override { isTopEq = true; }
289 void visit ( const TDLConceptBottom& ) override { isTopEq = false; }
290 void visit ( const TDLConceptName& expr ) override { isTopEq = topCLocal() && nc(expr.getEntity()); }
291 void visit ( const TDLConceptNot& expr ) override { isTopEq = isBotEquivalent(expr.getC()); }
292 void visit ( const TDLConceptAnd& expr ) override
293 {
294 for ( const auto* arg : expr )
295 if ( !isTopEquivalent(arg) ) // here isTopEq is false, so just return
296 return;
297 isTopEq = true;
298 }
299 void visit ( const TDLConceptOr& expr ) override
300 {
301 for ( const auto* arg : expr )
302 if ( isTopEquivalent(arg) ) // here isTopEq is true, so just return
303 return;
304 isTopEq = false;
305 }
306 void visit ( const TDLConceptOneOf& ) override { isTopEq = false; }
307 void visit ( const TDLConceptObjectSelf& expr ) override { isTopEq = isTopEquivalent(expr.getOR()); }
308 void visit ( const TDLConceptObjectValue& expr ) override { isTopEq = isTopEquivalent(expr.getOR()); }
309 void visit ( const TDLConceptObjectExists& expr ) override
310 { isTopEq = isMinTopEquivalent ( 1, expr.getOR(), expr.getC() ); }
311 void visit ( const TDLConceptObjectForall& expr ) override
312 { isTopEq = isTopEquivalent(expr.getC()) || isBotEquivalent(expr.getOR()); }
313 void visit ( const TDLConceptObjectMinCardinality& expr ) override
314 { isTopEq = isMinTopEquivalent ( expr.getNumber(), expr.getOR(), expr.getC() ); }
315 void visit ( const TDLConceptObjectMaxCardinality& expr ) override
316 { isTopEq = isMaxTopEquivalent ( expr.getNumber(), expr.getOR(), expr.getC() ); }
317 void visit ( const TDLConceptObjectExactCardinality& expr ) override
318 {
319 unsigned int n = expr.getNumber();
320 const TDLObjectRoleExpression* R = expr.getOR();
321 const TDLConceptExpression* C = expr.getC();
322 isTopEq = isMinTopEquivalent ( n, R, C ) && isMaxTopEquivalent ( n, R, C );
323 }
324 void visit ( const TDLConceptDataValue& expr ) override
325 { isTopEq = isTopEquivalent(expr.getDR()); }
326 void visit ( const TDLConceptDataExists& expr ) override
327 { isTopEq = isMinTopEquivalent ( 1, expr.getDR(), expr.getExpr() ); }
328 void visit ( const TDLConceptDataForall& expr ) override { isTopEq = isTopEquivalent(expr.getExpr()) || isBotEquivalent(expr.getDR()); }
329 void visit ( const TDLConceptDataMinCardinality& expr ) override
330 { isTopEq = isMinTopEquivalent ( expr.getNumber(), expr.getDR(), expr.getExpr() ); }
331 void visit ( const TDLConceptDataMaxCardinality& expr ) override
332 { isTopEq = isMaxTopEquivalent ( expr.getNumber(), expr.getDR(), expr.getExpr() ); }
333 void visit ( const TDLConceptDataExactCardinality& expr ) override
334 {
335 unsigned int n = expr.getNumber();
336 const TDLDataRoleExpression* R = expr.getDR();
337 const TDLDataExpression* D = expr.getExpr();
338 isTopEq = isMinTopEquivalent ( n, R, D ) && isMaxTopEquivalent ( n, R, D );
339 }
340
341 // object role expressions
342 void visit ( const TDLObjectRoleTop& ) override { isTopEq = true; }
343 void visit ( const TDLObjectRoleBottom& ) override { isTopEq = false; }
344 void visit ( const TDLObjectRoleName& expr ) override { isTopEq = topRLocal() && nc(expr.getEntity()); }
345 void visit ( const TDLObjectRoleInverse& expr ) override { isTopEq = isTopEquivalent(expr.getOR()); }
346 void visit ( const TDLObjectRoleChain& expr ) override
347 {
348 isTopEq = false;
349 for ( const auto* arg : expr )
350 if ( !isTopEquivalent(arg) ) // isTopEq is false here
351 return;
352 isTopEq = true;
353 }
354 // FaCT++ extension: equivalent to R(x,y) and C(x), so copy behaviour from ER.X
355 void visit ( const TDLObjectRoleProjectionFrom& expr ) override
356 { isTopEq = isMinTopEquivalent ( 1, expr.getOR(), expr.getC() ); }
357 // FaCT++ extension: equivalent to R(x,y) and C(y), so copy behaviour from ER.X
358 void visit ( const TDLObjectRoleProjectionInto& expr ) override
359 { isTopEq = isMinTopEquivalent ( 1, expr.getOR(), expr.getC() ); }
360
361 // data role expressions
362 void visit ( const TDLDataRoleTop& ) override { isTopEq = true; }
363 void visit ( const TDLDataRoleBottom& ) override { isTopEq = false; }
364 void visit ( const TDLDataRoleName& expr ) override { isTopEq = topRLocal() && nc(expr.getEntity()); }
365
366 // data expressions
367 void visit ( const TDLDataTop& ) override { isTopEq = true; }
368 void visit ( const TDLDataBottom& ) override { isTopEq = false; }
369 void visit ( const TDLDataTypeName& ) override { isTopEq = false; }
370 void visit ( const TDLDataTypeRestriction& ) override { isTopEq = false; }
371 void visit ( const TDLDataValue& ) override { isTopEq = false; }
372 void visit ( const TDLDataNot& expr ) override { isTopEq = isBotEquivalent(expr.getExpr()); }
373 void visit ( const TDLDataAnd& expr ) override
374 {
375 for ( const auto* arg : expr )
376 if ( !isTopEquivalent(arg) ) // here isTopEq is false, so just return
377 return;
378 isTopEq = true;
379 }
380 void visit ( const TDLDataOr& expr ) override
381 {
382 for ( const auto* arg : expr )
383 if ( isTopEquivalent(arg) ) // here isTopEq is true, so just return
384 return;
385 isTopEq = false;
386 }
387 void visit ( const TDLDataOneOf& ) override { isTopEq = false; }
388 }; // TopEquivalenceEvaluator
389
390 inline bool
391 BotEquivalenceEvaluator :: isTopEquivalent ( const TDLExpression& expr )
392 {
393 return TopEval->isTopEquivalent(expr);
394 }
395
396
397 /// syntactic locality checker for DL axioms
398 class SyntacticLocalityChecker: public GeneralSyntacticLocalityChecker
399 {
400 protected: // members
401 /// top evaluator
402 TopEquivalenceEvaluator TopEval;
403 /// bottom evaluator
404 BotEquivalenceEvaluator BotEval;
405
406 protected: // methods
407 /// @return true iff EXPR is top equivalent
408 bool isTopEquivalent ( const TDLExpression* expr ) override { return TopEval.isTopEquivalent(*expr); }
409 /// @return true iff EXPR is bottom equivalent
410 bool isBotEquivalent ( const TDLExpression* expr ) override { return BotEval.isBotEquivalent(*expr); }
411
412 public: // interface
413 /// init c'tor
414 explicit SyntacticLocalityChecker ( const TSignature* s )
415 : GeneralSyntacticLocalityChecker(s)
416 , TopEval(s)
417 , BotEval(s)
418 {
419 TopEval.setBotEval(&BotEval);
420 BotEval.setTopEval(&TopEval);
421 }
422 }; // SyntacticLocalityChecker
423
424 #endif