Debian Codegraph @

FaCT++.Python/lib/Kernel/dltree.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 DLTREE_H
21 #define DLTREE_H
22
23 #include <vector>
24 #include <iosfwd>
25
26 #include "globaldef.h"
27 #include "tLexeme.h"
28 #include "tsttree.h"
29
30 typedef TsTTree <TLexeme> DLTree;
31 class TRole;
32
33 // checking if the tree is in Simplified Normal Form
34 extern bool isSNF ( const DLTree* t );
35
36 // checks if two trees are the same (syntactically)
37 extern bool equalTrees ( const DLTree* t1, const DLTree* t2 );
38 // check whether t1=(and c1..cn), t2 = (and d1..dm) and ci = dj for all i
39 extern bool isSubTree ( const DLTree* t1, const DLTree* t2 );
40
41 // builds a copy of the formula t
42 inline DLTree* clone ( const DLTree* t ) { return (t==nullptr) ? nullptr : t->clone(); }
43
44 // check if DL tree is a concept constant
45 inline bool isConst ( const DLTree* t )
46 {
47 switch (t->Element().getToken())
48 {
49 case TOP:
50 case BOTTOM:
51 return true;
52 default:
53 return false;
54 }
55 }
56
57 // check if DL tree is a concept/individual name
58 inline bool isName ( const DLTree* t )
59 {
60 switch (t->Element().getToken())
61 {
62 case CNAME:
63 case INAME:
64 return true;
65 default:
66 return false;
67 }
68 }
69
70 // check if DL tree is a (data)role name
71 inline bool isRName ( const DLTree* t )
72 {
73 switch (t->Element().getToken())
74 {
75 case RNAME:
76 case DNAME:
77 return true;
78 default:
79 return false;
80 }
81 }
82
83 // check if DL tree is a concept-like name
84 inline bool isCN ( const DLTree* t )
85 {
86 switch (t->Element().getToken())
87 {
88 case TOP:
89 case BOTTOM:
90 case CNAME:
91 case INAME:
92 return true;
93 default:
94 return false;
95 }
96 }
97 /// check whether T is a bottom (empty) role
98 inline bool isBotRole ( const DLTree* t ) { return likely(isRName(t)) && unlikely(t->Element().getNE()->isBottom()); }
99 /// check whether T is a top (universal) role
100 inline bool isTopRole ( const DLTree* t ) { return likely(isRName(t)) && unlikely(t->Element().getNE()->isTop()); }
101
102 /// check whether T is an expression in the form (atmost 1 RNAME)
103 inline bool isFunctionalExpr ( const DLTree* t, const TNamedEntry* R )
104 {
105 return t->Element().getToken() == LE && R == t->Left()->Element().getNE() &&
106 t->Element().getData() == 1 && t->Right()->Element().getToken() == TOP;
107 }
108
109 // check if DL Tree represents negated ONE-OF constructor
110 inline bool isNegOneOf ( const DLTree* t )
111 {
112 switch (t->Element().getToken())
113 {
114 case AND:
115 return isNegOneOf(t->Left()) && isNegOneOf(t->Right());
116 case NOT:
117 return t->Left()->Element().getToken() == INAME;
118 default:
119 return false;
120 }
121 }
122 // check if DL Tree represents ONE-OF constructor
123 inline bool isOneOf ( const DLTree* t )
124 {
125 switch (t->Element().getToken())
126 {
127 case INAME:
128 return true;
129 case NOT:
130 return isNegOneOf(t->Left());
131 default:
132 return false;
133 }
134 }
135
136 // create SNF from given parts
137
138 /// create TOP element
139 inline DLTree* createTop ( void ) { return new DLTree(TLexeme(TOP)); }
140 /// create BOTTOM element
141 inline DLTree* createBottom ( void ) { return new DLTree(TLexeme(BOTTOM)); }
142
143 /// create a tree with tag TAG and an entry ENTRY
144 inline DLTree* createEntry ( Token tag, TNamedEntry* entry ) { return new DLTree ( TLexeme ( tag, entry ) ); }
145
146 /// create entry for the role R
147 extern DLTree* createRole ( TRole* R );
148 /// create inverse of role R
149 extern DLTree* createInverse ( DLTree* R );
150
151 /// create negation of given formula
152 extern DLTree* createSNFNot ( DLTree* C );
153 /// create conjunction of given formulas
154 extern DLTree* createSNFAnd ( DLTree* C, DLTree* D );
155 /// create conjunction of given formulas; aggressively reduce for the case C = (and D ...)
156 extern DLTree* createSNFReducedAnd ( DLTree* C, DLTree* D );
157 /// create disjunction of given formulas
158 inline DLTree* createSNFOr ( DLTree* C, DLTree* D )
159 { // C\or D -> \not(\not C\and\not D)
160 return createSNFNot ( createSNFAnd ( createSNFNot(C), createSNFNot(D) ) );
161 }
162 /// create universal restriction of given formulas (\AR.C)
163 extern DLTree* createSNFForall ( DLTree* R, DLTree* C );
164 /// create existential restriction of given formulas (\ER.C)
165 inline DLTree* createSNFExists ( DLTree* R, DLTree* C )
166 { // \ER.C -> \not\AR.\not C
167 return createSNFNot ( createSNFForall ( R, createSNFNot(C) ) );
168 }
169 /// create SELF restriction for role R
170 inline DLTree* createSNFSelf ( DLTree* R )
171 {
172 if ( unlikely(isBotRole(R)) )
173 return createBottom(); // loop on bottom role is always unsat
174 if ( unlikely(isTopRole(R)) )
175 return createTop(); // top role is reflexive
176 return new DLTree ( TLexeme(SELF), R );
177 }
178
179 /// create at-least (GE) restriction of given formulas (>= n R.C)
180 extern DLTree* createSNFGE ( unsigned int n, DLTree* R, DLTree* C );
181 /// create at-most (LE) restriction of given formulas (<= n R.C)
182 extern DLTree* createSNFLE ( unsigned int n, DLTree* R, DLTree* C );
183
184 // prints formula
185
186 extern const char* TokenName ( Token t );
187 extern std::ostream& operator << ( std::ostream& o, const DLTree *form );
188
189 /// helper that deletes temporary trees
190 class TreeDeleter
191 {
192 protected:
193 DLTree* ptr;
194 public:
195 explicit TreeDeleter ( DLTree* p ) : ptr(p) {}
196 ~TreeDeleter() { deleteTree(ptr); }
197 operator DLTree* ( void ) { return ptr; }
198 operator const DLTree* ( void ) const { return ptr; }
199 }; // TreeDeleter
200
201 #endif