Debian Codegraph @

FaCT++.Python/lib/FaCT++/parser.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 program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include "parser.h"
21
22 /********************* DLParser implementation ***************************/
23
24 void DLLispParser :: Parse ( void )
25 {
26 while ( Current != LEXEOF )
27 parseCommand ();
28 }
29
30 void DLLispParser :: parseCommand ( void )
31 {
32 MustBeM (LBRACK);
33 MustBe(ID);
34 LispToken t = scan.getCommandKeyword();
35 NextLex ();
36
37 switch (t)
38 {
39 case SUBSUMES:
40 case EQUAL_C:
41 {
42 TConceptExpr* left = getConceptExpression();
43 TConceptExpr* right = getConceptExpression();
44
45 try
46 {
47 if ( t == SUBSUMES )
48 Kernel->impliesConcepts ( left, right );
49 else
50 {
51 EManager->newArgList();
52 EManager->addArg(left);
53 EManager->addArg(right);
54 Kernel->equalConcepts();
55 }
56 }
57 catch ( const EFaCTPlusPlus& ex )
58 {
59 parseError(ex.what());
60 }
61 break;
62 }
63
64 case IMPLIES_R:
65 case EQUAL_R:
66 case DISJOINT_R:
67 case INVERSE:
68 {
69 TRoleExpr* R = (t == IMPLIES_R) ? getComplexRoleExpression() : getRoleExpression();
70 if ( isDataRole(R) )
71 tellRoleAxiom ( t, dynamic_cast<TDRoleExpr*>(R), getDRoleExpression() );
72 else if ( t != IMPLIES_R )
73 tellRoleAxiom ( t, dynamic_cast<TORoleExpr*>(R), getORoleExpression() );
74 else // implies_R
75 try
76 {
77 Kernel->impliesORoles ( dynamic_cast<TORoleComplexExpr*>(R), getORoleExpression() );
78 }
79 catch ( const EFaCTPlusPlus& ex )
80 {
81 parseError(ex.what());
82 }
83 break;
84 }
85
86 case FUNCTIONAL:
87 case TRANSITIVE:
88 case REFLEXIVE:
89 case IRREFLEXIVE:
90 case SYMMETRIC:
91 case ASYMMETRIC:
92 {
93 TRoleExpr* R = getRoleExpression();
94 if ( isDataRole(R) )
95 tellRoleAxiom ( t, dynamic_cast<TDRoleExpr*>(R), nullptr );
96 else
97 tellRoleAxiom ( t, dynamic_cast<TORoleExpr*>(R), nullptr );
98 break;
99 }
100
101 case ROLERANGE:
102 case ROLEDOMAIN:
103 {
104 TRoleExpr* R = getRoleExpression();
105 try
106 {
107 if ( isDataRole(R) )
108 {
109 TDRoleExpr* S = dynamic_cast<TDRoleExpr*>(R);
110 if ( t == ROLERANGE )
111 Kernel->setDRange ( S, getDataExpression() );
112 else
113 Kernel->setDDomain ( S, getConceptExpression() );
114 }
115 else
116 {
117 TORoleExpr* S = dynamic_cast<TORoleExpr*>(R);
118 if ( t == ROLERANGE )
119 Kernel->setORange ( S, getConceptExpression() );
120 else
121 Kernel->setODomain ( S, getConceptExpression() );
122 }
123 }
124 catch ( const EFaCTPlusPlus& ex )
125 {
126 parseError(ex.what());
127 }
128 break;
129 }
130
131 // disjoint-like operator
132 case DISJOINT:
133 case FAIRNESS:
134 case DIFFERENT:
135 case SAME:
136 parseConceptList ( /*singletonsOnly=*/ (t != DISJOINT) && (t != FAIRNESS) );
137 try
138 {
139 switch (t)
140 {
141 case DISJOINT:
142 Kernel->disjointConcepts();
143 return; // already read ')'
144 case FAIRNESS:
145 Kernel->setFairnessConstraint();
146 return; // already read ')'
147 case SAME:
148 Kernel->processSame();
149 return; // already read ')'
150 case DIFFERENT:
151 Kernel->processDifferent();
152 return; // already read ')'
153 default:
154 fpp_unreachable();
155 }
156 }
157 catch ( const EFaCTPlusPlus& ex )
158 {
159 parseError(ex.what());
160 }
161 break;
162
163 case PCONCEPT:
164 {
165 TConceptExpr* Name = getConcept();
166 if ( Current != RBRACK )
167 Kernel->impliesConcepts ( Name, getConceptExpression() );
168 else
169 Kernel->declare(Name);
170 break;
171 }
172
173 case CONCEPT:
174 EManager->newArgList();
175 EManager->addArg(getConcept());
176 EManager->addArg(getConceptExpression());
177 Kernel->equalConcepts();
178 break;
179
180 case DEFINDIVIDUAL: // just register singleton
181 Kernel->declare(getSingleton());
182 break;
183
184 case INSTANCE:
185 {
186 TIndividualExpr* Name = getSingleton();
187 Kernel->instanceOf ( Name, getConceptExpression() );
188 break;
189 }
190
191 case RELATED: // command is (Related id1 R id2);
192 try
193 {
194 TIndividualExpr* id1 = getSingleton();
195 TORoleExpr* R = getORoleExpression();
196 MustBe (ID); // second individual
197 TIndividualExpr* id2 = getSingleton();
198 Kernel->relatedTo ( id1, R, id2 );
199 }
200 catch ( const EFaCTPlusPlus& ex )
201 {
202 parseError(ex.what());
203 }
204 break;
205
206 case VALUEOF: // command is (valueOf id1 R data);
207 try
208 {
209 TIndividualExpr* id1 = getSingleton();
210 TDRoleExpr* R = getDRoleExpression();
211 TDataValueExpr* data = dynamic_cast<TDataValueExpr*>(getDataExpression());
212 Kernel->valueOf(id1, R, data);
213 }
214 catch (const EFaCTPlusPlus& ex)
215 {
216 parseError(ex.what());
217 }
218 break;
219
220 case PROLE:
221 case PATTR:
222 {
223 TORoleExpr* Name = getObjectRole();
224
225 if ( t == PATTR )
226 try
227 {
228 Kernel->setOFunctional(Name);
229 }
230 catch ( const EFaCTPlusPlus& ex )
231 {
232 parseError(ex.what());
233 }
234
235 if ( Current != RBRACK )
236 parseRoleArguments(Name);
237 else
238 Kernel->declare(Name);
239 break;
240 }
241
242 case DATAROLE: // register data role
243 Kernel->declare(getDataRole());
244 break;
245
246 default:
247 parseError ( "Unrecognised command" );
248 }
249
250 MustBeM ( RBRACK ); // skip bracket
251 }
252
253 /// generate object role axiom between R and S according to the operation TAG
254 void
255 DLLispParser :: tellRoleAxiom ( LispToken tag, TORoleExpr* R, TORoleExpr* S )
256 {
257 try
258 {
259 switch(tag)
260 {
261 case INVERSE:
262 Kernel->setInverseRoles ( R, S );
263 break;
264
265 case DISJOINT_R:
266 case EQUAL_R:
267 EManager->newArgList();
268 EManager->addArg(R);
269 EManager->addArg(S);
270 if ( tag == DISJOINT_R )
271 Kernel->disjointORoles();
272 else
273 Kernel->equalORoles();
274 break;
275
276 case FUNCTIONAL:
277 Kernel->setOFunctional(R);
278 break;
279 case TRANSITIVE:
280 Kernel->setTransitive(R);
281 break;
282 case REFLEXIVE:
283 Kernel->setReflexive(R);
284 break;
285 case IRREFLEXIVE:
286 Kernel->setIrreflexive(R);
287 break;
288 case SYMMETRIC:
289 Kernel->setSymmetric(R);
290 break;
291 case ASYMMETRIC:
292 Kernel->setAsymmetric(R);
293 break;
294
295 default:
296 parseError("Unrecognised object role command");
297 }
298 }
299 catch ( const EFaCTPlusPlus& ex )
300 {
301 parseError(ex.what());
302 }
303 }
304 /// generate data role axiom between R and S according to the operation TAG
305 void
306 DLLispParser :: tellRoleAxiom ( LispToken tag, TDRoleExpr* R, TDRoleExpr* S )
307 {
308 try
309 {
310 switch(tag)
311 {
312 case IMPLIES_R:
313 Kernel->impliesDRoles ( R, S );
314 break;
315
316 case DISJOINT_R:
317 case EQUAL_R:
318 EManager->newArgList();
319 EManager->addArg(R);
320 EManager->addArg(S);
321 if ( tag == DISJOINT_R )
322 Kernel->disjointDRoles();
323 else
324 Kernel->equalDRoles();
325 break;
326
327 case FUNCTIONAL:
328 Kernel->setDFunctional(R);
329 break;
330
331 default:
332 parseError("Unrecognised data role command");
333 }
334 }
335 catch ( const EFaCTPlusPlus& ex )
336 {
337 parseError(ex.what());
338 }
339 }
340
341 void DLLispParser :: parseConceptList ( bool singletonsOnly )
342 {
343 EManager->newArgList();
344
345 // continue with all concepts
346 while ( Current != RBRACK )
347 if ( singletonsOnly )
348 EManager->addArg(getSingleton());
349 else
350 EManager->addArg(getConceptExpression());
351
352 // skip RBRACK
353 MustBeM (RBRACK);
354 }
355
356 void DLLispParser :: parseRoleArguments ( TORoleExpr* R )
357 {
358 while ( Current != RBRACK )
359 if ( scan.isKeyword ("parents") || scan.isKeyword ("supers") )
360 { // followed by a list of parent role names
361 NextLex ();
362 MustBeM ( LBRACK );
363
364 while ( Current != RBRACK )
365 {
366 try
367 { // only object roles can have arguments
368 Kernel->impliesORoles ( R, getORoleExpression() );
369 }
370 catch ( const EFaCTPlusPlus& ex )
371 {
372 parseError(ex.what());
373 }
374 }
375 NextLex (); // skip last RBRACK
376 }
377 else if ( scan.isKeyword ("transitive") )
378 { // followed by NIL or (usually!) T
379 NextLex ();
380
381 try
382 {
383 Kernel->setTransitive(R);
384 }
385 catch ( const EFaCTPlusPlus& ex )
386 {
387 parseError(ex.what());
388 }
389
390 NextLex (); // skip token
391 }
392 else
393 parseError ( "use either :parents or :transitive command in role description" );
394 }
395
396 DLLispParser::TConceptExpr*
397 DLLispParser :: getConceptExpression ( void )
398 {
399 switch ( Code() )
400 {
401 case LBRACK: // complex description
402 return getComplexConceptExpression ();
403 case NUM: // numbers in concept expressions are constant names
404 return getConcept();
405 case ID:
406 {
407 switch ( scan.getNameKeyword() )
408 { // Top/Bottom or real ID
409 case L_TOP: NextLex(); return EManager->Top();
410 case L_BOTTOM: NextLex(); return EManager->Bottom();
411 default: return getConcept();
412 }
413 }
414 default: // else -- report syntax error
415 MustBe(ID);
416 return nullptr;
417 }
418 }
419
420 DLLispParser::TConceptExpr*
421 DLLispParser :: getComplexConceptExpression ( void )
422 {
423 MustBeM ( LBRACK );
424 LispToken T = scan.getExpressionKeyword();
425 unsigned int n = 0; // number for >= (<=) expression (or just 0)
426 TRoleExpr* R = nullptr;
427 TConceptExpr* C = nullptr;
428
429 NextLex ();
430
431 switch (T)
432 {
433 case L_GE:
434 case L_LE:
435 n = (unsigned int)scan.GetNumber();
436 NextLex ();
437 // fallthrough
438 case L_FORALL:
439 case L_EXISTS:
440 // first argument -- role name
441 R = getRoleExpression();
442 if ( isDataRole(R) ) // data expression
443 {
444 TDRoleExpr* A = dynamic_cast<TDRoleExpr*>(R);
445 TDataExpr* E = nullptr;
446
447 // second argument -- data expression
448 if ( Current == RBRACK )
449 E = EManager->DataTop(); // for (GE n R)
450 else
451 E = getDataExpression();
452
453 // skip right bracket
454 MustBeM(RBRACK);
455
456 if ( T == L_EXISTS )
457 return EManager->Exists ( A, E );
458 else if ( T == L_FORALL )
459 return EManager->Forall ( A, E );
460 else if ( T == L_GE )
461 return EManager->MinCardinality ( n, A, E );
462 else
463 return EManager->MaxCardinality ( n, A, E );
464 }
465 else
466 {
467 TORoleExpr* S = dynamic_cast<TORoleExpr*>(R);
468
469 // second argument -- data expression
470 if ( Current == RBRACK )
471 C = EManager->Top(); // for (GE n R)
472 else
473 C = getConceptExpression();
474
475 // skip right bracket
476 MustBeM(RBRACK);
477
478 if ( T == L_EXISTS )
479 return EManager->Exists ( S, C );
480 else if ( T == L_FORALL )
481 return EManager->Forall ( S, C );
482 else if ( T == L_GE )
483 return EManager->MinCardinality ( n, S, C );
484 else
485 return EManager->MaxCardinality ( n, S, C );
486 }
487
488 case REFLEXIVE: // self-reference
489 R = getORoleExpression();
490 // skip right bracket
491 MustBeM(RBRACK);
492 return EManager->SelfReference(dynamic_cast<TORoleExpr*>(R));
493
494 case L_NOT:
495 C = getConceptExpression();
496 // skip right bracket
497 MustBeM(RBRACK);
498 return EManager->Not(C);
499
500 case L_AND:
501 case L_OR: // multiple And's/Or's
502 EManager->newArgList();
503 do
504 {
505 EManager->addArg(getConceptExpression());
506 } while ( Current != RBRACK );
507
508 // list is parsed here
509 NextLex(); // skip ')'
510 return T == L_AND ? EManager->And() : EManager->Or();
511
512 case ONEOF:
513 parseConceptList(/*singletonsOnly=*/true);
514 return EManager->OneOf();
515
516 default: // error
517 parseError ( "Unknown concept constructor" );
518 return nullptr; // FSCO
519 }
520 }
521
522 DLLispParser::TDRoleExpr*
523 DLLispParser :: getDRoleExpression ( void )
524 {
525 MustBe ( ID, "Data role name expected" );
526 return getDataRole();
527 }
528
529 DLLispParser::TORoleExpr*
530 DLLispParser :: getORoleExpression ( void )
531 {
532 if ( Current != LBRACK )
533 return getObjectRole();
534
535 NextLex(); // skip '('
536 if ( scan.getExpressionKeyword() != L_INV )
537 MustBe ( L_INV, "only role names and their inverses are allowed as a role expression" );
538 NextLex(); // skip INV
539 TORoleExpr* ret = EManager->Inverse(getORoleExpression());
540 MustBeM(RBRACK);
541 return ret;
542 }
543
544 DLLispParser::TRoleExpr*
545 DLLispParser :: getRoleExpression ( void )
546 {
547 if ( Current != LBRACK )
548 return getRole();
549
550 NextLex(); // skip '('
551 if ( scan.getExpressionKeyword() != L_INV )
552 MustBe ( L_INV, "only role names and their inverses are allowed as a role expression" );
553 NextLex(); // skip INV
554 TORoleExpr* ret = EManager->Inverse(getORoleExpression());
555 MustBeM(RBRACK);
556 return ret;
557 }
558
559 DLLispParser::TRoleExpr*
560 DLLispParser :: getComplexRoleExpression ( void )
561 {
562 if ( Current != LBRACK )
563 return getRole();
564
565 NextLex(); // skip '('
566 LispToken keyword = scan.getExpressionKeyword();
567 NextLex(); // skip keyword
568 TORoleComplexExpr* ret = nullptr;
569 TORoleExpr* R;
570 switch ( keyword )
571 {
572 case L_INV: // inverse of a simple role
573 ret = EManager->Inverse(getORoleExpression());
574 break;
575 case L_RCOMPOSITION: // role composition expression = list of simple roles
576 EManager->newArgList();
577 while ( Current != RBRACK )
578 EManager->addArg(getORoleExpression());
579 ret = EManager->Compose();
580 break;
581 case L_PROJINTO: // role projection operator, parse simple role and concept
582 R = getORoleExpression();
583 ret = EManager->ProjectInto ( R, getConceptExpression() );
584 break;
585 case L_PROJFROM: // role projection operator, parse simple role and concept
586 R = getORoleExpression();
587 ret = EManager->ProjectFrom ( R, getConceptExpression() );
588 break;
589 default:
590 MustBe ( L_INV, "unknown expression in complex role constructor" );
591 }
592
593 MustBeM(RBRACK);
594 return ret;
595 }
596
597 DLLispParser::TDataExpr*
598 DLLispParser :: getDataExpression ( void )
599 {
600 // check for TOP/BOTTOM
601 if ( Code() == ID )
602 {
603 switch ( scan.getNameKeyword() )
604 { // Top/Bottom; can not be name
605 case L_TOP: NextLex(); return EManager->DataTop();
606 case L_BOTTOM: NextLex(); return EManager->DataBottom();
607 default: parseError ( "Unknown data constructor" ); return nullptr;
608 }
609 }
610
611 MustBeM(LBRACK); // always complex expression
612 LispToken T = scan.getExpressionKeyword();
613
614 NextLex ();
615
616 switch (T)
617 {
618 case DTGT: // facet ">"
619 {
620 TDataValueExpr* value = dynamic_cast<TDataValueExpr*>(getDataExpression());
621 MustBeM(RBRACK);
622 if ( value == nullptr )
623 parseError("Data value expected");
624 // the type of the expression is taken from value; it is basic type, so it is safe to make it RW
625 TDataTypeExpr* type = const_cast<TDataTypeExpr*>(value->getExpr());
626 return EManager->RestrictedType ( type, EManager->FacetMinExclusive(value) );
627 }
628 case DTGE: // facet ">="
629 {
630 TDataValueExpr* value = dynamic_cast<TDataValueExpr*>(getDataExpression());
631 MustBeM(RBRACK);
632 if ( value == nullptr )
633 parseError("Data value expected");
634 // the type of the expression is taken from value; it is basic type, so it is safe to make it RW
635 TDataTypeExpr* type = const_cast<TDataTypeExpr*>(value->getExpr());
636 return EManager->RestrictedType ( type, EManager->FacetMinInclusive(value) );
637 }
638 case DTLT: // facet "<"
639 {
640 TDataValueExpr* value = dynamic_cast<TDataValueExpr*>(getDataExpression());
641 MustBeM(RBRACK);
642 if ( value == nullptr )
643 parseError("Data value expected");
644 // the type of the expression is taken from value; it is basic type, so it is safe to make it RW
645 TDataTypeExpr* type = const_cast<TDataTypeExpr*>(value->getExpr());
646 return EManager->RestrictedType ( type, EManager->FacetMaxExclusive(value) );
647 }
648 case DTLE: // facet "<="
649 {
650 TDataValueExpr* value = dynamic_cast<TDataValueExpr*>(getDataExpression());
651 MustBeM(RBRACK);
652 if ( value == nullptr )
653 parseError("Data value expected");
654 // the type of the expression is taken from value; it is basic type, so it is safe to make it RW
655 TDataTypeExpr* type = const_cast<TDataTypeExpr*>(value->getExpr());
656 return EManager->RestrictedType ( type, EManager->FacetMaxInclusive(value) );
657 }
658 case L_NOT:
659 {
660 TDataExpr* expr = getDataExpression();
661 // skip right bracket
662 MustBeM ( RBRACK );
663 return EManager->DataNot(expr);
664 }
665 case DONEOF:
666 case L_AND:
667 case L_OR: // multiple And's/Or's
668 EManager->newArgList();
669 do
670 {
671 EManager->addArg(getDataExpression());
672 } while ( Current != RBRACK );
673
674 // list is parsed here
675 NextLex(); // skip ')'
676 return T == L_AND ? EManager->DataAnd() : T == L_OR ? EManager->DataOr() : EManager->DataOneOf();
677
678 case STRING: // expression (string <value>)
679 case NUMBER: // expression (number <value>)
680 case REAL: // expression (real <value>)
681 case BOOL:
682 {
683 TDataTypeExpr* type =
684 (T == STRING) ? EManager->getStrDataType():
685 (T == NUMBER) ? EManager->getIntDataType():
686 (T == REAL ) ? EManager->getRealDataType():
687 EManager->getBoolDataType();
688
689 if ( Current == RBRACK ) // just datatype
690 {
691 NextLex();
692 return type;
693 }
694
695 NextLex();
696 return getDTValue(type);
697 }
698
699 default: // error
700 parseError ( "Unknown data constructor" );
701 return nullptr; // FSCO
702 }
703 }