Debian Codegraph @

Kernel/tConcept.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 "tConcept.h"
21 #include "tRole.h"
22
23 void TConcept :: clear ( void )
24 {
25 // TNamedEntry clean
26 setId(0);
27 // ClassifiableEntry clean
28 taxVertex = nullptr;
29 toldSubsumers.clear();
30 setCompletelyDefined(false);
31 pSynonym = nullptr;
32 // TConcept clean
33 removeDescription();
34 setPrimitive();
35 pName = pBody = bpINVALID;
36 }
37
38 void TConcept :: addDesc ( DLTree* Desc )
39 {
40 fpp_assert (!isNonPrimitive()); // safety check
41
42 // FIXME!! check about reverse order
43 Description = createSNFAnd ( Desc, Description );
44 }
45
46 /// calculate value of classification TAG based on told subsumers. WARNING: no TS cycles allowed
47 CTTag TConcept :: determineClassTag ( void )
48 {
49 // for synonyms -- set tag as a primer's one
50 if ( isSynonym() )
51 return resolveSynonym(this)->getClassTag();
52
53 // check if it is non-primitive
54 if ( isNonPrimitive() )
55 return cttNonPrimitive;
56
57 // no told subsumers
58 if ( !hasToldSubsumers() )
59 return cttOrphan;
60
61 // now need to check all the told subsumers
62 bool hasLCD = false;
63 bool hasOther = false;
64 bool hasNP = false;
65
66 for ( auto& p: told() )
67 switch ( static_cast<TConcept*>(p)->getClassTag() )
68 {
69 case cttTrueCompletelyDefined:
70 break;
71 case cttOrphan:
72 case cttLikeCompletelyDefined:
73 hasLCD = true;
74 break;
75 case cttRegular:
76 hasOther = true;
77 break;
78 case cttHasNonPrimitiveTS:
79 case cttNonPrimitive:
80 hasNP = true;
81 break;
82 default:
83 fpp_unreachable();
84 }
85
86 // there are non-primitive TS
87 if ( hasNP )
88 return cttHasNonPrimitiveTS;
89
90 // has something different from CD-like ones (and not CD)
91 if ( hasOther || !isCompletelyDefined() )
92 return cttRegular;
93
94 // no more 'other' concepts here, and the CD-like structure
95 if ( hasLCD )
96 return cttLikeCompletelyDefined;
97
98 return cttTrueCompletelyDefined;
99 }
100
101 bool
102 TConcept :: hasSelfInDesc ( const DLTree* t ) const
103 {
104 if ( t == nullptr )
105 return false;
106
107 switch ( t->Element().getToken() )
108 {
109 case NAME: // if ID contains synonym of P
110 {
111 const ClassifiableEntry* name = static_cast<const ClassifiableEntry*>(t->Element().getNE());
112 return resolveSynonym(name) == this;
113 }
114 case AND:
115 return hasSelfInDesc(t->Left()) || hasSelfInDesc(t->Right());
116 case NOT: // a [= (not a) -> a [= BOTTOM; a [= (a or b) -> a [= TOP
117 switch ( t->Left()->Element().getToken() )
118 {
119 case AND:
120 case NAME:
121 return hasSelfInDesc(t->Left());
122 default:
123 return false;
124 }
125 default:
126 return false;
127 }
128 }
129
130 DLTree*
131 TConcept :: replaceSelfWithConst ( const DLTree* t ) const
132 {
133 if ( t == nullptr )
134 return nullptr;
135
136 switch ( t->Element().getToken() )
137 {
138 case NAME: // if ID contains synonym of P
139 {
140 const ClassifiableEntry* name = static_cast<const ClassifiableEntry*>(t->Element().getNE());
141 if ( resolveSynonym(name) == this )
142 return createTop();
143 else
144 break;
145 }
146 case AND: // a [= (and a b) -> a [= b
147 return createSNFAnd ( replaceSelfWithConst(t->Left()), replaceSelfWithConst(t->Right()) );
148 case NOT: // a [= (not a) -> a [= BOTTOM; a [= (a or b) -> a [= TOP
149 switch ( t->Left()->Element().getToken() )
150 {
151 case AND:
152 case NAME:
153 return createSNFNot(replaceSelfWithConst(t->Left()));
154 default:
155 break;
156 }
157 default:
158 break;
159 }
160 return clone(t);
161 }
162
163 /// init told subsumers of the concept by given DESCription; @return TRUE iff concept is CD
164 bool TConcept :: initToldSubsumers ( const DLTree* desc, RoleSSet& RolesProcessed )
165 {
166 // no description => nothing to do (and yes, it is told)
167 if ( desc == nullptr )
168 return true;
169
170 switch ( desc->Element().getToken() )
171 {
172 case TOP: // the 1st node
173 return true;
174 case NAME: // it is a concept ID
175 return addToldSubsumer(static_cast<TConcept*>(desc->Element().getNE()));
176 case AND: // add TS from BOTH parts of AND
177 return initToldSubsumers ( desc->Left(), RolesProcessed ) & initToldSubsumers ( desc->Right(), RolesProcessed );
178 case NOT: // Domains from \ER.C and (>= n R.C) are told concepts
179 {
180 const TLexeme& cur = desc->Left()->Element();
181
182 if ( cur.getToken() == FORALL || cur.getToken() == LE )
183 SearchTSbyRoleAndSupers ( resolveRole(desc->Left()->Left()), RolesProcessed );
184
185 return false;
186 }
187 case SELF: // Domains and Range from participating role
188 {
189 const TRole* R = resolveRole(desc->Left());
190 SearchTSbyRoleAndSupers ( R, RolesProcessed );
191 SearchTSbyRoleAndSupers ( R->inverse(), RolesProcessed );
192 return false;
193 }
194 default: // not told one
195 return false;
196 }
197 }
198
199 void TConcept :: SearchTSbyRole ( const TRole* R, RoleSSet& RolesProcessed )
200 {
201 const DLTree* Domain = R->getTDomain();
202 if ( Domain == nullptr || isConst(Domain) )
203 return;
204
205 // don't process the same role twice
206 if ( RolesProcessed.find(R) != RolesProcessed.end() )
207 return;
208
209 // mark role as processed; usually it's the only role, so set hint as a begin()
210 RolesProcessed.insert ( RolesProcessed.begin(), R );
211
212 // init TS by the domain of role
213 initToldSubsumers ( Domain, RolesProcessed ); // don't bother about result
214 }
215
216 void TConcept :: SearchTSbyRoleAndSupers ( const TRole* r, RoleSSet& RolesProcessed )
217 {
218 SearchTSbyRole ( r, RolesProcessed );
219
220 // do the same for all super-roles if necessary
221 // FIXME!! need to do the same for DomSupers (like SoR [= R)
222 for ( const auto& sup: r->ancestors() )
223 SearchTSbyRole ( sup, RolesProcessed );
224 }
225
226 unsigned int TConcept :: calculateTSDepth ( void )
227 {
228 if ( tsDepth > 0 )
229 return tsDepth;
230
231 unsigned int max = 0;
232
233 for ( auto& p: told() )
234 {
235 unsigned int cur = static_cast<TConcept*>(p)->calculateTSDepth();
236 if ( max < cur )
237 max = cur;
238 }
239
240 return (tsDepth = max+1);
241 }