Debian Codegraph @

FaCT++.Python/lib/Kernel/tConcept.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 TCONCEPT_H
21 #define TCONCEPT_H
22
23 #include <set>
24
25 #include "taxNamEntry.h"
26 #include "tNameSet.h"
27 #include "tLabeller.h"
28 #include "dltree.h"
29 #include "dlVertex.h"
30 #include "LogicFeature.h"
31
32 /// type of concept wrt classifiability
33 enum CTTag
34 {
35 /// not specified
36 cttUnspecified,
37 /// concept with all parents -- TCD
38 cttTrueCompletelyDefined,
39 /// concept w/o any told subsumers
40 cttOrphan,
41 /// concept with all parents -- LCD, TCD or Orphans
42 cttLikeCompletelyDefined,
43 /// concept with non-primitive TS
44 cttHasNonPrimitiveTS,
45 /// any other primitive concept
46 cttRegular,
47 /// any non-primitive concept (except synonyms)
48 cttNonPrimitive,
49 };
50
51 inline char getCTTagName ( CTTag tag )
52 {
53 switch(tag)
54 {
55 case cttUnspecified: return 'u';
56 case cttTrueCompletelyDefined: return 'T';
57 case cttOrphan: return 'O';
58 case cttLikeCompletelyDefined: return 'L';
59 case cttHasNonPrimitiveTS: return 'N';
60 case cttRegular: return 'r';
61 case cttNonPrimitive: return 'n';
62 default: return char();
63 }
64 }
65
66 class TRole;
67 class SaveLoadManager;
68
69 /// class for representing concept-like entries
70 class TConcept: public ClassifiableEntry
71 {
72 private: // members
73 /// label to use in relevant-only checks
74 TLabeller::LabelType rel = 0;
75
76 protected: // types
77 /// set of extra rules
78 typedef std::vector<size_t> ERSet;
79 /// set of roles
80 typedef std::set<const TRole*> RoleSSet;
81
82 public: // type interface
83 /// extra rules iterators
84 typedef ERSet::const_iterator er_iterator;
85
86 public: // members
87 /// description of a concept
88 DLTree* Description = nullptr;
89 /// classification type of concept: completely defined (true- or like-), no TS, other
90 CTTag classTag = cttUnspecified;
91 /// depth of the concept wrt told subsumers
92 unsigned int tsDepth = 0;
93
94 /// pointer to the entry in DAG with concept name
95 BipolarPointer pName = bpINVALID;
96 /// pointer to the entry in DAG with concept definition
97 BipolarPointer pBody = bpINVALID;
98
99 /// features for C
100 LogicFeatures posFeatures;
101 /// features for ~C
102 LogicFeatures negFeatures;
103
104 /// all extra rules for a given concept
105 ERSet erSet;
106
107 protected: // methods
108 // classification TAGs manipulation
109
110 /// calculate value of classification TAG based on told subsumers. WARNING: no TS cycles allowed
111 CTTag determineClassTag ( void );
112
113 /// @return true iff description contains references to THIS concept
114 bool hasSelfInDesc ( const DLTree* p ) const;
115 /// @return copy of P with every entry of THIS replaced with true
116 DLTree* replaceSelfWithConst ( const DLTree* p ) const;
117
118 // told subsumers interface
119
120 /// adds concept as a told subsumer of current one; @return value for CDC analysis
121 bool addToldSubsumer ( TConcept* p )
122 {
123 if ( p != this )
124 {
125 addParentIfNew(p);
126 if ( p->isSingleton() || p->isHasSP() )
127 setHasSP(); // this has singleton parent
128 }
129
130 // if non-primitive concept was found in a description, it's not CD
131 return p->isPrimitive();
132 }
133 /// init told subsumers of the concept by given DESCription; @return TRUE iff concept is CD
134 bool initToldSubsumers ( const DLTree* desc, RoleSSet& RolesProcessed );
135 /// init told subsumers of the concept by given DESCription; @return TRUE iff concept is CD
136 bool initToldSubsumers ( const DLTree* desc )
137 {
138 RoleSSet RolesProcessed;
139 return initToldSubsumers ( desc, RolesProcessed );
140 }
141 /// find told subsumers by given role's domain
142 void SearchTSbyRole ( const TRole* R, RoleSSet& RolesProcessed );
143 /// find told subsumers by given role and its supers domains
144 void SearchTSbyRoleAndSupers ( const TRole* R, RoleSSet& RolesProcessed );
145
146 public: // methods
147 /// the only c'tor
148 explicit TConcept ( const std::string& name )
149 : ClassifiableEntry (name)
150 {
151 setPrimitive();
152 }
153 /// no copy c'tor
154 TConcept ( const TConcept& ) = delete;
155 /// no assignment
156 TConcept& operator = ( const TConcept& ) = delete;
157 /// d'tor
158 ~TConcept() override { deleteTree(Description); }
159 /// clear all info of the concept. Use it in removeConcept()
160 void clear ( void );
161
162 // simple rules support
163
164 /// add index of a simple rule in TBox to the ER set
165 void addExtraRule ( size_t index )
166 {
167 erSet.push_back(index);
168 // FIXME!! double check this!
169 setCompletelyDefined(false);
170 }
171 /// check if a concept is in a disjoint relation with anything
172 bool hasExtraRules ( void ) const { return !erSet.empty(); }
173 /// iterator for accessing DJ elements
174 er_iterator er_begin ( void ) const { return erSet.begin(); }
175 /// iterator for accessing DJ elements
176 er_iterator er_end ( void ) const { return erSet.end(); }
177
178 // classification TAGs manipulation
179
180 /// get value of tag as it is
181 CTTag getClassTag ( void ) const { return classTag; }
182 /// get value of a tag; determine it if unset
183 CTTag getClassTag ( void )
184 {
185 if ( classTag == cttUnspecified )
186 classTag = determineClassTag();
187 return classTag;
188 }
189
190 // description manipulation
191
192 /// add concept expression to concept description
193 void addDesc ( DLTree* Desc );
194 /// @return true iff description contains top-level references to THIS concept
195 bool hasSelfInDesc ( void ) const { return hasSelfInDesc(Description); }
196 /// remove concept from its own definition (like in case C [= (or C ...)
197 void removeSelfFromDescription ( void )
198 {
199 if ( hasSelfInDesc() )
200 {
201 DLTree* desc = Description;
202 Description = replaceSelfWithConst(desc);
203 deleteTree(desc);
204 }
205 initToldSubsumers();
206 }
207 /// remove concept description (to save space)
208 void removeDescription ( void )
209 { // save Synonym value
210 deleteTree(Description);
211 Description = nullptr;
212 }
213 /// check whether it is possible to init this as a non-primitive concept with DESC
214 bool canInitNonPrim ( DLTree* desc )
215 {
216 if ( Description == nullptr )
217 return true;
218 if ( isNonPrimitive() && equalTrees(Description,desc) )
219 return true;
220 return false;
221 }
222 /// switch primitive concept to non-primitive with new definition; @return old definition
223 DLTree* makeNonPrimitive ( DLTree* desc )
224 {
225 DLTree* ret = Description;
226 Description = desc;
227 setPrimitive(false);
228 return ret;
229 }
230
231 /// init told subsumers of the concept by it's description
232 virtual void initToldSubsumers ( void )
233 {
234 toldSubsumers.clear();
235 clearHasSP();
236 // normalise description if the only parent is TOP
237 if ( isPrimitive() && Description && Description->Element() == TOP )
238 removeDescription();
239
240 bool CD = !hasExtraRules() && isPrimitive(); // not a completely defined if there are extra rules
241 if ( Description != nullptr ) // init (additional) told subsumers from definition
242 CD &= initToldSubsumers(Description);
243 setCompletelyDefined(CD);
244 }
245 /// init TOP told subsumer if necessary
246 void setToldTop ( TConcept* top )
247 {
248 if ( Description == nullptr && !hasToldSubsumers() )
249 addParent(top);
250 }
251 /// calculate depth wrt told subsumers; return the depth
252 unsigned int calculateTSDepth ( void );
253
254 // used in the start procedure of SAT/SUBSUME tests
255 BipolarPointer resolveId ( void ) const; // returns either pName or pBody
256
257 /// register a Primitive flag
258 FPP_ADD_FLAG(Primitive,0x10);
259 /// register a HasSingletonParent flag
260 FPP_ADD_FLAG(HasSP,0x20);
261 /// register a Nominal flag
262 FPP_ADD_FLAG(Nominal,0x40);
263 /// register a Singleton flag
264 FPP_ADD_FLAG(Singleton,0x80);
265
266 // concept non-primitivity methods
267
268 /// check if concept is non-primitive concept
269 bool isNonPrimitive ( void ) const { return !isPrimitive(); }
270
271 // relevance part
272
273 /// is given concept relevant to given Labeller's state
274 bool isRelevant ( const TLabeller& lab ) const { return lab.isLabelled(rel); }
275 /// make given concept relevant to given Labeller's state
276 void setRelevant ( const TLabeller& lab ) { lab.set(rel); }
277 /// make given concept irrelevant to given Labeller's state
278 void dropRelevant ( const TLabeller& lab ) { lab.clear(rel); }
279
280 // save/load interface; implementation is in SaveLoad.cpp
281
282 /// save entry
283 void Save ( SaveLoadManager& m ) const override;
284 /// load entry
285 void Load ( SaveLoadManager& m ) override;
286 }; // TConcept
287
288 /// Class for comparison of TConcepts wrt told subsumer depth
289 class TSDepthCompare
290 {
291 public:
292 bool operator() ( const TConcept* p, const TConcept* q ) const
293 { return p->tsDepth < q->tsDepth; }
294 }; // TSDepthCompare
295
296 //----------------------------------------------------------------------------
297 //-- implementation
298 //----------------------------------------------------------------------------
299
300 inline BipolarPointer TConcept :: resolveId ( void ) const
301 {
302 // check for Top/Bottom
303 if ( pName == bpINVALID )
304 return pBody;
305
306 if ( isSynonym() ) // resolve synonyms
307 return resolveSynonym(this)->resolveId();
308
309 return pName; // return concept's name
310 }
311
312 #endif