-
Notifications
You must be signed in to change notification settings - Fork 1
/
TBox.c
238 lines (214 loc) · 6.21 KB
/
TBox.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
#include "TBox.h"
#include "Tu.h"
#include "Tg.h"
#include "KnowledgeBase.h"
#include "ReazienerUtils.h"
#include "Params.h"
extern int g_iCommentIndent;
extern ExprNode* EXPRNODE_TOP;
extern ExprNode* EXPRNODE_BOTTOM;
TBox::TBox(KnowledgeBase* pKB)
{
m_pTuBox = new TuBox(pKB);
m_pTgBox = new TgBox(pKB);
}
bool TBox::addClass(ExprNode* pClassExpr)
{
bool bAdded = m_setClasses.insert(pClassExpr).second;
if( bAdded )
m_setAllClasses.clear();
return bAdded;
}
bool TBox::addAxiom(ExprNode* pAxiom, ExprNodeSet* pExplanation)
{
START_DECOMMENT2("TBox::addAxiom");
printExprNodeWComment("Axiom=", pAxiom);
// absorb nominals on the fly because sometimes they might end up in the
// Tu directly without going into Tg which is still less effective than absorbing
if( PARAMS_USE_NOMINAL_ABSORPTION() || PARAMS_USE_PSEUDO_NOMINALS() )
{
if( pAxiom->m_iExpression == EXPR_EQCLASSES )
{
ExprNode* pC1 = (ExprNode*)pAxiom->m_pArgs[0];
ExprNode* pC2 = (ExprNode*)pAxiom->m_pArgs[1];
// the first concept is oneOF
if( isOneOf(pC1) )
{
// absorb SubClassOf(c1,c2)
m_pTgBox->absorbOneOf(pC1, pC2, pExplanation);
if( isOneOf(pC2) )
{
// absorb SubClassOf(c2,c1)
m_pTgBox->absorbOneOf(pC2, pC1, pExplanation);
// axioms completely absorbed so return
END_DECOMMENT("TBox::addAxiom");
return TRUE;
}
else
{
// SubClassOf(c2,c1) is not absorbed so continue with
// addAxiom function
pAxiom = createExprNode(EXPR_SUBCLASSOF, pC2, pC1);
}
}
else if( isOneOf(pC2) )
{
// absorb SubClassOf(c2,c1)
m_pTgBox->absorbOneOf(pC2, pC1, pExplanation);
// SubClassOf(c1,c2) is not absorbed so continue with
// addAxiom function
pAxiom = createExprNode(EXPR_SUBCLASSOF, pC1, pC2);
}
}
else if( pAxiom->m_iExpression == EXPR_SUBCLASSOF )
{
ExprNode* pSubExpr = (ExprNode*)pAxiom->m_pArgs[0];
if( isOneOf(pSubExpr) )
{
ExprNode* pSupExpr = (ExprNode*)pAxiom->m_pArgs[1];
m_pTgBox->absorbOneOf(pSubExpr, pSupExpr, pExplanation);
END_DECOMMENT("TBox::addAxiom");
return TRUE;
}
}
}
bool bAddAxiom = addAxiom(pAxiom, pExplanation, FALSE);
END_DECOMMENT("TBox::addAxiom");
return bAddAxiom;
}
bool TBox::addAxiom(ExprNode* pAxiom, ExprNodeSet* pExplanation, bool bForceAddition)
{
START_DECOMMENT2("TBox::addAxiom");
printExprNodeWComment("Axiom=", pAxiom);
bool bAxiomExpAdded = addAxiomExplanation(pAxiom, pExplanation);
if( bAxiomExpAdded || bForceAddition )
{
if( !m_pTuBox->addIfUnfoldable(pAxiom) )
{
if( pAxiom->m_iExpression == EXPR_EQCLASSES )
{
// Try reversing the term if it is a 'same' construct
ExprNode* pName = (ExprNode*)pAxiom->m_pArgs[0];
ExprNode* pDesc = (ExprNode*)pAxiom->m_pArgs[1];
ExprNode* pReversedAxiom = createExprNode(EXPR_EQCLASSES, pDesc, pName);
if( !m_pTuBox->addIfUnfoldable(pReversedAxiom) )
m_pTgBox->addDef(pAxiom);
else
addAxiomExplanation(pReversedAxiom, pExplanation);
}
else
m_pTgBox->addDef(pAxiom);
}
}
END_DECOMMENT("TBox::addAxiom");
return bAxiomExpAdded;
}
bool TBox::addAxiomExplanation(ExprNode* pAxiom, ExprNodeSet* pExplanation)
{
SetOfExprNodeSet* pSetOfExprSet = NULL;
Expr2SetOfExprNodeSets::iterator i = m_mapTBoxAxioms.find(pAxiom);
if( i == m_mapTBoxAxioms.end() )
{
pSetOfExprSet = new SetOfExprNodeSet;
m_mapTBoxAxioms[pAxiom] = pSetOfExprSet;
}
else
pSetOfExprSet = (SetOfExprNodeSet*)i->second;
bool bAdded = FALSE;
if( pSetOfExprSet->find(pExplanation) == pSetOfExprSet->end() )
{
bAdded = TRUE;
pSetOfExprSet->insert(pExplanation);
}
if( bAdded )
{
for(ExprNodeSet::iterator j = pExplanation->begin(); j != pExplanation->end(); j++ )
{
ExprNode* pExplainAxiom = (ExprNode*)*j;
if( isEqual(pAxiom, pExplainAxiom) != 0 )
{
ExprNodeSet* pExprSet = NULL;
Expr2ExprNodeSet::iterator k = m_mapReverseExplain.find(pExplainAxiom);
if( k == m_mapReverseExplain.end() )
{
pExprSet = new ExprNodeSet;
m_mapReverseExplain[pExplainAxiom] = pExprSet;
}
else
pExprSet = (ExprNodeSet*)i->second;
pExprSet->insert(pAxiom);
}
}
}
return bAdded;
}
ExprNodeSet* TBox::getAxiomExplanation(ExprNode* pAxiom)
{
SetOfExprNodeSet* pSetOfSets = NULL;
Expr2SetOfExprNodeSets::iterator iFind = m_mapTBoxAxioms.find(pAxiom);
if( iFind == m_mapTBoxAxioms.end() )
{
//log.warn( "No explanation for " + axiom );
return NULL;
}
else
pSetOfSets = (SetOfExprNodeSet*)iFind->second;
// we won't be generating multiple explanations using axiom
// tracing so we just pick one explanation. the other option
// would be to return the union of all explanations which
// would cause Pellet to return non-minimal explanations sets
ExprNodeSet* pExplain = (ExprNodeSet*)*pSetOfSets->begin();
return pExplain;
}
void TBox::getAxioms(ExprNode* pTerm, ExprNodeSet* pAxioms)
{
TermDefinition* pTD = m_pTgBox->getTermDef(pTerm);
if( pTD )
{
pAxioms->insert(pTD->m_setSubClassAxioms.begin(), pTD->m_setSubClassAxioms.end());
pAxioms->insert(pTD->m_setEqClassAxioms.begin(), pTD->m_setEqClassAxioms.end());
}
pTD = m_pTuBox->getTermDef(pTerm);
if( pTD )
{
pAxioms->insert(pTD->m_setSubClassAxioms.begin(), pTD->m_setSubClassAxioms.end());
pAxioms->insert(pTD->m_setEqClassAxioms.begin(), pTD->m_setEqClassAxioms.end());
}
}
void TBox::absorb()
{
m_pTgBox->absorb(m_pTuBox, this);
}
void TBox::normalize()
{
m_pTuBox->normalizeDefinitions(this);
}
void TBox::internalize()
{
m_pTgBox->internalize();
}
Expr2ExprSetPairList* TBox::getUC()
{
if( m_pTgBox )
return m_pTgBox->getUC();
return NULL;
}
Expr2ExprSetPairList* TBox::unfold(ExprNode* pC)
{
if( m_pTuBox )
return m_pTuBox->unfold(pC);
return NULL;
}
void TBox::getAllClasses(ExprNodeSet* pSetAllClasses)
{
if( m_setAllClasses.size() != 0 )
{
pSetAllClasses->insert(m_setAllClasses.begin(), m_setAllClasses.end());
}
else
{
pSetAllClasses->insert(m_setClasses.begin(), m_setClasses.end());
pSetAllClasses->insert(EXPRNODE_TOP);
pSetAllClasses->insert(EXPRNODE_BOTTOM);
}
}