-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node.c
560 lines (476 loc) · 13.8 KB
/
Node.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#include "Node.h"
#include "Params.h"
#include "ReazienerUtils.h"
#include "KnowledgeBase.h"
#include "CompletionQueue.h"
#include "ABox.h"
#include "Clash.h"
#include "QueueElement.h"
#include "Individual.h"
extern DependencySet DEPENDENCYSET_EMPTY;
extern DependencySet DEPENDENCYSET_INDEPENDENT;
extern KnowledgeBase* g_pKB;
Node::Node(ExprNode* pName, ABox* pABox)
{
m_pABox = pABox;
m_pName = pName;
m_bIsRoot = !isAnon(pName);
m_bIsConceptRoot = FALSE;
m_pMergeDepends = &DEPENDENCYSET_INDEPENDENT;
m_pPruned = NULL;
m_iBranch = 0;
m_iStatus = NODE_CHANGED;
m_pMergeTo = this;
m_iDepth = 1;
}
Node::Node(Node* pNode, ABox* pABox)
{
m_pABox = pABox;
m_pName = pNode->m_pName;
m_bIsRoot = pNode->m_bIsRoot;
m_bIsConceptRoot = pNode->m_bIsConceptRoot;
m_pMergeDepends = pNode->m_pMergeDepends;
m_pMergeTo = pNode->m_pMergeTo;
m_setMerged = pNode->m_setMerged;
m_pPruned = pNode->m_pPruned;
// do not copy differents right now because we need to
// update node references later anyway
m_mDifferents.insert(pNode->m_mDifferents.begin(), pNode->m_mDifferents.end());
m_mDepends.insert(pNode->m_mDepends.begin(), pNode->m_mDepends.end());
if( pABox == NULL )
{
m_pMergeTo = NULL;
for(EdgeVector::iterator i = pNode->m_listInEdges.m_listEdge.begin(); i != pNode->m_listInEdges.m_listEdge.end(); i++ )
{
Edge* pEdge = (Edge*)*i;
Node* pTo = this;
Individual* pFrom = new Individual(pEdge->m_pFrom->m_pName);
Edge* pNewEdge = new Edge(pEdge->m_pRole, pFrom, pTo, pEdge->m_pDepends);
m_listInEdges.addEdge(pNewEdge);
}
}
else
{
for(EdgeVector::iterator i = pNode->m_listInEdges.m_listEdge.begin(); i != pNode->m_listInEdges.m_listEdge.end(); i++ )
m_listInEdges.addEdge((Edge*)*i);
}
m_iBranch = pNode->m_iBranch;
m_iStatus = NODE_CHANGED;
}
bool Node::isPruned()
{
return (m_pPruned!=NULL);
}
bool Node::isMerged()
{
return (m_pMergeTo!=this);
}
bool Node::isNominal()
{
return FALSE;
}
bool Node::isRootNominal()
{
return (m_bIsRoot&&isNominal());
}
bool Node::isRoot()
{
return (m_bIsRoot||isNominal());
}
Node* Node::getSame()
{
if( m_pMergeTo == NULL )
return NULL;
if( m_pMergeTo == this )
return this;
return m_pMergeTo->getSame();
}
void Node::addType(ExprNode* pC, DependencySet* pDS)
{
if( isPruned() )
assertFALSE("Adding type to a pruned node ");
else if( isMerged() )
return;
if( m_pABox->m_iCurrentBranchIndex >= 0 && PARAMS_USE_COMPLETION_QUEUE() )
m_pABox->m_pCompletionQueue->addEffected(m_pABox->m_iCurrentBranchIndex, m_pName);
DependencySet* pDSCopy = new DependencySet(pDS);
pDSCopy->m_iBranch = m_pABox->m_iCurrentBranchIndex;
int iMax = pDSCopy->getMax();
if( pDSCopy->m_iBranch == -1 && iMax != 0 )
pDSCopy->m_iBranch = iMax+1;
m_mDepends[pC] = pDSCopy;
m_pABox->m_bChanged = TRUE;
}
void Node::removeType(ExprNode* pC)
{
m_mDepends.erase(pC);
m_iStatus = NODE_CHANGED;
}
bool Node::hasType(ExprNode* pC)
{
return (m_mDepends.find(pC)!=m_mDepends.end());
}
void Node::addInEdge(Edge* pEdge)
{
m_listInEdges.addEdge(pEdge);
}
bool Node::removeInEdge(Edge* pEdge)
{
return m_listInEdges.removeEdge(pEdge);
}
DependencySet* Node::getMergeDependency(bool bAll)
{
if( !isMerged() || !bAll )
return m_pMergeDepends;
DependencySet* pDS = new DependencySet(m_pMergeDepends);
Node* pN = m_pMergeTo;
while( pN && pN->isMerged() )
{
pDS->unionDS(pN->m_pMergeDepends, m_pABox->doExplanation());
pN = (Node*)pN->m_pMergeTo;
}
return pDS;
}
DependencySet* Node::getDepends(ExprNode* pC)
{
ExprNode2DependencySetMap::iterator i = m_mDepends.find(pC);
if( i != m_mDepends.end() )
return (DependencySet*)i->second;
return NULL;
}
bool Node::isDifferent(Node* pNode)
{
return (m_mDifferents.find(pNode) != m_mDifferents.end());
}
bool Node::isSame(Node* pNode)
{
return (isEqual(getSame(), pNode->getSame()) == 0);
}
void Node::setDifferent(Node* pNode, DependencySet* pDS)
{
if( m_pABox->getBranch() >= 0 && PARAMS_USE_COMPLETION_QUEUE() )
m_pABox->m_pCompletionQueue->addEffected(m_pABox->getBranch(), pNode->m_pName);
if( isDifferent(pNode) )
return;
if( isSame(pNode) )
{
// CHW - added for incremental reasoning support -
// this is needed as we will need to backjump if possible
if( PARAMS_USE_INCREMENTAL_CONSISTENCE() )
{
DependencySet* pNewDS = pDS->unionNew(m_pMergeDepends, m_pABox->doExplanation());
pNewDS->unionDS(pNode->m_pMergeDepends, m_pABox->doExplanation());
m_pABox->setClash(new Clash(this, pNewDS, pNode->m_pName));
}
else
m_pABox->setClash(new Clash(this, pDS, pNode->m_pName));
return;
}
DependencySet* pDSCopy = new DependencySet(pDS);
pDSCopy->m_iBranch = m_pABox->getBranch();
m_mDifferents[pNode] = pDSCopy;
pNode->setDifferent(this, pDSCopy);
}
bool Node::isChanged(int iType)
{
return (m_iStatus & (1 << iType)) != 0;
}
void Node::setChanged(bool bChanged)
{
m_iStatus = bChanged ? NODE_CHANGED : NODE_UNCHANGED;
}
void Node::setChanged(int iType, bool bChanged)
{
if( bChanged )
{
m_iStatus = (m_iStatus | (1 << iType));
//Check if we need to updated the completion queue
//Currently we only updated the changed lists for checkDatatypeCount()
QueueElement* pNewElement = new QueueElement(m_pName, NULL);
//update the datatype queue
if( (iType == Node::ALL || iType == Node::MIN) && PARAMS_USE_COMPLETION_QUEUE() )
m_pABox->m_pCompletionQueue->add(pNewElement, CompletionQueue::DATATYPELIST);
//add node to effected list in queue
if( m_pABox->m_iCurrentBranchIndex >= 0 && PARAMS_USE_COMPLETION_QUEUE() )
m_pABox->addEffected(m_pABox->m_iCurrentBranchIndex, m_pName);
}
else
{
m_iStatus = (m_iStatus & ~(1 << iType));
//add node to effected list in queue
if( m_pABox->m_iCurrentBranchIndex >= 0 && PARAMS_USE_COMPLETION_QUEUE() )
m_pABox->addEffected(m_pABox->m_iCurrentBranchIndex, m_pName);
}
}
int isEqual(const Node* pNode1, const Node* pNode2)
{
return isEqual(pNode1->m_pName, pNode2->m_pName);
}
Node* Node::copyTo(ABox* pABox)
{
return NULL;
}
Node* Node::copy()
{
return copyTo(NULL);
}
void Node::updateNodeReferences()
{
m_pMergeTo = m_pABox->getNode(m_pMergeTo->m_pName);
Node2DependencySetMap diffs;
for(Node2DependencySetMap::iterator i = m_mDifferents.begin(); i != m_mDifferents.end(); i++ )
{
Node* pNode = (Node*)i->first;
diffs[m_pABox->getNode(pNode->m_pName)] = (DependencySet*)i->second;
}
m_mDifferents = diffs;
if( m_setMerged.size() > 0 )
{
NodeSet setSames;
for(NodeSet::iterator i = m_setMerged.begin(); i != m_setMerged.end(); i++ )
{
Node* pNode = (Node*)*i;
setSames.insert(m_pABox->getNode(pNode->m_pName));
}
m_setMerged = setSames;
}
EdgeVector aNewEdges;
for(EdgeVector::iterator i = m_listInEdges.m_listEdge.begin(); i != m_listInEdges.m_listEdge.end(); i++ )
{
Edge* pEdge = (Edge*)*i;
Individual* pFrom = m_pABox->getIndividual(pEdge->m_pFrom->m_pName);
Edge* pNewEdge = new Edge(pEdge->m_pRole, pFrom, this, pEdge->m_pDepends);
aNewEdges.push_back(pNewEdge);
if( !isPruned() )
pFrom->m_listOutEdges.addEdge(pNewEdge);
}
m_listInEdges.m_listEdge.clear();
for(EdgeVector::iterator i = aNewEdges.begin(); i != aNewEdges.end(); i++ )
m_listInEdges.addEdge((Edge*)*i);
}
void Node::unprune(int iBranch)
{
m_pPruned = NULL;
for(EdgeVector::iterator i = m_listInEdges.m_listEdge.begin(); i != m_listInEdges.m_listEdge.end(); i++ )
{
Edge* pEdge = (Edge*)*i;
DependencySet* pDS = pEdge->m_pDepends;
if( pDS->m_iBranch <= iBranch )
{
Individual* pPred = pEdge->m_pFrom;
Role* pRole = pEdge->m_pRole;
// if both pred and *this* were merged to other nodes (in that order)
// there is a chance we might duplicate the edge so first check for
// the existence of the edge
if( !pPred->hasRSuccessor(pRole, this) )
pPred->addOutEdge(pEdge);
}
}
}
void Node::undoSetSame()
{
m_pMergeTo->removeMerged(this);
m_pMergeDepends = &DEPENDENCYSET_INDEPENDENT;
m_pMergeTo = this;
}
void Node::removeMerged(Node* pNode)
{
m_setMerged.erase(pNode);
}
bool Node::hasObviousType(ExprNode* pC)
{
DependencySet* pDS = getDepends(pC);
if( pDS && pDS->isIndependent() )
return TRUE;
if( isIndividual() && pC->m_iExpression == EXPR_SOME )
{
Individual* pInd = (Individual*)this;
ExprNode* pR = (ExprNode*)pC->m_pArgs[0];
ExprNode* pD = (ExprNode*)pC->m_pArgs[1];
Role* pRole = g_pKB->getRole(pR);
EdgeList edgeList;
pInd->getRNeighborEdges(pRole, &edgeList);
for(EdgeVector::iterator i = edgeList.m_listEdge.begin(); i != edgeList.m_listEdge.end(); i++ )
{
Edge* pEdge = (Edge*)*i;
if( !pEdge->m_pDepends->isIndependent() )
continue;
Node* pY = pEdge->getNeighbor(pInd);
if( pY->hasObviousType(pD) )
return TRUE;
}
}
return FALSE;
}
bool Node::hasObviousType(ExprNodeSet* pSet)
{
for(ExprNodeSet::iterator i = pSet->begin(); i != pSet->end(); i++ )
{
ExprNode* pC = (ExprNode*)*i;
DependencySet* pDS = getDepends(pC);
if( pDS && pDS->isIndependent() )
return TRUE;
}
return FALSE;
}
void Node::setSame(Node* pNode, DependencySet* pDS)
{
if( isSame(pNode) )
return;
if( isDifferent(pNode) )
{
// CHW - added for incremental reasoning support -
// this is needed as we will need to backjump if possible
if( PARAMS_USE_INCREMENTAL_CONSISTENCE() )
m_pABox->setClash(Clash::nominal(this, pDS->unionNew(m_pMergeDepends, m_pABox->m_bDoExplanation)->unionDS(pNode->m_pMergeDepends, m_pABox->m_bDoExplanation), pNode->m_pName));
else
m_pABox->setClash(Clash::nominal(this, pDS, pNode->m_pName));
return;
}
m_pMergeTo = pNode;
m_pMergeDepends = new DependencySet(pDS);
m_pMergeDepends->m_iBranch = m_pABox->m_iCurrentBranchIndex;
pNode->addMerged(this);
}
void Node::addMerged(Node* pNode)
{
m_setMerged.insert(pNode);
}
void Node::inheritDifferents(Node* pY, DependencySet* pDS)
{
for(Node2DependencySetMap::iterator i = pY->m_mDifferents.begin(); i != pY->m_mDifferents.end(); i++ )
{
Node* pYDiff = (Node*)i->first;
DependencySet* pYDS = (DependencySet*)i->second;
DependencySet* pFinalDS = pDS->unionNew(pYDS, m_pABox->m_bDoExplanation);
setDifferent(pYDiff, pFinalDS);
}
}
DependencySet* Node::getDifferenceDependency(Node* pNode)
{
Node2DependencySetMap::iterator iFind = m_mDifferents.find(pNode);
if( iFind != m_mDifferents.end() )
return (DependencySet*)iFind->second;
return NULL;
}
bool Node::restore(int iBranch)
{
if( m_pPruned )
{
if( m_pPruned->m_iBranch > iBranch )
{
if( m_pMergeDepends->m_iBranch > iBranch )
undoSetSame();
unprune(iBranch);
}
else
return FALSE;
}
ExprNodes aConjunctions;
m_iStatus = NODE_CHANGED;
for(ExprNode2DependencySetMap::iterator i = m_mDepends.begin(); i != m_mDepends.end(); )
{
ExprNode* pC = (ExprNode*)i->first;
DependencySet* pD = (DependencySet*)i->second;
bool bRemoveType = PARAMS_USE_SMART_RESTORE() ? (pD->getMax()>=iBranch):(pD->m_iBranch>iBranch);
if( bRemoveType )
{
m_mDepends.erase(i++);
removeType(pC);
}
else
{
i++;
if( PARAMS_USE_SMART_RESTORE() && pC->m_iExpression == EXPR_AND )
{
aConjunctions.push_back(pC);
}
}
}
// with smart restore there is a possibility that we remove a conjunct
// but not the conjunction. this is the case if conjunct was added before
// the conjunction but depended on an earlier branch. so we need to make
// sure all conjunctions are actually applied
if( PARAMS_USE_SMART_RESTORE() )
{
for(ExprNodes::iterator i = aConjunctions.begin(); i != aConjunctions.end(); i++ )
{
ExprNode* pC = (ExprNode*)*i;
DependencySet* pD = getDepends(pC);
for(int j = 0; j < ((ExprNodeList*)pC->m_pArgList)->m_iUsedSize; j++ )
addType(((ExprNodeList*)pC->m_pArgList)->m_pExprNodes[j], pD);
}
}
ExprNodeMap::iterator iFind = m_pABox->m_mTypeAssertions.find(m_pName);
if( iFind != m_pABox->m_mTypeAssertions.end() )
{
ExprNode* pC = (ExprNode*)iFind->second;
addType(pC, &DEPENDENCYSET_INDEPENDENT);
}
for(Node2DependencySetMap::iterator i = m_mDifferents.begin(); i != m_mDifferents.end(); i++ )
{
Node* pNode = (Node*)i->first;
DependencySet* pDS = (DependencySet*)i->second;
if( pDS->m_iBranch > iBranch )
m_mDifferents.erase(i);
}
for(EdgeVector::iterator i = m_listInEdges.m_listEdge.begin(); i != m_listInEdges.m_listEdge.end(); )
{
Edge* pEdge = (Edge*)*i;
DependencySet* pDS = pEdge->m_pDepends;
if( pDS->m_iBranch > iBranch )
i = m_listInEdges.m_listEdge.erase(i);
else
i++;
}
return TRUE;
}
Individual* Node::getParent()
{
if( isBlockable() )
{
if( m_listInEdges.size() == 0 )
return NULL;
else
{
// reflexive properties!
for(EdgeVector::iterator i = m_listInEdges.m_listEdge.begin(); i != m_listInEdges.m_listEdge.end(); i++ )
{
Edge* pEdge = (Edge*)*i;
if( isEqual(pEdge->m_pFrom, this) != 0 )
return pEdge->m_pFrom;
}
}
}
return NULL;
}
void Node::getPredecessors(NodeSet* pAncestors)
{
m_listInEdges.getPredecessors(pAncestors);
}
void Node::getPath(ExprNodes* pPath)
{
if( isNamedIndividual() )
{
pPath->push_back(m_pName);
}
else
{
NodeSet setCycleCache;
Node* pNode = this;
while( pNode->m_listInEdges.size() > 0 )
{
Edge* pInEdge = pNode->m_listInEdges.m_listEdge.front();
pNode = pInEdge->m_pFrom;
if( setCycleCache.find(pNode) != setCycleCache.end() )
break;
setCycleCache.insert(pNode);
pPath->push_front( pInEdge->m_pRole->m_pName );
if( pNode->isNamedIndividual() )
{
pPath->push_front( pNode->m_pName );
break;
}
}
}
}