-
Notifications
You must be signed in to change notification settings - Fork 1
/
KnowledgeBase.c
1394 lines (1170 loc) · 40.9 KB
/
KnowledgeBase.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "KnowledgeBase.h"
#include "TBox.h"
#include "ABox.h"
#include "RBox.h"
#include "Expressivity.h"
#include "Branch.h"
#include "DependencySet.h"
#include "Params.h"
#include "SizeEstimate.h"
#include "Role.h"
#include "DependencyIndex.h"
#include "Individual.h"
#include "Literal.h"
#include "Edge.h"
#include "Taxonomy.h"
#include "TaxonomyBuilder.h"
#include "ConceptCache.h"
#include "CompletionQueue.h"
#include "CompletionStrategy.h"
#include "DependencyEntry.h"
#include "Dependency.h"
#include "Clash.h"
#include "ReazienerUtils.h"
#include "SHOINStrategy.h"
#include "SROIQStrategy.h"
#include "SHOIQStrategy.h"
#include "EmptySHNStrategy.h"
#include "CompletionStrategy.h"
extern DependencySet DEPENDENCYSET_EMPTY;
extern DependencySet DEPENDENCYSET_INDEPENDENT;
extern ExprNode* EXPRNODE_TOP;
extern ExprNode* EXPRNODE_BOTTOM;
extern int g_iCommentIndent;
KnowledgeBase::KnowledgeBase()
{
initExprNodes();
clear();
m_pDependencyIndex = new DependencyIndex(this);
m_pTaxonomy = NULL;
m_iKBStatus = KBSTATUS_ALL_CHANGED;
}
KnowledgeBase::~KnowledgeBase()
{
delete m_pTBox;
delete m_pABox;
if( m_pExpressivity )
delete m_pExpressivity;
}
void KnowledgeBase::clear()
{
m_pTBox = new TBox(this);
m_pABox = new ABox();
m_pRBox = new RBox();
m_pExpressivity = new Expressivity(this);
m_setIndividuals.clear();
m_mABoxAssertions.clear();
m_mapInstances.clear();
m_bABoxAddition = FALSE;
m_bABoxDeletion = FALSE;
m_pBuilder = NULL;
m_iKBStatus = KBSTATUS_ALL_CHANGED;
}
void KnowledgeBase::prepare()
{
if( !isChanged() )
return;
START_DECOMMENT2("KnowledgeBase::prepare");
bool bExplain = m_pABox->m_bDoExplanation;
m_pABox->m_bDoExplanation = TRUE;
bool bReuseTaxonomy = (m_pTaxonomy && !isTBoxChanged() && (m_pExpressivity->hasNominal()==FALSE || PARAMS_USE_PSEUDO_NOMINALS()));
if( isTBoxChanged() )
{
if( PARAMS_USE_ABSORPTION() )
m_pTBox->absorb();
m_pTBox->normalize();
m_pTBox->internalize();
}
if( isRBoxChanged() )
m_pRBox->prepare();
// The preparation of TBox and RBox is finished so we set the
// status to UNCHANGED now. Expressivity check can only work
// with prepared KB's
m_iKBStatus = KBSTATUS_UNCHANGED;
m_pExpressivity->compute();
m_mapInstances.clear();
m_pEstimate = new SizeEstimate(this);
m_pABox->m_bDoExplanation = bExplain;
m_pABox->clearCaches(!bReuseTaxonomy);
m_pABox->m_pCache->setMaxSize( 2*m_pTBox->m_setClasses.size() );
if( bReuseTaxonomy )
m_iKBStatus |= KBSTATUS_CLASSIFICATION;
else if( m_pTaxonomy )
{
delete m_pTaxonomy;
m_pTaxonomy = NULL;
}
END_DECOMMENT("KnowledgeBase::prepare");
}
void KnowledgeBase::addClass(ExprNode* pClassExpr)
{
if( isEqual(pClassExpr, EXPRNODE_TOP) == 0 || isComplexClass(pClassExpr) )
return;
if( m_pTBox->addClass(pClassExpr) )
m_iKBStatus |= KBSTATUS_TBOX_CHANGED;
}
void KnowledgeBase::addDisjointClass(ExprNode* pC1, ExprNode* pC2)
{
ExprNodeSet* pExplain = new ExprNodeSet;
if( PARAMS_USE_TRACING() )
pExplain->insert(createExprNode(EXPR_DISJOINTWITH, pC1, pC2));
addDisjointClass(pC1, pC2, pExplain);
}
void KnowledgeBase::addDisjointClass(ExprNode* pC1, ExprNode* pC2, ExprNodeSet* pExplanation)
{
m_iKBStatus |= KBSTATUS_TBOX_CHANGED;
ExprNode* pNotC1 = createExprNode(EXPR_NOT, pC1);
ExprNode* pNotC2 = createExprNode(EXPR_NOT, pC2);
m_pTBox->addAxiom(createExprNode(EXPR_SUBCLASSOF, pC1, pNotC2), pExplanation);
m_pTBox->addAxiom(createExprNode(EXPR_SUBCLASSOF, pC2, pNotC1), pExplanation);
}
void KnowledgeBase::addSubClass(ExprNode* pSub, ExprNode* pSup)
{
ExprNode* pSubAxiom = createExprNode(EXPR_SUBCLASSOF, pSub, pSup);
ExprNodeSet* pSetExplanation = new ExprNodeSet;
if( PARAMS_USE_TRACING() )
pSetExplanation->insert(pSubAxiom);
addSubClass(pSub, pSup, pSetExplanation);
}
void KnowledgeBase::addSubClass(ExprNode* pSub, ExprNode* pSup, ExprNodeSet* pExplanation)
{
START_DECOMMENT2("KnowledgeBase::addSubClass");
printExprNodeWComment("Sub=", pSub);
printExprNodeWComment("Sup=", pSup);
if( isEqual(pSub, pSup) == 0 )
{
END_DECOMMENT("KnowledgeBase::addSubClass");
return;
}
ExprNode* pSubAxiom = createExprNode(EXPR_SUBCLASSOF, pSub, pSup);
printExprNodeWComment("SubAxiom=", pSubAxiom);
m_pTBox->addAxiom(pSubAxiom, pExplanation);
m_iKBStatus |= KBSTATUS_TBOX_CHANGED;
END_DECOMMENT("KnowledgeBase::addSubClass");
}
void KnowledgeBase::addSameClasses(ExprNode* pExpr, ExprNode* pSameExpr)
{
addEquivalentClasses(pExpr, pSameExpr);
}
void KnowledgeBase::addEquivalentClasses(ExprNode* pExprNode1, ExprNode* pExprNode2)
{
START_DECOMMENT2("KnowledgeBase::addEquivalentClasses");
printExprNodeWComment("C1=", pExprNode1);
printExprNodeWComment("C2=", pExprNode2);
if( isEqual(pExprNode1, pExprNode2) == 0 )
{
END_DECOMMENT("KnowledgeBase::addEquivalentClasses");
return;
}
ExprNode* pSameAxiom = createExprNode(EXPR_EQCLASSES, pExprNode1, pExprNode2);
ExprNodeSet* pSetExplanation = new ExprNodeSet;
if( PARAMS_USE_TRACING() )
pSetExplanation->insert(pSameAxiom);
m_pTBox->addAxiom(pSameAxiom, pSetExplanation);
m_iKBStatus |= KBSTATUS_TBOX_CHANGED;
END_DECOMMENT("KnowledgeBase::addEquivalentClasses");
}
void KnowledgeBase::addDisjointClasses(ExprNode* pC1, ExprNode* pC2)
{
ExprNodeSet setExplanation;
if( PARAMS_USE_TRACING() )
setExplanation.insert(createExprNode(EXPR_DISJOINTWITH, pC1, pC2));
addDisjointClasses(pC1, pC2, &setExplanation);
}
void KnowledgeBase::addDisjointClasses(ExprNode* pC1, ExprNode* pC2, ExprNodeSet* pExplanation)
{
ExprNode* pNotC1 = createExprNode(EXPR_NOT, pC1);
ExprNode* pNotC2 = createExprNode(EXPR_NOT, pC2);
m_pTBox->addAxiom(createExprNode(EXPR_SUBCLASSOF, pC1, pNotC2), pExplanation);
m_pTBox->addAxiom(createExprNode(EXPR_SUBCLASSOF, pC2, pNotC1), pExplanation);
m_iKBStatus |= KBSTATUS_TBOX_CHANGED;
}
void KnowledgeBase::addComplementClasses(ExprNode* pC1, ExprNode* pC2)
{
ExprNode* pNotC2 = createExprNode(EXPR_NOT, pC2);
m_iKBStatus |= KBSTATUS_TBOX_CHANGED;
if( isEqual(pC1, pC2) == 0 )
return;
ExprNodeSet setExplanation;
if( PARAMS_USE_TRACING() )
setExplanation.insert(createExprNode(EXPR_COMPLEMENTOF, pC1, pC2));
ExprNode* pNotC1 = createExprNode(EXPR_NOT, pC1);
m_pTBox->addAxiom(createExprNode(EXPR_EQCLASSES, pC1, pNotC2), &setExplanation);
m_pTBox->addAxiom(createExprNode(EXPR_EQCLASSES, pC2, pNotC1), &setExplanation);
}
Node* KnowledgeBase::addIndividual(ExprNode* pExprNode)
{
Node* pNode = m_pABox->getNode(pExprNode);
if( pNode == NULL )
{
m_iKBStatus |= KBSTATUS_ABOX_CHANGED;
m_pABox->setSyntacticUpdate();
pNode = m_pABox->addIndividual(pExprNode);
m_setIndividuals.insert(pExprNode);
m_pABox->setSyntacticUpdate(FALSE);
}
else if( pNode->m_bLiteralNode )
assertFALSE("Trying to use a literal as an individual.");
// set addition flag
m_bABoxAddition = TRUE;
// if we can use inc reasoning then update pseudomodel
if( canUseIncrementalConsistency() )
{
m_pABox->getPseudoModel()->setSyntacticUpdate();
Node* pPseudoModelNode = m_pABox->getPseudoModel()->getNode(pExprNode);
if( pPseudoModelNode == NULL )
{
m_iKBStatus |= KBSTATUS_ABOX_CHANGED;
// add to pseudomodel - note branch must be temporarily set to 0
// to ensure that asssertion
// will not be restored during backtracking
int iBranch = m_pABox->getPseudoModel()->getBranch();
m_pABox->getPseudoModel()->setBranch(0);
pPseudoModelNode = m_pABox->getPseudoModel()->addIndividual(pExprNode);
m_pABox->getPseudoModel()->setBranch(iBranch);
// need to update the branch node count as this is node has been
// added other wise during back jumping this node can be removed
for(int i = 0; m_pABox->getPseudoModel()->m_iBranchSize; i++ )
m_pABox->getPseudoModel()->m_aBranches[i]->m_iNodeCount++;
}
m_pABox->m_setUpdatedIndividuals.insert(m_pABox->getPseudoModel()->getNode(pExprNode));
m_pABox->m_setNewIndividuals.insert(m_pABox->getPseudoModel()->getNode(pExprNode));
m_pABox->getPseudoModel()->setSyntacticUpdate(FALSE);
}
return pNode;
}
void KnowledgeBase::addType(ExprNode* pIndvidualExpr, ExprNode* pCExpr)
{
ExprNode* pTypeAxiom = createExprNode(EXPR_TYPE, pIndvidualExpr, pCExpr);
DependencySet* pDS = (PARAMS_USE_TRACING())?(new DependencySet(pTypeAxiom)):(&DEPENDENCYSET_INDEPENDENT);
// add type assertion to syntactic assertions and update dependency index
if( PARAMS_USE_INCREMENTAL_DELETION() )
{
ExprNodeSet* pSet = NULL;
map<int, ExprNodeSet*>::iterator i = m_mABoxAssertions.find(ASSERTION_TYPE);
if( i == m_mABoxAssertions.end() )
{
pSet = new ExprNodeSet;
m_mABoxAssertions[ASSERTION_TYPE] = pSet;
}
else
pSet = (ExprNodeSet*)i->second;
pSet->insert(pTypeAxiom);
}
addType(pIndvidualExpr, pCExpr, pDS);
}
void KnowledgeBase::addType(ExprNode* pIndividualExpr, ExprNode* pCExpr, DependencySet* pDS)
{
m_iKBStatus |= KBSTATUS_ABOX_CHANGED;
// set addition flag
m_bABoxAddition = TRUE;
m_pABox->setSyntacticUpdate();
m_pABox->addType(pIndividualExpr, pCExpr, pDS);
m_pABox->setSyntacticUpdate(FALSE);
// if use incremental reasoning then update the cached pseudo model as well
if( canUseIncrementalConsistency() )
{
// add this individuals to the affected list - used for inc.
// consistency checking
m_pABox->m_setUpdatedIndividuals.insert(m_pABox->getPseudoModel()->getNode(pIndividualExpr));
// as there can possibly be many branches in the pseudomodel, we
// need a workaround, so temporarily set branch to 0 and then add to
// the pseudomdel
int iBranch = m_pABox->getPseudoModel()->getBranch();
m_pABox->getPseudoModel()->setSyntacticUpdate();
m_pABox->getPseudoModel()->setBranch(0);
m_pABox->getPseudoModel()->addType(pIndividualExpr, pCExpr);
m_pABox->getPseudoModel()->setBranch(iBranch);
m_pABox->getPseudoModel()->setSyntacticUpdate(FALSE);
// incrementally update the expressivity of the KB, so that we do
// not have to reperform if from scratch!
updateExpressivity(pIndividualExpr, pCExpr);
}
}
void KnowledgeBase::addDomain(ExprNode* pP, ExprNode* pC, DependencySet* pDS)
{
START_DECOMMENT2("KnowledgeBase::addDomain");
printExprNodeWComment("pRole=", pP);
printExprNodeWComment("pDomain=", pC);
if( pDS == NULL )
{
if( PARAMS_USE_TRACING() )
pDS = new DependencySet(createExprNode(EXPR_DOMAIN, pP, pC));
else
pDS = &DEPENDENCYSET_INDEPENDENT;
}
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
Role* pRole = m_pRBox->getDefinedRole(pP);
// TODO Need to do something with the dependency set.
pRole->addDomain(pC, pDS);
END_DECOMMENT("KnowledgeBase::addDomain");
}
void KnowledgeBase::addRange(ExprNode* pP, ExprNode* pC, DependencySet* pDS)
{
if( pDS == NULL )
{
if( PARAMS_USE_TRACING() )
pDS = new DependencySet(createExprNode(EXPR_RANGE, pP, pC));
else
pDS = &DEPENDENCYSET_INDEPENDENT;
}
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
// CHECK HERE
Role* pRole = m_pRBox->getDefinedRole(pP);
// TODO Do something with dependency...
pRole->addRange(pC, pDS);
}
void KnowledgeBase::addDifferent(ExprNode* pExprNode1, ExprNode* pExprNode2)
{
m_iKBStatus |= KBSTATUS_ABOX_CHANGED;
// set addition flag
m_bABoxAddition = TRUE;
// if we can use incremental consistency checking then add to
// pseudomodel
if( canUseIncrementalConsistency() )
{
m_pABox->m_setUpdatedIndividuals.insert( (Node*)m_pABox->getPseudoModel()->getIndividual(pExprNode1) );
m_pABox->m_setUpdatedIndividuals.insert( (Node*)m_pABox->getPseudoModel()->getIndividual(pExprNode2) );
// add to pseudomodel - note branch must be temporarily set to 0 to
// ensure that asssertion
// will not be restored during backtracking
int iBranch = m_pABox->getPseudoModel()->getBranch();
m_pABox->getPseudoModel()->setBranch(0);
m_pABox->getPseudoModel()->addDifferent(pExprNode1, pExprNode2);
m_pABox->getPseudoModel()->setBranch(iBranch);
}
m_pABox->addDifferent(pExprNode1, pExprNode2);
}
void KnowledgeBase::addProperty(ExprNode* pRoleExpr)
{
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
m_pRBox->addRole(pRoleExpr);
}
bool KnowledgeBase::addPropertyValue(ExprNode* pP, ExprNode* pS, ExprNode* pO)
{
Individual* pSubj = m_pABox->getIndividual(pS);
Role* pRole = m_pRBox->getRole(pP);
Node* pObj = NULL;
if( pSubj == NULL )
{
//log.warn( s + " is not a known individual!" );
return FALSE;
}
if( pRole == NULL )
{
//log.warn( p + " is not a known property!" );
return FALSE;
}
if( !pRole->isObjectRole() && !pRole->isDatatypeRole() )
return FALSE;
ExprNode* pPropAxiom = createExprNode(EXPR_PROP, pP, pS, pO);
DependencySet* pDS = &DEPENDENCYSET_INDEPENDENT;
if( PARAMS_USE_TRACING() )
pDS = new DependencySet(pPropAxiom);
if( pRole->isObjectRole() )
{
pObj = m_pABox->getIndividual(pO);
if( pObj == NULL )
{
if( isLiteral(pO) )
{
//log.warn( "Ignoring literal value " + o + " for object property " + p );
return FALSE;
}
else
{
//log.warn( o + " is not a known individual!" );
return FALSE;
}
}
if( PARAMS_KEEP_ABOX_ASSERTIONS() )
{
ExprNodeSet* pSet = NULL;
map<int, ExprNodeSet*>::iterator i = m_mABoxAssertions.find(ASSERTION_OBJ_ROLE);
if( i == m_mABoxAssertions.end() )
{
pSet = new ExprNodeSet;
m_mABoxAssertions[ASSERTION_OBJ_ROLE] = pSet;
}
else
pSet = (ExprNodeSet*)i->second;
pSet->insert(pPropAxiom);
}
}
else if( pRole->isDatatypeRole() )
{
pObj = m_pABox->addLiteral(pO, pDS);
if( PARAMS_KEEP_ABOX_ASSERTIONS() )
{
ExprNodeSet* pSet = NULL;
map<int, ExprNodeSet*>::iterator i = m_mABoxAssertions.find(ASSERTION_DATA_ROLE);
if( i == m_mABoxAssertions.end() )
{
pSet = new ExprNodeSet;
m_mABoxAssertions[ASSERTION_DATA_ROLE] = pSet;
}
else
pSet = (ExprNodeSet*)i->second;
pSet->insert(pPropAxiom);
}
}
m_iKBStatus |= KBSTATUS_ABOX_CHANGED;
// set addition flag
m_bABoxAddition = TRUE;
Edge* pEdge = pSubj->addEdge(pRole, pObj, pDS);
if( PARAMS_USE_INCREMENTAL_DELETION() )
{
// add to syntactic assertions
m_setSyntacticAssertions.insert(pPropAxiom);
// add to dependency index
m_pDependencyIndex->addEdgeDependency(pEdge, pDS);
}
// if use inc. reasoning then we need to update the pseudomodel
if( canUseIncrementalConsistency() )
{
// add this individual to the affected list
m_pABox->m_setUpdatedIndividuals.insert( (Node*)m_pABox->getPseudoModel()->getIndividual(pS) );
// if this is an object property then add the object to the affected list
if( !pRole->isDatatypeRole() )
m_pABox->m_setUpdatedIndividuals.insert( (Node*)m_pABox->getPseudoModel()->getIndividual(pO) );
if( pRole->isObjectRole() )
{
pObj = m_pABox->getPseudoModel()->getIndividual(pO);
if( pObj->isPruned() || pObj->isMerged() )
pObj = pObj->getSame();
}
else if( pRole->isDatatypeRole() )
pObj = m_pABox->getPseudoModel()->addLiteral(pO);
// get the subject
Individual* pSubj2 = m_pABox->getPseudoModel()->getIndividual(pS);
if( pSubj2->isPruned() || pSubj2->isMerged() )
pSubj2 = (Individual*)pSubj2->getSame();
pDS = &DEPENDENCYSET_INDEPENDENT;
if( PARAMS_USE_TRACING() )
pDS = new DependencySet(createExprNode(EXPR_PROP, pP, pS, pO));
// add to pseudomodel - note branch must be temporarily set to 0 to
// ensure that asssertion
// will not be restored during backtracking
int iBranch = m_pABox->getPseudoModel()->getBranch();
m_pABox->getPseudoModel()->setBranch(0);
pSubj2->addEdge(pRole, pObj, pDS);
m_pABox->getPseudoModel()->setBranch(iBranch);
}
return TRUE;
}
void KnowledgeBase::addSubProperty(ExprNode* pSub, ExprNode* pSup)
{
START_DECOMMENT2("KnowledgeBase::addSubProperty");
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
m_pRBox->addSubRole(pSub, pSup);
END_DECOMMENT("KnowledgeBase::addSubProperty");
}
Role* KnowledgeBase::getRole(ExprNode* p)
{
return m_pRBox->getRole(p);
}
Role* KnowledgeBase::getProperty(ExprNode* p)
{
return m_pRBox->getRole(p);
}
int KnowledgeBase::getPropertyType(ExprNode* p)
{
Role* pRole = m_pRBox->getRole(p);
return (pRole==NULL)?Role::UNTYPED:pRole->m_iRoleType;
}
bool KnowledgeBase::isObjectProperty(ExprNode* p)
{
return (getPropertyType(p) == Role::OBJECT);
}
/**
* Add a new object property. If property was earlier defined to be a
* datatype property then this function will simply return without changing
* the KB.
*/
bool KnowledgeBase::addDatatypeProperty(ExprNode* pP)
{
bool bExists = (getPropertyType(pP) == Role::DATATYPE);
Role* pRole = m_pRBox->addDatatypeRole(pP);
if( bExists )
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
return (pRole!=NULL);
}
/**
* Add a new object property. If property was earlier defined to be a
* datatype property then this function will simply return without changing
* the KB.
*/
bool KnowledgeBase::addObjectProperty(ExprNode* pP)
{
bool bExists = (getPropertyType(pP) == Role::OBJECT);
Role* pRole = m_pRBox->addObjectRole(pP);
if( bExists )
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
return (pRole!=NULL);
}
void KnowledgeBase::addFunctionalProperty(ExprNode* pP)
{
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
Role* pRole = m_pRBox->getDefinedRole(pP);
DependencySet* pDS = &DEPENDENCYSET_INDEPENDENT;
if( PARAMS_USE_TRACING() )
pDS = new DependencySet(createExprNode(EXPR_FUNCTIONAL, pP));
pRole->setFunctional(TRUE, pDS);
}
void KnowledgeBase::addTransitiveProperty(ExprNode* pP)
{
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
Role* pRole = m_pRBox->getDefinedRole(pP);
DependencySet* pDS = &DEPENDENCYSET_INDEPENDENT;
if( PARAMS_USE_TRACING() )
pDS = new DependencySet(createExprNode(EXPR_TRANSITIVE, pP));
ExprNodeList* pList = createExprNodeList();
addExprToList(pList, pP);
addExprToList(pList, pP);
pRole->addSubRoleChain(pList, pDS);
}
void KnowledgeBase::addInverseProperty(ExprNode* pP1, ExprNode* pP2)
{
if( PARAMS_IGNORE_INVERSES() )
return ;
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
DependencySet* pDS = &DEPENDENCYSET_INDEPENDENT;
if( PARAMS_USE_TRACING() )
pDS = new DependencySet(createExprNode(EXPR_INVERSEPROPERTY, pP1, pP2));
m_pRBox->addInverseRole(pP1, pP2, pDS);
}
void KnowledgeBase::addEquivalentProperty(ExprNode* pP1, ExprNode* pP2)
{
m_iKBStatus |= KBSTATUS_RBOX_CHANGED;
m_pRBox->addEquivalentRole(pP1, pP2);
}
/* Check if we can use incremental consistency checking */
bool KnowledgeBase::canUseIncrementalConsistency()
{
if( m_pExpressivity == NULL )
return FALSE;
return (!(m_pExpressivity->hasNominal() && m_pExpressivity->hasInverse())) &&
// !m_pExpressivity->hasAdvancedRoles() &&
// !m_pExpressivity->hasCardinalityQ() &&
!isTBoxChanged() && !isRBoxChanged() && (m_pABox->getPseudoModel() != NULL) &&
PARAMS_USE_INCREMENTAL_CONSISTENCE() &&
// support additions only; also support deletions with or with
// additions, however tracing must be on to support incremental deletions
((!m_bABoxDeletion && m_bABoxAddition) ||
(m_bABoxDeletion && PARAMS_USE_INCREMENTAL_DELETION()));
}
void KnowledgeBase::updateExpressivity(ExprNode* pIndividualExpr, ExprNode* pCExpr)
{
// if the tbox or rbox changed then we cannot use incremental reasoning!
if( !isChanged() || isTBoxChanged() || isRBoxChanged() )
return;
// update status
m_iKBStatus = KBSTATUS_UNCHANGED;
// update expressivity given this individual
m_pExpressivity->processIndividual(pIndividualExpr, pCExpr);
// update the size estimate as this could be a new individual
m_pEstimate = new SizeEstimate(this);
}
bool KnowledgeBase::isConsistent()
{
checkConsistency();
return m_bConsistent;
}
/**
* Returns true if the consistency check has been done and nothing in th KB
* has changed after that.
*/
bool KnowledgeBase::isConsistencyDone()
{
// check if consistency bit is set but none of the change bits
return (m_iKBStatus & (KBSTATUS_CONSISTENCY|KBSTATUS_ALL_CHANGED) == KBSTATUS_CONSISTENCY);
}
void KnowledgeBase::checkConsistency()
{
if( isConsistencyDone() )
return;
// always turn on explanations for the first consistency check
bool bExplain = m_pABox->m_bDoExplanation;
m_pABox->m_bDoExplanation = TRUE;
// only prepare if we are not going to use the incremental
// consistency checking approach
if( !canUseIncrementalConsistency() )
prepare();
m_bConsistent = m_pABox->isConsistent();
m_pABox->m_bDoExplanation = bExplain;
if( !m_bConsistent )
{
// log.warn( "Inconsistent ontology. Reason: " + getExplanation() );
}
m_iKBStatus |= KBSTATUS_CONSISTENCY;
m_bABoxAddition = FALSE;
m_bABoxDeletion = FALSE;
m_setDeletedAssertions.clear();
}
Expressivity* KnowledgeBase::getExpressivity()
{
// if we can use incremental reasoning then expressivity has been
// updated as only the ABox was incrementally changed
if( canUseIncrementalConsistency() )
return m_pExpressivity;
prepare();
return m_pExpressivity;
}
CompletionStrategy* KnowledgeBase::chooseStrategy(ABox* pABox)
{
return chooseStrategy(pABox, getExpressivity());
}
/**
* Choose a completion strategy based on the expressivity of the KB. The
* abox given is not necessarily the ABox that belongs to this KB but can be
* a derivative.
*/
CompletionStrategy* KnowledgeBase::chooseStrategy(ABox* pABox, Expressivity* pExpressivity)
{
// if there are dl-safe rules present, use RuleStrategy which is a subclass of SHOIN
// only problem is, we're using SHOIN everytime there are rule- it is
// faster to use SHN + Rules in some cases
#if 0
if( getRules().size() > 0 &&
( expressivity.hasNominal() ||
!( abox.size() == 1 && abox.getNodes().iterator().next().getName().equals( ATermUtils.CONCEPT_SAT_IND ) ) ) ) {
if ( PelletOptions.USE_CONTINUOUS_RULES ) {
return new ContinuousRulesStrategy( abox );
}
else {
return new RuleStrategy( abox );
}
}
if( PelletOptions.DEFAULT_COMPLETION_STRATEGY != null ) {
Class[] types = new Class[] { ABox.class };
Object[] args = new Object[] { abox };
try {
Constructor<? extends CompletionStrategy> cons = PelletOptions.DEFAULT_COMPLETION_STRATEGY.getConstructor( types );
return cons.newInstance( args );
} catch( Exception e ) {
e.printStackTrace();
throw new InternalReasonerException(
"Failed to create the default completion strategy defined in PelletOptions!" );
}
}
#endif
if( PARAMS_USE_COMPLETION_STRATEGY() )
{
bool bEmptyStrategy = FALSE;
if( m_pABox->size() == 1 )
{
Individual* pIndividual = (Individual*)(pABox->m_mNodes.begin())->second;
if( pIndividual->m_listOutEdges.m_listEdge.size() == 0 )
bEmptyStrategy = TRUE;
}
bool bFullDatatypeReasoning = (PARAMS_USE_FULL_DATATYPE_REASONING() && (pExpressivity->hasCardinalityD()||pExpressivity->hasKeys()));
if( !bFullDatatypeReasoning )
{
if( pExpressivity->hasComplexSubRoles() )
return (new SROIQStrategy(pABox));
else if( pExpressivity->hasCardinalityQ() )
return (new SHOIQStrategy(pABox));
else if( pExpressivity->hasNominal() )
{
if( pExpressivity->hasInverse() )
return (new SHOINStrategy(pABox));
return (new SHONStrategy(pABox));
}
else if( pExpressivity->hasInverse() )
return (new SHINStrategy(pABox));
else if( bEmptyStrategy && !pExpressivity->hasCardinalityD() && !pExpressivity->hasKeys() )
return (new EmptySHNStrategy(pABox));
return (new SHNStrategy(pABox));
}
}
return (new SROIQStrategy(pABox));
}
/**
* Method to remove all stuctures dependent on an ABox assertion from the
* abox. This is used for incremental reasoning under ABox deletions.
*/
void KnowledgeBase::restoreDependencies()
{
// iterate over all removed assertions
for(ExprNodeSet::iterator i = m_setDeletedAssertions.begin(); i != m_setDeletedAssertions.end(); i++ )
{
ExprNode* pAssertion = (ExprNode*)*i;
// get the dependency entry
DependencyEntry* pDE = m_pDependencyIndex->getDependencies(pAssertion);
if( pDE )
{
// restore the entry
restoreDependency(pAssertion, pDE);
}
// remove the entry in the index for this assertion
m_pDependencyIndex->removeDependencies(pAssertion);
}
}
/**
* Perform the actual rollback of a depenedency entry
*/
void KnowledgeBase::restoreDependency(ExprNode* pAssertion, DependencyEntry* pEntry)
{
START_DECOMMENT2("KnowledgeBase::restoreDependency");
for(EdgeSet::iterator i = pEntry->m_setEdges.begin(); i != pEntry->m_setEdges.end(); i++ )
restoreEdge(pAssertion, (Edge*)*i);
for(SetOfDependency::iterator i = pEntry->m_setTypes.begin(); i != pEntry->m_setTypes.end(); i++ )
restoreType(pAssertion, (TypeDependency*)*i);
for(SetOfDependency::iterator i = pEntry->m_setMerges.begin(); i != pEntry->m_setMerges.end(); i++ )
restoreMerge(pAssertion, (MergeDependency*)*i);
for(SetOfDependency::iterator i = pEntry->m_setBranchAdds.begin(); i != pEntry->m_setBranchAdds.end(); i++ )
restoreBranchAdd(pAssertion, (BranchAddDependency*)*i);
for(SetOfDependency::iterator i = pEntry->m_setBranchCloses.begin(); i != pEntry->m_setBranchCloses.end(); i++ )
restoreCloseBranch(pAssertion, (CloseBranchDependency*)*i);
if( pEntry->m_pClashDependency )
restoreClash(pAssertion, pEntry->m_pClashDependency);
END_DECOMMENT("KnowledgeBase::restoreDependency");
}
void KnowledgeBase::restoreEdge(ExprNode* pAssertion, Edge* pEdge)
{
START_DECOMMENT2("KnowledgeBase::restoreEdge");
// the edge could have previously been removed so return
if( pEdge == NULL )
{
END_DECOMMENT("KnowledgeBase::restoreEdge");
return;
}
// get the object
Individual* pSubj = m_pABox->m_pPseudoModel->getIndividual( pEdge->m_pFrom->m_pName );
Node* pObj = m_pABox->m_pPseudoModel->getNode( pEdge->m_pTo->m_pName );
Role* pRole = getRole( pEdge->m_pRole->m_pName );
// loop over all edges for the subject
EdgeList aEdgesTo;
pSubj->getEdgesTo(pObj, pRole, &aEdgesTo);
for(EdgeVector::iterator i = aEdgesTo.m_listEdge.begin(); i != aEdgesTo.m_listEdge.end(); i++ )
{
Edge* pE = (Edge*)*i;
if( ::isEqual(pEdge->m_pRole, pRole) == 0 )
{
// get dependency set for the edge
DependencySet* pDS = pE->m_pDepends;
// clean it
pDS->removeExplain(pAssertion);
// remove if the dependency set is empty
if( pDS->m_setExplain.size() == 0 )
{
// need to check if the
pSubj->removeEdge(pE);
// add to updated individuals
m_pABox->m_setUpdatedIndividuals.insert(pSubj);
// TODO: Do we need to add literals?
if( pObj->isIndividual() )
m_pABox->m_setUpdatedIndividuals.insert((Individual*)pObj);
}
break;
}
}
END_DECOMMENT("KnowledgeBase::restoreEdge");
}
void KnowledgeBase::restoreType(ExprNode* pAssertion, TypeDependency* pType)
{
START_DECOMMENT2("KnowledgeBase::restoreType");
// get the dependency set - Note: we must normalize the concept
DependencySet* pDS = NULL;
ExprNode2DependencySetMap* pDSetMap = &(m_pABox->m_pPseudoModel->getNode(pType->m_pInd)->m_mDepends);
ExprNode2DependencySetMap::iterator iFind = pDSetMap->find( normalize(pType->m_pType) );
if( iFind != pDSetMap->end() )
pDS = (DependencySet*)iFind->second;
// return if null - this can happen as currently I have dupilicates in
// the index
if( pDS == NULL || isEqual(pType->m_pType, EXPRNODE_TOP) == 0 )
{
END_DECOMMENT("KnowledgeBase::restoreType");
return;
}
// clean it
pDS->removeExplain(pAssertion);
// remove if the explanation set is empty
if( pDS->m_setExplain.size() == 0 )
{
m_pABox->m_pPseudoModel->removeType(pType->m_pInd, pType->m_pType);
// add to updated individuals
Node* pN = m_pABox->m_pPseudoModel->getNode( pType->m_pInd );
if( pN && pN->isIndividual() )
{
Individual* pInd = (Individual*)pN;
m_pABox->m_setUpdatedIndividuals.insert(pInd);
// also need to add all edge object to updated individuals -
// this is needed to fire allValues/domain/range rules etc.
for(EdgeVector::iterator i = pInd->m_listInEdges.m_listEdge.begin(); i != pInd->m_listInEdges.m_listEdge.end(); i++ )
{
Edge* pE = (Edge*)*i;
m_pABox->m_setUpdatedIndividuals.insert(pE->m_pFrom);
}
for(EdgeVector::iterator i = pInd->m_listOutEdges.m_listEdge.begin(); i != pInd->m_listOutEdges.m_listEdge.end(); i++ )
{
Edge* pE = (Edge*)*i;
if( pE->m_pTo->isIndividual() )
m_pABox->m_setUpdatedIndividuals.insert(pE->m_pTo);
}
}
}
END_DECOMMENT("KnowledgeBase::restoreType");
}
void KnowledgeBase::restoreMerge(ExprNode* pAssertion, MergeDependency* pMerge)
{
START_DECOMMENT2("KnowledgeBase::restoreMerge");
// get merge dependency
DependencySet* pDS = m_pABox->m_pPseudoModel->getNode(pMerge->m_pInd)->m_pMergeDepends;
// clean it
pDS->removeExplain(pAssertion);
// undo merge if empty
if( pDS->m_setExplain.size() == 0 )
{
// get nodes
Node* pInd = m_pABox->m_pPseudoModel->getNode(pMerge->m_pInd);
Node* pMergedToInd = m_pABox->m_pPseudoModel->getNode(pMerge->m_pMergeIntoInd);
// check that they are actually the same - else throw error
if( !pInd->isSame(pMergedToInd) )
assertFALSE("Restore merge error: not same");
if( !pInd->isPruned() )
assertFALSE("Restore merge error: not pruned");
// unprune to prune branch
pInd->unprune(pInd->m_pPruned->m_iBranch);
// undo set same
pInd->undoSetSame();
// add to updated
// Note that ind.unprune may add edges, however we do not need to
// add them to the updated individuals as
// they will be added when the edge is removed from the node which
// this individual was merged to