Debian Codegraph @

Kernel/ReasonerNom.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 "ReasonerNom.h"
21
22 //-----------------------------------------------------------------------------
23 //-- internal nominal reasoning interface
24 //-----------------------------------------------------------------------------
25
26 // register all nominals defined in TBox
27 void
28 NominalReasoner :: initNominalVector ( void )
29 {
30 Nominals.clear();
31
32 for ( TBox::i_iterator pi = tBox.i_begin(); pi != tBox.i_end(); ++pi )
33 if ( !(*pi)->isSynonym() )
34 Nominals.push_back(*pi);
35 }
36
37 /// prepare Nominal Reasoner to a new job
38 void
39 NominalReasoner :: prepareReasoner ( void )
40 {
41 if ( LLM.isWritable(llSRState) )
42 LL << "\nInitNominalReasoner:";
43
44 restore(1);
45
46 // check whether branching op is not a barrier...
47 if ( dynamic_cast<BCBarrier*>(bContext) == nullptr )
48 { // replace it with a barrier
49 Stack.pop();
50 createBCBarrier();
51 }
52 // save the barrier (also remember the entry to be produced)
53 save();
54 // free the memory used in the pools before
55 Stack.clearPools();
56
57 // clear last session information
58 resetSessionFlags();
59 }
60
61 bool
62 NominalReasoner :: consistentNominalCloud ( void )
63 {
64 if ( LLM.isWritable(llBegSat) )
65 LL << "\n--------------------------------------------\n"
66 "Checking consistency of an ontology with individuals:";
67 if ( LLM.isWritable(llGTA) )
68 LL << "\n";
69
70 bool result = false;
71
72 // reserve the root for the forthcoming reasoning
73 if ( initNewNode ( CGraph.getRoot(), DepSet(), bpTOP ) ||
74 initNominalCloud() ) // clash during initialisation
75 result = false;
76 else // perform a normal reasoning
77 result = runSat();
78
79 if ( result && noBranchingOps() )
80 { // all nominal cloud is classified w/o branching -- make a barrier
81 if ( LLM.isWritable(llSRState) )
82 LL << "InitNominalReasoner[";
83 curNode = nullptr;
84 createBCBarrier();
85 save();
86 nonDetShift = 1; // the barrier doesn't introduce branching itself
87 if ( LLM.isWritable(llSRState) )
88 LL << "]";
89 }
90
91 if ( LLM.isWritable(llSatResult) )
92 LL << "\nThe ontology is " << (result ? "consistent" : "INCONSISTENT");
93
94 if ( !result )
95 return false;
96
97 // ABox is consistent -> create cache for every nominal in KB
98 for ( auto& ind: Nominals )
99 updateClassifiedSingleton(ind);
100
101 if (tBox.precacheRelated)
102 precacheRelated();
103
104 return true;
105 }
106
107 /// create nominal nodes for all individuals in TBox
108 bool
109 NominalReasoner :: initNominalCloud ( void )
110 {
111 // create nominal nodes and fills them with initial values
112 for ( const auto& ind: Nominals )
113 if ( initNominalNode(ind) )
114 return true; // ABox is inconsistent
115
116 // create edges between related nodes
117 for ( TBox::RelatedCollection::const_iterator q = tBox.RelatedI.begin(); q != tBox.RelatedI.end(); ++q, ++q )
118 if ( initRelatedNominals(*q) )
119 return true; // ABox is inconsistent
120
121 // create disjoint markers on nominal nodes
122 if ( tBox.Different.empty() )
123 return false;
124
125 DepSet dummy; // empty dep-set for the CGraph
126
127 for ( const auto& di: tBox.Different )
128 {
129 CGraph.initIR();
130 for ( const auto& ind: di )
131 if ( CGraph.setCurIR ( resolveSynonym(ind)->node, dummy ) ) // different(c,c)
132 return true;
133 CGraph.finiIR();
134 }
135
136 // init was OK
137 return false;
138 }
139
140 bool
141 NominalReasoner :: initRelatedNominals ( const TRelated* rel )
142 {
143 DlCompletionTree* from = resolveSynonym(rel->a)->node;
144 DlCompletionTree* to = resolveSynonym(rel->b)->node;
145 TRole* R = resolveSynonym(rel->R);
146 DepSet dep; // empty dep-set
147
148 // check if merging will lead to clash because of disjoint roles
149 if ( R->isDisjoint() && checkDisjointRoleClash ( from, to, R, dep ) )
150 return true;
151
152 // create new edge between FROM and TO
153 DlCompletionTreeArc* pA =
154 CGraph.addRoleLabel ( from, to, /*isPredEdge=*/false, R, dep );
155
156 // return OK iff setup new edge didn't lead to clash
157 // do NOT need to re-check anything: nothing was processed yet
158 return setupEdge ( pA, dep );
159 }
160
161 bool NominalReasoner::hasIndividuals(const DlCompletionTree* node)
162 {
163 auto& label = node->label().getLabel(false);
164
165 for (auto p = label.begin(); p != label.end(); p++)
166 {
167 auto pName = (*p).bp();
168
169 if (pName > 0 && (DLHeap[pName].Type() == dtPSingleton || DLHeap[pName].Type() == dtNSingleton))
170 return true;
171 }
172
173 return false;
174 }
175
176 std::vector<TIndividual*> NominalReasoner::getIndividuals(const DlCompletionTree* node)
177 {
178 std::vector<TIndividual*> individuals;
179
180 auto& label = node->label().getLabel(false);
181
182 for (auto p = label.begin(); p != label.end(); p++)
183 {
184 auto pName = (*p).bp();
185
186 if (pName > 0 && (DLHeap[pName].Type() == dtPSingleton || DLHeap[pName].Type() == dtNSingleton))
187 individuals.push_back(static_cast<TIndividual*>(DLHeap[pName].getConcept()));
188 }
189
190 return individuals;
191 }
192
193 void NominalReasoner::followTransition(std::map<TIndividual*, std::map<const TRole*, std::set<TIndividual*>>>& role_map, const DlCompletionTree* first, const TRole* role, const DlCompletionTree* current, const RoleAutomaton& automaton, RAState state, std::map<std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>>, std::map<RAState, std::set<const DlCompletionTree*>>>& visited, std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>> blocker_stack)
194 {
195 // Check whether we already visited this function with this state and this node
196 // If so we won't be able to derive anything new - just return
197
198 if (visited.count(blocker_stack) == 0)
199 visited[blocker_stack] = std::map<RAState, std::set<const DlCompletionTree*>>();
200
201 if (visited[blocker_stack].count(state) == 0)
202 visited[blocker_stack][state] = std::set<const DlCompletionTree*>();
203
204 if (visited[blocker_stack][state].count(current) > 0)
205 return;
206
207 visited[blocker_stack][state].insert(current);
208
209
210 // Collect all states accessible through an empty transition from the current state
211
212 std::set<RAState> states;
213 states.insert(state);
214
215 for (const RATransition& transition : automaton[state])
216 {
217 if (transition.begin() == transition.end())
218 states.insert(transition.final());
219 }
220
221
222 // If the set of states contains the final state (and there are no blockers) - add a relation to the map of individual relations
223
224 if (blocker_stack.empty() && states.count(automaton.final()) > 0)
225 {
226 for (TIndividual* a : getIndividuals(first))
227 {
228 for (TIndividual* b : getIndividuals(current))
229 {
230 if (role_map.count(a) == 0)
231 role_map[a] = std::map<const TRole*, std::set<TIndividual*>>();
232
233 if (role_map[a].count(role) == 0)
234 role_map[a][role] = std::set<TIndividual*>();
235
236 role_map[a][role].insert(b);
237 }
238 }
239 }
240
241
242 // Check the current node for possible (non-empty) transitions from the current state and follow them
243
244 for (auto p_arc = current->begin(); p_arc != current->end(); p_arc++)
245 {
246 DlCompletionTreeArc* arc = *p_arc;
247
248 const TRole* next_role = arc->getRole();
249
250 if (next_role != nullptr)
251 {
252 const DlCompletionTree* next_node = arc->getArcEnd();
253
254 std::vector<std::pair<const DlCompletionTree*, std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>>>> next_nodes;
255
256 if (!blocker_stack.empty() && (*blocker_stack.rbegin()).first == next_node)
257 {
258 std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>> smaller_blocker_stack(blocker_stack);
259 smaller_blocker_stack.pop_back();
260 next_nodes.push_back(std::pair<const DlCompletionTree*, std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>>>((*blocker_stack.rbegin()).second, smaller_blocker_stack));
261 }
262
263 next_nodes.push_back(std::pair<const DlCompletionTree*, std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>>>(next_node, blocker_stack));
264
265 std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>> bigger_blocker_stack(blocker_stack);
266
267 if (next_node->getBlocker() != nullptr && next_node->getBlocker() != next_node)
268 {
269 auto pair = std::pair<const DlCompletionTree*, const DlCompletionTree*>(next_node->getBlocker(), next_node);
270
271 if (std::find(bigger_blocker_stack.begin(), bigger_blocker_stack.end(), pair) == bigger_blocker_stack.end())
272 {
273 bigger_blocker_stack.push_back(pair);
274 next_nodes.push_back(std::pair<const DlCompletionTree*, std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>>>(next_node->getBlocker(), bigger_blocker_stack));
275 }
276 }
277
278 for (auto& next : next_nodes)
279 {
280 const DlCompletionTree* next_node = next.first;
281 std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>> next_blocker_stack = next.second;
282
283 for (RAState state : states)
284 {
285 for (const RATransition& transition : automaton[state])
286 {
287 for (RATransition::const_iterator q = transition.begin(); q != transition.end(); q++)
288 {
289 if (*q == next_role)
290 followTransition(role_map, first, role, next_node, automaton, transition.final(), visited, next_blocker_stack);
291 }
292 }
293 }
294 }
295 }
296 }
297 }
298
299 void NominalReasoner::precacheRelated()
300 {
301 auto& ORM = tBox.getORM();
302 std::map<TIndividual*, std::map<const TRole*, std::set<TIndividual*>>> role_map;
303
304
305 // Collect information of all relations going out of all individual nodes in the completion graph
306
307 for (auto p_node = CGraph.begin(); p_node != CGraph.end(); p_node++)
308 {
309 const DlCompletionTree* node = *p_node;
310
311 if (hasIndividuals(node))
312 {
313 for (RoleMaster::iterator p = ORM.begin(); p != ORM.end(); p++)
314 {
315 //if (std::string(getIndividuals(node)[0]->getName()) == "i0" && std::string((*p)->getName()) == "isSecondCousinOf")
316 // int a = 0;
317
318 const RoleAutomaton& automaton = (*p)->getAutomaton();
319 std::map<std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>>, std::map<RAState, std::set<const DlCompletionTree*>>> empty_visited;
320 std::vector<std::pair<const DlCompletionTree*, const DlCompletionTree*>> empty_blocker_stack;
321 followTransition(role_map, node, *p, node, automaton, automaton.initial(), empty_visited, empty_blocker_stack);
322 }
323
324 for (auto p_neighbor = node->begin(); p_neighbor != node->end(); p_neighbor++)
325 {
326 const DlCompletionTreeArc* neighbor = *p_neighbor;
327 const TRole* role = neighbor->getRole();
328
329 if (role != nullptr && role->isDataRole())
330 {
331 for (TIndividual* a : getIndividuals(node))
332 {
333 DLVertex& vertex = DLHeap[neighbor->getArcEnd()->getInit()];
334 a->addDataValueCache(role, static_cast<TDataEntry*>(vertex.getConcept()));
335 }
336 }
337 }
338 }
339 }
340
341
342 // Set individual cache for all collected relations
343
344 for (auto& ind_roles : role_map)
345 {
346 TIndividual* a = ind_roles.first;
347
348 for (auto& role_obj : ind_roles.second)
349 {
350 const TRole* role = role_obj.first;
351 a->setRelatedCache(role, std::vector<const TIndividual*>(role_obj.second.begin(), role_obj.second.end()));
352 }
353 }
354
355
356 // For the rest - either copy related from the role synonym or set up an empty cache
357
358 std::vector<const TIndividual*> empty_cache;
359
360 for (auto& individual : Nominals)
361 {
362 for (RoleMaster::iterator role = ORM.begin(); role != ORM.end(); role++)
363 {
364 if (!individual->hasRelatedCache(*role))
365 {
366 TRole* synonym = static_cast<TRole*>((*role)->getSynonym());
367
368 if (synonym != nullptr && individual->hasRelatedCache(synonym))
369 individual->setRelatedCache(*role, individual->getRelatedCache(synonym));
370 else
371 individual->setRelatedCache(*role, empty_cache);
372 }
373 }
374 }
375 }